AC_ARG_ENABLE 在 m4_foreach_w 循环中:没有帮助字符串

AC_ARG_ENABLE in an m4_foreach_w loop: no help string

我希望通过以下方式生成很多 --enable-*/--disable-* 选项:

COMPONENTS([a b c], [yes])

其中第二个参数是自动 enable_* 变量的默认值。我的第一次尝试是在 m4_foreach_w 中写一个 AC_ARG_ENABLE(...),但到目前为止,我只是让第一个组件出现在 ./configure --help 输出中。

如果我添加手写的 AC_ARG_ENABLEs,它们将照常工作。

无论如何,--enable-*/--disable-* 选项都可以正常工作,只是缺少帮助文本。

这里是重现问题的完整代码:

AC_INIT([foo], 1.0)
AM_INIT_AUTOMAKE([foreign])

AC_DEFUN([COMPONENTS],
[
    m4_foreach_w([component], [], [
        AS_ECHO(["Processing [component] component with default enable="])
        AC_ARG_ENABLE([component],
            [AS_HELP_STRING([--enable-[]component], [component] component)],
            ,
            [enable_[]AS_TR_SH([component])=]
        )
    ])
    AC_ARG_ENABLE([x],
        [AS_HELP_STRING([--enable-[]x], [component x])],
        ,
        [enable_[]AS_TR_SH([x])=]
    )
    AC_ARG_ENABLE([y],
        [AS_HELP_STRING([--enable-[]y], [component y])],
        ,
        [enable_[]AS_TR_SH([y])=]
    )
])

COMPONENTS([a b c], [yes])

for var in a b c x y; do
    echo -n "$enable_$var="
    eval echo "$enable_$var"
done
AC_CONFIG_FILES(Makefile)
AC_OUTPUT

还有一个空 Makefile.am。要验证选项是否有效:

$ ./configure --disable-a --disable-b --disable-d --disable-x
configure: WARNING: unrecognized options: --disable-d
...
Processing component a with default enable=yes
Processing component b with default enable=yes
Processing component c with default enable=yes
$enable_a=no
$enable_b=no
$enable_c=yes
$enable_x=no
$enable_y=yes

在我查阅了 autoconf 源代码后,我发现这与 AC_ARG_ENABLE 实现中的 m4_divert_once 调用有关:

# AC_ARG_ENABLE(FEATURE, HELP-STRING, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
# ------------------------------------------------------------------------
AC_DEFUN([AC_ARG_ENABLE],
[AC_PROVIDE_IFELSE([AC_PRESERVE_HELP_ORDER],
[],
[m4_divert_once([HELP_ENABLE], [[
Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]]])])dnl
m4_divert_once([HELP_ENABLE], [])dnl
_AC_ENABLE_IF([enable], [], [], [])dnl
])# AC_ARG_ENABLE

# m4_divert_once(DIVERSION-NAME, CONTENT)
# ---------------------------------------
# Output CONTENT into DIVERSION-NAME once, if not already there.
# An end of line is appended for free to CONTENT.
m4_define([m4_divert_once],
[m4_expand_once([m4_divert_text([], [])])])

我猜 HELP-STRING 参数以未扩展的形式被记住,所以它只为所有组件添加一次。手动扩展 AC_HELP_STRING 做我想要的:

AC_DEFUN([COMPONENTS],
[
    m4_foreach_w([comp], [], [
        AS_ECHO(["Processing component 'comp' with default enable="])
        AC_ARG_ENABLE([comp],
            m4_expand([AS_HELP_STRING([--enable-comp], enable component comp)]),
            ,
            [enable_[]AS_TR_SH([comp])=]
        )
    ])
])

COMPONENTS([a b c x y], [yes])

我找不到正确引用 components 的方法,使其在 m4_foreach_w 中用作循环变量后显示为字符串,所以我只是重命名它以节省时间麻烦了。