2

I have intermediate files that I want to keep only if they were successfully produced.

Here is my current attempt:

all: foo.result

%.result: %.intermediate
        cp $^ $@

%.input:
        touch $@

%.intermediate: %.input
        touch $@
        $(if $(FAIL),exit 1)

.PRECIOUS: %.intermediate
.DELETE_ON_ERROR: %.intermediate

clean:
        rm -f *.intermediate *.input *.result

To make it painfully clear what I'm after, run this and try to get no output:

(make clean && make foo.result) |&> /dev/null; if [[ ! -e foo.intermediate ]]; then echo "make removed precious intermediate file"; fi; (make clean && make foo.result FAIL=1) |&> /dev/null; if [[ -e foo.intermediate ]]; then echo "make did not remove corrupt file"; fi;

1 Answers1

2

The manual says

.PRECIOUS The targets which .PRECIOUS depends on are given the following special treatment: if make is killed or interrupted during the execution of their recipes, the target is not deleted. See Interrupting or Killing make. Also, if the target is an intermediate file, it will not be deleted after it is no longer needed, as is normally done. See Chains of Implicit Rules. In this latter respect it overlaps with the .SECONDARY special target.

You can also list the target pattern of an implicit rule (such as ‘%.o’) as a prerequisite file of the special target .PRECIOUS to preserve intermediate files created by rules whose target patterns match that file’s name.

.INTERMEDIATE The targets which .INTERMEDIATE depends on are treated as intermediate files. See Chains of Implicit Rules. .INTERMEDIATE with no prerequisites has no effect.

.SECONDARY The targets which .SECONDARY depends on are treated as intermediate files, except that they are never automatically deleted. See Chains of Implicit Rules.

.SECONDARY with no prerequisites causes all targets to be treated as secondary (i.e., no target is removed because it is considered intermediate).

Therefore you should not be using .PRECIOUS, you should use .SECONDARY. However you can not use a % on the right-hand-side of a .SECONDARY rule. You could leave it blank (this makes everything secondary), or give a list.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52