beremiz

GCC BUILD: Prevent Memory Error when compiling huge projects.
py2compat
23 months ago, Edouard Tisserant
f3a27721100f
Parents 75c37c3cc75a
Children 90a67418b8cf
GCC BUILD: Prevent Memory Error when compiling huge projects.

Also deleted dead code.
--- 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
@@ -36,6 +36,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.
@@ -103,6 +115,7 @@
self.srcmd5 = {}
def append_cfile_deps(self, src, deps):
+ src = open(os.path.join(self.buildpath, src), "r").read()
for l in src.splitlines():
res = includes_re.match(l)
if res is not None:
@@ -110,23 +123,13 @@
if os.path.exists(os.path.join(self.buildpath, depfn)):
deps.append(depfn)
- def concat_deps(self, bn):
- # read source
- src = open(os.path.join(self.buildpath, bn), "r").read()
- # update direct dependencies
- deps = []
- self.append_cfile_deps(src, deps)
- # recurse through deps
- # TODO detect cicular deps.
- return reduce(operator.concat, map(self.concat_deps, deps), src)
-
def check_and_update_hash_and_deps(self, bn):
# 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).hexdigest()
+ newhash = compute_file_md5(src)
# compare
match = (oldhash == newhash)
if not match:
@@ -140,15 +143,6 @@
# TODO detect cicular deps.
return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match)
- def calc_source_md5(self):
- wholesrcdata = ""
- for _Location, CFilesAndCFLAGS, _DoCalls in self.CTRInstance.LocationCFilesAndCFLAGS:
- # Get CFiles list to give it to makefile
- for CFile, _CFLAGS in CFilesAndCFLAGS:
- CFileName = os.path.basename(CFile)
- wholesrcdata += self.concat_deps(CFileName)
- return hashlib.md5(wholesrcdata).hexdigest()
-
def build(self):
# Retrieve compiler and linker
self.compiler = self.getCompiler()
@@ -225,7 +219,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")