summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/prerequisite-types.html
blob: 394d9dc96cdd031fa8f32e6a4f5c4ba5ecdbe58c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  <h1 class="section">Types of Prerequisites</h1>       <p>There are two different types of prerequisites understood by GNU <code>make</code>: normal prerequisites, described in the previous section, and <em>order-only</em> prerequisites. A normal prerequisite makes two statements: first, it imposes an order in which recipes will be invoked: the recipes for all prerequisites of a target will be completed before the recipe for the target is started. Second, it imposes a dependency relationship: if any prerequisite is newer than the target, then the target is considered out-of-date and must be rebuilt. </p> <p>Normally, this is exactly what you want: if a target’s prerequisite is updated, then the target should also be updated. </p> <p>Occasionally you may want to ensure that a prerequisite is built before a target, but <em>without</em> forcing the target to be updated if the prerequisite is updated. <em>Order-only</em> prerequisites are used to create this type of relationship. Order-only prerequisites can be specified by placing a pipe symbol (<code>|</code>) in the prerequisites list: any prerequisites to the left of the pipe symbol are normal; any prerequisites to the right are order-only: </p> <div class="example"> <pre class="example"><var>targets</var> : <var>normal-prerequisites</var> | <var>order-only-prerequisites</var>
</pre>
</div> <p>The normal prerequisites section may of course be empty. Also, you may still declare multiple lines of prerequisites for the same target: they are appended appropriately (normal prerequisites are appended to the list of normal prerequisites; order-only prerequisites are appended to the list of order-only prerequisites). Note that if you declare the same file to be both a normal and an order-only prerequisite, the normal prerequisite takes precedence (since they have a strict superset of the behavior of an order-only prerequisite). </p> <p>Order-only prerequisites are never checked when determining if the target is out of date; even order-only prerequisites marked as phony (see <a href="phony-targets">Phony Targets</a>) will not cause the target to be rebuilt. </p> <p>Consider an example where your targets are to be placed in a separate directory, and that directory might not exist before <code>make</code> is run. In this situation, you want the directory to be created before any targets are placed into it but, because the timestamps on directories change whenever a file is added, removed, or renamed, we certainly don’t want to rebuild all the targets whenever the directory’s timestamp changes. One way to manage this is with order-only prerequisites: make the directory an order-only prerequisite on all the targets: </p> <div class="example"> <pre class="example">OBJDIR := objdir
OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)

$(OBJDIR)/%.o : %.c
        $(COMPILE.c) $(OUTPUT_OPTION) $&lt;

all: $(OBJS)

$(OBJS): | $(OBJDIR)

$(OBJDIR):
        mkdir $(OBJDIR)
</pre>
</div> <p>Now the rule to create the <samp>objdir</samp> directory will be run, if needed, before any ‘<samp>.o</samp>’ is built, but no ‘<samp>.o</samp>’ will be built because the <samp>objdir</samp> directory timestamp changed. </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/Prerequisite-Types.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html</a>
  </p>
</div>