multi-wildcard pattern rules of GNU Make

Viewed 13851

I want to write something like regex:

SRC:="a.dat.1 a.dat.2"    
$(SRC): %.dat.%: (\\1).rlt.(\\2)    
      dat2rlt $^ $@

so that a.dat.1 and a.dat.2 will give a.rlt.1 and a.rlt.2.

In GNU Make info page, it says "the % can be used only once".

Is there some trick to achieve this in GNU Make?

3 Answers

Using named variables, we can write more readable code (based on answer of Paljas):

letters:=a b c
numbers:=1 2 3 4

define GEN_RULE
$(letter).dat.$(number) : $(letter).rlt.$(number)
    ./rlt2dat $$< $$@
endef

$(foreach number,$(numbers), \
  $(foreach letter,$(letters), \
    $(eval $(GEN_RULE)) \
  ) \
)

We can generate SRC in a similar way. Note that using that method SRC will contain all the combinations. That may or may not be beneficial.

Related