如何推迟 makefile 先决条件中的 foreach 变量扩展?
我有以下 makefile,它从 terraform 输出创建一个 config.json 文件,然后解析该 config.json 文件并创建一个 makefile 列表变量,然后循环该列表并相应地触发目标。目标通过循环列表 abc xyz
将目录从 output/%/csv
复制到 current/%/csv
CONFIG = config.json
tf:
terraform output -json > $(CONFIG)
config_file: tf
$(eval obj := $(shell jq -c '.objects.value[]' $(CONFIG)))
output/%/csv : current/%/csv
mkdir -p $(@D)
cp -r $< $@
apply: $(foreach X, $(obj), output/$X/csv)
上面的 makefile 给出了一个空输出。我期望这个结果,因为先决条件立即扩展并且具有空值,为了解决这个问题,我添加了 .SECONDEXPANSION:
以使先决条件在延迟阶段扩展。但随后出现错误没有规则来制作目标输出//csv',apply需要'。停止。
这意味着变量仍然被分配一个空值。
.SECONDEXPANSION:
CONFIG = config.json
tf:
terraform output -json > $(CONFIG)
config_file: tf
$(eval obj := $(shell jq -c '.objects.value[]' $(CONFIG)))
output/%/csv : current/%/csv
mkdir -p $(@D)
cp -r $< $@
apply: $(foreach $X, $$(obj), output/$$(X)/csv)
我在这里缺少什么东西还是有更好的方法来做到这一点?
I have the following makefile that creates a config.json
file from terraform output and then parses that config.json file and creates a makefile list variable, I then loop over that list and trigger targets accordingly. The targets copies the directories from output/%/csv
to current/%/csv
by looping over the list abc xyz
CONFIG = config.json
tf:
terraform output -json > $(CONFIG)
config_file: tf
$(eval obj := $(shell jq -c '.objects.value[]' $(CONFIG)))
output/%/csv : current/%/csv
mkdir -p $(@D)
cp -r lt; $@
apply: $(foreach X, $(obj), output/$X/csv)
The above makefile gives an empty output. I expected this result since the prerequisites are expanded immediately and have empty value, to resolve this I added .SECONDEXPANSION:
to have the prerequisites expanded in the deferred phase. But then got an error No rule to make target output//csv', needed by apply'. Stop.
This means the variables are still assigned an empty value.
.SECONDEXPANSION:
CONFIG = config.json
tf:
terraform output -json > $(CONFIG)
config_file: tf
$(eval obj := $(shell jq -c '.objects.value[]' $(CONFIG)))
output/%/csv : current/%/csv
mkdir -p $(@D)
cp -r lt; $@
apply: $(foreach $X, $(obj), output/$(X)/csv)
Is there something that I am missing here or is there any better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有理由使用
eval
在配方中设置obj
make 变量。在几乎所有示例中,我看到在配方中使用eval
或shell
make 函数时,这是一个错误。那么下面的呢?
There is no reason to set the
obj
make variable in a recipe witheval
. In almost all examples I saw whereeval
orshell
make functions were used in a recipe it was an error.What about the following?