trondd
trondd

Reputation: 908

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

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.

Upvotes: 15

Views: 14940

Answers (3)

reinierpost
reinierpost

Reputation: 8591

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.)

Upvotes: 9

Didier Trosset
Didier Trosset

Reputation: 37427

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

Upvotes: 17

Saar Drimer
Saar Drimer

Reputation: 1191

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

Upvotes: 1

Related Questions