Multiple stems (%) in a Makefile prerequisite

Viewed 54

I'm trying to use the following Makefile to create the target foo.a with foo.foo.b as a prerequisite, using the stem character (%) as shown.

%.a: %.%.b
    touch $@

However running touch foo.foo.b; make foo.a doesn't work with No rule to make target. The debug output below shows that the stem % is only expanded once. How to get the desired behavior of replacing all % in the prerequisite?

Considering target file 'sdf.a'.
 File 'sdf.a' does not exist.
 Looking for an implicit rule for 'sdf.a'.
 Trying implicit prerequisite 'sdf.%.b'.
 Looking for a rule with intermediate file 'sdf.%.b'.
  Avoiding implicit rule recursion.
  Trying pattern rule with stem 'sdf.%.b'.
  Trying implicit prerequisite 'sdf.%.b,v'.
  (..)
 No implicit rule found for 'sdf.a'.
 Finished prerequisites of target file 'sdf.a'.
Must remake target 'sdf.a'.
make: *** No rule to make target 'sdf.a'.  Stop.

Side note: this seems like a trivial question but for some reason I can't find the answer, maybe I'm using the wrong search terms or overlooking something simple, sorry if that is the case.

1 Answers

The stem is only replaced once. If you wish to have stem contents inserted more than once, you would need to use secondary expansion, i.e.:

$ cat Makefile
.SECONDEXPANSION:

%.a: $$*.$$*.b
        echo Making $@ from $<

Output:

$ touch foo.foo.b
$ make -s foo.a
Making foo.a from foo.foo.b
Related