bug-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: escaped newline in macro expansion in command line


From: Humm
Subject: Re: escaped newline in macro expansion in command line
Date: Fri, 24 Dec 2021 23:30:23 +0000

(woops, sorry for replying off-list first; mutt doesn’t like me)

Quoth Paul Smith:
In your example the backslash is part of a variable expansion: it's
INSIDE the variable expansion so it will be handled by make as part of
expanding the variable and won't ever be passed to the shell.

POSIX.1-2017 says about this (98624-98631):

When an escaped <newline> (one preceded by a <backslash>) is found anywhere in the makefile except in a command line, an include line, or a line immediately preceding an include line, it shall be replaced, along with any leading white space on the following line, with a single <space>. When an escaped <newline> is found in a command line in a makefile, the command line shall contain the <backslash>, the <newline>, and the next line, except that the first character of the next line shall not be included if it is a <tab>. When an escaped <newline> is found in an include line or in a line immediately preceding an include line, the behavior is unspecified.

So per POSIX, it doesn’t matter in what specific context an escaped newline is, it only matters in what sort of line it is, even if it is in a macro expansion.

It's handled as if you wrote:

 M = word
 _X = ${M:word=a\
 b

 all: ; echo ${_X}

Where you would expect to see "a b".

Here, the escaped newline is in a macro definition line, and thus replaced by a blank before defining _X as “${M:word=a b”.

There is an argument to be made that the backslash should be managed as
if it were part of a command line and not part of a variable assignment
because it appears in a recipe, even though it's part of a make
variable expansion.

That’s the argument I’m making.

If the backslash were handled that way then make
would see this:

 echo ${M:word=ab}

after the backslash/newline was removed and it would run the command:

 echo ab

which is still not

 echo a\
 b

but would give equivalent output in this case.

Nope, it wouldn’t remove the escaped newline, it would pass it to the shell, which would then elide it.

        all:
                echo 'a\
                b'

passes to the shell

        echo 'a\
        b'

which outputs

        a\
        b

This, gmake handles right (and bmake—the other make I have right here to test—does not).

--
Humm



reply via email to

[Prev in Thread] Current Thread [Next in Thread]