Make: How to get prerequisite N

Viewed 68

Let's say I have a rule like this.

cmd is badly written, so I actually can't switch the order of the flags. -f must go before -d.

out: foo.txt mydir fizz.txt
    cmd -f fizz.txt -d mydir foo.txt

Is it possible to get the Nth prerequisite so that I can rewrite my rule something like this (pseudocode)?

out: foo.txt mydir fizz.txt
    cmd -f $^[2] -d $^[1] $<
1 Answers

Is it possible to get the Nth prerequisite

You can get the Nth word from a list with $(word N,list), which should do the trick (untested):

out: foo.txt mydir fizz.txt
    cmd -f $(word 3,$^) -d $(word 2,$^) $<

Note that indexing words starts from 1, not 0. For details see https://www.gnu.org/software/make/manual/make.html#Text-Functions

Related