简体   繁体   中英

Makefile: creating a file before building any target

( my question is different from Force Makefile to execute script before building targets )

I've got the following 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). 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'? Is there a another way to create this kind of log file ?

EDIT : I'm using a parallel make (option -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:

__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. 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.

Non-answer, but snakemake (and there are others, I suspect) supports tracking of rules (the code), parameters, inputs, and executable versions.

https://bitbucket.org/johanneskoester/snakemake

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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