Programming Utilities Guide

Pattern-Matching Rules: An Alternative to Suffix Rules

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.


Note -

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 $< > $@

make's Default Suffix Rules and Predefined Macros

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.orm -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

Use 

Macro 

Default Value  

Library Archive 

ARar
ARFLAGSrv

Assembler Commands 

ASas
ASFLAGS
COMPILE.s$(AS) $(ASFLAGS)
COMPILE.S$(CC) $(ASFLAGS) $(CPPFLAGS) -target -c

C Compiler Commands 

CCcc
CFLAGS
CPPFLAGS
COMPILE.c$(CC) $(CFLAGS) $(CPPFLAGS) -c
LINK.c $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)

C++ Compiler Commands [For backward compatibility, the C++ macros have alternate forms. For C++C, you can instead use CCC; instead of C++FLAGS, you can use CCFLAGS; for COMPILE.C, you can use COMPILE.cc; and LINK.cc can be substituted for LINK.C. Note that these alternate forms will disappear for future releases.]

CCCCC
CCFLAGS
COMPILE.cc$(CCC) $(CCFLAGS) $(CPPFLAGS) -c
LINK.cc$(CCC) $(CCFLAGS) $(CPPFLAGS) $(LDFLAGS)

C++ SVr4 Compiler Commands 

(C++C)CC
(C++FLAGS)-O
COMPILE.C$(C++C) $(C++FLAGS) $(CPPFLAGS) -c
LINK.C$(C++C) $(C++FLAGS) $(CPPFLAGS) $(LDFLAGS) -target

FORTRAN 77 Compiler Commands 

FC in SVr4f77
FFLAGS
COMPILE.f$(FC) $(FFLAGS) -c

LINK.f  

$(FC) $(FFLAGS) $(LDFLAGS)
COMPILE.F$(FC) $(FFLAGS) $(CPPFLAGS) -c
LINK.F $(FC) $(FFLAGS) $(CPPFLAGS) $(LDFLAGS)

Link Editor Command 

LDld
LDFLAGS

lex Command 

LEXlex
LFLAGS
LEX.l $(LEX) $(LFLAGS) -t

lint Command 

LINTlint
LINTFLAGS
LINT.c$(LINT) $(LINTFLAGS) $(CPPFLAGS)

Modula 2 Commands 

M2Cm2c
M2FLAGS
MODFLAGS
DEFFLAGS
COMPILE.def$(M2C) $(M2FLAGS) $(DEFFLAGS)
COMPILE.mod$(M2C) $(M2FLAGS) $(MODFLAGS)

NeWS  

CPScps
CPSFLAGS

Pascal Compiler Commands 

PCpc
PFLAGS
COMPILE.p$(PC) $(PFLAGS) $(CPPFLAGS) -c
LINK.p$(PC) $(PFLAGS) $(CPPFLAGS) $(LDFLAGS)

Ratfor Compilation Commands 

RFLAGS
COMPILE.r$(FC) $(FFLAGS) $(RFLAGS) -c
LINK.r$(FC) $(FFLAGS) $(RFLAGS) $(LDFLAGS)

rm Command 

RMrm -f

yacc Command 

YACCyacc
YFLAGS
YACC.y$(YACC) $(YFLAGS)

Suffixes List 

SUFFIXES.o .c .c~ .cc .cc~ .C .C~ .y .y~ .l .l~ .s .s~ .sh .sh~ .S .S~ .ln .h .h~ .f .f~ .F .F~ .mod .mod~ .sym .def .def~ .p .p~ .r .r~ .cps .cps~ .Y .Y~ .L .L~
 

SCCS get Command 

.SCCS_GETsccs $(SCCSFLAGS) get $(SCCSGETFLAGS) $@ -G$@
SCCSGETFLAGS-s