Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error checking behavior of makefile

If my program has to return different values (e.g. 0, 1, 2, 3, etc) for different outcomes (mostly errors), the makefile that calls this program would have to stop executing the rest of the makefile commands. Is there a way to continue executing the makefile even if that command produced an error (return a non-zero value)?

Thank you all.

like image 835
return 0 Avatar asked Jun 19 '12 16:06

return 0


2 Answers

mycommand; test $? -gt 3 

Change the test as appropriate. I don't think using - or -k is a good idea, as it will stop make from detecting errors.

Note the 2 commands MUST be on the same line, when in a makefile.

Added after comment feedback: You can also do the following, to allow you to unconditionally run follow-on commands before reacting to an error:

mycommand; status=$PIPESTATUS; follow-on-command && test $status -gt 3
like image 115
ctrl-alt-delor Avatar answered Oct 04 '22 23:10

ctrl-alt-delor


To test a command that I know will return a non-zero exit code, I use like this:

test: mybadcommand
    mybadcommand; test $$? -eq 1

This tests that mybadcommand returns a 1 exit value. Note that in Makefiles, you are required to escape the shell variable which refers to exit statuses, because in the world of make $? means dependencies that are newer than the target.

Thus, in the above snippet, be careful to use $$? rather than $?.

like image 36
dbn Avatar answered Oct 05 '22 00:10

dbn