A pattern-matching rule is similar to an implicit rule in function. Pattern-matching rules are easier to write, and more powerful, because you can specify a relationship between a target and a dependency based on prefixes (including path names) and suffixes, or both. A pattern-matching rule is a target entry of the form:
tp%ts: dp%ds rule
where tp and ts are the optional prefix and suffix in the target name, dp and ds are the (optional) prefix and suffix in the dependency name, and % is a wild card that stands for a base name common to both.
make checks for pattern-matching rules ahead of suffix rules. While this allows you to override the standard implicit rules, it is not recommended.
If there is no rule for building a target, make searches for a pattern-matching rule, before checking for a suffix rule. If make can use a pattern-matching rule, it does so.
If the target entry for a pattern-matching rule contains no rule, make processes the target file as if it had an explicit target entry with no rule; make therefore searches for a suffix rule, attempts to retrieve a version of the target file from SCCS, and finally, treats the target as having a null rule (flagging that target as updated in the current run).
A pattern-matching rule for formatting a troff source file into a troff output file looks like:
%.tr: %.ms troff -t -ms $< > $@
The following tables show the standard set of suffix rules and predefined macros supplied to make in the default makefile, /usr/share/lib/make/make.rules.
Table 4-8 Standard Suffix Rules
Use |
Suffix Rule Name |
Command Line(s) |
---|---|---|
Assembly Files | .s.o | $(COMPILE.s) -o $@ $< |
.s | $(COMPILE.s) -o $@ $< | |
.s.a | $(COMPILE.s) -o $% $< | |
$(AR) $(ARFLAGS) $@ $% | ||
$(RM) $% | ||
.S.o | $(COMPILE.S) -o $@ $< | |
.S.a | $(COMPILE.S) -o $% $ | |
$(AR) $(ARFLAGS) $@ $% | ||
$(RM) $% | ||
C Files (.c Rules) | .c | $(LINK.c) -o $@ $< $(LDLIBS) |
.c.ln | $(LINT.c) $(OUTPUT_OPTION) -i $< | |
.c.o | $(COMPILE.c) $(OUTPUT_OPTION) $< | |
.c.a | $(COMPILE.c) -o $% $< | |
$(AR) $(ARFLAGS) $@ $% | ||
$(RM) $% | ||
C++ Files | .cc | $(LINK.cc) -o $@ $< $(LDLIBS) |
.cc.o | $(COMPILE.cc) $(OUTPUT_OPTION) $< | |
.cc.a | $(COMPILE.cc) -o $% $< | |
$(AR) $(ARFLAGS) $@ $% | ||
$(RM) $% | ||
C++ Files (SVr4 style) | .C | $(LINK.C) -o $@ $< $(LDFLAGS) $*.c |
.C.o | $(COMPILE.C) $< | |
.C.a | $(COMPILE.C) $< | |
$(AR) $(ARFLAGS) $@ $*.o | ||
$(RM) -f $*.o | ||
FORTRAN 77 Files | .cc.o | $(LINK.f) -o $@ $< $(LDLIBS) |
.cc.a | $(COMPILE.f) $(OUTPUT_OPTION) $< | |
$(COMPILE.f) -o $% $< | ||
$(AR) $(ARFLAGS) $@ $% | ||
| $(RM) $% | |
.F | $(LINK.F) -o $@ $< $(LDLIBS) | |
.F.o | $(COMPILE.F) $(OUTPUT_OPTION) $< | |
.F.a | $(COMPILE.F) -o $% $< | |
$(AR) $(ARFLAGS) $@ $% | ||
$(RM) $% | ||
lex Files | .l | $(RM) $*.c |
$(LEX.l) $< > $*.c | ||
$(LINK.c) -o $@ $*.c $(LDLIBS) | ||
$(RM) $*.c | ||
.l.c | $(RM) $@ | |
$(LEX.l) $< > $@ | ||
.l.ln | $(RM) $*.c | |
$(LEX.l) $< > $*.c | ||
$(LINT.c) -o $@ -i $*.c | ||
$(RM) $*.c | ||
.l.o | $(RM) $*.c | |
$(LEX.l) $< > $*.c | ||
$(COMPILE.c) -o $@ $*.c | ||
$(RM) $*.c | ||
.L.C | $(LEX) $(LFLAGS) $< | |
.L.o | $(LEX)(LFLAGS) $< | |
$(COMPILE.C) lex.yy.c | ||
.L.o | rm -f lex.yy.c | |
mv lex.yy.o $@ | ||
Modula 2 Files | .mod | $(COMPILE.mod) -o $@ -e $@ $< |
.mod.o | $(COMPILE.mod) -o $@ $< | |
.def.sym | $(COMPILE.def) -o $@ $< | |
NeWS | .cps.h | $(CPS) $(CPSFLAGS) $*.cps |
Pascal Files | .p | $(LINK.p) -o $@ $< $(LDLIBS) |
.p.o | $(COMPILE.p) $(OUTPUT_OPTION) $< | |
Ratfor Files | .r | $(LINK.r) -o $@ $< $(LDLIBS) |
.r.o | $(COMPILE.r) $(OUTPUT_OPTION) $< | |
.r.a | $(COMPILE.r) -o $% $< | |
$(AR) $(ARFLAGS) $@ $% | ||
$(RM) $% | ||
Shell Scripts | .sh | $(RM) $@ |
cat $< >$@ | ||
chmod +x $@ | ||
yacc Files (.yc Rules) | .y | $(YACC.y) $< |
$(LINK.c) -o $@ y.tab.c $(LDLIBS) | ||
$(RM) y.tab.c | ||
.y.c | $(YACC.y) $< | |
mv y.tab.c $@ | ||
.y.ln | $(YACC.y) $< | |
$(LINT.c) -o $@ -i y.tab.c | ||
$(RM) y.tab.c | ||
.y.o | $(YACC.y) $< | |
$(COMPILE.c) -o $@ y.tab.c | ||
$(RM) y.tab.c | ||
yacc Files (SVr4) | .Y.C | $(YACC) $(YFLAGS) $< |
mv y.tab.c $@ | ||
.Y.o | $(YACC) $(YFLAGS) $< | |
$(COMPILE.c) y.tab.c | ||
rm -f y.tab.c | ||
mv y.tab.o $@ |
Table 4-9 Predefined and Dynamic Macros