Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.ONESHELL not working properly in makefile

From the docs:

.ONESHELL

If .ONESHELL is mentioned as a target, then when a target is built all lines of the recipe will be given to a single invocation of the shell rather than each line being invoked separately (*note Recipe Execution: Execution.).



So, a makefile, like:

.ONESHELL :

all ::
    echo 'foo
    bar'

Running, I get:

$ make
echo 'foo
/bin/sh: 1: Syntax error: Unterminated quoted string
makefile:4: recipe for target 'all' failed
make: [all] Error 2 (ignored)



Trying, with almost the same makefile, but adding a - prefix to the recipe, to ignore errors, as documented:

To ignore errors in a recipe line, write a '-' at the beginning of the line's text (after the initial tab). The '-' is discarded before the line is passed to the shell for execution.


The makefile, like:

.ONESHELL :

all ::
    -echo 'foo
    bar'

Running, I get:

$ make
echo 'foo
/bin/sh: 1: Syntax error: Unterminated quoted string
makefile:4: recipe for target 'all' failed
make: [all] Error 2 (ignored)
bar'
/bin/sh: 1: Syntax error: Unterminated quoted string
makefile:4: recipe for target 'all' failed
make: [all] Error 2 (ignored)



Why?

like image 994
Ji Cha Avatar asked Aug 22 '15 06:08

Ji Cha


1 Answers

GNU Make 3.81 does not support .ONESHELL, but 3.82 does.

$ /usr/gnu/bin/make --version
GNU Make 3.82
$ /usr/bin/gmake --version
GNU Make 3.81
$ cat gnu.mk
.ONESHELL:

all:
    echo 'foo' "$$$$"
    echo 'bar' "$$$$"
$ /usr/bin/gmake -f gnu.mk
echo 'foo' "$$"
foo 3100
echo 'bar' "$$"
bar 3101
$ /usr/gnu/bin/make -f gnu.mk
echo 'foo' "$$"
echo 'bar' "$$"
foo 3103
bar 3103
$

I deduce that you're using GNU Make 3.81 from 2006 (or possibly an earlier version, but 3.80 is from 2002); 3.82 is from 2010. The current version is 4.1 from 2014. The online documentation applies to the current version. While lots of the material also applies to older versions, not all of it does.

I also observe that the original makefile with the single single quote on the first line and another on the second seems to cause trouble even with GNU Make 3.82. However, when the lines are each OK, it does seem to work. That's a bit puzzling. And, now I've also installed GNU Make 4.1 (I'd already got it downloaded, but hadn't built it), it too has problems with the bust.mk file below.

$ cat bust.mk
.ONESHELL:

all:
    echo 'foo
    bar' "$$$$"
$ /usr/gnu/bin/make -f bust.mk
echo 'foo
/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [all] Error 2
$

I'm not sure I understand how GNU Make gets confused by that. It might be worth filing a bug for it. OTOH, I'm not sure I'm going to get worried about it, either. But you'll need to take this odd behaviour into account in your future testing.

like image 98
Jonathan Leffler Avatar answered Sep 22 '22 15:09

Jonathan Leffler