Why does Make skip the prerequisite once the target file is generated?

79 Views Asked by At

I don't understand that once the target file - $(TEST_LOG) has been generated, both make check_tests and make eval_test will skip the prerequisite but execute the recipe. Can someone give some guidance and help me to understand it?

Let's say, $(TEST_LOG) was generated one day ago, comparing with current timestamp, it is out-dated. Then user do make eval_test now, it should remake test.log within current timestamp. But in this case, the target $(TEST_LOG) is skipped. But it do execute the rule. In my opinion, if make skip the prerequisite, it will skip to execute recipe as well. Why does make act in this way in this case?

TEST_LOG = test.log

check_tests: $(TEST_LOG) eval_test


$(TEST_LOG):
    @echo "-- ERROR --" |& tee -a $@

eval_test: $(TEST_LOG)
    @if [ `grep -cs "ERROR" $(TEST_LOG)` -eq 0 ]; then exit 0; else exit 1 ; fi

Thanks, TM

1

There are 1 best solutions below

0
Beta On

If you want Make to rebuild test.log every time you call make test.log or make eval_test, you can add one line to the makefile:

TEST_LOG = test.log

.PHONY: $(TEST_LOG)

This tells Make that test.log is not to be considered a file name, and therefore that if it is called for (as a prerequisite) the rule must be executed, regardless of what files exist.

(The PHONY line can be almost anywhere in the makefile, as long as it is after the definition of TEST_LOG, and not in a rule.)