对于 Makefile 变量的每个目标

For each on target of Makefile variable

我的 makefile 如下所示

apps = app1 app2 app3

all: dir app1 app2 app3 zip cleanup

现在我想在 apps 变量的列表上做一些循环,

类似于

`loop on apps

endloop`

makefile 中是否可以对其进行循环,我需要在 apps 变量列表

上进行循环

更新

假设这个变量(apps)是由我的程序在 make 文件中 生成的 ,它为每个项目提供不同的应用程序值,有时是 apps= app1 app2 有时是 apps= app1 有时可以是 20 个应用或更多 apps= app1 app2 appN

我如何迭代 apps 变量并做一些事情,例如在每次迭代中打印如下内容:

now im in `app1`
now im in `app2`
etc

尝试以下操作时

.PHONY: foo
all: foo
APPS = app1 app2 app3 app4
foo : $(APPS)
    for $$f in $(APPS); do echo $$f is here; done

我收到以下错误:

make: *** No rule to make targetapp1',foo'. Stop.

需要

也许你想要类似的东西(我假设 Makefile 的某些 other 部分描述了 app1app2 等.. 应该按照适当的规则构建)。

foo: app1 app2 app3 app4 
     for f in $^ ; do echo $$f is here ; done

或(稍好)

APPS = app1 app2 app3 app4
foo : $(APPS)
     for f in $(APPS); do echo $$f is here; done

并且您可能对 foreach function in make (or perhaps its eval function 感兴趣,也许与其他内容混合)

最后,您的 makefile(或其中的某些部分 included)可以由某些(shell、AWK、Python、...)脚本(甚至另一个用 C++ 编码的程序,Java,Ocaml,任何你想要的)

我建议使用 remake 作为 remake -x 来调试您的 Makefile

您还可以在某些食谱中生成一些 submakefile 和 运行 $(MAKE) -f submakefile

我强烈建议您至少花一个小时 完整地阅读 documentation of GNU make before coding something in your Makefile. And you could also need to read the documentation of bash

最后,您可能会考虑其他一些构建自动化工具,例如 ninja(其哲学是 不同build.ninja 文件应该是generatedyou 必须为其提供生成器)

对于您的下一个问题,请提供一些 MCVE

我看到你有点来回走动,所以让我发表评论,可能有帮助也可能没有帮助。

通常你不会在make recipes里面写循环,因为make本身就提供了"looping"。因此,当您编写如下规则时:

all: app1 app2 app3 app4

make 将尝试构建这些先决条件中的每一个,一次构建一个。因此,如果您想要一个 makefile 为 apps 变量中的每个条目回显一行,您可以这样做:

all: $(apps)

$(apps):
        @echo $@

这告诉 make 从目标 all 开始并尝试 "build" 它的每个先决条件,即 apps 变量中的值。

然后您为如何构建应用程序定义一个规则,对于每个应用程序,您说规则是 echo $@ 其中 $@ 是扩展到当前的 automatic variable -建筑目标。

在 make 中,语法:

foo bar biz:
        some command

是 shorthand 用于并等同于写作:

foo:
        some command
bar:
        some command
biz:
        some command

编写 makefile 时的关键是您要考虑如何编写规则以从零个或多个先决条件文件创建 一个 文件(目标)。然后你让 make 担心如何将所有这些先决条件连接在一起并正确排序。

预计到达时间 如果你想为 $(apps) 变量中保存的长列表中的一个特定目标制定特殊规则,你可以这样做:

$(filter-out bar,$(apps)):
        @echo print $@

bar:
        some other command