autotools 用户变量的优先级是什么?
What is the precedence in autotools user variables?
如果您的项目有一个 configure.ac 和许多 Makefile.am 个文件,可以传递一个用户变量,例如 CFLAGS 以配置为
CFLAGS=DEBUG_OPTIONS ./configure
但是,如果在 configure.ac 中,你有这样的语句
CFLAGS=NON_DEBUG_OPTIONS(你可以假设这是在 AC_PROG_CC 之后)
最终对象是否可调试?
从 Autoconf 的角度来看,CFLAGS
is an output variable,就像您的 configure.ac 通过 AC_SUBST
宏显式声明的那样。输出变量的细节在 Autoconf 文档中散布了一些,但基本上,
[they] are shell variables whose values are substituted into files
that configure outputs.
(Autoconf manual, section 7.2)
如果尚未设置该变量(例如来自环境的),AC_PROG_CC
宏会为CFLAGS
提供默认值;否则,它保持值不变。这就是为什么在命令行上以您显示的方式向 Autoconf configure
脚本指定 CFLAGS
和某些其他输出变量通常有效的原因——这是标准的 shell 语法导致要放置在命令环境中的变量绑定 运行.
手册接着说,
[Being an output variable] means that AC_OUTPUT
replaces instances of ‘@variable@
’ in
input files with the value that the shell variable variable
has when
AC_OUTPUT
is called.
(强调已添加。)虽然这在 AC_SUBST
宏的文档中,但它适用于预设输出变量,与通过 AC_SUBST
设置的输出变量相同。因此,如果 configure
在 AC_OUTPUT
之前为 CFLAGS
分配了一个新值,那么最后分配的值将被替换到您的 Makefile 中。那么,在直接回答您的问题时,您描述的情况将导致构建具有非调试选项。
另请注意,这根本不是优先级问题,因为只涉及一个变量。在环境中设置的 CFLAGS
实际上是 与 configure
脚本在内部访问和使用的相同变量 ,您可以通过赋值在内部进行修改,最终是替换到您的 Makefile 中。因此,这是一个简单的操作顺序问题。
最后,我观察到只有当您违反关于 "user variables"(例如 CFLAGS
)的 Autotools 原则时,您的问题才会出现。该变量的 Autoconf 文档是这样写的:
Sometimes package developers are tempted to set user variables such as
CFLAGS
because it appears to make their job easier. However, the
package itself should never set a user variable, particularly not to
include switches that are required for proper compilation of the
package. Since these variables are documented as being for the package
builder, that person rightfully expects to be able to override any of
these variables at build time. If the package developer needs to add
switches without interfering with the user, the proper way to do that
is to introduce an additional variable. Automake makes this easy by
introducing AM_CFLAGS
(see Flag Variables Ordering), but the concept
is the same even if Automake is not used.
(强调已添加。)
最后,请注意,用户始终可以通过在 make
命令行上设置一个值来覆盖输出变量或其他生成变量,作为 make
的参数,而不是 或除了将其指定为 configure
.
如果您的项目有一个 configure.ac 和许多 Makefile.am 个文件,可以传递一个用户变量,例如 CFLAGS 以配置为
CFLAGS=DEBUG_OPTIONS ./configure
但是,如果在 configure.ac 中,你有这样的语句 CFLAGS=NON_DEBUG_OPTIONS(你可以假设这是在 AC_PROG_CC 之后) 最终对象是否可调试?
从 Autoconf 的角度来看,CFLAGS
is an output variable,就像您的 configure.ac 通过 AC_SUBST
宏显式声明的那样。输出变量的细节在 Autoconf 文档中散布了一些,但基本上,
[they] are shell variables whose values are substituted into files that configure outputs.
(Autoconf manual, section 7.2)
如果尚未设置该变量(例如来自环境的),AC_PROG_CC
宏会为CFLAGS
提供默认值;否则,它保持值不变。这就是为什么在命令行上以您显示的方式向 Autoconf configure
脚本指定 CFLAGS
和某些其他输出变量通常有效的原因——这是标准的 shell 语法导致要放置在命令环境中的变量绑定 运行.
手册接着说,
[Being an output variable] means that
AC_OUTPUT
replaces instances of ‘@variable@
’ in input files with the value that the shell variablevariable
has whenAC_OUTPUT
is called.
(强调已添加。)虽然这在 AC_SUBST
宏的文档中,但它适用于预设输出变量,与通过 AC_SUBST
设置的输出变量相同。因此,如果 configure
在 AC_OUTPUT
之前为 CFLAGS
分配了一个新值,那么最后分配的值将被替换到您的 Makefile 中。那么,在直接回答您的问题时,您描述的情况将导致构建具有非调试选项。
另请注意,这根本不是优先级问题,因为只涉及一个变量。在环境中设置的 CFLAGS
实际上是 与 configure
脚本在内部访问和使用的相同变量 ,您可以通过赋值在内部进行修改,最终是替换到您的 Makefile 中。因此,这是一个简单的操作顺序问题。
最后,我观察到只有当您违反关于 "user variables"(例如 CFLAGS
)的 Autotools 原则时,您的问题才会出现。该变量的 Autoconf 文档是这样写的:
Sometimes package developers are tempted to set user variables such as
CFLAGS
because it appears to make their job easier. However, the package itself should never set a user variable, particularly not to include switches that are required for proper compilation of the package. Since these variables are documented as being for the package builder, that person rightfully expects to be able to override any of these variables at build time. If the package developer needs to add switches without interfering with the user, the proper way to do that is to introduce an additional variable. Automake makes this easy by introducingAM_CFLAGS
(see Flag Variables Ordering), but the concept is the same even if Automake is not used.
(强调已添加。)
最后,请注意,用户始终可以通过在 make
命令行上设置一个值来覆盖输出变量或其他生成变量,作为 make
的参数,而不是 或除了将其指定为 configure
.