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.

97 lines
3.6 KiB

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