GCC BUILD: Prevent Memory Error when compiling huge projects.
authorEdouard Tisserant <edouard@beremiz.fr>
Thu, 04 Jul 2024 11:24:04 +0200
changeset 3982 69d161fc2e08
parent 3981 74035ea6792c
child 3983 466be4f52cb9
GCC BUILD: Prevent Memory Error when compiling huge projects.

Also deleted dead code.
targets/toolchain_gcc.py
--- a/targets/toolchain_gcc.py	Wed Jul 03 11:44:01 2024 +0200
+++ b/targets/toolchain_gcc.py	Thu Jul 04 11:24:04 2024 +0200
@@ -38,6 +38,18 @@
 includes_re = re.compile(r'\s*#include\s*["<]([^">]*)[">].*')
 
 
+def compute_file_md5(filetocheck):
+    hasher = hashlib.md5()
+    with open(filetocheck, 'rb') as afile:
+        while True:
+            buf = afile.read(65536)
+            if len(buf) > 0:
+                hasher.update(buf)
+            else:
+                break
+    return hasher.hexdigest()
+
+
 class toolchain_gcc(object):
     """
     This abstract class contains GCC specific code.
@@ -136,9 +148,9 @@
         # Get latest computed hash and deps
         oldhash, deps = self.srcmd5.get(bn, (None, []))
         # read source
-        src = open(os.path.join(self.buildpath, bn)).read()
+        src = os.path.join(self.buildpath, bn)
         # compute new hash
-        newhash = hashlib.md5(src.encode()).hexdigest()
+        newhash = compute_file_md5(src)
         # compare
         match = (oldhash == newhash)
         if not match:
@@ -256,7 +268,7 @@
             self.CTRInstance.logger.write("   [pass]  " + ' '.join(obns)+" -> " + self.bin + "\n")
 
         # Calculate md5 key and get data for the new created PLC
-        self.md5key = hashlib.md5(open(self.bin_path, "rb").read()).hexdigest()
+        self.md5key = compute_file_md5(self.bin_path)
 
         # Store new PLC filename based on md5 key
         f = open(self._GetMD5FileName(), "w")