rules_mk.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Used by the make system to generate a rules.mk
  2. """
  3. from pathlib import Path
  4. from dotty_dict import dotty
  5. from milc import cli
  6. from qmk.decorators import automagic_keyboard, automagic_keymap
  7. from qmk.info import _json_load, info_json
  8. from qmk.path import is_keyboard, normpath
  9. def process_mapping_rule(kb_info_json, rules_key, info_dict):
  10. """Return the rules.mk line(s) for a mapping rule.
  11. """
  12. if not info_dict.get('to_c', True):
  13. return None
  14. info_key = info_dict['info_key']
  15. key_type = info_dict.get('value_type', 'str')
  16. try:
  17. rules_value = kb_info_json[info_key]
  18. except KeyError:
  19. return None
  20. if key_type == 'array':
  21. return f'{rules_key} ?= {" ".join(rules_value)}'
  22. elif key_type == 'bool':
  23. return f'{rules_key} ?= {"on" if rules_value else "off"}'
  24. elif key_type == 'mapping':
  25. return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()])
  26. return f'{rules_key} ?= {rules_value}'
  27. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  28. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  29. @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.')
  30. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  31. @automagic_keyboard
  32. @automagic_keymap
  33. def generate_rules_mk(cli):
  34. """Generates a rules.mk file from info.json.
  35. """
  36. if not cli.config.generate_rules_mk.keyboard:
  37. cli.log.error('Missing paramater: --keyboard')
  38. cli.subcommands['info'].print_help()
  39. return False
  40. if not is_keyboard(cli.config.generate_rules_mk.keyboard):
  41. cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard)
  42. return False
  43. kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard))
  44. info_rules_map = _json_load(Path('data/mappings/info_rules.json'))
  45. rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', '']
  46. # Iterate through the info_rules map to generate basic rules
  47. for rules_key, info_dict in info_rules_map.items():
  48. new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict)
  49. if new_entry:
  50. rules_mk_lines.append(new_entry)
  51. # Iterate through features to enable/disable them
  52. if 'features' in kb_info_json:
  53. for feature, enabled in kb_info_json['features'].items():
  54. if feature == 'bootmagic_lite' and enabled:
  55. rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite')
  56. else:
  57. feature = feature.upper()
  58. enabled = 'yes' if enabled else 'no'
  59. rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}')
  60. # Show the results
  61. rules_mk = '\n'.join(rules_mk_lines) + '\n'
  62. if cli.args.output:
  63. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  64. if cli.args.output.exists():
  65. cli.args.output.replace(cli.args.output.name + '.bak')
  66. cli.args.output.write_text(rules_mk)
  67. if cli.args.quiet:
  68. print(cli.args.output)
  69. else:
  70. cli.log.info('Wrote rules.mk to %s.', cli.args.output)
  71. else:
  72. print(rules_mk)