summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/immediate-assignment.html
blob: eff5701ddfa46dff235fed7d37014bd0e89cf652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  <h1 class="subsection">Immediately Expanded Variable Assignment</h1>    <p>Another form of assignment allows for immediate expansion, but unlike simple assignment the resulting variable is recursive: it will be re-expanded again on every use. In order to avoid unexpected results, after the value is immediately expanded it will automatically be quoted: all instances of <code>$</code> in the value after expansion will be converted into <code>$$</code>. This type of assignment uses the ‘<samp>:::=</samp>’ operator. For example, </p> <div class="example"> <pre class="example">var = first
OUT :::= $(var)
var = second
</pre>
</div> <p>results in the <code>OUT</code> variable containing the text ‘<samp>first</samp>’, while here: </p> <div class="example"> <pre class="example">var = one$$two
OUT :::= $(var)
var = three$$four
</pre>
</div> <p>results in the <code>OUT</code> variable containing the text ‘<samp>one$$two</samp>’. The value is expanded when the variable is assigned, so the result is the expansion of the first value of <code>var</code>, ‘<samp>one$two</samp>’; then the value is re-escaped before the assignment is complete giving the final result of ‘<samp>one$$two</samp>’. </p> <p>The variable <code>OUT</code> is thereafter considered a recursive variable, so it will be re-expanded when it is used. </p> <p>This seems functionally equivalent to the ‘<samp>:=</samp>’ / ‘<samp>::=</samp>’ operators, but there are a few differences: </p> <p>First, after assignment the variable is a normal recursive variable; when you append to it with ‘<samp>+=</samp>’ the value on the right-hand side is not expanded immediately. If you prefer the ‘<samp>+=</samp>’ operator to expand the right-hand side immediately you should use the ‘<samp>:=</samp>’ / ‘<samp>::=</samp>’ assignment instead. </p> <p>Second, these variables are slightly less efficient than simply expanded variables since they do need to be re-expanded when they are used, rather than merely copied. However since all variable references are escaped this expansion simply un-escapes the value, it won’t expand any variables or run any functions. </p> <p>Here is another example: </p> <div class="example"> <pre class="example">var = one$$two
OUT :::= $(var)
OUT += $(var)
var = three$$four
</pre>
</div> <p>After this, the value of <code>OUT</code> is the text ‘<samp>one$$two $(var)</samp>’. When this variable is used it will be expanded and the result will be ‘<samp>one$two three$four</samp>’. </p> <p>This style of assignment is equivalent to the traditional BSD <code>make</code> ‘<samp>:=</samp>’ operator; as you can see it works slightly differently than the GNU <code>make</code> ‘<samp>:=</samp>’ operator. The <code>:::=</code> operator is added to the POSIX specification in Issue 8 to provide portability. </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/Immediate-Assignment.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Immediate-Assignment.html</a>
  </p>
</div>