summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/parsing-makefiles.html
blob: 2210e939b8832428e26506f2e6618315e1bffada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  <h1 class="section">How Makefiles Are Parsed</h1>   <p>GNU <code>make</code> parses makefiles line-by-line. Parsing proceeds using the following steps: </p> <ol> <li> Read in a full logical line, including backslash-escaped lines (see <a href="splitting-lines">Splitting Long Lines</a>). </li>
<li> Remove comments (see <a href="makefile-contents">What Makefiles Contain</a>). </li>
<li> If the line begins with the recipe prefix character and we are in a rule context, add the line to the current recipe and read the next line (see <a href="recipe-syntax">Recipe Syntax</a>). </li>
<li> Expand elements of the line which appear in an <em>immediate</em> expansion context (see <a href="reading-makefiles">How <code>make</code> Reads a Makefile</a>). </li>
<li> Scan the line for a separator character, such as ‘<samp>:</samp>’ or ‘<samp>=</samp>’, to determine whether the line is a macro assignment or a rule (see <a href="recipe-syntax">Recipe Syntax</a>). </li>
<li> Internalize the resulting operation and read the next line. </li>
</ol> <p>An important consequence of this is that a macro can expand to an entire rule, <em>if it is one line long</em>. This will work: </p> <div class="example"> <pre class="example">myrule = target : ; echo built

$(myrule)
</pre>
</div> <p>However, this will not work because <code>make</code> does not re-split lines after it has expanded them: </p> <div class="example"> <pre class="example">define myrule
target:
        echo built
endef

$(myrule)
</pre>
</div> <p>The above makefile results in the definition of a target ‘<samp>target</samp>’ with prerequisites ‘<samp>echo</samp>’ and ‘<samp>built</samp>’, as if the makefile contained <code>target: echo built</code>, rather than a rule with a recipe. Newlines still present in a line after expansion is complete are ignored as normal whitespace. </p> <p>In order to properly expand a multi-line macro you must use the <code>eval</code> function: this causes the <code>make</code> parser to be run on the results of the expanded macro (see <a href="eval-function">Eval Function</a>). </p><div class="_attribution">
  <p class="_attribution-p">
    Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br>
    <a href="https://www.gnu.org/software/make/manual/html_node/Parsing-Makefiles.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Parsing-Makefiles.html</a>
  </p>
</div>