Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the string matched by '%' from a pattern rule in pattern matching functions

Tags:

makefile

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: "$+
like image 839
Tudor Berariu Avatar asked May 24 '26 05:05

Tudor Berariu


1 Answers

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: $+'
like image 197
Etan Reisner Avatar answered May 26 '26 12:05

Etan Reisner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!