summaryrefslogtreecommitdiffstats
path: root/lib/python/qmk/cli/generate/rules_mk.py
blob: b262e3c66610bbedd5ff30271f4d538274b806c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Used by the make system to generate a rules.mk
"""
from milc import cli

from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.path import is_keyboard, normpath

info_to_rules = {
    'board': 'BOARD',
    'bootloader': 'BOOTLOADER',
    'processor': 'MCU',
}


@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.')
@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
@automagic_keyboard
@automagic_keymap
def generate_rules_mk(cli):
    """Generates a rules.mk file from info.json.
    """
    # Determine our keyboard(s)
    if not cli.config.generate_rules_mk.keyboard:
        cli.log.error('Missing paramater: --keyboard')
        cli.subcommands['info'].print_help()
        return False

    if not is_keyboard(cli.config.generate_rules_mk.keyboard):
        cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard)
        return False

    # Build the info.json file
    kb_info_json = info_json(cli.config.generate_rules_mk.keyboard)
    rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', '']

    # Bring in settings
    for info_key, rule_key in info_to_rules.items():
        if info_key in kb_info_json:
            rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}')

    # Find features that should be enabled
    if 'features' in kb_info_json:
        for feature, enabled in kb_info_json['features'].items():
            if feature == 'bootmagic_lite' and enabled:
                rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite')
            else:
                feature = feature.upper()
                enabled = 'yes' if enabled else 'no'
                rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}')

    # Set the LED driver
    if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']:
        driver = kb_info_json['led_matrix']['driver']
        rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}')

    # Add community layouts
    if 'community_layouts' in kb_info_json:
        rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}')

    # Show the results
    rules_mk = '\n'.join(rules_mk_lines) + '\n'

    if cli.args.output:
        cli.args.output.parent.mkdir(parents=True, exist_ok=True)
        if cli.args.output.exists():
            cli.args.output.replace(cli.args.output.name + '.bak')
        cli.args.output.write_text(rules_mk)

        if cli.args.quiet:
            print(cli.args.output)
        else:
            cli.log.info('Wrote info_config.h to %s.', cli.args.output)

    else:
        print(rules_mk)