1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<h1 class="section">Setting Variables</h1> <p>To set a variable from the makefile, write a line starting with the variable name followed by one of the assignment operators ‘<samp>=</samp>’, ‘<samp>:=</samp>’, ‘<samp>::=</samp>’, or ‘<samp>:::=</samp>’. Whatever follows the operator and any initial whitespace on the line becomes the value. For example, </p> <div class="example"> <pre class="example">objects = main.o foo.o bar.o utils.o
</pre>
</div> <p>defines a variable named <code>objects</code> to contain the value ‘<samp>main.o foo.o bar.o utils.o</samp>’. Whitespace around the variable name and immediately after the ‘<samp>=</samp>’ is ignored. </p> <p>Variables defined with ‘<samp>=</samp>’ are <em>recursively expanded</em> variables. Variables defined with ‘<samp>:=</samp>’ or ‘<samp>::=</samp>’ are <em>simply expanded</em> variables; these definitions can contain variable references which will be expanded before the definition is made. Variables defined with ‘<samp>:::=</samp>’ are <em>immediately expanded</em> variables. The different assignment operators are described in See <a href="flavors">The Two Flavors of Variables</a>. </p> <p>The variable name may contain function and variable references, which are expanded when the line is read to find the actual variable name to use. </p> <p>There is no limit on the length of the value of a variable except the amount of memory on the computer. You can split the value of a variable into multiple physical lines for readability (see <a href="splitting-lines">Splitting Long Lines</a>). </p> <p>Most variable names are considered to have the empty string as a value if you have never set them. Several variables have built-in initial values that are not empty, but you can set them in the usual ways (see <a href="implicit-variables">Variables Used by Implicit Rules</a>). Several special variables are set automatically to a new value for each rule; these are called the <em>automatic</em> variables (see <a href="automatic-variables">Automatic Variables</a>). </p> <p>If you’d like a variable to be set to a value only if it’s not already set, then you can use the shorthand operator ‘<samp>?=</samp>’ instead of ‘<samp>=</samp>’. These two settings of the variable ‘<samp>FOO</samp>’ are identical (see <a href="origin-function">The <code>origin</code> Function</a>): </p> <div class="example"> <pre class="example">FOO ?= bar
</pre>
</div> <p>and </p> <div class="example"> <pre class="example">ifeq ($(origin FOO), undefined)
FOO = bar
endif
</pre>
</div> <p>The shell assignment operator ‘<samp>!=</samp>’ can be used to execute a shell script and set a variable to its output. This operator first evaluates the right-hand side, then passes that result to the shell for execution. If the result of the execution ends in a newline, that one newline is removed; all other newlines are replaced by spaces. The resulting string is then placed into the named recursively-expanded variable. For example: </p> <div class="example"> <pre class="example">hash != printf '\043'
file_list != find . -name '*.c'
</pre>
</div> <p>If the result of the execution could produce a <code>$</code>, and you don’t intend what follows that to be interpreted as a make variable or function reference, then you must replace every <code>$</code> with <code>$$</code> as part of the execution. Alternatively, you can set a simply expanded variable to the result of running a program using the <code>shell</code> function call. See <a href="shell-function">The <code>shell</code> Function</a>. For example: </p> <div class="example"> <pre class="example">hash := $(shell printf '\043')
var := $(shell find . -name "*.c")
</pre>
</div> <p>As with the <code>shell</code> function, the exit status of the just-invoked shell script is stored in the <code>.SHELLSTATUS</code> variable. </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/Setting.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Setting.html</a>
</p>
</div>
|