summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/pattern_002dspecific.html
blob: 11ff5f3099d88ece3ca0b05c5e07ad2ff9398cd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <h1 class="section">Pattern-specific Variable Values</h1>   <p>In addition to target-specific variable values (see <a href="target_002dspecific">Target-specific Variable Values</a>), GNU <code>make</code> supports pattern-specific variable values. In this form, the variable is defined for any target that matches the pattern specified. </p> <p>Set a pattern-specific variable value like this: </p> <div class="example"> <pre class="example"><var>pattern</var> … : <var>variable-assignment</var>
</pre>
</div> <p>where <var>pattern</var> is a %-pattern. As with target-specific variable values, multiple <var>pattern</var> values create a pattern-specific variable value for each pattern individually. The <var>variable-assignment</var> can be any valid form of assignment. Any command line variable setting will take precedence, unless <code>override</code> is specified. </p> <p>For example: </p> <div class="example"> <pre class="example">%.o : CFLAGS = -O
</pre>
</div> <p>will assign <code>CFLAGS</code> the value of ‘<samp>-O</samp>’ for all targets matching the pattern <code>%.o</code>. </p> <p>If a target matches more than one pattern, the matching pattern-specific variables with longer stems are interpreted first. This results in more specific variables taking precedence over the more generic ones, for example: </p> <div class="example"> <pre class="example">%.o: %.c
        $(CC) -c $(CFLAGS) $(CPPFLAGS) $&lt; -o $@

lib/%.o: CFLAGS := -fPIC -g
%.o: CFLAGS := -g

all: foo.o lib/bar.o
</pre>
</div> <p>In this example the first definition of the <code>CFLAGS</code> variable will be used to update <samp>lib/bar.o</samp> even though the second one also applies to this target. Pattern-specific variables which result in the same stem length are considered in the order in which they were defined in the makefile. </p> <p>Pattern-specific variables are searched after any target-specific variables defined explicitly for that target, and before target-specific variables defined for the parent target. </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/Pattern_002dspecific.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Pattern_002dspecific.html</a>
  </p>
</div>