title: Why does patsubst stop working when using secondary expansion of $$*?
body: <p>Here is an example from the <a href="http://www.gnu.org/software/make/manual/make.html#Secondary-Expansion" rel="noreferrer">GNU Make manual section on Secondary Expansion</a> (slightly simplified):</p>
  
  <pre><code>foo_SRCS := bar.c baz.c
  
  .SECONDEXPANSION:
  # $$@ expands to the target ("foo" in this case)
  foo: $$(patsubst %.c,%.o,$$($$@_SRCS))
  </code></pre>
  
  <p>This works great; it builds <code>bar.o</code> and <code>baz.o</code>:</p>
  
  <pre><code>cc    -c -o bar.o bar.c
  cc    -c -o baz.o baz.c
  </code></pre>
  
  <p>But if I tweak this example only slightly, the <code>patsubst</code> stops working:</p>
  
  <pre><code>all: foo.a
  
  foo_SRCS := bar.c baz.c
  
  .SECONDEXPANSION:
  # $$* expands to the stem of the match ("foo" in this case).
  %.a: $$(patsubst %.c,%.o,$$($$*_SRCS))
          ar rcs $@ $^
  </code></pre>
  
  <p>It is no longer building <code>bar.o</code> and <code>baz.o</code>, and instead is using the <code>*.c</code> files directly as prerequisites!</p>
  
  <pre><code>ar rcs foo.a bar.c baz.c
  </code></pre>
  
  <p>Please note that the <code>$$($$*_SRCS)</code> part is clearly working, as evidenced by the fact that it found <code>foo_SRCS</code> and used that as the prerequisites.  But for some reason the <code>patsubst</code> part has become a no-op!  Instead of replacing <code>%.c</code> with <code>%.o</code>, it is just using <code>foo_SRCS</code> directly.</p>
  
  <p>What is going on here?  How can I get my example to work?</p>
  
  <p>EDIT: I had a theory that the <code>%</code> characters inside the <code>patsubst</code> were getting evaluated early, using the stem match (foo), so that the patsubst itself was looking something like this:</p>
  
  <pre><code>$(patsubst foo.c,foo.o,bar.c baz.c)
  </code></pre>
  
  <p>To test this theory, I added a file called <code>foo.c</code> to <code>foo_SRCS</code>:</p>
  
  <pre><code>all: foo.a
  
  foo_SRCS := foo.c bar.c baz.c
  
  .SECONDEXPANSION:
  # $$* expands to the stem of the match ("foo" in this case).
  %.a: $$(patsubst %.c,%.o,$$($$*_SRCS))
          ar rcs $@ $^
  </code></pre>
  
  <p>That resulted in something even weirder:</p>
  
  <pre><code>make: *** No rule to make target `foo.a', needed by `all'.  Stop.
  </code></pre>