How to have GNU make explicitly test for failure?

MakefileGnu Make

Makefile Problem Overview


After years of not using make, I find myself needing it again, the gnu version now. I'm pretty sure I should be able to do what I want, but haven't figured out how, or found an answer with Google, etc.

I'm trying to create a test target which will execute my program a number of times, saving the results in a log file. Some tests should cause my program to abort. Unfortunately, my makefile aborts on the first test which leads to an error. I have something like:

# Makefile
# 
test:
        myProg -h > test.log              # Display help
        myProg good_input >> test.log     # should run fine
        myProg bad_input1 >> test.log      # Error 1
        myProg bad_input2 >> test.log      # Error 2

With the above, make quits after the bad_input1 run, never getting to the bad_input2 run.

Makefile Solutions


Solution 1 - Makefile

Put a - before the command, e.g.:

-myProg bad_input >> test.log

GNU make will then ignore the process exit code.

Solution 2 - Makefile

Try running it as

make -i

or

make --ignore-errors

which ignores all errors in all rules.

I'd also suggest running it as

make -i 2>&1 | tee results

so that you got all the errors and output to see what happened.

Just blindly continuing on after an error is probably not what you're really wanting to do. The make utility, by its very nature, is usually relying on successful completion of previous commands so that it can use the artefacts of those commands as pre-requisites for commands to be executed later on.

BTW I'd highly recommend getting a copy of [the O'Reilly book on make][1]. The first edition has an excellent overview of the basic nature of make, specifically its backward chaining behaviour. Later editions are still good but the first ed. still has the clearest explanation of what's actually happening. In fact, my own copy is the first thing I pass to people who come to me to ask "WTF? questions" about make! (-:

[1]: http://oreilly.com/catalog/make3/book/ "The third edition is available online here"

Solution 3 - Makefile

The proper solution if you want to require the target to fail is to negate its exit code.

# Makefile
# 
test:
    myProg -h > test.log              # Display help
    myProg good_input >> test.log     # should run fine
    ! myProg bad_input1 >> test.log      # Error 1
    ! myProg bad_input2 >> test.log      # Error 2

Now, it is an error to succeed in those two cases.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreenMattView Question on Stackoverflow
Solution 1 - Makefileplease delete meView Answer on Stackoverflow
Solution 2 - MakefileRob WellsView Answer on Stackoverflow
Solution 3 - MakefiletripleeeView Answer on Stackoverflow