GNU make 将按什么顺序满足先决条件?
假设我们有规则:
a: b c d e
并且 b
、c
、d
和 e
彼此独立。
b
、c
、d
、e
的生成顺序是否已定义?看起来一般会按照b
、c
、d
、e
的顺序制作,但有时可能会这样发生这种情况,顺序会有所不同吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,顺序未定义。这就是使用声明性面向依赖编程的要点:计算机可以选择最佳的评估顺序,或者实际上,甚至可以同时评估它们。
No, the order is not defined. That is the whole point in using declarative dependency-oriented programming: that the computer can pick the optimal evaluation order, or in fact, evaluate them even at the same time.
这取决于先决条件的类型。根据 GNU Make 手册,第 4.2 节:
It depends on the type of the prerequisite. According tho the GNU Make Manual, Section 4.2:
根据您提供的规则,按正确顺序。对于您的特定示例,这可能意味着多个不同(4!= 24,从内存中)订单中的任何一个。
只要遵守依赖关系,所有
make
程序都可以自由选择它们喜欢的顺序。如果您的示例中还有其他规则,例如c: b
,则c
将在b
之前制定(但事实并非如此) ,正如您所指出的)。如果您需要依赖特定的命令,则需要更多规则来执行它。否则
make
可以做它想做的事。 GNU Make 的文档仅说明如何规则 的处理顺序,而不是规则内依赖项的处理顺序。最合乎逻辑的顺序(无论如何对我来说)是它们列出的顺序,但这并不能保证。In the right order, based on the rules you provide. For your particular example, that could mean any one of a number of different (4! = 24, from memory) orders.
All
make
programs are free to choose the order they like as long as the dependencies are honored. If there were other rules in your example, sayc: b
, thenc
would be made beforeb
(but that isn't the case, as you point out).If you need to rely on a specific order, you'll need more rules to enforce it. Otherwise
make
can do what it pleases. The documentation for GNU Make only states how rules are processed, not the order in which dependencies within a rule are processed. The most logical order (to me, anyway) would be the order in which they're listed but that's not guaranteed.当然,如果我使用
make -j a
,它们可能会同时构建(取决于是否b
、c
、d
或e
依次具有其他/相互关联的依赖关系)。Sure, if I use
make -j a
, they might all get built at the same time (depending on whetherb
,c
,d
, ore
in turn have other/interrelated dependencies).不,当没有依赖关系时,您不能依赖顺序。
No, you can't count on the ordering when there are no dependency relationships.
我将添加此内容以供将来参考。虽然 GNU Make 在处理先决条件时可能没有定义特定的顺序,但 POSIX make 要求按照指定的顺序处理它们,即从左到右。大多数实现都遵循此规则。 POSIX 甚至给出了一个示例,其中不遵循此规则的 make 实现可能会破坏程序的构建过程:
其中 lex.o 最终使用了不正确的 y.tab.h。虽然这可以用与 GNU Make 一起使用的方式重写,但我想我应该分享这个关于先决条件排序的花絮。
I'll just add this for future reference. While GNU Make may not define a specific order when it comes to processing prerequisites, POSIX make requires that they be handled in the order they've been specified, namely left-to-right. Most implementations follow this rule. POSIX even gives an example where a make implementation not following this rule could break the build process of a program:
where lex.o ends up using an incorrect y.tab.h. While this can be rewritten in a way that works with GNU Make, I thought I'd share this tidbit about prerequisite ordering.
如果顺序很重要,您可以使用 递归 make 有选择地强制执行它。例如,假设您不关心 b 和 c 的生成顺序,只要它们都在 d 之前生成,并且 d 在 e 之前生成即可。然后你可以将你的规则写为:
请注意,根据 d 和 e 的复杂性,这种方法可能会对你的构建时间产生负面影响:请参阅 递归使人认为有害 (PDF) 反对这样做的论点。
If order matters, you can selectively enforce it using recursive make. For example, suppose you don't care what order b and c are made in, as long as they're both made before d and d is made before e. Then you could write your rule as:
Be aware that depending on the complexity of d and e, this approach may do Bad Things to your build times: see Recursive Make Considered Harmful (PDF) for arguments against doing it this way.