autotools 是否有 AC_ARG_ENABLE action-if-given 的缩写

问题描述 投票:0回答:3

我可能需要添加很多

AC_ARG_ENABLE
,目前我正在使用下面的语法,这是我唯一能工作的语法,但我想知道是否已经有一些m4宏用于简单的action-if-given测试我正在使用的(我做了一些搜索,但尚未发现任何结果)或更好的更清晰的语法。

我见过一些空的例子

[]
但就是无法让它工作,我需要创建一个新的宏吗?

AC_ARG_ENABLE([welcome],
    AS_HELP_STRING([--enable-welcome], [Enable welcome route example @<:@default=yes@:>@]),
    [case "${enableval}" in
        yes) enable_welcome=true ;;
        no)  enable_welcome=false ;;
        *) AC_MSG_ERROR([bad value ${enableval} for --enable-welcome]) ;;
     esac],[enable_welcome=true])
AM_CONDITIONAL([ENABLE_WELCOME], [test x$enable_welcome = xtrue])

这是我如何在

Makefile.am

上使用它
if ENABLE_WELCOME
...
endif
autotools autoconf automake
3个回答
4
投票

我只是将

AC_ARG_ENABLE
与处理选项本身分开,以分离选项处理逻辑,并保持
configure.ac
易于阅读:

AC_ARG_ENABLE([welcome],
  [AS_HELP_STRING([--enable-welcome], [... description ... ])],,
  [enable_welcome=yes])

# i.e., omit '[<action-if-given>]', still sets '$enable_welcome'

enable_welcome=`echo $enable_welcome` # strip whitespace trick.
case $enable_welcome in
  yes | no) ;; # only acceptable options.
  *) AC_MSG_ERROR([unknown option '$enable_welcome' for --enable-welcome]) ;;
esac

# ... other options that may affect $enable_welcome value ...

AM_CONDITIONAL([ENABLE_WELCOME], [test x$enable_welcome = xyes])

当然,

autoconf
促进使用可移植的shell结构,如
AS_CASE
AS_IF
等。可能是“正确的事情”,但我发现语法很烦人。如果我受到 shell 限制的困扰,我想我将不得不考虑它们。

如果此

yes/no
构造频繁出现,您可以使用
AC_DEFUN
定义自己的函数,需要一些最小的
m4
概念。但是您应该能够找到大量有关如何访问函数参数和返回值的示例。


3
投票

我更深入地研究了这个问题,目前下面的语法是最干净、紧凑且冗余较少的(在我看来仍然太多,我只是错过了在这里粘贴的一个替换)我可以开始使用一个简单的启用选项,其中我只想要

yes
no
并带有默认值和错误消息:

AC_ARG_ENABLE([form-helper],
    AS_HELP_STRING([--enable-form-helper], [Enable Form helper @<:@default=yes@:>@]),
    [AS_CASE(${enableval}, [yes], [], [no], [],
             [AC_MSG_ERROR([bad value ${enableval} for --enable-form-helper])])],
    [enable_form_helper=yes])
AM_CONDITIONAL([ENABLE_FORM_HELPER], [test x$enable_form_helper = xyes])

0
投票

你根本不需要在这里重新发明轮子。无需滚动自己的代码或通过

[]
清除默认代码,只需通过保留 action-if-given 和 action-if-not-given 部分未设置来“使用”默认代码。这里的另一个“技巧”是对默认启用的选项和默认禁用的选项使用不同的测试表单(!= xno而不是
= xyes
)。
使用它来默认为真:

AC_ARG_ENABLE([welcome], AS_HELP_STRING([--disable-welcome], [Disable welcome route example])) AM_CONDITIONAL([WELCOME], [test x"$enable_welcome" != x"no"])

或者如果您希望默认值是错误的,请改用此安排:

AC_ARG_ENABLE([welcome], AS_HELP_STRING([--enable-welcome], [Enable welcome route example])) AM_CONDITIONAL([WELCOME], [test x"$enable_welcome" = x"yes"])

无论哪种方式,您都可以在 
Makefile.am

中使用:

if WELCOME
...
endif

© www.soinside.com 2019 - 2024. All rights reserved.