makefilegnu-make

How to have GNU make explicitly test for failure?


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.


Solution

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