diff options
Diffstat (limited to 'lib/python/qmk/cli/generate/rules_mk.py')
-rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index b262e3c66..af740f341 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py | |||
@@ -1,16 +1,37 @@ | |||
1 | """Used by the make system to generate a rules.mk | 1 | """Used by the make system to generate a rules.mk |
2 | """ | 2 | """ |
3 | from pathlib import Path | ||
4 | |||
5 | from dotty_dict import dotty | ||
3 | from milc import cli | 6 | from milc import cli |
4 | 7 | ||
5 | from qmk.decorators import automagic_keyboard, automagic_keymap | 8 | from qmk.decorators import automagic_keyboard, automagic_keymap |
6 | from qmk.info import info_json | 9 | from qmk.info import _json_load, info_json |
7 | from qmk.path import is_keyboard, normpath | 10 | from qmk.path import is_keyboard, normpath |
8 | 11 | ||
9 | info_to_rules = { | 12 | |
10 | 'board': 'BOARD', | 13 | def process_mapping_rule(kb_info_json, rules_key, info_dict): |
11 | 'bootloader': 'BOOTLOADER', | 14 | """Return the rules.mk line(s) for a mapping rule. |
12 | 'processor': 'MCU', | 15 | """ |
13 | } | 16 | if not info_dict.get('to_c', True): |
17 | return None | ||
18 | |||
19 | info_key = info_dict['info_key'] | ||
20 | key_type = info_dict.get('value_type', 'str') | ||
21 | |||
22 | try: | ||
23 | rules_value = kb_info_json[info_key] | ||
24 | except KeyError: | ||
25 | return None | ||
26 | |||
27 | if key_type == 'array': | ||
28 | return f'{rules_key} ?= {" ".join(rules_value)}' | ||
29 | elif key_type == 'bool': | ||
30 | return f'{rules_key} ?= {"on" if rules_value else "off"}' | ||
31 | elif key_type == 'mapping': | ||
32 | return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()]) | ||
33 | |||
34 | return f'{rules_key} ?= {rules_value}' | ||
14 | 35 | ||
15 | 36 | ||
16 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | 37 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') |
@@ -22,7 +43,6 @@ info_to_rules = { | |||
22 | def generate_rules_mk(cli): | 43 | def generate_rules_mk(cli): |
23 | """Generates a rules.mk file from info.json. | 44 | """Generates a rules.mk file from info.json. |
24 | """ | 45 | """ |
25 | # Determine our keyboard(s) | ||
26 | if not cli.config.generate_rules_mk.keyboard: | 46 | if not cli.config.generate_rules_mk.keyboard: |
27 | cli.log.error('Missing paramater: --keyboard') | 47 | cli.log.error('Missing paramater: --keyboard') |
28 | cli.subcommands['info'].print_help() | 48 | cli.subcommands['info'].print_help() |
@@ -32,16 +52,18 @@ def generate_rules_mk(cli): | |||
32 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) | 52 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) |
33 | return False | 53 | return False |
34 | 54 | ||
35 | # Build the info.json file | 55 | kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard)) |
36 | kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) | 56 | info_rules_map = _json_load(Path('data/mappings/info_rules.json')) |
37 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] | 57 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] |
38 | 58 | ||
39 | # Bring in settings | 59 | # Iterate through the info_rules map to generate basic rules |
40 | for info_key, rule_key in info_to_rules.items(): | 60 | for rules_key, info_dict in info_rules_map.items(): |
41 | if info_key in kb_info_json: | 61 | new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict) |
42 | rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') | 62 | |
63 | if new_entry: | ||
64 | rules_mk_lines.append(new_entry) | ||
43 | 65 | ||
44 | # Find features that should be enabled | 66 | # Iterate through features to enable/disable them |
45 | if 'features' in kb_info_json: | 67 | if 'features' in kb_info_json: |
46 | for feature, enabled in kb_info_json['features'].items(): | 68 | for feature, enabled in kb_info_json['features'].items(): |
47 | if feature == 'bootmagic_lite' and enabled: | 69 | if feature == 'bootmagic_lite' and enabled: |
@@ -51,15 +73,6 @@ def generate_rules_mk(cli): | |||
51 | enabled = 'yes' if enabled else 'no' | 73 | enabled = 'yes' if enabled else 'no' |
52 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') | 74 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') |
53 | 75 | ||
54 | # Set the LED driver | ||
55 | if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: | ||
56 | driver = kb_info_json['led_matrix']['driver'] | ||
57 | rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') | ||
58 | |||
59 | # Add community layouts | ||
60 | if 'community_layouts' in kb_info_json: | ||
61 | rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') | ||
62 | |||
63 | # Show the results | 76 | # Show the results |
64 | rules_mk = '\n'.join(rules_mk_lines) + '\n' | 77 | rules_mk = '\n'.join(rules_mk_lines) + '\n' |
65 | 78 | ||
@@ -72,7 +85,7 @@ def generate_rules_mk(cli): | |||
72 | if cli.args.quiet: | 85 | if cli.args.quiet: |
73 | print(cli.args.output) | 86 | print(cli.args.output) |
74 | else: | 87 | else: |
75 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | 88 | cli.log.info('Wrote rules.mk to %s.', cli.args.output) |
76 | 89 | ||
77 | else: | 90 | else: |
78 | print(rules_mk) | 91 | print(rules_mk) |