summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/substitution-refs.html
blob: 4b6b000f577b0da74131c954b84196901ac00be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
  <h1 class="subsection">Substitution References</h1>       <p>A <em>substitution reference</em> substitutes the value of a variable with alterations that you specify. It has the form ‘<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>’ (or ‘<samp>${<var>var</var>:<var>a</var>=<var>b</var>}</samp>’) and its meaning is to take the value of the variable <var>var</var>, replace every <var>a</var> at the end of a word with <var>b</var> in that value, and substitute the resulting string. </p> <p>When we say “at the end of a word”, we mean that <var>a</var> must appear either followed by whitespace or at the end of the value in order to be replaced; other occurrences of <var>a</var> in the value are unaltered. For example: </p> <div class="example"> <pre class="example">foo := a.o b.o l.a c.o
bar := $(foo:.o=.c)
</pre>
</div> <p>sets ‘<samp>bar</samp>’ to ‘<samp>a.c b.c l.a c.c</samp>’. See <a href="setting">Setting Variables</a>. </p> <p>A substitution reference is shorthand for the <code>patsubst</code> expansion function (see <a href="text-functions">Functions for String Substitution and Analysis</a>): ‘<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>’ is equivalent to ‘<samp>$(patsubst %<var>a</var>,%<var>b</var>,<var>var</var>)</samp>’. We provide substitution references as well as <code>patsubst</code> for compatibility with other implementations of <code>make</code>. </p>  <p>Another type of substitution reference lets you use the full power of the <code>patsubst</code> function. It has the same form ‘<samp>$(<var>var</var>:<var>a</var>=<var>b</var>)</samp>’ described above, except that now <var>a</var> must contain a single ‘<samp>%</samp>’ character. This case is equivalent to ‘<samp>$(patsubst <var>a</var>,<var>b</var>,$(<var>var</var>))</samp>’. See <a href="text-functions">Functions for String Substitution and Analysis</a>, for a description of the <code>patsubst</code> function. For example: </p> <div class="example"> <pre class="example">foo := a.o b.o l.a c.o
bar := $(foo:%.o=%.c)
</pre>
</div> <p>sets ‘<samp>bar</samp>’ to ‘<samp>a.c b.c l.a c.c</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/Substitution-Refs.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Substitution-Refs.html</a>
  </p>
</div>