make: Chained Rules

1 
1 10.4 Chains of Implicit Rules
1 =============================
1 
1 Sometimes a file can be made by a sequence of implicit rules.  For
1 example, a file 'N.o' could be made from 'N.y' by running first Yacc and
1 then 'cc'.  Such a sequence is called a "chain".
1 
1    If the file 'N.c' exists, or is mentioned in the makefile, no special
1 searching is required: 'make' finds that the object file can be made by
1 C compilation from 'N.c'; later on, when considering how to make 'N.c',
1 the rule for running Yacc is used.  Ultimately both 'N.c' and 'N.o' are
1 updated.
1 
1    However, even if 'N.c' does not exist and is not mentioned, 'make'
1 knows how to envision it as the missing link between 'N.o' and 'N.y'!
1 In this case, 'N.c' is called an "intermediate file".  Once 'make' has
1 decided to use the intermediate file, it is entered in the data base as
1 if it had been mentioned in the makefile, along with the implicit rule
1 that says how to create it.
1 
1    Intermediate files are remade using their rules just like all other
1 files.  But intermediate files are treated differently in two ways.
1 
1    The first difference is what happens if the intermediate file does
1 not exist.  If an ordinary file B does not exist, and 'make' considers a
1 target that depends on B, it invariably creates B and then updates the
1 target from B.  But if B is an intermediate file, then 'make' can leave
1 well enough alone.  It won't bother updating B, or the ultimate target,
1 unless some prerequisite of B is newer than that target or there is some
1 other reason to update that target.
1 
1    The second difference is that if 'make' _does_ create B in order to
1 update something else, it deletes B later on after it is no longer
1 needed.  Therefore, an intermediate file which did not exist before
1 'make' also does not exist after 'make'.  'make' reports the deletion to
1 you by printing a 'rm -f' command showing which file it is deleting.
1 
1    Ordinarily, a file cannot be intermediate if it is mentioned in the
1 makefile as a target or prerequisite.  However, you can explicitly mark
1 a file as intermediate by listing it as a prerequisite of the special
1 target '.INTERMEDIATE'.  This takes effect even if the file is mentioned
1 explicitly in some other way.
1 
1    You can prevent automatic deletion of an intermediate file by marking
1 it as a "secondary" file.  To do this, list it as a prerequisite of the
1 special target '.SECONDARY'.  When a file is secondary, 'make' will not
1 create the file merely because it does not already exist, but 'make'
1 does not automatically delete the file.  Marking a file as secondary
1 also marks it as intermediate.
1 
1    You can list the target pattern of an implicit rule (such as '%.o')
1 as a prerequisite of the special target '.PRECIOUS' to preserve
1 intermediate files made by implicit rules whose target patterns match
1 that file's name; see ⇒Interrupts.
1 
1    A chain can involve more than two implicit rules.  For example, it is
1 possible to make a file 'foo' from 'RCS/foo.y,v' by running RCS, Yacc
1 and 'cc'.  Then both 'foo.y' and 'foo.c' are intermediate files that are
1 deleted at the end.
1 
1    No single implicit rule can appear more than once in a chain.  This
1 means that 'make' will not even consider such a ridiculous thing as
1 making 'foo' from 'foo.o.o' by running the linker twice.  This
1 constraint has the added benefit of preventing any infinite loop in the
1 search for an implicit rule chain.
1 
1    There are some special implicit rules to optimize certain cases that
1 would otherwise be handled by rule chains.  For example, making 'foo'
1 from 'foo.c' could be handled by compiling and linking with separate
1 chained rules, using 'foo.o' as an intermediate file.  But what actually
1 happens is that a special rule for this case does the compilation and
1 linking with a single 'cc' command.  The optimized rule is used in
1 preference to the step-by-step chain because it comes earlier in the
1 ordering of rules.
1