Chapter 5: Writing Recipes in Rules 41
5 Writing Recipes in Rules
The recipe of a rule consists of one or more shell command lines to be executed, one at a
time, in the order they appear. Typically, the result of executing these commands is that
the target of the rule is brought up to date.
Users use many different shell programs, but recipes in makefiles are always interpreted
by /bin/sh unless the makefile specifies otherwise. See Section 5.3 [Recipe Execution],
page 44.
5.1 Recipe Syntax
Makefiles have the unusual property that there are really two distinct syntaxes in one
file. Most of the makefile uses make syntax (see Chapter 3 [Writing Makefiles], page 11).
However, recipes are meant to be interpreted by the shell and so they are written using
shell syntax. The make program does not try to understand shell syntax: it performs only
a very few specific translations on the content of the recipe before handing it to the shell.
Each line in the recipe must start with a tab (or the first character in the value of the
.RECIPEPREFIX variable; see
Section 6.14 [Special Variables], page 73), except that the first
recipe line may be attached to the target-and-prerequisites line with a semicolon in between.
Any line in the makefile that begins with a tab and appears in a “rule context” (that is,
after a rule has been started until another rule or variable definition) will be considered
part of a recipe for that rule. Blank lines and lines of just comments may appear among
the recipe lines; they are ignored.
Some consequences of these rules include:
• A blank line that begins with a tab is not blank: it’s an empty recipe (see Section 5.9
[Empty Recipes], page 57).
• A comment in a recipe is not a make comment; it will be passed to the shell as-is.
Whether the shell treats it as a comment or not depends on your shell.
• A variable definition in a “rule context” which is indented by a tab as the first character
on the line, will be considered part of a recipe, not a make variable definition, and passed
to the shell.
• A conditional expression (ifdef, ifeq, etc. see Section 7.2 [Syntax of Conditionals],
page 78) in a “rule context” which is indented by a tab as the first character on the
line, will be considered part of a recipe and be passed to the shell.
5.1.1 Splitting Recipe Lines
One of the few ways in which make does interpret recipes is checking for a backslash just
before the newline. As in normal makefile syntax, a single logical recipe line can be split
into multiple physical lines in the makefile by placing a backslash before each newline. A
sequence of lines like this is considered a single recipe line, and one instance of the shell will
be invoked to run it.
However, in contrast to how they are treated in other places in a makefile (see
Section 3.1.1 [Splitting Long Lines], page 12), backslash/newline pairs are not removed
from the recipe. Both the backslash and the newline characters are preserved and passed
to the shell. How the backslash/newline is interpreted depends on your shell. If the first
Comentários a estes Manuais