123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
-
-
-
- import os,subprocess,re,json,hashlib
-
-
-
-
-
-
-
-
-
- def extract_defines(filepath):
- f = open(filepath, encoding="utf8").read().split("\n")
- a = []
- for line in f:
- sline = line.strip(" \t\n\r")
- if sline[:7] == "#define":
-
- kv = sline[8:].strip().split(' ')
- a.append(kv[0])
- return a
-
-
- def get_file_sha256sum(filepath):
- sha256_hash = hashlib.sha256()
- with open(filepath,"rb") as f:
-
- for byte_block in iter(lambda: f.read(4096),b""):
- sha256_hash.update(byte_block)
- return sha256_hash.hexdigest()
-
-
-
-
- import zipfile
- def compress_file(filepath, outputbase):
- with zipfile.ZipFile(outputbase + '.zip', 'w', compression=zipfile.ZIP_BZIP2, compresslevel=9) as zipf:
- zipf.write(filepath, compress_type=zipfile.ZIP_BZIP2, compresslevel=9)
-
-
-
-
-
-
- def compute_build_signature(env):
- if 'BUILD_SIGNATURE' in env:
- return
-
-
- files_to_keep = [ 'Marlin/Configuration.h', 'Marlin/Configuration_adv.h' ]
-
- build_dir=os.path.join(env['PROJECT_BUILD_DIR'], env['PIOENV'])
-
-
- hashes = ''
- for header in files_to_keep:
- hashes += get_file_sha256sum(header)[0:10]
-
- marlin_json = os.path.join(build_dir, 'marlin_config.json')
- marlin_zip = os.path.join(build_dir, 'mc')
-
-
- try:
- with open(marlin_json, 'r') as infile:
- conf = json.load(infile)
- if conf['__INITIAL_HASH'] == hashes:
-
- compress_file(marlin_json, marlin_zip)
- return
- except:
- pass
-
-
- from preprocessor import run_preprocessor
- complete_cfg = run_preprocessor(env)
-
-
- real_defines = {}
- all_defines = []
- for header in files_to_keep:
- defines = extract_defines(header)
-
- all_defines = all_defines + defines
-
- real_defines[header.split('/')[-1]] = defines
-
- r = re.compile(r"\(+(\s*-*\s*_.*)\)+")
-
-
- defines = {}
- for line in complete_cfg:
-
-
- key_val = line[8:].strip().decode().split(' ')
- key, value = key_val[0], ' '.join(key_val[1:])
-
-
- if len(key) > 2 and key[0:2] == "__" :
- continue
-
- if '(' in key and ')' in key:
- continue
-
-
- if r.match(value):
- continue
-
- defines[key] = value if len(value) else ""
-
- if not 'CONFIGURATION_EMBEDDING' in defines:
- return
-
-
- resolved_defines = {}
- for key in defines:
-
- if key[0:6] == "BOARD_" and key != "BOARD_INFO_NAME":
- continue
-
- if key[-5:] == "_NAME" and key != "CUSTOM_MACHINE_NAME":
- continue
-
- if key[-11:] == "_T_DECLARED":
- continue
-
- if not (key in all_defines) and key != "DETAILED_BUILD_VERSION" and key != "STRING_DISTRIBUTION_DATE":
- continue
-
-
- resolved_defines[key] = defines[key]
-
-
-
- data = {}
- data['__INITIAL_HASH'] = hashes
-
- for header in real_defines:
- data[header] = {}
-
-
- for key in resolved_defines:
- for header in real_defines:
- if key in real_defines[header]:
- data[header][key] = resolved_defines[key]
-
-
- data['VERSION'] = {}
- data['VERSION']['DETAILED_BUILD_VERSION'] = resolved_defines['DETAILED_BUILD_VERSION']
- data['VERSION']['STRING_DISTRIBUTION_DATE'] = resolved_defines['STRING_DISTRIBUTION_DATE']
- try:
- curver = subprocess.check_output(["git", "describe", "--match=NeVeRmAtCh", "--always"]).strip()
- data['VERSION']['GIT_REF'] = curver.decode()
- except:
- pass
-
- with open(marlin_json, 'w') as outfile:
- json.dump(data, outfile, separators=(',', ':'))
-
-
- compress_file(marlin_json, marlin_zip)
-
-
- with open('Marlin/src/mczip.h','wb') as result_file:
- result_file.write(b'#ifndef NO_CONFIGURATION_EMBEDDING_WARNING\n')
- result_file.write(b' #warning "Generated file \'mc.zip\' is embedded (Define NO_CONFIGURATION_EMBEDDING_WARNING to suppress this warning.)"\n')
- result_file.write(b'#endif\n')
- result_file.write(b'const unsigned char mc_zip[] PROGMEM = {\n ')
- count = 0
- for b in open(os.path.join(build_dir, 'mc.zip'), 'rb').read():
- result_file.write(b' 0x%02X,' % b)
- count += 1
- if (count % 16 == 0):
- result_file.write(b'\n ')
- if (count % 16):
- result_file.write(b'\n')
- result_file.write(b'};\n')
|