summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/target_002dspecific.html
blob: da3359f536dab0a48268464f90ac3ccb0fecb967 (plain)
1
2
3
4
5
6
7
8
9
10
11
  <h1 class="section">Target-specific Variable Values</h1>   <p>Variable values in <code>make</code> are usually global; that is, they are the same regardless of where they are evaluated (unless they’re reset, of course). Exceptions to that are variables defined with the <code>let</code> function (see <a href="let-function">Let Function</a>) or the <code>foreach</code> function (see <a href="foreach-function">Foreach Function</a>, and automatic variables (see <a href="automatic-variables">Automatic Variables</a>). </p> <p>Another exception are <em>target-specific variable values</em>. This feature allows you to define different values for the same variable, based on the target that <code>make</code> is currently building. As with automatic variables, these values are only available within the context of a target’s recipe (and in other target-specific assignments). </p> <p>Set a target-specific variable value like this: </p> <div class="example"> <pre class="example"><var>target</var> … : <var>variable-assignment</var>
</pre>
</div> <p>Target-specific variable assignments can be prefixed with any or all of the special keywords <code>export</code>, <code>unexport</code>, <code>override</code>, or <code>private</code>; these apply their normal behavior to this instance of the variable only. </p> <p>Multiple <var>target</var> values create a target-specific variable value for each member of the target list individually. </p> <p>The <var>variable-assignment</var> can be any valid form of assignment; recursive (‘<samp>=</samp>’), simple (‘<samp>:=</samp>’ or ‘<samp>::=</samp>’), immediate (‘<samp>::=</samp>’), appending (‘<samp>+=</samp>’), or conditional (‘<samp>?=</samp>’). All variables that appear within the <var>variable-assignment</var> are evaluated within the context of the target: thus, any previously-defined target-specific variable values will be in effect. Note that this variable is actually distinct from any “global” value: the two variables do not have to have the same flavor (recursive vs. simple). </p> <p>Target-specific variables have the same priority as any other makefile variable. Variables provided on the command line (and in the environment if the ‘<samp>-e</samp>’ option is in force) will take precedence. Specifying the <code>override</code> directive will allow the target-specific variable value to be preferred. </p> <p>There is one more special feature of target-specific variables: when you define a target-specific variable that variable value is also in effect for all prerequisites of this target, and all their prerequisites, etc. (unless those prerequisites override that variable with their own target-specific variable value). So, for example, a statement like this: </p> <div class="example"> <pre class="example">prog : CFLAGS = -g
prog : prog.o foo.o bar.o
</pre>
</div> <p>will set <code>CFLAGS</code> to ‘<samp>-g</samp>’ in the recipe for <samp>prog</samp>, but it will also set <code>CFLAGS</code> to ‘<samp>-g</samp>’ in the recipes that create <samp>prog.o</samp>, <samp>foo.o</samp>, and <samp>bar.o</samp>, and any recipes which create their prerequisites. </p> <p>Be aware that a given prerequisite will only be built once per invocation of make, at most. If the same file is a prerequisite of multiple targets, and each of those targets has a different value for the same target-specific variable, then the first target to be built will cause that prerequisite to be built and the prerequisite will inherit the target-specific value from the first target. It will ignore the target-specific values from any other targets. </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/Target_002dspecific.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html</a>
  </p>
</div>