更改 configure.ac 中的 *FLAGS 与使用子项目缓存 [英] Changing *FLAGS in configure.ac vs. caching with subprojects
问题描述
假设我希望在我的 configure
脚本中向 CFLAGS
添加一个特定标志,该标志应该传播到所有子项目的 configure
脚本:
Say I wish to add a specific flag to CFLAGS
within my configure
script, that should propagate to all subprojects' configure
scripts:
CFLAGS+=" -Dfoobar"
export CFLAGS
AC_CONFIG_SUBDIRS([sub])
这在 configure
被简单调用时有效.一旦发生以下情况之一:
This works when configure
is invoked trivially. As soon as one of the following happens:
CFLAGS
在调用configure
时在环境中导出CFLAGS
在configure
命令行设置- 使用缓存(
configure -C
)
CFLAGS
is exported in the environment whenconfigure
is invokedCFLAGS
is set onconfigure
command-line- caching is used (
configure -C
)
这种方法不再有效.在前两种情况下,导出的 CFLAGS
被简单地忽略了;在最后一个中,configure
失败了
This approach no longer works. In the first two cases, the exported CFLAGS
is simply ignored; and in the last one, configure
fails with
configure: error: `CFLAGS' 未在上次运行中设置
configure: error: `CFLAGS' was not set in the previous run
<小时>
我设法通过以下方式使其可靠地工作:
I have managed to get this working reliably by:
AM_CFLAGS+=" -Dfoobar"
export AM_CFLAGS
AC_SUBST([AM_CFLAGS]) # repeat this line in every configure.ac for each *FLAGS
AC_CONFIG_SUBDIRS([sub])
考虑到有多个子项目,以及多个 *FLAGS
变量可能需要像这样设置,这是中途可以但仍然不是最理想的.有没有办法只通过破解顶级 configure.ac
来完成这项工作?
Considering there are multiple subprojects, and multiple *FLAGS
variables that may need to be set like this, this is half-way okay but still suboptimal. Is there a way to make this work by only hacking the top-level configure.ac
?
推荐答案
最终的解决方案是 un-precious 受影响的变量:
The ultimate solution was to un-precious the affected variables:
顶级 configure.ac:
Top-level configure.ac:
AC_INIT([test], [0.1])
AC_CONFIG_MACRO_DIR([m4]) # for ax_append_flag.m4
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_PROG_SED
# Modify the CFLAGS. AX_APPEND_FLAG makes sure not to add the flag if it's already there
AX_APPEND_FLAG([-Dtop-configure], [CFLAGS])
AC_DEFUN([AX_UNPRECIOUS], [
m4_define([_AC_PRECIOUS_VARS], m4_bpatsubst(_AC_PRECIOUS_VARS, [$1
], []))
])
AX_UNPRECIOUS([CFLAGS])
export CFLAGS
AC_CONFIG_SUBDIRS([sub])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
在幕后,CFLAGS
从未被视为宝贵的,因此从未缓存或传递给子包 configure
s—他们将其视为环境变量 独占,然后自己缓存在通用的顶级config.cache
中.
Behind the curtains, CFLAGS
is never seen as precious and thus never cached or passed to sub-package configure
s—they see it as an environment variable exclusively, and then cache it themselves in the common top-level config.cache
.
这非常可靠,并且通过允许缓存值甚至跨顶级配置运行(并且更简单)来改进我之前的解决方案.
This works very reliably, and improves upon my previous solution by allowing cached values even across top-level configure runs (and by being simpler).
这篇关于更改 configure.ac 中的 *FLAGS 与使用子项目缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!