【发布时间】:2021-11-12 23:11:22
【问题描述】:
我有一个用例,我需要在规则中多次运行相同的命令。但是,命令参数需要根据另一个命令的返回值进行更改。我发现可以使用$(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
此时,我开始觉得宏可能不是实现所需功能的正确方法,但我不确定如何去做并避免重复大量代码。欢迎提出任何建议!
【问题讨论】: