是否有 'make' 规则 运行 一个命令恰好一次,但前提是 makefile 的其余部分有事情要做?

Is there a 'make' rule to run a command exactly once but only if the rest of the makefile has something to do?

想象一下,我想在我构建的任何可执行文件上打上一个特殊的标记(不要担心该标记的内容)。我有自己创建图章的规则。但我不希望重新创建图章,除非 makefile 的其余部分会做一些事情:

而且它必须与 make -j 一起工作(并行)。这是一个伪代码 makefile 开始(它不起作用):

all: a b

.PHONY: buildstamp
buildstamp: all
        date > buildstamp.txt

a: a.cpp
        cp buildstamp.txt a

b: b.cpp
        cp buildstamp.txt b

同样,buildstamp 应该只在 运行 更新任何目标时,即使那样它也应该只 运行 一次(不是每个目标一次)。而且它必须在任何这些目标 运行 之前完成(否则 buildstamp.txt 可能会有部分结果。

谢谢!

.PHONY: all

all: a b

a b: %: %.cpp buildstamp.txt 
        cp buildstamp.txt $@

buildstamp.txt: a.cpp b.cpp
        date > buildstamp.txt