signature.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #!/usr/bin/env python3
  2. #
  3. # signature.py
  4. #
  5. import schema, subprocess, re, json, hashlib
  6. from datetime import datetime
  7. from pathlib import Path
  8. from functools import reduce
  9. def enabled_defines(filepath):
  10. '''
  11. Return all enabled #define items from a given C header file in a dictionary.
  12. A "#define" in a multi-line comment could produce a false positive if it's not
  13. preceded by a non-space character (like * in a multi-line comment).
  14. Output:
  15. Each entry is a dictionary with a 'name' and a 'section' key. We end up with:
  16. { MOTHERBOARD: { name: "MOTHERBOARD", section: "hardware" }, ... }
  17. TODO: Drop the 'name' key as redundant. For now it's useful for debugging.
  18. This list is only used to filter config-defined options from those defined elsewhere.
  19. Because the option names are the keys, only the last occurrence is retained.
  20. This means the actual used value might not be reflected by this function.
  21. The Schema class does more complete parsing for a more accurate list of options.
  22. While the Schema class parses the configurations on its own, this script will
  23. get the preprocessor output and get the intersection of the enabled options from
  24. our crude scraping method and the actual compiler output.
  25. We end up with the actual configured state,
  26. better than what the config files say. You can then use the
  27. resulting config.ini to produce more exact configuration files.
  28. '''
  29. outdict = {}
  30. section = "user"
  31. spatt = re.compile(r".*@section +([-a-zA-Z0-9_\s]+)$") # @section ...
  32. f = open(filepath, encoding="utf8").read().split("\n")
  33. incomment = False
  34. for line in f:
  35. sline = line.strip()
  36. m = re.match(spatt, sline) # @section ...
  37. if m: section = m.group(1).strip() ; continue
  38. if incomment:
  39. if '*/' in sline:
  40. incomment = False
  41. continue
  42. else:
  43. mpos, spos = sline.find('/*'), sline.find('//')
  44. if mpos >= 0 and (spos < 0 or spos > mpos):
  45. incomment = True
  46. continue
  47. if sline[:7] == "#define":
  48. # Extract the key here (we don't care about the value)
  49. kv = sline[8:].strip().split()
  50. outdict[kv[0]] = { 'name':kv[0], 'section': section }
  51. return outdict
  52. # Compute the SHA256 hash of a file
  53. def get_file_sha256sum(filepath):
  54. sha256_hash = hashlib.sha256()
  55. with open(filepath,"rb") as f:
  56. # Read and update hash string value in blocks of 4K
  57. for byte_block in iter(lambda: f.read(4096),b""):
  58. sha256_hash.update(byte_block)
  59. return sha256_hash.hexdigest()
  60. #
  61. # Compress a JSON file into a zip file
  62. #
  63. import zipfile
  64. def compress_file(filepath, storedname, outpath):
  65. with zipfile.ZipFile(outpath, 'w', compression=zipfile.ZIP_BZIP2, compresslevel=9) as zipf:
  66. zipf.write(filepath, arcname=storedname, compress_type=zipfile.ZIP_BZIP2, compresslevel=9)
  67. ignore = ('CONFIGURATION_H_VERSION', 'CONFIGURATION_ADV_H_VERSION', 'CONFIG_EXAMPLES_DIR', 'CONFIG_EXPORT')
  68. #
  69. # Compute a build signature and/or export the configuration
  70. #
  71. def compute_build_signature(env):
  72. '''
  73. Compute the build signature by extracting all configuration settings and
  74. building a unique reversible signature that can be included in the binary.
  75. The signature can be reversed to get a 1:1 equivalent configuration file.
  76. Used by common-dependencies.py after filtering build files by feature.
  77. '''
  78. if 'BUILD_SIGNATURE' in env: return
  79. env.Append(BUILD_SIGNATURE=1)
  80. build_path = Path(env['PROJECT_BUILD_DIR'], env['PIOENV'])
  81. json_name = 'marlin_config.json'
  82. marlin_json = build_path / json_name
  83. marlin_zip = build_path / 'mc.zip'
  84. # ANSI colors
  85. green = "\u001b[32m"
  86. yellow = "\u001b[33m"
  87. red = "\u001b[31m"
  88. # Definitions from these files will be kept
  89. header_paths = ('Marlin/Configuration.h', 'Marlin/Configuration_adv.h')
  90. # Check if we can skip processing
  91. hashes = ''
  92. for header in header_paths:
  93. hashes += get_file_sha256sum(header)[0:10]
  94. # Read a previously exported JSON file
  95. # Same configuration, skip recomputing the build signature
  96. same_hash = False
  97. try:
  98. with marlin_json.open() as infile:
  99. conf = json.load(infile)
  100. same_hash = conf['__INITIAL_HASH'] == hashes
  101. if same_hash:
  102. compress_file(marlin_json, json_name, marlin_zip)
  103. except:
  104. pass
  105. # Extract "enabled" #define lines by scraping the configuration files.
  106. # This data also contains the @section for each option.
  107. conf_defines = {}
  108. conf_names = []
  109. for hpath in header_paths:
  110. # Get defines in the form of { name: { name:..., section:... }, ... }
  111. defines = enabled_defines(hpath)
  112. # Get all unique define names into a flat array
  113. conf_names += defines.keys()
  114. # Remember which file these defines came from
  115. conf_defines[hpath.split('/')[-1]] = defines
  116. # Get enabled config options based on running GCC to preprocess the config files.
  117. # The result is a list of line strings, each starting with '#define'.
  118. from preprocessor import run_preprocessor
  119. build_output = run_preprocessor(env)
  120. # Dumb regex to filter out some dumb macros
  121. r = re.compile(r"\(+(\s*-*\s*_.*)\)+")
  122. # Extract all the #define lines in the build output as key/value pairs
  123. build_defines = {}
  124. for line in build_output:
  125. # Split the define from the value.
  126. key_val = line[8:].strip().decode().split(' ')
  127. key, value = key_val[0], ' '.join(key_val[1:])
  128. # Ignore values starting with two underscore, since it's low level
  129. if len(key) > 2 and key[0:2] == "__": continue
  130. # Ignore values containing parentheses (likely a function macro)
  131. if '(' in key and ')' in key: continue
  132. # Then filter dumb values
  133. if r.match(value): continue
  134. build_defines[key] = value if len(value) else ""
  135. #
  136. # Continue to gather data for CONFIGURATION_EMBEDDING or CONFIG_EXPORT
  137. #
  138. if not ('CONFIGURATION_EMBEDDING' in build_defines or 'CONFIG_EXPORT' in build_defines):
  139. return
  140. # Filter out useless macros from the output
  141. cleaned_build_defines = {}
  142. for key in build_defines:
  143. # Remove all boards now
  144. if key.startswith("BOARD_") and key != "BOARD_INFO_NAME": continue
  145. # Remove all keys ending by "_T_DECLARED" as it's a copy of extraneous system stuff
  146. if key.endswith("_T_DECLARED"): continue
  147. # Remove keys that are not in the #define list in the Configuration list
  148. if key not in conf_names + [ 'DETAILED_BUILD_VERSION', 'STRING_DISTRIBUTION_DATE' ]: continue
  149. # Add to a new dictionary for simplicity
  150. cleaned_build_defines[key] = build_defines[key]
  151. # And we only care about defines that (most likely) came from the config files
  152. # Build a dictionary of dictionaries with keys: 'name', 'section', 'value'
  153. # { 'file1': { 'option': { 'name':'option', 'section':..., 'value':... }, ... }, 'file2': { ... } }
  154. real_config = {}
  155. for header in conf_defines:
  156. real_config[header] = {}
  157. for key in cleaned_build_defines:
  158. if key in conf_defines[header]:
  159. if key[0:2] == '__': continue
  160. val = cleaned_build_defines[key]
  161. real_config[header][key] = { 'file':header, 'name': key, 'value': val, 'section': conf_defines[header][key]['section']}
  162. def tryint(key):
  163. try: return int(build_defines[key])
  164. except: return 0
  165. # Get the CONFIG_EXPORT value and do an extended dump if > 100
  166. # For example, CONFIG_EXPORT 102 will make a 'config.ini' with a [config:] group for each schema @section
  167. config_dump = tryint('CONFIG_EXPORT')
  168. extended_dump = config_dump > 100
  169. if extended_dump: config_dump -= 100
  170. # Get the schema class for exports that require it
  171. if config_dump in (3, 4) or (extended_dump and config_dump in (2, 5)):
  172. try:
  173. conf_schema = schema.extract()
  174. except Exception as exc:
  175. print(red + "Error: " + str(exc))
  176. conf_schema = None
  177. #
  178. # CONFIG_EXPORT 2 = config.ini, 5 = Config.h
  179. # Get sections using the schema class
  180. #
  181. if extended_dump and config_dump in (2, 5):
  182. if not conf_schema: exit(1)
  183. # Start with a preferred @section ordering
  184. preorder = ('info','user','machine','extruder','bed temp','fans','stepper drivers','geometry','homing','endstops','probes','lcd','interface','host','reporting')
  185. sections = { key:{} for key in preorder }
  186. # Group options by schema @section
  187. for header in real_config:
  188. for name in real_config[header]:
  189. #print(f" name: {name}")
  190. if name in ignore: continue
  191. ddict = real_config[header][name]
  192. #print(f" real_config[{header}][{name}]:", ddict)
  193. sect = ddict['section']
  194. if sect not in sections: sections[sect] = {}
  195. sections[sect][name] = ddict
  196. #
  197. # CONFIG_EXPORT 2 = config.ini
  198. #
  199. if config_dump == 2:
  200. print(yellow + "Generating config.ini ...")
  201. ini_fmt = '{0:40} = {1}'
  202. ext_fmt = '{0:40} {1}'
  203. if extended_dump:
  204. # Extended export will dump config options by section
  205. # We'll use Schema class to get the sections
  206. if not conf_schema: exit(1)
  207. # Then group options by schema @section
  208. sections = {}
  209. for header in real_config:
  210. for name in real_config[header]:
  211. #print(f" name: {name}")
  212. if name not in ignore:
  213. ddict = real_config[header][name]
  214. #print(f" real_config[{header}][{name}]:", ddict)
  215. sect = ddict['section']
  216. if sect not in sections: sections[sect] = {}
  217. sections[sect][name] = ddict
  218. # Get all sections as a list of strings, with spaces and dashes replaced by underscores
  219. long_list = [ re.sub(r'[- ]+', '_', x).lower() for x in sections.keys() ]
  220. # Make comma-separated lists of sections with 64 characters or less
  221. sec_lines = []
  222. while len(long_list):
  223. line = long_list.pop(0) + ', '
  224. while len(long_list) and len(line) + len(long_list[0]) < 64 - 1:
  225. line += long_list.pop(0) + ', '
  226. sec_lines.append(line.strip())
  227. sec_lines[-1] = sec_lines[-1][:-1] # Remove the last comma
  228. else:
  229. sec_lines = ['all']
  230. # Build the ini_use_config item
  231. sec_list = ini_fmt.format('ini_use_config', sec_lines[0])
  232. for line in sec_lines[1:]: sec_list += '\n' + ext_fmt.format('', line)
  233. config_ini = build_path / 'config.ini'
  234. with config_ini.open('w') as outfile:
  235. filegrp = { 'Configuration.h':'config:basic', 'Configuration_adv.h':'config:advanced' }
  236. vers = build_defines["CONFIGURATION_H_VERSION"]
  237. dt_string = datetime.now().strftime("%Y-%m-%d at %H:%M:%S")
  238. outfile.write(
  239. f'''#
  240. # Marlin Firmware
  241. # config.ini - Options to apply before the build
  242. #
  243. # Generated by Marlin build on {dt_string}
  244. #
  245. [config:base]
  246. #
  247. # ini_use_config - A comma-separated list of actions to apply to the Configuration files.
  248. # The actions will be applied in the listed order.
  249. # - none
  250. # Ignore this file and don't apply any configuration options
  251. #
  252. # - base
  253. # Just apply the options in config:base to the configuration
  254. #
  255. # - minimal
  256. # Just apply the options in config:minimal to the configuration
  257. #
  258. # - all
  259. # Apply all 'config:*' sections in this file to the configuration
  260. #
  261. # - another.ini
  262. # Load another INI file with a path relative to this config.ini file (i.e., within Marlin/)
  263. #
  264. # - https://me.myserver.com/path/to/configs
  265. # Fetch configurations from any URL.
  266. #
  267. # - example/Creality/Ender-5 Plus @ bugfix-2.1.x
  268. # Fetch example configuration files from the MarlinFirmware/Configurations repository
  269. # https://raw.githubusercontent.com/MarlinFirmware/Configurations/bugfix-2.1.x/config/examples/Creality/Ender-5%20Plus/
  270. #
  271. # - example/default @ release-2.0.9.7
  272. # Fetch default configuration files from the MarlinFirmware/Configurations repository
  273. # https://raw.githubusercontent.com/MarlinFirmware/Configurations/release-2.0.9.7/config/default/
  274. #
  275. # - [disable]
  276. # Comment out all #defines in both Configuration.h and Configuration_adv.h. This is useful
  277. # to start with a clean slate before applying any config: options, so only the options explicitly
  278. # set in config.ini will be enabled in the configuration.
  279. #
  280. # - [flatten] (Not yet implemented)
  281. # Produce a flattened set of Configuration.h and Configuration_adv.h files with only the enabled
  282. # #defines and no comments. A clean look, but context-free.
  283. #
  284. {sec_list}
  285. {ini_fmt.format('ini_config_vers', vers)}
  286. ''' )
  287. if extended_dump:
  288. # Loop through the sections
  289. for skey in sorted(sections):
  290. #print(f" skey: {skey}")
  291. sani = re.sub(r'[- ]+', '_', skey).lower()
  292. outfile.write(f"\n[config:{sani}]\n")
  293. opts = sections[skey]
  294. for name in sorted(opts):
  295. val = opts[name]['value']
  296. if val == '': val = 'on'
  297. #print(f" {name} = {val}")
  298. outfile.write(ini_fmt.format(name.lower(), val) + '\n')
  299. else:
  300. # Standard export just dumps config:basic and config:advanced sections
  301. for header in real_config:
  302. outfile.write(f'\n[{filegrp[header]}]\n')
  303. for name in sorted(real_config[header]):
  304. if name in ignore: continue
  305. val = real_config[header][name]['value']
  306. if val == '': val = 'on'
  307. outfile.write(ini_fmt.format(name.lower(), val) + '\n')
  308. #
  309. # CONFIG_EXPORT 5 = Config.h
  310. #
  311. if config_dump == 5:
  312. print(yellow + "Generating Config-export.h ...")
  313. config_h = Path('Marlin', 'Config-export.h')
  314. with config_h.open('w') as outfile:
  315. filegrp = { 'Configuration.h':'config:basic', 'Configuration_adv.h':'config:advanced' }
  316. vers = build_defines["CONFIGURATION_H_VERSION"]
  317. dt_string = datetime.now().strftime("%Y-%m-%d at %H:%M:%S")
  318. out_text = f'''/**
  319. * Config.h - Marlin Firmware distilled configuration
  320. * Usage: Place this file in the 'Marlin' folder with the name 'Config.h'.
  321. *
  322. * Exported by Marlin build on {dt_string}.
  323. */
  324. '''
  325. subs = (('Bltouch','BLTouch'),('hchop','hChop'),('Eeprom','EEPROM'),('Gcode','G-code'),('lguard','lGuard'),('Idex','IDEX'),('Lcd','LCD'),('Mpc','MPC'),('Pid','PID'),('Psu','PSU'),('Scara','SCARA'),('Spi','SPI'),('Tmc','TMC'),('Tpara','TPARA'))
  326. define_fmt = '#define {0:40} {1}'
  327. if extended_dump:
  328. # Loop through the sections
  329. for skey in sections:
  330. #print(f" skey: {skey}")
  331. opts = sections[skey]
  332. headed = False
  333. for name in sorted(opts):
  334. if name in ignore: continue
  335. val = opts[name]['value']
  336. if not headed:
  337. head = reduce(lambda s, r: s.replace(*r), subs, skey.title())
  338. out_text += f"\n//\n// {head}\n//\n"
  339. headed = True
  340. out_text += define_fmt.format(name, val).strip() + '\n'
  341. else:
  342. # Dump config options in just two sections, by file
  343. for header in real_config:
  344. out_text += f'\n/**\n * Overrides for {header}\n */\n'
  345. for name in sorted(real_config[header]):
  346. if name in ignore: continue
  347. val = real_config[header][name]['value']
  348. out_text += define_fmt.format(name, val).strip() + '\n'
  349. outfile.write(out_text)
  350. #
  351. # CONFIG_EXPORT 3 = schema.json, 4 = schema.yml
  352. #
  353. if config_dump in (3, 4):
  354. if conf_schema:
  355. #
  356. # 3 = schema.json
  357. #
  358. if config_dump in (3, 13):
  359. print(yellow + "Generating schema.json ...")
  360. schema.dump_json(conf_schema, build_path / 'schema.json')
  361. if config_dump == 13:
  362. schema.group_options(conf_schema)
  363. schema.dump_json(conf_schema, build_path / 'schema_grouped.json')
  364. #
  365. # 4 = schema.yml
  366. #
  367. elif config_dump == 4:
  368. print(yellow + "Generating schema.yml ...")
  369. try:
  370. import yaml
  371. except ImportError:
  372. env.Execute(env.VerboseAction(
  373. '$PYTHONEXE -m pip install "pyyaml"',
  374. "Installing YAML for schema.yml export",
  375. ))
  376. import yaml
  377. schema.dump_yaml(conf_schema, build_path / 'schema.yml')
  378. #
  379. # Produce a JSON file for CONFIGURATION_EMBEDDING or CONFIG_EXPORT == 1
  380. # Skip if an identical JSON file was already present.
  381. #
  382. if not same_hash and (config_dump == 1 or 'CONFIGURATION_EMBEDDING' in build_defines):
  383. with marlin_json.open('w') as outfile:
  384. json_data = {}
  385. if extended_dump:
  386. print(yellow + "Extended dump ...")
  387. for header in real_config:
  388. confs = real_config[header]
  389. json_data[header] = {}
  390. for name in confs:
  391. c = confs[name]
  392. s = c['section']
  393. if s not in json_data[header]: json_data[header][s] = {}
  394. json_data[header][s][name] = c['value']
  395. else:
  396. for header in real_config:
  397. conf = real_config[header]
  398. #print(f"real_config[{header}]", conf)
  399. for name in conf:
  400. json_data[name] = conf[name]['value']
  401. json_data['__INITIAL_HASH'] = hashes
  402. # Append the source code version and date
  403. json_data['VERSION'] = {
  404. 'DETAILED_BUILD_VERSION': cleaned_build_defines['DETAILED_BUILD_VERSION'],
  405. 'STRING_DISTRIBUTION_DATE': cleaned_build_defines['STRING_DISTRIBUTION_DATE']
  406. }
  407. try:
  408. curver = subprocess.check_output(["git", "describe", "--match=NeVeRmAtCh", "--always"]).strip()
  409. json_data['VERSION']['GIT_REF'] = curver.decode()
  410. except:
  411. pass
  412. json.dump(json_data, outfile, separators=(',', ':'))
  413. #
  414. # The rest only applies to CONFIGURATION_EMBEDDING
  415. #
  416. if not 'CONFIGURATION_EMBEDDING' in build_defines:
  417. (build_path / 'mc.zip').unlink(missing_ok=True)
  418. return
  419. # Compress the JSON file as much as we can
  420. if not same_hash:
  421. compress_file(marlin_json, json_name, marlin_zip)
  422. # Generate a C source file containing the entire ZIP file as an array
  423. with open('Marlin/src/mczip.h','wb') as result_file:
  424. result_file.write(
  425. b'#ifndef NO_CONFIGURATION_EMBEDDING_WARNING\n'
  426. + b' #warning "Generated file \'mc.zip\' is embedded (Define NO_CONFIGURATION_EMBEDDING_WARNING to suppress this warning.)"\n'
  427. + b'#endif\n'
  428. + b'const unsigned char mc_zip[] PROGMEM = {\n '
  429. )
  430. count = 0
  431. for b in (build_path / 'mc.zip').open('rb').read():
  432. result_file.write(b' 0x%02X,' % b)
  433. count += 1
  434. if count % 16 == 0: result_file.write(b'\n ')
  435. if count % 16: result_file.write(b'\n')
  436. result_file.write(b'};\n')
  437. if __name__ == "__main__":
  438. # Build required. From command line just explain usage.
  439. print("Use schema.py to export JSON and YAML from the command-line.")
  440. print("Build Marlin with CONFIG_EXPORT 2 to export 'config.ini'.")