Workaround in Makefile for recursive rule matching (#15988)

* Bodge for recursive rule matching

* Bodge for recursive rule matching - force python3

* Tidy up
This commit is contained in:
Joel Challis 2022-01-25 00:50:35 +00:00 committed by GitHub
parent 5f23345886
commit 7df371750f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,53 +87,20 @@ endef
# a function that returns the value # a function that returns the value
COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND) COMPARE_AND_REMOVE_FROM_RULE = $(eval $(call COMPARE_AND_REMOVE_FROM_RULE_HELPER,$1))$(RULE_FOUND)
# Try to find a match for the start of the rule to be checked
# Recursively try to find a match for the start of the rule to be checked
# $1 The list to be checked
# If a match is found, then RULE_FOUND is set to true
# and MATCHED_ITEM to the item that was matched
define TRY_TO_MATCH_RULE_FROM_LIST_HELPER3
ifneq ($1,)
ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,$$(firstword $1)),true)
MATCHED_ITEM := $$(firstword $1)
else
$$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$$(wordlist 2,9999,$1)))
endif
endif
endef
# A recursive helper function for finding the longest match
# $1 The list to be checked
# It works by always removing the currently matched item from the list
define TRY_TO_MATCH_RULE_FROM_LIST_HELPER2
# Stop the recursion when the list is empty
ifneq ($1,)
RULE_BEFORE := $$(RULE)
$$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER3,$1))
# If a match is found in the current list, otherwise just return what we had before
ifeq ($$(RULE_FOUND),true)
# Save the best match so far and call itself recursively
BEST_MATCH := $$(MATCHED_ITEM)
BEST_MATCH_RULE := $$(RULE)
RULE_FOUND := false
RULE := $$(RULE_BEFORE)
$$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$$(filter-out $$(MATCHED_ITEM),$1)))
endif
endif
endef
# Recursively try to find the longest match for the start of the rule to be checked
# $1 The list to be checked # $1 The list to be checked
# If a match is found, then RULE_FOUND is set to true # If a match is found, then RULE_FOUND is set to true
# and MATCHED_ITEM to the item that was matched # and MATCHED_ITEM to the item that was matched
define TRY_TO_MATCH_RULE_FROM_LIST_HELPER define TRY_TO_MATCH_RULE_FROM_LIST_HELPER
BEST_MATCH := # Split on ":", padding with empty strings to avoid indexing issues
$$(eval $$(call TRY_TO_MATCH_RULE_FROM_LIST_HELPER2,$1)) TOKEN1:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[0])" $$(RULE))
ifneq ($$(BEST_MATCH),) TOKENr:=$$(shell python3 -c "import sys; print((sys.argv[1].split(':',1)+[''])[1])" $$(RULE))
FOUNDx:=$$(shell echo $1 | tr " " "\n" | grep -Fx $$(TOKEN1))
ifneq ($$(FOUNDx),)
RULE := $$(TOKENr)
RULE_FOUND := true RULE_FOUND := true
RULE := $$(BEST_MATCH_RULE) MATCHED_ITEM := $$(TOKEN1)
MATCHED_ITEM := $$(BEST_MATCH)
else else
RULE_FOUND := false RULE_FOUND := false
MATCHED_ITEM := MATCHED_ITEM :=