manpagez: man pages & more
info make
Home | html | info | man

File: make.info,  Node: Pattern Intro,  Next: Pattern Examples,  Prev: Pattern_Rules.php">Pattern Rules,  Up: Pattern_Rules.php">Pattern Rules

10.5.1 Introduction to Pattern Rules
------------------------------------

A pattern rule contains the character '%' (exactly one of them) in the
target; otherwise, it looks exactly like an ordinary rule.  The target
is a pattern for matching file names; the '%' matches any nonempty
substring, while other characters match only themselves.

   For example, '%.c' as a pattern matches any file name that ends in
'.c'.  's.%.c' as a pattern matches any file name that starts with 's.',
ends in '.c' and is at least five characters long.  (There must be at
least one character to match the '%'.)  The substring that the '%'
matches is called the "stem".

   '%' in a prerequisite of a pattern rule stands for the same stem that
was matched by the '%' in the target.  In order for the pattern rule to
apply, its target pattern must match the file name under consideration
and all of its prerequisites (after pattern substitution) must name
files that exist or can be made.  These files become prerequisites of
the target.

   Thus, a rule of the form

     %.o : %.c ; RECIPE...

specifies how to make a file 'N.o', with another file 'N.c' as its
prerequisite, provided that 'N.c' exists or can be made.

   There may also be prerequisites that do not use '%'; such a
prerequisite attaches to every file made by this pattern rule.  These
unvarying prerequisites are useful occasionally.

   A pattern rule need not have any prerequisites that contain '%', or
in fact any prerequisites at all.  Such a rule is effectively a general
wildcard.  It provides a way to make any file that matches the target
pattern.  *Note Last Resort::.

   More than one pattern rule may match a target.  In this case 'make'
will choose the "best fit" rule.  *Note How Patterns Match: Pattern
Match.

   Pattern rules may have more than one target; however, every target
must contain a '%' character.  Multiple target patterns in pattern rules
are always treated as grouped targets (*note Multiple Targets in a Rule:
Multiple Targets.) regardless of whether they use the ':' or '&:'
separator.

   There is one exception: if a pattern target is out of date or does
not exist and the makefile does not need to build it, then it will not
cause the other targets to be considered out of date.  Note that this
historical exception will be removed in future versions of GNU 'make'
and should not be relied on.  If this situation is detected 'make' will
generate a warning _pattern recipe did not update peer target_; however,
'make' cannot detect all such situations.  Please be sure that your
recipe updates _all_ the target patterns when it runs.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.