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">Defining Canned Recipes</h1> <p>When the same sequence of commands is useful in making various targets, you can define it as a canned sequence with the <code>define</code> directive, and refer to the canned sequence from the recipes for those targets. The canned sequence is actually a variable, so the name must not conflict with other variable names. </p> <p>Here is an example of defining a canned recipe: </p> <div class="example"> <pre class="example">define run-yacc =
yacc $(firstword $^)
mv y.tab.c $@
endef
</pre>
</div> <p>Here <code>run-yacc</code> is the name of the variable being defined; <code>endef</code> marks the end of the definition; the lines in between are the commands. The <code>define</code> directive does not expand variable references and function calls in the canned sequence; the ‘<samp>$</samp>’ characters, parentheses, variable names, and so on, all become part of the value of the variable you are defining. See <a href="multi_002dline">Defining Multi-Line Variables</a>, for a complete explanation of <code>define</code>. </p> <p>The first command in this example runs Yacc on the first prerequisite of whichever rule uses the canned sequence. The output file from Yacc is always named <samp>y.tab.c</samp>. The second command moves the output to the rule’s target file name. </p> <p>To use the canned sequence, substitute the variable into the recipe of a rule. You can substitute it like any other variable (see <a href="reference">Basics of Variable References</a>). Because variables defined by <code>define</code> are recursively expanded variables, all the variable references you wrote inside the <code>define</code> are expanded now. For example: </p> <div class="example"> <pre class="example">foo.c : foo.y
$(run-yacc)
</pre>
</div> <p>‘<samp>foo.y</samp>’ will be substituted for the variable ‘<samp>$^</samp>’ when it occurs in <code>run-yacc</code>’s value, and ‘<samp>foo.c</samp>’ for ‘<samp>$@</samp>’. </p> <p>This is a realistic example, but this particular one is not needed in practice because <code>make</code> has an implicit rule to figure out these commands based on the file names involved (see <a href="implicit-rules">Using Implicit Rules</a>). </p> <p>In recipe execution, each line of a canned sequence is treated just as if the line appeared on its own in the rule, preceded by a tab. In particular, <code>make</code> invokes a separate sub-shell for each line. You can use the special prefix characters that affect command lines (‘<samp>@</samp>’, ‘<samp>-</samp>’, and ‘<samp>+</samp>’) on each line of a canned sequence. See <a href="recipes">Writing Recipes in Rules</a>. For example, using this canned sequence: </p> <div class="example"> <pre class="example">define frobnicate =
@echo "frobnicating target $@"
frob-step-1 $< -o $@-step-1
frob-step-2 $@-step-1 -o $@
endef
</pre>
</div> <p><code>make</code> will not echo the first line, the <code>echo</code> command. But it <em>will</em> echo the following two recipe lines. </p> <p>On the other hand, prefix characters on the recipe line that refers to a canned sequence apply to every line in the sequence. So the rule: </p> <div class="example"> <pre class="example">frob.out: frob.in
@$(frobnicate)
</pre>
</div> <p>does not echo <em>any</em> recipe lines. (See <a href="echoing">Recipe Echoing</a>, for a full explanation of ‘<samp>@</samp>’.) </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/Canned-Recipes.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Canned-Recipes.html</a>
</p>
</div>
|