1

I want to generate a list of equivalent targets:

TARGETLIST = a b c d e f 

define RULE
$(subst X,$(1),file_X.txt) $(subst X,$(1),otherfile_X.txt) &: # some dependency
    # some commands
endef

$(foreach _t, $(TARGETLIST), $(eval $(call RULE, $(_t))))

This does not do what I want: If I want to build file_b.txt for example the recipe is not found. How do I fix this?

HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
  • 1
    what do the dependencies look like and any relation the example elements `a b c ...` ? – Milag Aug 12 '20 at 15:01
  • @Milag That does not seem to matter. It does not work for no dependencies. – HerpDerpington Aug 12 '20 at 15:03
  • 2
    a `patsubst` could easily generate `file_a.txt file_b.txt ...` from the initial list, the result assigned to another var, and use that var leading a target rule; details of dependencies could vary the design – Milag Aug 12 '20 at 15:09
  • 1
    @Milag That does not work; I left some part out of my answer. See the updte. Each `file_X.dat` is grouped with another `otherfile_X.dat`. – HerpDerpington Aug 12 '20 at 15:11

1 Answers1

2

Your problem comes from the double expansion that takes place in your macro when calling foreach-eval-call. You need to double the $ of your $(subst...: $$(subst.... But why not simply letting call do your substitutions?

TARGETLIST = a b c d e f 

define RULE
file_$(1).txt otherfile_$(1).txt &: # some dependency
    # some commands
endef
$(foreach _t,$(TARGETLIST),$(eval $(call RULE,$(_t))))

call automatically replaces the $(1) occurrences in the RULE macro by the current $(_t) of the foreach loop. This is what call is made for.

Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51