118 GNU make
Once make has decided to use the intermediate file, it is entered in the data base as if it had
been mentioned in the makefile, along with the implicit rule that says how to create it.
Intermediate files are remade using their rules just like all other files. But intermediate
files are treated differently in two ways.
The first difference is what happens if the intermediate file does not exist. If an ordinary
file b does not exist, and make considers a target that depends on b, it invariably creates b
and then updates the target from b. But if b is an intermediate file, then make can leave well
enough alone. It won’t bother updating b, or the ultimate target, unless some prerequisite
of b is newer than that target or there is some other reason to update that target.
The second difference is that if make does create b in order to update something else,
it deletes b later on after it is no longer needed. Therefore, an intermediate file which did
not exist before make also does not exist after make. make reports the deletion to you by
printing a ‘rm -f’ command showing which file it is deleting.
Ordinarily, a file cannot be intermediate if it is mentioned in the makefile as a target
or prerequisite. However, you can explicitly mark a file as intermediate by listing it as
a prerequisite of the special target .INTERMEDIATE. This takes effect even if the file is
mentioned explicitly in some other way.
You can prevent automatic deletion of an intermediate file by marking it as a secondary
file. To do this, list it as a prerequisite of the special target .SECONDARY. When a file
is secondary, make will not create the file merely because it does not already exist, but
make does not automatically delete the file. Marking a file as secondary also marks it as
intermediate.
You can list the target pattern of an implicit rule (such as ‘%.o’) as a prerequisite of the
special target .PRECIOUS to preserve intermediate files made by implicit rules whose target
patterns match that file’s name; see Section 5.6 [Interrupts], page 50.
A chain can involve more than two implicit rules. For example, it is possible to make a
file foo from RCS/foo.y,v by running RCS, Yacc and cc. Then both foo.y and foo.c are
intermediate files that are deleted at the end.
No single implicit rule can appear more than once in a chain. This means that make
will not even consider such a ridiculous thing as making foo from foo.o.o by running the
linker twice. This constraint has the added benefit of preventing any infinite loop in the
search for an implicit rule chain.
There are some special implicit rules to optimize certain cases that would otherwise
be handled by rule chains. For example, making foo from foo.c could be handled by
compiling and linking with separate chained rules, using foo.o as an intermediate file. But
what actually happens is that a special rule for this case does the compilation and linking
with a single cc command. The optimized rule is used in preference to the step-by-step
chain because it comes earlier in the ordering of rules.
10.5 Defining and Redefining Pattern Rules
You define an implicit rule by writing a pattern rule. A pattern rule looks like an ordinary
rule, except that its target contains the character ‘%’ (exactly one of them). The target is
considered a pattern for matching file names; the ‘%’ can match any nonempty substring,
while other characters match only themselves. The prerequisites likewise use ‘%’ to show
how their names relate to the target name.
Comentarios a estos manuales