Peter B
Peter B

Reputation: 493

why does "make" delete target files only if implicit

Suppose I have a Makefile like this

B1.txt: A1.txt
    python big_long_program.py A1.txt > $@

correct1.txt: B1.txt reference.txt
    diff -q B1.txt reference.txt
    touch $@

Then the output when I make correct1.txt is pretty well what I would expect:

python big_long_program.py A1.txt > B1.txt
diff -q B1.txt reference.txt
touch correct1.txt

Now if I have lots of files, B1.txt, B2.txt, B3.txt etc, so create an implicit rule:

B%.txt: A%.txt
    python big_long_program.py A$*.txt > $@

correct%.txt: B%.txt reference.txt
    diff -q B$*.txt reference.txt
    touch $@

Instead this happens when I make correct1.txt:

python big_long_program.py A1.txt > B1.txt
diff -q B1.txt reference.txt
touch correct1.txt
rm B1.txt

i.e. there difference is that now the file B1.txt has been deleted, which in many cases is really bad.

So why are implicit rules different? Or am I doing something wrong?

Upvotes: 4

Views: 222

Answers (1)

Reinier Torenbeek
Reinier Torenbeek

Reputation: 17383

You are not doing anything wrong. The behavior you observe and analyze is documented in 10.4 Chains of Implicit Rules. It states that intermediate files are indeed treated differently.

The second difference is that if make does create b in order to update something else, it deletes b later on after it is no longer needed. Therefore, an intermediate file which did not exist before make also does not exist after make. make reports the deletion to you by printing a rm -f command showing which file it is deleting.

The documentation does not explicitly explain why it behaves like this. Looking in the file ChangeLog.1, there is a reference to the remove_intermediates function as far back as 1988. At that time, disk space was expensive and at a premium.

If you do not want this behavior, mention the targets you want to keep somewhere in the makefile as an explicit prerequisite or target or use the .PRECIOUS or the .SECONDARY special built-in targets for that.

With thanks to MadScientist for the additional comments, see below.

Upvotes: 4

Related Questions