命令行变量 - 在 makefile 中 - 扩展为奇怪的值

Command-line variables - in makefile - expand to weird values

来自docs:

Target-specific variables have the same priority as any other makefile variable. Variables provided on the command line (and in the environment if the '-e' option is in force) will take precedence. Specifying the 'override' directive will allow the target-specific variable value to be preferred.

所以,一个简单的 makefile,例如:

# A pattern-specific variable assignment.
% : foo += file

all : x ;

# Target is a double-colon w/o dependencies, so Make will ALWAYS run its commands.
x ::
    @echo '$(foo)'

运行,我们得到:

# Override makefile-level variables, with a command-line assignment.
$ make foo=cmd
cmd cmd cmd

# Set the value in the environment, And tell Make to prefer it over any makefile-level definitions.
$ foo=env make --environment-overrides
env file file

现在回到上面引用的文档:

Variables provided on the command line (and in the environment if the '-e' option is in force) will take precedence.

似乎,使用其中之一:

  1. 命令行分配。
  2. 环境设置变量,并使用 -e (--environment-overrides)。

具有两者相同效果,即覆盖文件级(makefile)变量。

但是,结果相差很大。请记住,命令行中给出的值是:cmd,而环境中给出的值是:env.

现在,比较命令行覆盖与环境覆盖的值:

  1. cmd cmd cmd(用于 命令行 覆盖)。
  2. env file file(针对 环境 覆盖)。

因此,对于命令行,Make 会重复 相同的 值,即 cmd3 次,因为环境覆盖,情况不同。也就是说,Make 将 "repeat" 环境级值:env1 次,然后重复 - none 其他 - 覆盖 文件级值:file.

现在,命令行的"override"与的"override"的情况完全不同环境,这本身就很奇怪,这里的问题就大了。

因为,制定规则为命令行(或环境)值提供 "priority",为什么它坚持附加 "other" 值(如环境覆盖的情况,其中Make 追加 "file file"),或者在命令行覆盖的情况下(其中 Make 重复相同的值 ***3* 次)。认真的吗?

它有什么意义呢?这些不一致和 奇怪 结果的理由是什么?

我相信这里的答案与 的答案有关。 (并且可能是 env 覆盖版本中的错误。)

全局变量和特定于目标的变量是不同的变量。

cmd cmd cmd 结果是因为当您编写 %: foo += file 时,make 将其存储为变量 foo 的当前值的附加值 foofile.

但是,当您在命令行上设置 foo 时,使 覆盖 目标特定变量 foo 的值是 cmd 而不是 file。因此,当 make 每次获取 cmd cmd cmd.

时都会连接变量

我认为,该解释应该能让您 env env env 理解,但我不确定为什么没有。这可能是一个错误,也可能是关于环境覆盖变量和目标特定变量值如何工作的一些其他细节。我不确定。

(检查这两种情况下 make -p 的输出,了解我对特定于目标的变量值的含义。)