rules_mk.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.info import info_json
  7. from qmk.json_schema import json_load, validate
  8. from qmk.keyboard import keyboard_completer, keyboard_folder
  9. from qmk.keymap import locate_keymap
  10. from qmk.commands import dump_lines
  11. from qmk.path import normpath
  12. from qmk.constants import GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE
  13. def process_mapping_rule(kb_info_json, rules_key, info_dict):
  14. """Return the rules.mk line(s) for a mapping rule.
  15. """
  16. if not info_dict.get('to_c', True):
  17. return None
  18. info_key = info_dict['info_key']
  19. key_type = info_dict.get('value_type', 'raw')
  20. try:
  21. rules_value = kb_info_json[info_key]
  22. except KeyError:
  23. return None
  24. if key_type in ['array', 'list']:
  25. return f'{rules_key} ?= {" ".join(rules_value)}'
  26. elif key_type == 'bool':
  27. return f'{rules_key} ?= {"yes" if rules_value else "no"}'
  28. elif key_type == 'mapping':
  29. return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()])
  30. elif key_type == 'str':
  31. return f'{rules_key} ?= "{rules_value}"'
  32. return f'{rules_key} ?= {rules_value}'
  33. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  34. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  35. @cli.argument('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
  36. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate rules.mk for.')
  37. @cli.argument('-km', '--keymap', arg_only=True, help='Keymap to generate rules.mk for.')
  38. @cli.subcommand('Used by the make system to generate rules.mk from info.json', hidden=True)
  39. def generate_rules_mk(cli):
  40. """Generates a rules.mk file from info.json.
  41. """
  42. # Determine our keyboard/keymap
  43. if cli.args.keymap:
  44. km = locate_keymap(cli.args.keyboard, cli.args.keymap)
  45. km_json = json_load(km)
  46. validate(km_json, 'qmk.keymap.v1')
  47. kb_info_json = dotty(km_json.get('config', {}))
  48. else:
  49. kb_info_json = dotty(info_json(cli.args.keyboard))
  50. info_rules_map = json_load(Path('data/mappings/info_rules.json'))
  51. rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE]
  52. # Iterate through the info_rules map to generate basic rules
  53. for rules_key, info_dict in info_rules_map.items():
  54. new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict)
  55. if new_entry:
  56. rules_mk_lines.append(new_entry)
  57. # Iterate through features to enable/disable them
  58. if 'features' in kb_info_json:
  59. for feature, enabled in kb_info_json['features'].items():
  60. feature = feature.upper()
  61. enabled = 'yes' if enabled else 'no'
  62. rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}')
  63. # Set SPLIT_TRANSPORT, if needed
  64. if kb_info_json.get('split', {}).get('transport', {}).get('protocol') == 'custom':
  65. rules_mk_lines.append('SPLIT_TRANSPORT ?= custom')
  66. # Set CUSTOM_MATRIX, if needed
  67. if kb_info_json.get('matrix_pins', {}).get('custom'):
  68. if kb_info_json.get('matrix_pins', {}).get('custom_lite'):
  69. rules_mk_lines.append('CUSTOM_MATRIX ?= lite')
  70. else:
  71. rules_mk_lines.append('CUSTOM_MATRIX ?= yes')
  72. # Show the results
  73. dump_lines(cli.args.output, rules_mk_lines)
  74. if cli.args.output:
  75. if cli.args.quiet:
  76. if cli.args.escape:
  77. print(cli.args.output.as_posix().replace(' ', '\\ '))
  78. else:
  79. print(cli.args.output)
  80. else:
  81. cli.log.info('Wrote rules.mk to %s.', cli.args.output)