Jamie
Jamie

Reputation: 7411

How can I create make pattern rule that can expand the target as a dependency?

I'm trying to create a makefile that has rule something like:

~/$ cat > Makefile << EOF
FILES=a b c
$(FILES): % : src/%/%
        @echo "$@ $<"
EOF

Running the command gives:

~/$ make a
make: *** No rule to make target `src/a/%', needed by `a'.  Stop.

And what I'd like to see is:

~/$ make a
a src/a/a

How do a create rule to expand the second %?

Upvotes: 0

Views: 515

Answers (1)

Ise Wisteria
Ise Wisteria

Reputation: 11659

In this case, secondary expansion might help.
For example:

FILES=a b c
.SECONDEXPANSION:
$(FILES): % : src/$$*/$$*
    @echo "$@ $<"

If your GNU make's version is 3.80 or lower, $* might not work. In that case, $@ and some text manipulation might be needed instead.

Upvotes: 1

Related Questions