signature.py 20 KB

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