rules_mk.py 2.2 KB

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