title: nested defines in makefile
body: <p>I'm very new to creating makefiles, so this question might be trivial. If I'm using bad coding practices, I would appreciate to be pointed at it.</p>
  
  <p>I am trying to create a generic makefile that creates rules based on input variables. At the end of my posting is a "minimal" example.</p>
  
  <p>I got 2 problems.</p>
  
  <ol>
  <li><p>In line 35 I instantiate the meta_template. I would expect the variable $(Dirs) to be expanded and then the meta_template to be called with each entry of $(Dirs). But it only expands till the second last, ie. the result of make terminates with:</p>
  
  <p><code>make: *** No rule to make target 'Dir03Target01Tag01', needed by 'Dir03'.  Stop.</code></p></li>
  <li><p>In line 21 I want to create a shortcut rule, a rule that prerequisites all tags to a given example.
  Eg: <code>Dir01Target02: Dir01Target02Tag04 Dir01Target02Tag05</code></p>
  
  <p>First I was puzzled that I couldn't use <code>$($$(1)Tags)</code> but had to use <code>$$($$(1)Tags)</code> to access the according Tags. (stored in Target01Tags for example).</p>
  
  <p>Using <code>$(addprefix)</code> does not seem to expand as I expected, ie the result of <code>make Dir01Target01</code> is:</p>
  
  <p><code>Create Rule Dir01Target01 with Dir01Target01Tag01 Tag02 Tag03</code></p>
  
  <p>where I expected it to be:</p>
  
  <p><code>Create Rule Dir01Target01 with Dir01Target01Tag01 Dir01Target01Tag02 Dir01Target01Tag03</code></p></li>
  </ol>
  
  <p><strong>edit:</strong> I found the answer to question 2. I had <code>$(addprefix $(1)$$(1),$$($$(1)Tags))</code>alter to <code>$$(addprefix $(1)$$(1),$$($$(1)Tags))</code>. I think it's because this needs to be expanded in 2 runs. So after the first run <code>$(addprefix Dirxx$(1),$($(1)Tags))</code> remains.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code># Targets to compile
  Targets = Target01 Target02
  
  # Tags to add to Targets
  Target01Tags = Tag01 Tag02 Tag03
  Target02Tags = Tag04 Tag05
  
  # Some List (in the actual makefile a list of directories $(wildcard NumeratedDir*))
  Dirs = Dir01 Dir02 Dir03
  
  all: $(Dirs)
  
  define meta_template
  $(1): $(foreach target,$(Targets),$(foreach tag,$($(target)Tags),$(1)$(target)$(tag)))
  
  define compile_meta
  # $(1) 1st argument of meta_compile (&lt;Dirs&gt;xx, eg Dir03)
  # $$(1) 1st argument of compile_template (&lt;Targets&gt;, eg Target01)
  
  $(1)$$(1): #$(addprefix $(1)$$(1),$($$(1)Tags))
      @echo Create Rule $(1)$$(1) with $(addprefix $(1)$$(1),$$($$(1)Tags))  &lt;---- Line21
  endef
  $(foreach target,$(Targets),$(eval $(call compile_meta,$(target))))
  
  define compile_template
  # $(1) 1st argument of meta_compile (&lt;Dirs&gt;xx, eg Dir03)
  # $$(1) 1st argument of compile_template (&lt;Targets&gt;, eg Target01)
  # $$(2) 2nd argument of compile_template (&lt;Targets&gt;Tags, eg Tag01)
  
  # create rule using arguments (eg Dir03Target01Tag01)
  $(1)$$(1)$$(2):
      @echo Create Rule $(1)$$(1)$$(2)
  
  endef
  $(foreach target,$(Targets),$(foreach tag,$($(target)Tags),$(eval $(call compile_template,$(target),$(tag))))) &lt;--- Line35
  
  endef
  $(foreach prog,$(Dirs),$(eval $(call meta_template,$(prog))))
  </code></pre>