Skip to content

Commit

Permalink
Resolve keyboard_aliases when processing keyboard make targets
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr committed Jan 20, 2025
1 parent cedd49c commit 79d4cba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
36 changes: 24 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ 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)

# As TRY_TO_MATCH_RULE_FROM_LIST_HELPER, but with additional
# resolution of DEFAULT_FOLDER and keyboard_aliases.hjson for provided rule
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 --allow-unknown $$(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_KB
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)
$$(eval $$(call $1,$$(ITEM$1)))
Expand All @@ -138,7 +161,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_KB,$$(shell $(QMK_BIN) list-keyboards)),true)
KEYBOARD_RULE=$$(MATCHED_ITEM)
$$(eval $$(call PARSE_KEYBOARD,$$(MATCHED_ITEM)))
else
Expand Down Expand Up @@ -170,17 +193,6 @@ define PARSE_KEYBOARD
# include the correct makefile to determine the actual name of it
CURRENT_KB := $1

# 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)))
Expand Down
1 change: 1 addition & 0 deletions lib/python/qmk/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
'qmk.cli.new.keymap',
'qmk.cli.painter',
'qmk.cli.pytest',
'qmk.cli.resolve_alias',
'qmk.cli.test.c',
'qmk.cli.userspace.add',
'qmk.cli.userspace.compile',
Expand Down
16 changes: 16 additions & 0 deletions lib/python/qmk/cli/resolve_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from qmk.keyboard import keyboard_folder

from milc import cli


@cli.argument('--allow-unknown', arg_only=True, action='store_true', help="Return original if rule is not a valid keyboard.")
@cli.argument('keyboard', arg_only=True, help='The keyboard\'s name')
@cli.subcommand('Resolve DEFAULT_FOLDER and any keyboard_aliases for provided rule')
def resolve_alias(cli):
try:
print(keyboard_folder(cli.args.keyboard))
except ValueError:
if cli.args.allow_unknown:
print(cli.args.keyboard)
else:
raise

0 comments on commit 79d4cba

Please sign in to comment.