hojusaram
hojusaram

Reputation: 517

make - Building multiple targets using pattern substitution

I would like to do something like this:

%_0.x: %.y
tool $^ -b 0 -o $@

%_1.x: %.y
    tool $^ -b 1 -o $@

.
.    
.
%_n.x: %.y
    tool $^ -b n -o $@

However, I would rather not list each rule separately as I don't know what n could be.

I tried to do something like this, thinking if I can get the %_0.x rule working then I can generalise it for any n. However this does not work.

%.x : $$(subst\ _0,.y,%)
    @echo Making $@ with option 0 using $^

%_1.x : %.y
    @echo Making $@ with option 1 using $^ -- $(subst _1.x,.y,$@)

file.y:
    @echo Making $@

Looking at make -p output, I see the following. Somehow the subst is not being expanded.

   Trying implicit prerequisite `$(subst _0,.y,file_0)'.
   Looking for a rule with intermediate file `$(subst _0,.y,file_0)'.
    Avoiding implicit rule recursion.

This is what my makefile outputs:

$ make file_0.x
make: *** No rule to make target `file_0.x'.  Stop.

$ make file_1.x
Making file.y
Making file_1.x with option 1 using file.y -- file.y

Upvotes: 0

Views: 53

Answers (2)

Eric
Eric

Reputation: 471

Instead of using subst, we could sed which is more flexible, like

.SECONDEXPANSION:
%.x: $$(shell echo %.y | sed 's/_[0-9]*//')
        @echo Making $@ with option $$(echo $@ | sed 's/.*_\([0-9]*\).*/\1/')  using $^

output likes,

touch file.y
make file_1.x
Making file_1.x with option 1 using file.y
make file_2.x
Making file_2.x with option 2 using file.y

Upvotes: 1

Beta
Beta

Reputation: 99094

One of the great shortcomings of Make is its inability to handle multiple wildcards.

I suggest this gruesome hack:

.SECONDEXPANSION:
%.x: $$(firstword $$(subst _, ,$$@)).y
    @echo Making $@ with option $(lastword $(subst _, ,$*)) using $^

Upvotes: 3

Related Questions