make: Canned Recipes

1 
1 5.8 Defining Canned Recipes
1 ===========================
1 
1 When the same sequence of commands is useful in making various targets,
1 you can define it as a canned sequence with the 'define' directive, and
1 refer to the canned sequence from the recipes for those targets.  The
1 canned sequence is actually a variable, so the name must not conflict
1 with other variable names.
1 
1    Here is an example of defining a canned recipe:
1 
1      define run-yacc =
1      yacc $(firstword $^)
1      mv y.tab.c $@
1      endef
1 
1 Here 'run-yacc' is the name of the variable being defined; 'endef' marks
1 the end of the definition; the lines in between are the commands.  The
1 'define' directive does not expand variable references and function
1 calls in the canned sequence; the '$' characters, parentheses, variable
1 names, and so on, all become part of the value of the variable you are
1 defining.  ⇒Defining Multi-Line Variables Multi-Line, for a
1 complete explanation of 'define'.
1 
1    The first command in this example runs Yacc on the first prerequisite
1 of whichever rule uses the canned sequence.  The output file from Yacc
1 is always named 'y.tab.c'.  The second command moves the output to the
1 rule's target file name.
1 
1    To use the canned sequence, substitute the variable into the recipe
11 of a rule.  You can substitute it like any other variable (⇒Basics
 of Variable References Reference.).  Because variables defined by
1 'define' are recursively expanded variables, all the variable references
1 you wrote inside the 'define' are expanded now.  For example:
1 
1      foo.c : foo.y
1              $(run-yacc)
1 
1 'foo.y' will be substituted for the variable '$^' when it occurs in
1 'run-yacc''s value, and 'foo.c' for '$@'.
1 
1    This is a realistic example, but this particular one is not needed in
1 practice because 'make' has an implicit rule to figure out these
11 commands based on the file names involved (⇒Using Implicit Rules
 Implicit Rules.).
1 
1    In recipe execution, each line of a canned sequence is treated just
1 as if the line appeared on its own in the rule, preceded by a tab.  In
1 particular, 'make' invokes a separate sub-shell for each line.  You can
1 use the special prefix characters that affect command lines ('@', '-',
11 and '+') on each line of a canned sequence.  ⇒Writing Recipes in
 Rules Recipes.  For example, using this canned sequence:
1 
1      define frobnicate =
1      @echo "frobnicating target $@"
1      frob-step-1 $< -o $@-step-1
1      frob-step-2 $@-step-1 -o $@
1      endef
1 
1 'make' will not echo the first line, the 'echo' command.  But it _will_
1 echo the following two recipe lines.
1 
1    On the other hand, prefix characters on the recipe line that refers
1 to a canned sequence apply to every line in the sequence.  So the rule:
1 
1      frob.out: frob.in
1              @$(frobnicate)
1 
1 does not echo _any_ recipe lines.  (⇒Recipe Echoing Echoing, for a
1 full explanation of '@'.)
1