rules_mk.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
  30. @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.')
  31. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  32. @automagic_keyboard
  33. @automagic_keymap
  34. def generate_rules_mk(cli):
  35. """Generates a rules.mk file from info.json.
  36. """
  37. if not cli.config.generate_rules_mk.keyboard:
  38. cli.log.error('Missing parameter: --keyboard')
  39. cli.subcommands['info'].print_help()
  40. return False
  41. if not is_keyboard(cli.config.generate_rules_mk.keyboard):
  42. cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard)
  43. return False
  44. kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard))
  45. info_rules_map = _json_load(Path('data/mappings/info_rules.json'))
  46. rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', '']
  47. # Iterate through the info_rules map to generate basic rules
  48. for rules_key, info_dict in info_rules_map.items():
  49. new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict)
  50. if new_entry:
  51. rules_mk_lines.append(new_entry)
  52. # Iterate through features to enable/disable them
  53. if 'features' in kb_info_json:
  54. for feature, enabled in kb_info_json['features'].items():
  55. if feature == 'bootmagic_lite' and enabled:
  56. rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite')
  57. else:
  58. feature = feature.upper()
  59. enabled = 'yes' if enabled else 'no'
  60. rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}')
  61. # Show the results
  62. rules_mk = '\n'.join(rules_mk_lines) + '\n'
  63. if cli.args.output:
  64. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  65. if cli.args.output.exists():
  66. cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
  67. cli.args.output.write_text(rules_mk)
  68. if cli.args.quiet:
  69. if cli.args.escape:
  70. print(cli.args.output.as_posix().replace(' ', '\\ '))
  71. else:
  72. print(cli.args.output)
  73. else:
  74. cli.log.info('Wrote rules.mk to %s.', cli.args.output)
  75. else:
  76. print(rules_mk)