title: "define" and "endef" syntax-rules in makefile
body: <p><strong><em>From the <a href="https://www.gnu.org/software/make/manual/html_node/Multi_002dLine.html#Multi_002dLine" rel="nofollow">docs</a>:</em></strong></p>
  
  <blockquote>
    <p>Note that lines beginning with the recipe prefix character are
    considered part of a recipe, so any <code>define</code> or <code>endef</code> strings
    appearing on such a line will not be considered <code>make</code> directives.</p>
  </blockquote>
  
  <p><br></p>
  
  <p>My worry is, that is <strong><em>not</em></strong> always the case.
  <br></p>
  
  <p><strong><em>As evident, by the following makfile:</em></strong></p>
  
  <pre class="lang-make prettyprint-override"><code>ifeq "x" "y"
  define xxx
  else
  foo = 1
      endef
  else
  bar = 2
  endif
  
  
  
  all ::
      @echo 'foo is: "$(foo)"'
      @echo 'bar is: "$(bar)"'
  </code></pre>
  
  <p><strong><em>Running, we get:</em></strong></p>
  
  <pre class="lang-console prettyprint-override"><code>$ make
  foo is: ""
  bar is: "2"
  </code></pre>
  
  <p><br>
  <br></p>
  
  <p>Going back to the makefile, it is evident that the lines <strong><em>3</em></strong> and <strong><em>4</em></strong> inside the <code>ifeq</code> directive, i.e.</p>
  
  <pre class="lang-make prettyprint-override"><code>ifeq "x" "y"
  define xxx
  else
  foo = 1
      endef
  else
  bar = 2
  endif
  </code></pre>
  
  <p>are ignored by Make.</p>
  
  <p>Put simply, make ignores these 2 lines, within the <code>define xx</code></p>
  
  <pre class="lang-make prettyprint-override"><code>else
  foo = 1
  </code></pre>
  
  <p>Because they are inside a <code>define</code>, and therefore, Make does not evaluate the <code>else</code> as a <em>directive</em> (i.e. as a <em>else</em> part, for <code>ifeq "x" "y"</code>).</p>
  
  <p>But, Make clearly parses that <code>endef</code> line (<strong><em>5th</em></strong> line in the <code>ifeq</code> <em>block</em>), as a directive to <strong><em>end</em></strong> the <em>define</em> directive.</p>
  
  <p>Hence, after ending the <code>define</code> <em>block</em> with the <code>endef</code> line, Make will parse later lines, that is:</p>
  
  <pre class="lang-make prettyprint-override"><code>else
  bar = 2
  endif
  </code></pre>
  
  <p>As part of the <code>ifeq</code> block, and because the opening directive <code>ifeq "x" "y"</code> evaluated to <code>false</code>, Make will take the <code>else</code> part as a <code>true</code> statement, and assign the value <code>2</code> to the variable <code>bar</code>, and so on.</p>
  
  <p>Why is all this important?</p>
  
  <p>Because, clearly, Make allows here a <code>endef</code> line to be parsed as a directive, even though, it is prefixed with a <code>&lt;tab&gt;</code> character.</p>
  
  <p>That is in clear violation of the documentation, quoted above:</p>
  
  <blockquote>
    <p>Note that lines beginning with the recipe prefix character are
    considered part of a recipe, so any <code>define</code> or <code>endef</code> strings
    appearing on such a line will not be considered <code>make</code> directives.</p>
  </blockquote>
  
  <p>Agree?</p>