您可以覆盖作为 运行 autoconf 的一部分生成的默认配置帮助消息吗?

Can you override the default configure help message generated as part of running autoconf?

我想修改一个 configure.ac 脚本,这样当我通过 autoconf 生成它的配置脚本时,它就会有一条自定义帮助消息。

例如:

$autoconf
$./configure --help

产量

"Hello World"

而不是谈论微调安装目录和修改构建标志的默认设置。

这可能吗?

查看 autoconf general.m4 脚本中的 _AC_INIT_HELP 宏。它负责打印帮助消息。

此脚本将文本插入不同的 diversions,如 general.m4:

中所列
dnl The order of the diversions here is
dnl - HELP_BEGIN
dnl   which may be extended by extra generic options such as with X or
dnl   AC_ARG_PROGRAM.  Displayed only in long --help.
dnl
dnl - HELP_CANON
dnl   Support for cross compilation (--build, --host and --target).
dnl   Display only in long --help.
dnl
dnl - HELP_ENABLE
dnl   which starts with the trailer of the HELP_BEGIN, HELP_CANON section,
dnl   then implements the header of the non generic options.
dnl
dnl - HELP_WITH
dnl
dnl - HELP_VAR
dnl
dnl - HELP_VAR_END
dnl
dnl - HELP_END
dnl   initialized below, in which we dump the trailer (handling of the
dnl   recursion for instance).

显示 Hello World 帮助消息的最简单方法是在 configure.ac 文件的末尾插入以下代码:

m4_cleardivert([HELP_BEGIN])dnl
m4_cleardivert([HELP_CANON])dnl
m4_cleardivert([HELP_ENABLE])dnl
m4_cleardivert([HELP_WITH])dnl
m4_cleardivert([HELP_VAR])dnl
m4_cleardivert([HELP_VAR_END])dnl
m4_cleardivert([HELP_END])dnl
m4_divert_push([HELP_BEGIN])dnl
  cat <<_ACEOF
Hello World
_ACEOF
m4_divert_pop([HELP_BEGIN])dnl
m4_divert_push([HELP_END])dnl
exit 0
m4_divert_pop([HELP_END])dnl

它将清除所有转移并插入您的自定义文本,而无需包含任何自定义 m4 脚本。 exit 需要在显示帮助时停止处理 configure 脚本。


如果您想对帮助文本进行更多更改,您可以在 configure.ac 文件的开头包含您自己的 m4 脚本:

m4_include([custom_help.m4])

_AC_INIT_HELP 宏复制到您的 custom_help.m4 脚本并根据您的需要进行修改。

我尝试了 ,但发现它导致无条件打印“Hello world”,而不管 --help!这可能是由于 autoconf 中的版本差异所致;我使用的是 2.69 版本。

我发现的是检查 $ac_init_help 并且仅在打印消息后立即退出的变体。这发生在 configure.acconfigure.in:

的末尾
dnl# Clear the default help message.
m4_cleardivert([HELP_BEGIN])dnl
m4_cleardivert([HELP_CANON])dnl
m4_cleardivert([HELP_ENABLE])dnl
m4_cleardivert([HELP_WITH])dnl
m4_cleardivert([HELP_VAR])dnl
m4_cleardivert([HELP_VAR_END])dnl
m4_cleardivert([HELP_END])dnl

dnl# Specify custom help.
m4_divert_push([HELP_BEGIN])dnl
if test "$ac_init_help" = "long"; then
  cat <<_ACEOF
Hello world.  Remember this is processed by M4, so you will need
to quote any string that contains square brackets, for example:

Usage: my-program [[options]] [[file [file..]]]

_ACEOF

  # Stop after printing the help.
  exit 0
fi
m4_divert_pop([HELP_BEGIN])dnl

然后它在有和没有--help的情况下都有效:

$ ./configure --help
Hello world.  Remember this is processed by M4, so you will need
to quote any string that contains square brackets, for example:

Usage: my-program [options] [file [file..]]

$ ./configure
checking for gcc... gcc
[...]