qmk_firmware/lib/python/qmk/cli/generate/rules_mk.py
Zach White 8d9bfdc254
Add a lot more data to info.json (#13366)
* add some split data to info.json

* add tags

* add half of config_options.md to info.json

* add support for designating master split

* sort out split transport and primary

* fix bad data in UNUSED_PINS

* fixup custom transport

* wip

* allow for setting split right half keyboard matrix

* add SPLIT_USB_DETECT

* minor cleanup

* fix an erroneous message

* rework split.usb_detect

* adding missing rgblight vars to info.json

* add mouse_key to info.json

* add all remaining options from docs/config_options.md

* fix audio voices

* qmk info: Change text output to use dotted notation

* tweak layout output

* resolve alias names

* break out some functions to make flake8 happy

* add a field for bootloader instructions

* qmk generate-info-json: add a write-to-file argument

Adds an argument that instructs qmk generate-info-json to write the output to a file instead of just to the terminal.

* -arg_only, +action

Because it was never my intention that one would have to specify a value for the argument that enables writing the file.

* Bring qmk generate-info-json inline with other generate commands

* pytest fixup

* fix esca/getawayvan

* fix data driven errors for bpiphany converters

* features.force_nkro -> usb.force_nkro

* split.primary->split.main

* fix esca/getawayvan_f042

* fix the bpiphany converters for real

* fix bpiphany/tiger_lily

* Apply suggestions from code review

Co-authored-by: Nick Brassel <nick@tzarc.org>

* fix generate-api errors

* fix matrix pin extraction for split boards

* fix ploopyco/trackball_nano/rev1_001

Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com>
Co-authored-by: Nick Brassel <nick@tzarc.org>
2021-08-16 23:33:30 +01:00

109 lines
4.1 KiB
Python
Executable File

"""Used by the make system to generate a rules.mk
"""
from pathlib import Path
from dotty_dict import dotty
from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.json_schema import json_load
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard, normpath
def process_mapping_rule(kb_info_json, rules_key, info_dict):
"""Return the rules.mk line(s) for a mapping rule.
"""
if not info_dict.get('to_c', True):
return None
info_key = info_dict['info_key']
key_type = info_dict.get('value_type', 'str')
try:
rules_value = kb_info_json[info_key]
except KeyError:
return None
if key_type == 'array':
return f'{rules_key} ?= {" ".join(rules_value)}'
elif key_type == 'bool':
return f'{rules_key} ?= {"on" if rules_value else "off"}'
elif key_type == 'mapping':
return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()])
return f'{rules_key} ?= {rules_value}'
@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('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, 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.
"""
if not cli.config.generate_rules_mk.keyboard:
cli.log.error('Missing parameter: --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
kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard))
info_rules_map = json_load(Path('data/mappings/info_rules.json'))
rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', '']
# Iterate through the info_rules map to generate basic rules
for rules_key, info_dict in info_rules_map.items():
new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict)
if new_entry:
rules_mk_lines.append(new_entry)
# Iterate through features to enable/disable them
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 SPLIT_TRANSPORT, if needed
if kb_info_json.get('split', {}).get('transport', {}).get('protocol') == 'custom':
rules_mk_lines.append('SPLIT_TRANSPORT ?= custom')
# Set CUSTOM_MATRIX, if needed
if kb_info_json.get('matrix_pins', {}).get('custom'):
if kb_info_json.get('matrix_pins', {}).get('custom_lite'):
rules_mk_lines.append('CUSTOM_MATRIX ?= lite')
else:
rules_mk_lines.append('CUSTOM_MATRIX ?= yes')
# 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.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(rules_mk)
if cli.args.quiet:
if cli.args.escape:
print(cli.args.output.as_posix().replace(' ', '\\ '))
else:
print(cli.args.output)
else:
cli.log.info('Wrote rules.mk to %s.', cli.args.output)
else:
print(rules_mk)