空目标规则是否可以替代 GNU Make 中的 .DEFAULT_GOAL?

发布于 2024-07-26 21:21:01 字数 1159 浏览 2 评论 0原文

GNU Make 版本 3.81 引入了一个名为 .DEFAULT_GOAL 的特殊变量,如果命令行上没有指定目标,它可以用来告诉 Make 应该构建哪个目标(或目标)。 否则,Make 将简单地创建它遇到的第一个目标。

考虑一下:

bar: a b c
        ${MAKE_BAR_COMMANDS}

foo: x y z
        ${MAKE_FOO_COMMANDS}

使​​用上面假设的 Makefile,运行 make 将构建“bar”,因为它是 Make 遇到的第一个目标。 但是如果我们添加 .DEFAULT_GOAL,就像这样……

.DEFAULT_GOAL := foo    # Build foo by default, even if it's not first.

bar: a b c
        ${MAKE_BAR_COMMANDS}

foo: x y z
        ${MAKE_FOO_COMMANDS}

那么如果我们只运行 make,Make(版本 3.81)将构建“foo”。

我发现这个 .DEFAULT_GOAL 变量在我构建的模块化、可重用 Makefile“框架”中非常有用。 但是,我发现许多系统仍然具有 GNU Make 3.80 或更早版本,并且它们不支持此变量。

在玩弄东西时,我注意到简单地指定一个空目标规则似乎与 .DEFAULT_GOAL 具有相同的效果,即使在 3.81 之前的 GNU Make 版本上也是如此:

foo:    # Empty target rule

bar: a b c
        ${MAKE_BAR_COMMANDS}

foo: x y z
        ${MAKE_FOO_COMMANDS}

我知道添加到目标规则是有效且合法的,并且/或通过以这种方式指定附加目标规则来满足先决条件。 但是,我想知道这样做是否可能会带来一些潜在的不良副作用,而这些副作用在使用 .DEFAULT_GOAL 时不会发生。

我想我想知道为什么要引入 .DEFAULT_GOAL 如果您可以使用简单的空目标规则实现相同的目标。 这让我怀疑它们可能不会导致完全相同相同的行为。

因此,底线问题是:使用 .DEFAULT_GOAL 与空目标规则之间是否存在外部可检测的差异?

GNU Make version 3.81 introduced a special variable named .DEFAULT_GOAL that can be used to tell Make which goal (or target) should be built if no target was specified on the command line. Otherwise Make will simply make the first target it encounters.

Consider:

bar: a b c
        ${MAKE_BAR_COMMANDS}

foo: x y z
        ${MAKE_FOO_COMMANDS}

With the above hypothetical Makefile, running make will build "bar", because it is the first target that Make encounters. But if we add .DEFAULT_GOAL, like so ...

.DEFAULT_GOAL := foo    # Build foo by default, even if it's not first.

bar: a b c
        ${MAKE_BAR_COMMANDS}

foo: x y z
        ${MAKE_FOO_COMMANDS}

... then Make (version 3.81) will build "foo" if we run just make.

I've found this .DEFAULT_GOAL variable to be quite useful in a modular, re-usable Makefile "framework" that I've built. However, I'm finding that many systems still have GNU Make 3.80 or older and they don't support this variable.

While playing around with things, I noticed that simply specifying an empty target rule seems to have the same effect as .DEFAULT_GOAL, even on pre-3.81 versions of GNU Make:

foo:    # Empty target rule

bar: a b c
        ${MAKE_BAR_COMMANDS}

foo: x y z
        ${MAKE_FOO_COMMANDS}

I know that it is valid and legal to add to a target's rules and/or prerequesites by specifying additional target rules in this manner. However, I'm wondering if doing this might introduce some potentially undesirable side-effects that do not occur when using .DEFAULT_GOAL.

I guess I'm left wondering why .DEFAULT_GOAL was introduced if you can achieve the same thing with a simple empty target rule. This makes me suspect that they maybe don't result in exactly the same behavior.

So the bottom line questions is: is there an externally detectable difference between using .DEFAULT_GOAL versus an empty target rule?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

丶视觉 2024-08-02 21:21:02

您的“foo:#空目标规则”与双冒号规则冲突。

如果您排除这些规则(:: 规则),则仅当它是第一条规则时才有效。 因此,如果您在开始时“包含clear-variables.mk”,并且通常有一个复杂的包含文件网络,
这个“.DEFAULT_GOAL”会让它变得明显& 已检查,只有 1 条规则可以默认。

Your "foo: # Empty target rule" conflicts with double-colon rules.

If you rule those out (:: rules), it works only if it is the first rule. So, if you "include clear-variables.mk" at the beginning, and in general have a complicated net of include files,
this ".DEFAULT_GOAL" will make it obvious & checked, that only 1 rule can be default.

等风也等你 2024-08-02 21:21:01

我无法给你一个明确的答案,但我可以提出引入它的其他原因:

  1. .DEFAULT_GOAL 的作用显而易见; 即标签上的内容。 单行空目标的存在和含义很容易被掩盖,尤其是在较大的 Makefile 中。
  2. 您实际上可以在 Makefile 中的其他位置读取 $(.DEFAULT_GOAL) 的值。 测试表明,无论是显式设置还是通过选择第一条规则来确定,都没有区别。

I can't give you a definite answer, but I can suggest other reasons for its introduction:

  1. It's immediately obvious what .DEFAULT_GOAL does; i.e. what it says on the label. The presence and meaning of a single-line empty target is easily glossed over, especially in a larger Makefile
  2. You can actually read the value of $(.DEFAULT_GOAL) back elsewhere in the Makefile. Testing suggests it makes no difference whether it's explicitly set, or whether it's determined by picking the first rule.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文