I have two sets of files $(Xs) and $(Ys). Each .x file depends on an arbitrary number of .y files based on its name. For each <name>.x file I have a number of <name>_*.y files.
I can write individual rules for the .x files and use a function to compute the dependencies for it.
.PHONY: build
Xs = a.x b.x
Ys = a_1.y a_2.y b_1.y b_2.y
build: $(Xs)
a.x: $(filter a%,$(Ys))
@echo $@" with dependencies: "$+
b.x: $(filter b%,$(Ys))
@echo $@" with dependencies: "$+
%.y:
@echo "y : "$@
... or I could write a pattern rule for all .x files and enumerate the dependencies
$(Xs) : %.x : %_1.y %_2.y
@echo $@" with dependencies: "$+
But can I do both at the same time? I don't know how to get the string matched by % in the rule and use it in $(filter).
$(Xs) : %.x : $(filter ???,$(Ys))
@echo $@" with dependencies: "$+
You can do this with Secondary Expansion and a Canned Recipe (needed to work around the double use of % in filter and the static pattern rule's prereq list.
.PHONY: build
Xs = a.x b.x
Ys = a_1.y a_2.y b_1.y b_2.y
build: $(Xs)
%.y:
@echo 'y : $@'
define yf
$(filter $(1)_%.y,$(Ys))
endef
.SECONDEXPANSION:
$(Xs) : %.x : $$(call yf,%)
@echo '$@ with dependencies: $+'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With