Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement in ONESHELL Makefile not working

Tags:

makefile

I get /bin/sh: -c: line 1: syntax error: unexpected end of file for the if statement in the makefile below — what's wrong? (My prompt is /tmp >.)

/tmp > make test
set -e
if [[ -f /tmp/device ]] ; then  
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [test] Error 2
/tmp > cat Makefile 
.ONESHELL:
test:   
    set -e
    if [[ -f /tmp/device ]] ; then  
    echo 'Do something' 
    fi

I am using GNU Make 3.81.

like image 604
Victor Avatar asked Dec 25 '22 04:12

Victor


1 Answers

Using GNU Make 3.81, the .ONESHELL feature is demonstrably not supported — you showed that, and I showed that. I tested on Mac OS X 10.11.5, with the supplied /usr/bin/make as GNU Make 3.81. I demonstrated to my satisfaction by adding strategically placed echo PID=$$$$ lines before set -e, after it, and so on, and observed different PID values. GNU Make version 3.81 is from 2006 (there was also version 3.82 from 2010, before version 4.x was released, starting with 4.0 in 2013).

The current version of GNU Make is 4.2.1 (June 2016). That version does support the feature; your script works as expected when using a sufficiently recent version of GNU Make. It is a feature that has been around for a while — you probably don't have to upgrade to the latest to get the support, but why would you go with a down-version if you're going to upgrade anyway.

If you wish to use the .ONSHELL: feature, you'll have to ensure you are using a new enough version of GNU Make (newer than version 3.81). If that's not feasible, don't use the feature.


Reading the NEWS file from 4.2.1, it is clear that ONESHELL was added to GNU Make version 3.82:

Version 3.82 (28 Jul 2010)

…

* New special target: .ONESHELL instructs make to invoke a single instance
  of the shell and provide it with the entire recipe, regardless of how many
  lines it contains.  As a special feature to allow more straightforward
  conversion of makefiles to use .ONESHELL, any recipe line control
  characters ('@', '+', or '-') will be removed from the second and
  subsequent recipe lines.  This happens _only_ if the SHELL value is deemed
  to be a standard POSIX-style shell.  If not, then no interior line control
  characters are removed (as they may be part of the scripting language used
  with the alternate SHELL).
like image 104
Jonathan Leffler Avatar answered Jan 04 '23 06:01

Jonathan Leffler