【发布时间】:2015-04-28 20:01:27
【问题描述】:
我想修改一个 configure.ac 脚本,以便当我通过 autoconf 生成它的配置脚本时,它会有一个自定义帮助消息。
例如:
$autoconf
$./configure --help
产量
"Hello World"
而不是讨论微调安装目录和修改构建标志的默认值。
这可能吗?
【问题讨论】:
我想修改一个 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 脚本并根据您的需要进行修改。
【讨论】:
我尝试了answer by baf,但发现它导致“Hello world”被无条件打印,不管--help!这可能是由于autoconf 的版本差异;我使用的是 2.69 版。
我发现作为一种变体可以检查$ac_init_help,并且仅在打印消息后才退出。这在configure.ac 或configure.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
[...]
【讨论】: