tags:

views:

658

answers:

3

I've got a slightly hackish makefile for running tests:

### Run the tests

tests := tests/test1 tests/test2 ...

test: $(tests)

$(tests): %: %.c
    gcc -o $@ $(testflags) $<
    $@

It works, but it makes Make do something I've never seen it do before. My test is currently broken, and causes a bus error. Make gives the following output:

gcc -o tests/test1 [flags blah blah] tests/test1.c
tests/test1
make: *** [tests/test1] Bus error
make: *** Deleting file `tests/test1'

I'm curious about the last line. I've never seen Make do that before. Why does Make delete the compiled test?

Note: I edited this example pretty heavily to make it simpler. I might have introduced some mistakes.

+3  A: 

Because the target might not have been built correctly. The next time you make the project, it will attempt to rebuild the target. If the file had not been removed, make would have no way of knowing something went wrong. make can't know that the failure was coming from a test rather than the process that builds the target.


Whether or not this behavior is desirable in your case depends on the nature of the tests. If you plan on fixing the test so that it does not cause a Bus error, removing the target isn't a big deal. If you want to use the target for debugging later, you'll need to make a change to your make process.

One way to not delete targets is to use the .DELETE_ON_ERROR target.


Another might be:

$(tests): %: %.c
    gcc -o $@ $(testflags) $<
    -$@

Not tested, but the documentation indicates the target will not be removed:

When an error happens that make has not been told to ignore, it implies that the current target cannot be correctly remade, and neither can any other that depends on it either directly or indirectly. No further commands will be executed for these targets, since their preconditions have not been achieved.

and:

Usually when a command fails, if it has changed the target file at all, the file is corrupted and cannot be used—or at least it is not completely updated. Yet the file's time stamp says that it is now up to date, so the next time make runs, it will not try to update that file. The situation is just the same as when the command is killed by a signal; see Interrupts. So generally the right thing to do is to delete the target file if the command fails after beginning to change the file. make will do this if .DELETE_ON_ERROR appears as a target. This is almost always what you want make to do, but it is not historical practice; so for compatibility, you must explicitly request it.

Jon Ericson
+2  A: 

One way to avoid this behaviour is to split the build and test execution into two steps:

tests := tests/test1 tests/test2 ...

test: $(tests) runtests

$(tests): %: %.c
    gcc -o $@ $(testflags) $<

runtests: %.out: %
    $< | tee $@

(There's probably errors in my make syntax, anybody feel free to correct it.) The general idea is to have the test run generate an output file, which makes it easier for make to run each test individually.

Greg Hewgill
+1 for being the only answer to point out that the behavior was caused by a buggy makefile that combined generating the test program and running it into a single rule.
R..
+1  A: 

This is the default behaviour of make. When a command returns an error code (e.g. non-zero return) then the make target is deleted. The .PRECIOUS and .IGNORE makefile directives can change this behaviour.

an0nym0usc0ward