rules_mk.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Used by the make system to generate a rules.mk
  2. """
  3. from milc import cli
  4. from qmk.decorators import automagic_keyboard, automagic_keymap
  5. from qmk.info import info_json
  6. from qmk.path import is_keyboard, normpath
  7. info_to_rules = {
  8. 'board': 'BOARD',
  9. 'bootloader': 'BOOTLOADER',
  10. 'processor': 'MCU',
  11. }
  12. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  13. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  14. @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.')
  15. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  16. @automagic_keyboard
  17. @automagic_keymap
  18. def generate_rules_mk(cli):
  19. """Generates a rules.mk file from info.json.
  20. """
  21. # Determine our keyboard(s)
  22. if not cli.config.generate_rules_mk.keyboard:
  23. cli.log.error('Missing paramater: --keyboard')
  24. cli.subcommands['info'].print_help()
  25. return False
  26. if not is_keyboard(cli.config.generate_rules_mk.keyboard):
  27. cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard)
  28. return False
  29. # Build the info.json file
  30. kb_info_json = info_json(cli.config.generate_rules_mk.keyboard)
  31. rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', '']
  32. # Bring in settings
  33. for info_key, rule_key in info_to_rules.items():
  34. if info_key in kb_info_json:
  35. rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}')
  36. # Find features that should be enabled
  37. if 'features' in kb_info_json:
  38. for feature, enabled in kb_info_json['features'].items():
  39. if feature == 'bootmagic_lite' and enabled:
  40. rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite')
  41. else:
  42. feature = feature.upper()
  43. enabled = 'yes' if enabled else 'no'
  44. rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}')
  45. # Set the LED driver
  46. if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']:
  47. driver = kb_info_json['led_matrix']['driver']
  48. rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}')
  49. # Add community layouts
  50. if 'community_layouts' in kb_info_json:
  51. rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}')
  52. # Show the results
  53. rules_mk = '\n'.join(rules_mk_lines) + '\n'
  54. if cli.args.output:
  55. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  56. if cli.args.output.exists():
  57. cli.args.output.replace(cli.args.output.name + '.bak')
  58. cli.args.output.write_text(rules_mk)
  59. if cli.args.quiet:
  60. print(cli.args.output)
  61. else:
  62. cli.log.info('Wrote info_config.h to %s.', cli.args.output)
  63. else:
  64. print(rules_mk)