在 Makefile 中多次评估动态宏

Evaluating a dynamic macro multiple times in a Makefile

我有一个用例,我需要在规则中多次 运行 相同的命令。但是,命令参数需要根据另一个命令的 return 值进行更改。我发现可以执行 $(call foo_exec) 从规则中调用宏,这很棒。但是,请考虑以下简化代码:

define foo_exec
        @echo $(if $(filter sylvester,$(shell cat cats.txt)),Found Sylvester!,No Sylvester found!)
endef

build:
        $(call foo_exec)
        @echo sylvester > cats.txt
        $(call foo_exec)

如果我 运行 make build,我得到以下输出:

cat: cats.txt: No such file or directory
cat: cats.txt: No such file or directory
No Sylvester found!
No Sylvester found!

它肯定会写 cats.txt,但是,在创建该文件之前,宏似乎只被评估了一次。

此外,在我的实际代码中,在该宏中创建变量会很有用,但我似乎也无法完成这项工作。以下代码:

define foo_exec
        MESSAGE := $(if $(filter sylvester,$(shell cat cats.txt)),Found Sylvester!,No Sylvester found!)
        @echo $(MESSAGE)
endef

build:
        $(call foo_exec)
        @echo sylvester > cats.txt
        $(call foo_exec)

产生这个输出:

cat: cats.txt: No such file or directory
cat: cats.txt: No such file or directory
MESSAGE := No Sylvester found!
/bin/sh: MESSAGE: command not found
make: *** [build] Error 127

在这一点上,我开始觉得宏可能不是实现所需功能的正确方法,但我不确定如何着手避免重复大量代码。欢迎提出任何建议!

以下作品

define foo_exec
        @if egrep -s -q sylvester cats.txt; then echo "Found Sylvester"; else echo "No Sylvester found!"; fi
endef

build:
        $(call foo_exec)
        @echo sylvester > cats.txt
        $(call foo_exec)

有了这个输出:

$ make build
No Sylvester found!
Found Sylvester

问题是宏在 build 配方启动时展开。因此,我们不希望宏扩展检查 cats.txt 文件是否存在。相反,我们希望宏生成 bash 代码来执行检查

我可能没解释清楚!