make: Prerequisite Types

1 
1 4.3 Types of Prerequisites
1 ==========================
1 
1 There are actually two different types of prerequisites understood by
1 GNU 'make': normal prerequisites such as described in the previous
1 section, and "order-only" prerequisites.  A normal prerequisite makes
1 two statements: first, it imposes an order in which recipes will be
1 invoked: the recipes for all prerequisites of a target will be completed
1 before the recipe for the target is run.  Second, it imposes a
1 dependency relationship: if any prerequisite is newer than the target,
1 then the target is considered out-of-date and must be rebuilt.
1 
1    Normally, this is exactly what you want: if a target's prerequisite
1 is updated, then the target should also be updated.
1 
1    Occasionally, however, you have a situation where you want to impose
1 a specific ordering on the rules to be invoked _without_ forcing the
1 target to be updated if one of those rules is executed.  In that case,
1 you want to define "order-only" prerequisites.  Order-only prerequisites
1 can be specified by placing a pipe symbol ('|') in the prerequisites
1 list: any prerequisites to the left of the pipe symbol are normal; any
1 prerequisites to the right are order-only:
1 
1      TARGETS : NORMAL-PREREQUISITES | ORDER-ONLY-PREREQUISITES
1 
1    The normal prerequisites section may of course be empty.  Also, you
1 may still declare multiple lines of prerequisites for the same target:
1 they are appended appropriately (normal prerequisites are appended to
1 the list of normal prerequisites; order-only prerequisites are appended
1 to the list of order-only prerequisites).  Note that if you declare the
1 same file to be both a normal and an order-only prerequisite, the normal
1 prerequisite takes precedence (since they have a strict superset of the
1 behavior of an order-only prerequisite).
1 
1    Consider an example where your targets are to be placed in a separate
1 directory, and that directory might not exist before 'make' is run.  In
1 this situation, you want the directory to be created before any targets
1 are placed into it but, because the timestamps on directories change
1 whenever a file is added, removed, or renamed, we certainly don't want
1 to rebuild all the targets whenever the directory's timestamp changes.
1 One way to manage this is with order-only prerequisites: make the
1 directory an order-only prerequisite on all the targets:
1 
1      OBJDIR := objdir
1      OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1 
1      $(OBJDIR)/%.o : %.c
1              $(COMPILE.c) $(OUTPUT_OPTION) $<
1 
1      all: $(OBJS)
1 
1      $(OBJS): | $(OBJDIR)
1 
1      $(OBJDIR):
1              mkdir $(OBJDIR)
1 
1    Now the rule to create the 'objdir' directory will be run, if needed,
1 before any '.o' is built, but no '.o' will be built because the 'objdir'
1 directory timestamp changed.
1