Modify backslash/newline handling for POSIX.

We fixed Savannah 16670 but that broke previously-working makefiles
that relied on the GNU make behavior.  The POSIX behavior doesn't
seem to me to be better, and can be obtained using GNU make as well,
so put it back as the default behavior and require .POSIX to
get the POSIX behavior.
Add a new section to the manual discussing backslash/newline handling.
Update the test suite.
This commit is contained in:
Paul Smith 2012-03-03 18:45:08 +00:00
parent 6405534814
commit 88f1bc8b55
6 changed files with 138 additions and 47 deletions

View File

@ -1,3 +1,12 @@
2012-03-03 Paul Smith <psmith@gnu.org>
* misc.c (collapse_continuations): Only use POSIX-style
backslash/newline handling if the .POSIX target is set.
Addresses Savannah bug #16670 without backward-incompatibility.
* NEWS: Document behavior change.
* doc/make.texi (Splitting Lines): New section describing how to
use backslash/newline to split long lines.
2012-02-26 Paul Smith <psmith@gnu.org> 2012-02-26 Paul Smith <psmith@gnu.org>
* implicit.c (pattern_search): Check the stem length to avoid * implicit.c (pattern_search): Check the stem length to avoid

11
NEWS
View File

@ -16,11 +16,12 @@ A complete list of bugs fixed in this version is available here:
http://sv.gnu.org/bugs/index.php?group=make&report_id=111&fix_release_id=101&set=custom http://sv.gnu.org/bugs/index.php?group=make&report_id=111&fix_release_id=101&set=custom
* WARNING: Backward-incompatibility! * WARNING: Backward-incompatibility!
This version of make adheres to the POSIX backslash/newline handling, If .POSIX is specified, then make adheres to the POSIX backslash/newline
introducing the following differences: handling requirements, which introduces the following changes to the
* Each backslash/newline in a variable value is replaced with a space; standard backslash/newline handling in non-recipe lines:
multiple consecutive backslash/newlines do not condense into one space. * Any trailing space before the backslash is preserved
* In recipes, a recipe prefix following a backslash-newlines is removed. * Each backslash/newline (plus subsequent whitespace) is converted to a
single space
* New command line option: --trace enables tracing of targets. When enabled * New command line option: --trace enables tracing of targets. When enabled
the recipe to be invoked is printed even if it would otherwise be suppressed the recipe to be invoked is printed even if it would otherwise be suppressed

View File

@ -144,6 +144,10 @@ Writing Makefiles
* Reading Makefiles:: How makefiles are parsed. * Reading Makefiles:: How makefiles are parsed.
* Secondary Expansion:: How and when secondary expansion is performed. * Secondary Expansion:: How and when secondary expansion is performed.
What Makefiles Contain
* Splitting Lines:: Splitting long lines in makefiles
Writing Rules Writing Rules
* Rule Example:: An example explained. * Rule Example:: An example explained.
@ -206,7 +210,7 @@ Writing Recipes in Rules
Recipe Syntax Recipe Syntax
* Splitting Lines:: Breaking long recipe lines for readability. * Splitting Recipe Lines:: Breaking long recipe lines for readability.
* Variables in Recipes:: Using @code{make} variables in recipes. * Variables in Recipes:: Using @code{make} variables in recipes.
Recipe Execution Recipe Execution
@ -602,8 +606,9 @@ clean :
@end example @end example
@noindent @noindent
We split each long line into two lines using backslash-newline; this is We split each long line into two lines using backslash/newline; this is
like using one long line, but is easier to read. like using one long line, but is easier to read. @xref{Splitting Lines,
, Splitting Long Lines}.
@cindex continuation lines @cindex continuation lines
@cindex @code{\} (backslash), for continuation lines @cindex @code{\} (backslash), for continuation lines
@cindex backslash (@code{\}), for continuation lines @cindex backslash (@code{\}), for continuation lines
@ -1037,6 +1042,48 @@ as @code{make} comments or as recipe text, depending on the context in
which the variable is evaluated. which the variable is evaluated.
@end itemize @end itemize
@menu
* Splitting Lines:: Splitting long lines in makefiles
@end menu
@node Splitting Lines, , Makefile Contents, Makefile Contents
@subsection Splitting Long Lines
@cindex splitting long lines
@cindex long lines, splitting
@cindex backslash (@code{\}), to quote newlines
Makefiles use a ``line-based'' syntax in which the newline character
is special and marks the end of a statement. GNU @code{make} has no
limit on the length of a statement line, up to the amount of memory in
your computer.
However, it is difficult to read lines which are too long to display
without wrapping or scrolling. So, you can format your makefiles for
readability by adding newlines into the middle of a statement: you do
this by escaping the internal newlines with a backslash (@code{\})
character. Where we need to make a distinction we will refer to
``physical lines'' as a single line ending with a newline (regardless
of whether it is escaped) and a ``logical line'' being a complete
statement including all escaped newlines up to the first non-escaped
newline.
The way in which backslash/newline combinations are handled depends on
whether the statement is a recipe line or a non-recipe line. Handling
of backslash/newline in a recipe line is discussed later
(@pxref{Splitting Recipe Lines}).
Outside of recipe lines, backslash/newlines are converted into a
single space character. Once that is done, all whitespace around the
backslash/newline is condensed into a single space: this includes all
whitespace preceding the backslash, all whitespace at the beginning of
the line after the backslash/newline, and any consecutive
backslash/newline combinations.
If the @code{.POSIX} special target is defined then backslash/newline
handling is modified slightly to conform to POSIX.2: first, whitespace
preceding a backslash is not removed and second, consecutive
backslash/newlines are not condensed.
@node Makefile Names, Include, Makefile Contents, Makefiles @node Makefile Names, Include, Makefile Contents, Makefiles
@section What Name to Give Your Makefile @section What Name to Give Your Makefile
@cindex makefile name @cindex makefile name
@ -3364,7 +3411,7 @@ nonzero status).
With the GNU C compiler, you may wish to use the @samp{-MM} flag instead With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
of @samp{-M}. This omits prerequisites on system header files. of @samp{-M}. This omits prerequisites on system header files.
@xref{Preprocessor Options, , Options Controlling the Preprocessor, @xref{Preprocessor Options, , Options Controlling the Preprocessor,
gcc.info, Using GNU CC}, for details. gcc, Using GNU CC}, for details.
@cindex @code{sed} (shell command) @cindex @code{sed} (shell command)
The purpose of the @code{sed} command is to translate (for example): The purpose of the @code{sed} command is to translate (for example):
@ -3493,11 +3540,11 @@ line, will be considered part of a recipe and be passed to the shell.
@end itemize @end itemize
@menu @menu
* Splitting Lines:: Breaking long recipe lines for readability. * Splitting Recipe Lines:: Breaking long recipe lines for readability.
* Variables in Recipes:: Using @code{make} variables in recipes. * Variables in Recipes:: Using @code{make} variables in recipes.
@end menu @end menu
@node Splitting Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax @node Splitting Recipe Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
@subsection Splitting Recipe Lines @subsection Splitting Recipe Lines
@cindex recipes, splitting @cindex recipes, splitting
@cindex splitting recipes @cindex splitting recipes
@ -3516,13 +3563,14 @@ 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. 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 However, in contrast to how they are treated in other places in a
makefile, backslash-newline pairs are @emph{not} removed from the makefile (@pxref{Splitting Lines, , Splitting Long Lines}),
recipe. Both the backslash and the newline characters are preserved backslash/newline pairs are @emph{not} removed from the recipe. Both
and passed to the shell. How the backslash-newline is interpreted the backslash and the newline characters are preserved and passed to
depends on your shell. If the first character of the next line after the shell. How the backslash/newline is interpreted depends on your
the backslash-newline is the recipe prefix character (a tab by shell. If the first character of the next line after the
default; @pxref{Special Variables}), then that character (and only backslash/newline is the recipe prefix character (a tab by default;
that character) is removed. Whitespace is never added to the recipe. @pxref{Special Variables}), then that character (and only that
character) is removed. Whitespace is never added to the recipe.
For example, the recipe for the all target in this makefile: For example, the recipe for the all target in this makefile:
@ -3593,14 +3641,14 @@ you specify a different shell in your makefiles it may treat them
differently. differently.
Sometimes you want to split a long line inside of single quotes, but Sometimes you want to split a long line inside of single quotes, but
you don't want the backslash-newline to appear in the quoted content. you don't want the backslash/newline to appear in the quoted content.
This is often the case when passing scripts to languages such as Perl, This is often the case when passing scripts to languages such as Perl,
where extraneous backslashes inside the script can change its meaning where extraneous backslashes inside the script can change its meaning
or even be a syntax error. One simple way of handling this is to or even be a syntax error. One simple way of handling this is to
place the quoted string, or even the entire command, into a place the quoted string, or even the entire command, into a
@code{make} variable then use the variable in the recipe. In this @code{make} variable then use the variable in the recipe. In this
situation the newline quoting rules for makefiles will be used, and situation the newline quoting rules for makefiles will be used, and
the backslash-newline will be removed. If we rewrite our example the backslash/newline will be removed. If we rewrite our example
above using this method: above using this method:
@example @example
@ -3626,7 +3674,7 @@ If you like, you can also use target-specific variables
a tighter correspondence between the variable and the recipe that a tighter correspondence between the variable and the recipe that
uses it. uses it.
@node Variables in Recipes, , Splitting Lines, Recipe Syntax @node Variables in Recipes, , Splitting Recipe Lines, Recipe Syntax
@subsection Using Variables in Recipes @subsection Using Variables in Recipes
@cindex variable references in recipes @cindex variable references in recipes
@cindex recipes, using variables in @cindex recipes, using variables in
@ -4585,7 +4633,7 @@ fixed limit on the size of the environment, and putting so much
information into the value of @code{MAKEFLAGS} can exceed it. If you information into the value of @code{MAKEFLAGS} can exceed it. If you
see the error message @samp{Arg list too long}, this may be the problem. see the error message @samp{Arg list too long}, this may be the problem.
@findex .POSIX @findex .POSIX
@cindex POSIX.2 @cindex POSIX
(For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
in the makefile. You probably do not care about this.) in the makefile. You probably do not care about this.)
@ -5463,12 +5511,9 @@ The variable name may contain function and variable references, which
are expanded when the line is read to find the actual variable name to use. are expanded when the line is read to find the actual variable name to use.
There is no limit on the length of the value of a variable except the There is no limit on the length of the value of a variable except the
amount of memory on the computer. When a variable definition is long, amount of memory on the computer. You can split the value of a
it is a good idea to break it into several lines by inserting variable into multiple physical lines for readability
backslash-newline at convenient places in the definition. This will (@pxref{Splitting Lines, ,Splitting Long Lines}).
make the makefile easier to read. Every backslash-newline, along with
any leading whitespace on the following line, will be replaced by a
single space in the value of the variable.
Most variable names are considered to have the empty string as a value if Most variable names are considered to have the empty string as a value if
you have never set them. Several variables have built-in initial values you have never set them. Several variables have built-in initial values
@ -10725,7 +10770,7 @@ Implicit Rules}) allows one pattern rule for installing members in an
archive (@pxref{Archive Update}) to be sufficient. archive (@pxref{Archive Update}) to be sufficient.
@item @item
The arrangement of lines and backslash-newline combinations in The arrangement of lines and backslash/newline combinations in
recipes is retained when the recipes are printed, so they appear as recipes is retained when the recipes are printed, so they appear as
they do in the makefile, except for the stripping of initial they do in the makefile, except for the stripping of initial
whitespace. whitespace.

11
misc.c
View File

@ -112,12 +112,17 @@ collapse_continuations (char *line)
/* Skip the newline. */ /* Skip the newline. */
++in; ++in;
/* If the newline is escaped, discard following whitespace leaving just
one space. POSIX requires that each backslash/newline/following
whitespace sequence be reduced to a single space. */
if (backslash) if (backslash)
{ {
/* Backslash/newline handling:
In traditional GNU make all trailing whitespace, consecutive
backslash/newlines, and any leading whitespace on the next line
is reduced to a single space.
In POSIX, each backslash/newline and is replaced by a space. */
in = next_token (in); in = next_token (in);
if (! posix_pedantic)
while (out > line && isblank ((unsigned char)out[-1]))
--out;
*out++ = ' '; *out++ = ' ';
} }
else else

View File

@ -1,3 +1,8 @@
2012-03-03 Paul Smith <psmith@gnu.org>
* scripts/misc/bs-nl: Check for POSIX and non-POSIX
backslash/newline handling. Addresses Savannah bug #16670.
2012-01-29 Paul Smith <psmith@gnu.org> 2012-01-29 Paul Smith <psmith@gnu.org>
* scripts/variables/flavors: Add tests for ::= * scripts/variables/flavors: Add tests for ::=

View File

@ -50,27 +50,53 @@ run_make_test("squote:;\@echo 'squ\\\n\t\t ote'\n",
# Backslash-newlines in variable values # Backslash-newlines in variable values
# Simple # Simple
run_make_test(" run_make_test(q!
var = he\\\nllo var = he\
var:;\@echo '|\$(var)|'", llo
var:;@echo '|$(var)|'!,
'', "|he llo|"); '', "|he llo|");
# Preserve preceding space # Condense trailing space
run_make_test(" run_make_test(q!
var = he \\\nllo var = he \
var:;\@echo '|\$(var)|'", llo
'', "|he llo|"); var:;@echo '|$(var)|'!,
'', "|he llo|");
# Remove leading space # Remove leading space
run_make_test(" run_make_test(q!
var = he\\\n llo var = he\
var:;\@echo '|\$(var)|'", llo
var:;@echo '|$(var)|'!,
'', "|he llo|"); '', "|he llo|");
# One space per bs-nl # Multiple bs/nl condensed
run_make_test(" run_make_test(q!
var = he\\\n\\\n\\\n llo var = he\
var:;\@echo '|\$(var)|'", \
\
llo
var:;@echo '|$(var)|'!,
'', "|he llo|");
# POSIX: Preserve trailing space
run_make_test(q!
.POSIX:
x = y
var = he \
llo
var:;@echo '|$(var)|'!,
'', "|he llo|");
# POSIX: One space per bs-nl
run_make_test(q!
.POSIX:
x = y
var = he\
\
\
llo
var:;@echo '|$(var)|'!,
'', "|he llo|"); '', "|he llo|");
1; 1;