Brian Malehorn
Brian Malehorn

Reputation: 2685

"%" in $(wildcard) not expanded?

I want to encode the the rule "to make <name>.done, you need all files of the pattern <name>.needed.*. I've attempted to write this with this Makefile:

%.done: $(wildcard %.needed.*)
    cat $^ > $@

Yet when I run touch foo.needed.bar && make foo.done, all I get is

cat  > foo.done

It appears the % inside $(wildcard) is being interpreted as a literal "%". How can I get it expanded to the right value ("foo" in this case)?

Upvotes: 0

Views: 825

Answers (1)

Kusalananda
Kusalananda

Reputation: 15613

The % is just a placeholder for "any string" in pattern matching. It has no special meaning in the wildcard function and is interpreted literally.

You might attempt using $* instead (which would expand to the stem of the filename), but unfortunately it won't work either:

%.done: $(wildcard $*.needed.*)

The reason it doesn't work is that the automatic variables ($* is one of them) are not available for use in the dependency list.

The workaround is to request a secondary expansion for the target:

.SECONDEXPANSION:
%.done: $$(wildcard $$*.needed.*)

This will prompt GNU Make to go over the rule a second time after processing the Makefile as usual, expanding any escaped variables that weren't expanded the first time around. The second time around, the automatic variable have their appropriate values.

Upvotes: 3

Related Questions