makefile - 在创建顶级目标时告诉 'make' 忽略依赖项

标签 makefile gnu-make

我正在运行以下类型的管道:

digestA: hugefileB hugefileC
    cat $^ > $@
    rm $^

hugefileB:
    touch $@

hugefileC:
    touch $@

目标大文件B 大文件C 非常大并且需要很长时间来计算(并且需要 Make 的能力)。但曾经摘要A 已创建,则无需保留其依赖项:它会删除这些依赖项以释放磁盘空间。

现在,如果我再次调用'make',巨大文件B 大文件C 将被重建,而 摘要A 已经可以了。

有什么方法可以告诉“make”避免重新编译依赖项?

注意:我不想在“digestA”的规则中构建两个依赖项。

最佳答案

使用 "intermediate files" GNU Make 的特点:

Intermediate files are remade using their rules just like all other files. But intermediate files are treated differently in two ways.

The first difference is what happens if the intermediate file does not exist. If an ordinary file b does not exist, and make considers a target that depends on b, it invariably creates b and then updates the target from b. But if b is an intermediate file, then make can leave well enough alone. It won't bother updating b, or the ultimate target, unless some prerequisite of b is newer than that target or there is some other reason to update that target.

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.

Ordinarily, a file cannot be intermediate if it is mentioned in the makefile as a target or prerequisite. However, you can explicitly mark a file as intermediate by listing it as a prerequisite of the special target .INTERMEDIATE. This takes effect even if the file is mentioned explicitly in some other way.

You can prevent automatic deletion of an intermediate file by marking it as a secondary file. To do this, list it as a prerequisite of the special target .SECONDARY. When a file is secondary, make will not create the file merely because it does not already exist, but make does not automatically delete the file. Marking a file as secondary also marks it as intermediate.


因此,将以下行添加到 Makefile 中就足够了:
.INTERMEDIATE : hugefileB hugefileC
第一次调用make:
$ make
touch hugefileB
touch hugefileC
cat hugefileB hugefileC > digestA
rm hugefileB hugefileC
下一次:
$ make
make: `digestA' is up to date.

关于makefile - 在创建顶级目标时告诉 'make' 忽略依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12199237/

相关文章:

通过Makefile在特定目录下创建目标文件

c# - 多语言开发

makefile - 在 GNU make 中保留 $^ 中先决条件的顺序

r - 在 RStudio 中使用 Makefile

makefile - GNU Makefile,检测架构

makefile - GNU Makefile 等效于 shell 'TRAP' 命令,用于在退出时简明识别构建失败

linux - 多行 bash 脚本和 make 文件指令中评估的 bash 变量的串联

c++ - 如何更改 Netbeans 7 配置特定的 makefile 宏

c - 构建时自动创建目录

makefile - 铛:警告:-l *:'链接器'输入未使用