Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check the exit status of a Makefile shell invocation?

Tags:

makefile

I have a Makefile which runs a program which on success return a non-zero value, and on failure return another non-zero value. I know that I can ignore the exit status by prefixing the command with -, but that does not work because I need to know if the command succeeded.

like image 566
trondd Avatar asked Apr 28 '11 10:04

trondd


3 Answers

You can test the returned value on a second command on the same Makefile line, using the shell $? variable that contains the last returned value.

For example with the false command that would obviously stop the compilation:

test:
    /bin/false ; /usr/bin/test "$$?" -eq 1     # <-- make does not stop here
    /bin/echo "Continues ..."
    /bin/false                                 # <-- make stops here
like image 183
Didier Trosset Avatar answered Oct 05 '22 14:10

Didier Trosset


Use

command || [ $$? -eq v ]

as your command, substituting command with the command, and v with the value returned on success.

(This is just a more compact version of Didier Trosset's answer.)

like image 42
reinierpost Avatar answered Oct 05 '22 15:10

reinierpost


Depending on how the tool behaves on fail, you could just check for the existence of the output file. something like:

@if test ! -f $(FILE); then exit 2; fi
like image 35
Saar Drimer Avatar answered Oct 05 '22 14:10

Saar Drimer