make: Pattern Intro
1
1 10.5.1 Introduction to Pattern Rules
1 ------------------------------------
1
1 A pattern rule contains the character '%' (exactly one of them) in the
1 target; otherwise, it looks exactly like an ordinary rule. The target
1 is a pattern for matching file names; the '%' matches any nonempty
1 substring, while other characters match only themselves.
1
1 For example, '%.c' as a pattern matches any file name that ends in
1 '.c'. 's.%.c' as a pattern matches any file name that starts with 's.',
1 ends in '.c' and is at least five characters long. (There must be at
1 least one character to match the '%'.) The substring that the '%'
1 matches is called the "stem".
1
1 '%' in a prerequisite of a pattern rule stands for the same stem that
1 was matched by the '%' in the target. In order for the pattern rule to
1 apply, its target pattern must match the file name under consideration
1 and all of its prerequisites (after pattern substitution) must name
1 files that exist or can be made. These files become prerequisites of
1 the target.
1
1 Thus, a rule of the form
1
1 %.o : %.c ; RECIPE...
1
1 specifies how to make a file 'N.o', with another file 'N.c' as its
1 prerequisite, provided that 'N.c' exists or can be made.
1
1 There may also be prerequisites that do not use '%'; such a
1 prerequisite attaches to every file made by this pattern rule. These
1 unvarying prerequisites are useful occasionally.
1
1 A pattern rule need not have any prerequisites that contain '%', or
1 in fact any prerequisites at all. Such a rule is effectively a general
1 wildcard. It provides a way to make any file that matches the target
1 pattern. ⇒Last Resort.
1
1 More than one pattern rule may match a target. In this case 'make'
11 will choose the "best fit" rule. ⇒How Patterns Match Pattern
Match.
1
1 Pattern rules may have more than one target. Unlike normal rules,
1 this does not act as many different rules with the same prerequisites
1 and recipe. If a pattern rule has multiple targets, 'make' knows that
1 the rule's recipe is responsible for making all of the targets. The
1 recipe is executed only once to make all the targets. When searching
1 for a pattern rule to match a target, the target patterns of a rule
1 other than the one that matches the target in need of a rule are
1 incidental: 'make' worries only about giving a recipe and prerequisites
1 to the file presently in question. However, when this file's recipe is
1 run, the other targets are marked as having been updated themselves.
1