You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.6 KiB

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