Makefile:在配方中定义规则和先决条件

Makefile: defining rules and prerequisites in recipes

我有一个设置,我想用 make 处理的文件依赖于另一个程序的输出。构建程序及其所有先决条件 这也是一项复杂的任务,所以我也想为此使用 make 。现在我的问题是,似乎无法在 Makefile 配方中生成规则和先决条件。考虑以下代码:

bar:
    echo target1 target2 target3 > bar

foo: bar
    $(eval BAR := $(shell cat bar))

define FUN
$(1):
    touch a$(1)
endef

ifdef BAR
$(foreach i,$BAR,$(eval $(call FUN,$(i))))
endif

blub: foo $(BAR)

我用 bar 配方替换了一大组复杂的配方,这些配方最终生成了我想要的文件列表。实际上,生成bar的内容是非常复杂的,应该通过一组Makefile recipes来完成,而不能仅仅通过(如上所示):

来完成
BAR:=$(shell echo target1 target2 target3)

我想将 foreach 循环放入 foo 的配方中,但因 prerequisites cannot be defined in recipes 而失败,这是有道理的,也在 function define in makefile[= 中进行了解释26=]

但是好像当我做make blub的时候foo eval的BAR到一个不同的值的时候,blub的先决条件没有重新评价。

所以我认为最终我在寻找两件事:

谢谢!

编辑:解决方案

在@user657267 的帮助下,以下似乎解决了我的问题:

.PHONY: all
all: blub

-include bar.make

.PHONY: blub
blub: $(BAR)
    echo $^

bar.make: Makefile
    printf 'BAR=target1 target2 target3\n' > $@
    printf 'target1 target2 target3:\n' >>$@
    printf '\ttouch $$@' >> $@

.PHONY: clean
clean:
    rm -f target1 target2 target3 bar.make

听起来你应该使用 make 的自我改造功能

-include bar.make

blub: $(BAR)
    @echo $^

bar.make:
    @echo BAR := target1 target2 target3 > $@
    @echo target1 target2 target3: ; touch $$@ >> $@

显然 bar.make 的配方是人为设计的,在现实世界中,它们可能会调用某种输出有效 makefile 的脚本。