如何推迟 makefile 先决条件中的 foreach 变量扩展?

发布于 2025-01-11 05:31:34 字数 1011 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2025-01-18 05:31:34

没有理由使用 eval 在配方中设置 obj make 变量。在几乎所有示例中,我看到在配方中使用 evalshell make 函数时,这是一个错误。

那么下面的呢?

OBJ     := $(shell terraform output -json | jq -c '.objects.value[]')
TARGETS := $(patsubst %,output/%/csv,$(OBJ))

output/%/csv : current/%/csv
    mkdir -p $(@D)
    cp -r 
lt; $@

.PHONY: apply
apply: $(TARGETS)

There is no reason to set the obj make variable in a recipe with eval. In almost all examples I saw where eval or shell make functions were used in a recipe it was an error.

What about the following?

OBJ     := $(shell terraform output -json | jq -c '.objects.value[]')
TARGETS := $(patsubst %,output/%/csv,$(OBJ))

output/%/csv : current/%/csv
    mkdir -p $(@D)
    cp -r 
lt; $@

.PHONY: apply
apply: $(TARGETS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文