简体   繁体   English

对于Makefile变量中的每个目标

[英]For each on target of Makefile variable

I've makefile which looks like follows 我有如下的makefile

apps = app1 app2 app3

all: dir app1 app2 app3 zip cleanup

Now I want to do some loop on the list of apps varible, 现在,我想对可变的apps列表进行一些循环,

something like 就像是

`loop on apps

endloop`

Is it possible in makefile to loop on it, I need to do loop on apps varible list 是否可以在makefile中对其进行循环,我需要对apps变量列表进行循环

update 更新

lets say this variable( apps ) is generated by my program in the make file, which provide for each project diffrent values of apps, sometimes its apps= app1 app2 sometimes its apps= app1 and sometimes can be 20 apps or more apps= app1 app2 appN 可以说这个变量( apps )是由我的程序在make文件中生成的,它为每个项目提供了不同的app值,有时其apps= app1 app2有时其apps= app1有时可以是20个或更多apps= app1 app2 appN

How can I iterate on apps varible and do something, for example in each iteration print something like: 我如何迭代apps变量并执行某些操作,例如在每次迭代中打印如下内容:

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

When trying the following 尝试以下时

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

I got following error: 我收到以下错误:

make: *** No rule to make target app1', needed by foo'. Stop. make: *** No rule to make target app1',而foo'. Stop.需要foo'. Stop. foo'. Stop.

I see you've gone back and forth a bit, so let me just make a comment which may or may not help. 我看到您来回走了一点,所以让我发表评论可能会或可能不会有帮助。

In general you don't write loops inside make recipes, because make itself provides "looping". 通常,您不会在make配方中编写循环,因为make本身提供了“循环”。 So when you write a rule like: 因此,当您编写如下规则时:

all: app1 app2 app3 app4

make will try to build each one of those prerequisites, one at a time. make将尝试一次构建其中的每个先决条件。 So if you wanted to have a makefile that echoed a line for each entry in the apps variable you would do it like this: 因此,如果您希望有一个makefile可以为apps变量中的每个条目回显一行,则可以这样操作:

all: $(apps)

$(apps):
        @echo $@

This tells make to start with a target all and try to "build" each of its prerequisites which are the values in the apps variable. 这告诉make从all目标开始,并尝试“构建”其每个前提条件,这些前提条件是apps变量中的值。

Then you define a rule for how to build the apps, and for each one you say that the rule is echo $@ where $@ is an automatic variable that expands to the currently-building target. 然后定义一个有关如何构建应用程序的规则,对于每个应用程序,您都说该规则是echo $@ ,其中$@是一个自动变量 ,可以扩展到当前正在构建的目标。

In make, the syntax: 在make中,语法为:

foo bar biz:
        some command

is shorthand for, and identical to, writing: 是写的简写,并且等同于:

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

The crucial thing when writing makefiles is that you think how to write a rule to create one file (target) from zero or more prerequisite files. 编写makefile时至关重要的是,您要考虑如何编写规则以从零个或多个前提文件中创建一个文件(目标)。 Then you let make worry about how to connect all those prerequisites together and order them properly. 然后,让您担心如何将所有这些先决条件连接在一起并正确订购它们。

ETA If you want a special rule for one particular target in a long list held in the $(apps) variable, you can do this: ETA如果要对$(apps)变量中包含的一长串目标中的特定目标使用特殊规则,可以执行以下操作:

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

bar:
        some other command

Maybe you want something like (and I assume that some other part of the Makefile describes how app1 and app2 etc... should be built with appropriate rules). 也许您想要类似的东西(我假设Makefile 其他部分描述了应如何使用适当的规则来构建app1app2等...)。

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

or (slightly better) 或(稍微好一点)

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

and you might be interested in the foreach function in make (or perhaps its eval function , perhaps mixed with something else) 并且您可能对make中的foreach函数 (或它的eval函数 ,也许与其他函数混合)感兴趣

At last, your makefile (or some parts included in it) could be generated by some (shell, AWK, Python, ...) script (or even another program coded in C++, Java, Ocaml, whatever you want) 最后,您的makefile(或其中包含的某些部分)可以由某些(shell,AWK,Python等)脚本(或什至是其他用C ++,Java,Ocaml编码的程序)生成。

I recommend using remake , as remake -x , to debug your Makefile 我建议使用remake作为remake -x来调试您的Makefile

You could also generate some submakefile and run $(MAKE) -f submakefile in some recipe. 您也可以生成一些子submakefile并在某些配方中运行$(MAKE) -f submakefile

I strongly recommend taking at least one hour in reading entirely the documentation of GNU make before coding something in your Makefile . 我强烈建议您花至少一个小时完全阅读GNU的make文件编码在你的东西之前Makefile And you could also need to read the documentation of bash . 而且您可能还需要阅读bash文档

At last, you might consider some other build automation tool, like ninja (whose philosophy is different : the build.ninja file is expected to be generated and you have to provide a generator for it) 最后,您可能会考虑使用其他一些构建自动化工具,例如ninja (其原理有所不同 :应该会生成 build.ninja文件,并且必须为其提供生成器)

For your next question, please provide some MCVE 对于您的下一个问题,请提供一些MCVE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM