Tom
Tom

Reputation: 4592

Makefile using wildcard and the stem for prerequisites

So I have a Makefile:

broken-%: $(wildcard src/%/*)
    @echo $*
    @echo $^

working-%: src/a/*
    @echo $*
    @echo $^

$* is % (the stem) and $^ is prerequisites

And the directory structure is

│   Makefile
│
└───src
    └───a
            main.java

Using the Makefile:

> make broken-a
a

> make working-a
a
src/a/main.java
> 

Both of these should have the same outputs but they don't.

For some reason $(wildcard src/%/*) with % set to a is returning nothing. It might be because wildcards in prerequisites are evaluated automatically so src/%/* is evaluated before the wildcard function is called?

Does anyone know how to get this working?

Upvotes: 7

Views: 2233

Answers (1)

user657267
user657267

Reputation: 21000

Patterns aren't expanded until the rule is actually applied during the second phase, so when the file is first parsed by make you're calling wildcard with the literal string src/%/*.

Secondary expansion can be used to work around this

.SECONDEXPANSION:
broken-%: $$(wildcard src/%/*)
    @echo $*
    @echo $^

Upvotes: 9

Related Questions