summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/parsing-makefiles.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gnu_make/parsing-makefiles.html
new repository
Diffstat (limited to 'devdocs/gnu_make/parsing-makefiles.html')
-rw-r--r--devdocs/gnu_make/parsing-makefiles.html23
1 files changed, 23 insertions, 0 deletions
diff --git a/devdocs/gnu_make/parsing-makefiles.html b/devdocs/gnu_make/parsing-makefiles.html
new file mode 100644
index 00000000..2210e939
--- /dev/null
+++ b/devdocs/gnu_make/parsing-makefiles.html
@@ -0,0 +1,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>