From af1f965c823b18f1e0f9dc0bb1f2590d612e37ab Mon Sep 17 00:00:00 2001 From: zvecr Date: Thu, 14 Mar 2024 13:29:58 +0000 Subject: [PATCH 1/2] Deprecate DEFAULT_FOLDER --- Makefile | 17 ++++------------- data/mappings/info_rules.hjson | 1 + lib/python/qmk/cli/ci/validate_aliases.py | 6 +----- lib/python/qmk/cli/list/keyboards.py | 3 +-- lib/python/qmk/cli/migrate.py | 4 ++-- lib/python/qmk/info.py | 11 ----------- lib/python/qmk/keyboard.py | 22 ++++++++++++++-------- 7 files changed, 23 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 5fcd6bbf0f3..92d1ef61832 100644 --- a/Makefile +++ b/Makefile @@ -138,7 +138,7 @@ define PARSE_RULE $$(eval $$(call PARSE_TEST)) # If the rule starts with the name of a known keyboard, then continue # the parsing from PARSE_KEYBOARD - else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(shell $(QMK_BIN) list-keyboards --no-resolve-defaults)),true) + else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(shell $(QMK_BIN) list-keyboards)),true) KEYBOARD_RULE=$$(MATCHED_ITEM) $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM))) else @@ -172,15 +172,6 @@ define PARSE_KEYBOARD # KEYBOARD_FOLDERS := $$(subst /, , $(CURRENT_KB)) - DEFAULT_FOLDER := $$(CURRENT_KB) - - # We assume that every rules.mk will contain the full default value - $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/rules.mk) - ifneq ($$(DEFAULT_FOLDER),$$(CURRENT_KB)) - $$(eval include $(ROOT_DIR)/keyboards/$$(DEFAULT_FOLDER)/rules.mk) - endif - CURRENT_KB := $$(DEFAULT_FOLDER) - # 5/4/3/2/1 KEYBOARD_FOLDER_PATH_1 := $$(CURRENT_KB) KEYBOARD_FOLDER_PATH_2 := $$(patsubst %/,%,$$(dir $$(KEYBOARD_FOLDER_PATH_1))) @@ -242,7 +233,7 @@ endef # if we are going to compile all keyboards, match the rest of the rule # for each of them define PARSE_ALL_KEYBOARDS - $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(shell $(QMK_BIN) list-keyboards --no-resolve-defaults))) + $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYBOARD,$(shell $(QMK_BIN) list-keyboards))) endef # Prints a list of all known keymaps for the given keyboard @@ -434,7 +425,7 @@ git-submodules: git-submodule .PHONY: list-keyboards list-keyboards: - $(QMK_BIN) list-keyboards --no-resolve-defaults | tr '\n' ' ' + $(QMK_BIN) list-keyboards | tr '\n' ' ' .PHONY: list-tests list-tests: @@ -442,7 +433,7 @@ list-tests: .PHONY: generate-keyboards-file generate-keyboards-file: - $(QMK_BIN) list-keyboards --no-resolve-defaults + $(QMK_BIN) list-keyboards .PHONY: clean clean: diff --git a/data/mappings/info_rules.hjson b/data/mappings/info_rules.hjson index 64972af63b0..340b3fe711d 100644 --- a/data/mappings/info_rules.hjson +++ b/data/mappings/info_rules.hjson @@ -55,5 +55,6 @@ // Items we want flagged in lint "CTPC": {"info_key": "_deprecated.ctpc", "deprecated": true, "replace_with": "CONVERT_TO=proton_c"}, "CONVERT_TO_PROTON_C": {"info_key": "_deprecated.ctpc", "deprecated": true, "replace_with": "CONVERT_TO=proton_c"}, + "DEFAULT_FOLDER": {"info_key": "_deprecated.default_folder", "deprecated": true} "VIAL_ENABLE": {"info_key": "_invalid.vial", "invalid": true} } diff --git a/lib/python/qmk/cli/ci/validate_aliases.py b/lib/python/qmk/cli/ci/validate_aliases.py index 7f781d43970..8b062dbe566 100644 --- a/lib/python/qmk/cli/ci/validate_aliases.py +++ b/lib/python/qmk/cli/ci/validate_aliases.py @@ -2,7 +2,7 @@ """ from milc import cli -from qmk.keyboard import resolve_keyboard, keyboard_folder, keyboard_alias_definitions +from qmk.keyboard import keyboard_folder, keyboard_alias_definitions def _safe_keyboard_folder(target): @@ -17,10 +17,6 @@ def _target_keyboard_exists(target): if not target: return False - # If the target directory existed but there was no rules.mk or rules.mk was incorrectly parsed, then we can't build it. - if not resolve_keyboard(target): - return False - # If the target directory exists but it itself has an invalid alias or invalid rules.mk, then we can't build it either. if not _safe_keyboard_folder(target): return False diff --git a/lib/python/qmk/cli/list/keyboards.py b/lib/python/qmk/cli/list/keyboards.py index 405b9210e4c..8b6c4516733 100644 --- a/lib/python/qmk/cli/list/keyboards.py +++ b/lib/python/qmk/cli/list/keyboards.py @@ -5,10 +5,9 @@ from milc import cli import qmk.keyboard -@cli.argument('--no-resolve-defaults', arg_only=True, action='store_false', help='Ignore any "DEFAULT_FOLDER" within keyboards rules.mk') @cli.subcommand("List the keyboards currently defined within QMK") def list_keyboards(cli): """List the keyboards currently defined within QMK """ - for keyboard_name in qmk.keyboard.list_keyboards(cli.args.no_resolve_defaults): + for keyboard_name in qmk.keyboard.list_keyboards(): print(keyboard_name) diff --git a/lib/python/qmk/cli/migrate.py b/lib/python/qmk/cli/migrate.py index 0bab5c1949f..d0f195d7376 100644 --- a/lib/python/qmk/cli/migrate.py +++ b/lib/python/qmk/cli/migrate.py @@ -6,14 +6,14 @@ from dotty_dict import dotty from milc import cli -from qmk.keyboard import keyboard_completer, keyboard_folder, resolve_keyboard +from qmk.keyboard import keyboard_completer, keyboard_folder from qmk.info import info_json, find_info_json from qmk.json_encoders import InfoJSONEncoder from qmk.json_schema import json_load def _candidate_files(keyboard): - kb_dir = Path(resolve_keyboard(keyboard)) + kb_dir = Path(keyboard) cur_dir = Path('keyboards') files = [] diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index 5948b66b5e4..9d2d3f0e549 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -215,12 +215,6 @@ def _validate(keyboard, info_data): def info_json(keyboard): """Generate the info.json data for a specific keyboard. """ - cur_dir = Path('keyboards') - root_rules_mk = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') - - if 'DEFAULT_FOLDER' in root_rules_mk: - keyboard = root_rules_mk['DEFAULT_FOLDER'] - info_data = { 'keyboard_name': str(keyboard), 'keyboard_folder': str(keyboard), @@ -991,11 +985,6 @@ def find_info_json(keyboard): keyboard_parent = keyboard_path.parent info_jsons = [keyboard_path / 'info.json', keyboard_path / 'keyboard.json'] - # Add DEFAULT_FOLDER before parents, if present - rules = rules_mk(keyboard) - if 'DEFAULT_FOLDER' in rules: - info_jsons.append(Path(rules['DEFAULT_FOLDER']) / 'info.json') - # Add in parent folders for least specific for _ in range(5): if keyboard_parent == base_path: diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index fcf5b5b1588..8d4112eeaf0 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -72,7 +72,15 @@ base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep @lru_cache(maxsize=1) def keyboard_alias_definitions(): - return json_load(Path('data/mappings/keyboard_aliases.hjson')) + data = json_load(Path('data/mappings/keyboard_aliases.hjson')) + + # Add fake entries to maintain DEFAULT_FOLDER logic for CLI + for kb in list_keyboards(False): + res = _resolve_keyboard(kb) + if res != kb: + data[kb] = {'target': res} + + return data def is_all_keyboards(keyboard): @@ -131,8 +139,6 @@ def keyboard_folder(keyboard): if keyboard == last_keyboard: break - keyboard = resolve_keyboard(keyboard) - if not qmk.path.is_keyboard(keyboard): raise ValueError(f'Invalid keyboard: {keyboard}') @@ -144,7 +150,7 @@ def keyboard_aliases(keyboard): Includes the keyboard itself. """ - aliases = json_load(Path('data/mappings/keyboard_aliases.hjson')) + aliases = keyboard_alias_definitions() if keyboard in aliases: keyboard = aliases[keyboard].get('target', keyboard) @@ -190,13 +196,13 @@ def list_keyboards(resolve_defaults=True): found = map(_find_name, paths) if resolve_defaults: - found = map(resolve_keyboard, found) + found = map(_resolve_keyboard, found) return sorted(set(found)) @lru_cache(maxsize=None) -def resolve_keyboard(keyboard): +def _resolve_keyboard(keyboard): cur_dir = Path('keyboards') rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') while 'DEFAULT_FOLDER' in rules and keyboard != rules['DEFAULT_FOLDER']: @@ -216,7 +222,7 @@ def config_h(keyboard): """ config = {} cur_dir = Path('keyboards') - keyboard = Path(resolve_keyboard(keyboard)) + keyboard = Path(keyboard) for dir in keyboard.parts: cur_dir = cur_dir / dir @@ -235,7 +241,7 @@ def rules_mk(keyboard): a dictionary representing the content of the entire rules.mk tree for a keyboard """ cur_dir = Path('keyboards') - keyboard = Path(resolve_keyboard(keyboard)) + keyboard = Path(keyboard) rules = parse_rules_mk_file(cur_dir / keyboard / 'rules.mk') for i, dir in enumerate(keyboard.parts): From a5fd4fd83e198a7ff067d8f32cafa64d37afa7be Mon Sep 17 00:00:00 2001 From: zvecr Date: Sat, 10 Aug 2024 14:38:17 +0100 Subject: [PATCH 2/2] Resolve alias within Makefile --- Makefile | 21 ++++++++++++++++++++- keyboards/kprepublic/jj40/rules.mk | 1 - lib/python/qmk/cli/__init__.py | 1 + lib/python/qmk/cli/resolve_alias.py | 8 ++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) delete mode 100644 keyboards/kprepublic/jj40/rules.mk create mode 100644 lib/python/qmk/cli/resolve_alias.py diff --git a/Makefile b/Makefile index 92d1ef61832..389b7b6595a 100644 --- a/Makefile +++ b/Makefile @@ -110,8 +110,27 @@ define TRY_TO_MATCH_RULE_FROM_LIST_HELPER endif endef +define TRY_TO_MATCH_RULE_FROM_LIST_HELPER_KB + # Split on ":", padding with empty strings to avoid indexing issues + TOKEN1:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[0])" $$(RULE)) + TOKENr:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[1])" $$(RULE)) + + TOKEN1:=$$(shell $(QMK_BIN) resolve-alias $$(TOKEN1)) + + FOUNDx:=$$(shell echo $1 | tr " " "\n" | grep -Fx $$(TOKEN1)) + ifneq ($$(FOUNDx),) + RULE := $$(TOKENr) + RULE_FOUND := true + MATCHED_ITEM := $$(TOKEN1) + else + RULE_FOUND := false + MATCHED_ITEM := + endif +endef + # Make it easier to call TRY_TO_MATCH_RULE_FROM_LIST TRY_TO_MATCH_RULE_FROM_LIST = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER,$1))$(RULE_FOUND) +TRY_TO_MATCH_RULE_FROM_LIST_KB = $(eval $(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER_KB,$1))$(RULE_FOUND) define ALL_IN_LIST_LOOP OLD_RULE$1 := $$(RULE) @@ -138,7 +157,7 @@ define PARSE_RULE $$(eval $$(call PARSE_TEST)) # If the rule starts with the name of a known keyboard, then continue # the parsing from PARSE_KEYBOARD - else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(shell $(QMK_BIN) list-keyboards)),true) + else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST_KB,$$(shell $(QMK_BIN) list-keyboards)),true) KEYBOARD_RULE=$$(MATCHED_ITEM) $$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM))) else diff --git a/keyboards/kprepublic/jj40/rules.mk b/keyboards/kprepublic/jj40/rules.mk deleted file mode 100644 index fa09523958c..00000000000 --- a/keyboards/kprepublic/jj40/rules.mk +++ /dev/null @@ -1 +0,0 @@ -DEFAULT_FOLDER = kprepublic/jj40/rev1 diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 0baf19a629d..293af1c0538 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -31,6 +31,7 @@ safe_commands = [ ] subcommands = [ + 'qmk.cli.resolve_alias', 'qmk.cli.ci.validate_aliases', 'qmk.cli.bux', 'qmk.cli.c2json', diff --git a/lib/python/qmk/cli/resolve_alias.py b/lib/python/qmk/cli/resolve_alias.py new file mode 100644 index 00000000000..60edca93eed --- /dev/null +++ b/lib/python/qmk/cli/resolve_alias.py @@ -0,0 +1,8 @@ +from qmk.keyboard import keyboard_folder + +from milc import cli + +@cli.argument('keyboard', arg_only=True, help='TODO.') +@cli.subcommand('TODO') +def resolve_alias(cli): + print(keyboard_folder(cli.args.keyboard))