title: GNU Make - Set MAKEFILE variable from shell command output within a rule/target
body: <p>I'm trying to put together some complicated makefile rules to automate building a project against multiple compilers. I have one rule that creates some dynamically generated variables and assigns variables to them, then passes those variables along to a call to build a separate rule.</p>
  
  <pre><code>.PHONY: all
  all:
      @echo "Detected CPULIST:${CPULIST_DETECTED}"
      @echo "CPULIST:${CPULIST}"
      @for cpu in $(CPULIST_DETECTED); do                   \
          echo "CPU:$${cpu}:";                                \
          eval VARIANTLIST_DETECTED=$(shell 2&gt;&amp;1 find ./.build/linux/$$cpu -mindepth 1 -maxdepth 1 | grep -v '\.svn' |  grep -v warning ); \
          eval echo "Detected Variant List:$${VARIANTLIST_DETECTED}"; \
          eval variant_$${cpu}=$${cpu};                      \
          eval echo "variant_\$${cpu}:\$${variant_$${cpu}}"; \
          $(MAKE) build CPULIST=$${cpu};                     \
      done
  
  .PHONY: build
  build: sanity_check $(TARGET)
      @true
  </code></pre>
  
  <p>I'm having two issues. The first is that, despite double-escaping <code>cpu</code> via <code>$$cpu</code>, it translates to null in the line:</p>
  
  <pre><code>eval VARIANTLIST_DETECTED=$(shell 2&gt;&amp;1 find ./.build/linux/$$cpu -mindepth 1 -maxdepth 1 | grep -v '\.svn' |  grep -v warning ); \
  </code></pre>
  
  <p>So, the find command searches against <code>./.build/linux/</code> for each iteration of the loop rather than looping through <code>./.build/linux/arm</code> and <code>./.build/linux/x86</code> like I would expect. I've included a post where this is described in the references below. I suspect this might be causing a problem because I'm attempting this within a rule itself rather than in the global assignment portion of the makefile (before the rules).</p>
  
  <p>The other problem is occurring at the exact same line. It seems that the <code>shell</code> command is evaluated, assigned to <code>VARIANTLIST_DETECTED</code>, but then <code>VARIANTLIST_DETECTED</code> is executed as if it were a command, since I get the following error during build:</p>
  
  <pre><code>Detected CPULIST:arm x86
  CPULIST:
  CPU:arm:
  /bin/sh: 3: ./.build/linux/x86: Permission denied
  Detected Variant List:
  variant_arm:arm
  </code></pre>
  
  <p>It's attempting to run the first result in my query as if it were a command. That line should also be something like <code>./.build/linux/x86/so</code>.</p>
  
  <p>How do I go about resolving these two issues? They are the last two thorns impeding completion of my makefiles.</p>
  
  <p>Thanks!</p>
  
  <p><strong>References</strong></p>
  
  <ol>
  <li><em>Assign a makefile variable value to a bash command result?</em>, Accessed 2014-06-19, <a href="https://stackoverflow.com/questions/2373081/assign-a-makefile-variable-value-to-a-bash-command-result"><code>&lt;https://stackoverflow.com/questions/2373081/assign-a-makefile-variable-value-to-a-bash-command-result&gt;</code></a> </li>
  </ol>