仅在成功时保留中间文件
Keep intermediate files only on success
我有一些中间文件,只有在它们成功生成后我才想保留它们。
这是我目前的尝试:
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
为了让我痛苦地弄清楚我在追求什么,运行 这个并尝试没有输出:
(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;
手册说
.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).
因此你不应该使用 .PRECIOUS
,你应该使用 .SECONDARY
。但是,您不能在 .SECONDARY
规则的右侧使用 %
。你可以留空(这样一切都是次要的),或者给出一个列表。
我有一些中间文件,只有在它们成功生成后我才想保留它们。
这是我目前的尝试:
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
为了让我痛苦地弄清楚我在追求什么,运行 这个并尝试没有输出:
(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;
手册说
.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).
因此你不应该使用 .PRECIOUS
,你应该使用 .SECONDARY
。但是,您不能在 .SECONDARY
规则的右侧使用 %
。你可以留空(这样一切都是次要的),或者给出一个列表。