make: Pattern Match
1
1 10.5.4 How Patterns Match
1 -------------------------
1
1 A target pattern is composed of a '%' between a prefix and a suffix,
1 either or both of which may be empty. The pattern matches a file name
1 only if the file name starts with the prefix and ends with the suffix,
1 without overlap. The text between the prefix and the suffix is called
1 the "stem". Thus, when the pattern '%.o' matches the file name
1 'test.o', the stem is 'test'. The pattern rule prerequisites are turned
1 into actual file names by substituting the stem for the character '%'.
1 Thus, if in the same example one of the prerequisites is written as
1 '%.c', it expands to 'test.c'.
1
1 When the target pattern does not contain a slash (and it usually does
1 not), directory names in the file names are removed from the file name
1 before it is compared with the target prefix and suffix. After the
1 comparison of the file name to the target pattern, the directory names,
1 along with the slash that ends them, are added on to the prerequisite
1 file names generated from the pattern rule's prerequisite patterns and
1 the file name. The directories are ignored only for the purpose of
1 finding an implicit rule to use, not in the application of that rule.
1 Thus, 'e%t' matches the file name 'src/eat', with 'src/a' as the stem.
1 When prerequisites are turned into file names, the directories from the
1 stem are added at the front, while the rest of the stem is substituted
1 for the '%'. The stem 'src/a' with a prerequisite pattern 'c%r' gives
1 the file name 'src/car'.
1
1 A pattern rule can be used to build a given file only if there is a
1 target pattern that matches the file name, _and_ all prerequisites in
1 that rule either exist or can be built. The rules you write take
1 precedence over those that are built in. Note however, that a rule
1 whose prerequisites actually exist or are mentioned always takes
1 priority over a rule with prerequisites that must be made by chaining
1 other implicit rules.
1
1 It is possible that more than one pattern rule will meet these
1 criteria. In that case, 'make' will choose the rule with the shortest
1 stem (that is, the pattern that matches most specifically). If more
1 than one pattern rule has the shortest stem, 'make' will choose the
1 first one found in the makefile.
1
1 This algorithm results in more specific rules being preferred over
1 more generic ones; for example:
1
1 %.o: %.c
1 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
1
1 %.o : %.f
1 $(COMPILE.F) $(OUTPUT_OPTION) $<
1
1 lib/%.o: lib/%.c
1 $(CC) -fPIC -c $(CFLAGS) $(CPPFLAGS) $< -o $@
1
1 Given these rules and asked to build 'bar.o' where both 'bar.c' and
1 'bar.f' exist, 'make' will choose the first rule and compile 'bar.c'
1 into 'bar.o'. In the same situation where 'bar.c' does not exist, then
1 'make' will choose the second rule and compile 'bar.f' into 'bar.o'.
1
1 If 'make' is asked to build 'lib/bar.o' and both 'lib/bar.c' and
1 'lib/bar.f' exist, then the third rule will be chosen since the stem for
1 this rule ('bar') is shorter than the stem for the first rule
1 ('lib/bar'). If 'lib/bar.c' does not exist then the third rule is not
1 eligible and the second rule will be used, even though the stem is
1 longer.
1