简体   繁体   English

Makefile:在构建任何目标之前创建文件

[英]Makefile: creating a file before building any target

( my question is different from Force Makefile to execute script before building targets ) (我的问题与Force Makefile在构建目标之前执行脚本不同)

I've got the following Makefile : 我有以下Makefile

.PHONY: dump_params all

all: tmpA

tmpA: tmpB tmpC dump_params
    cat $(filter tmp%,$^) > $@

tmpB: dump_params
    touch $@

tmpC: dump_params
    touch $@

dump_params:
    echo "Makefile was run." >> config.txt

with the target dump_params I want to create/append a file each time a new target is invoked (to keep track of the version of the tools used). 目标dump_params我想在每次调用新目标时创建/附加文件(以跟踪所用工具的版本)。 However, when I call 但是,当我打电话的时候

make tmpA

all the targets are built from scratch 所有目标都是从头开始构建的

$ make tmpA
echo "Makefile was run " >> config.txt
touch tmpB
touch tmpC
cat tmpB tmpC > tmpA

$ make tmpA
echo "Makefile was run." >> config.txt
touch tmpB
touch tmpC
cat tmpB tmpC > tmpA

How can I prevent Make to re-build everything because of that target 'dump_params'? 如何防止Make重新构建所有内容,因为该目标是'dump_params'? Is there a another way to create this kind of log file ? 有没有另一种方法来创建这种日志文件?

EDIT : I'm using a parallel make (option -j). 编辑 :我正在使用并行make(选项-j)。 Defining a macro to create the file in the statements section is not an option. 定义宏以在语句部分中创建文件不是一种选择。

Use order-only prerequisites ? 使用仅订单的先决条件

.PHONY: dump_params all

all: tmpA

tmpA: tmpB tmpC | dump_params
    cat $(filter tmp%,$^) > $@

tmpB: | dump_params
    touch $@

tmpC: | dump_params
    touch $@

dump_params:
    echo "Makefile was run." >> config.txt

Another option is to use immediately expanded shell functions, like: 另一种选择是立即使用扩展的shell函数,例如:

__dummy := $(shell echo "Makefile was run." >> config.txt)

Since it's immediately expanded the shell script will be invoked once, as the makefile is read in. There's no need to define a dump_params target or include it as a prerequisite. 由于它会立即展开,因此在读入makefile时将调用一次shell脚本。无需定义dump_params目标或将其作为先决条件包含在内。 This is more old-school, but has the advantage that it will run for every invocation of make, without having to go through and ensure every target has the proper order-only prerequisite defined. 这是一个更老的学校,但它的优势在于它将为make的每次调用运行,而不必经历并确保每个目标都具有定义的正确的仅订单先决条件。

Non-answer, but snakemake (and there are others, I suspect) supports tracking of rules (the code), parameters, inputs, and executable versions. 非答案,但是snakemake(还有其他人,我怀疑)支持跟踪规则(代码),参数,输入和可执行版本。

https://bitbucket.org/johanneskoester/snakemake https://bitbucket.org/johanneskoester/snakemake

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM