autotoolsautoconfautomake

autotools is there a short for AC_ARG_ENABLE action-if-given


I may need to add a lot of AC_ARG_ENABLE, currently I'm using the syntax below which is the only I got working, but I wonder if there's already some m4 macro for a simple action-if-given test as I'm using (I did some search but found nothing yet) or a better cleaner syntax.

I've seen some example with it empty [] but just can't get it working, do I need to create a new macro?

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])

here how I use it on the Makefile.am

if ENABLE_WELCOME
...
endif

Solution

  • I just separate AC_ARG_ENABLE from handling the option itself, to separate the option handling logic, and keep configure.ac easy to read:

    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])
    

    Of course, autoconf promotes the use of portable shell constructs, like AS_CASE, AS_IF, etc. Probably the "right thing" to do, but I find the syntax annoying. If I get bitten by shell limitations, I guess I'll have to consider them.

    If this yes/no construct appear frequently, you might define your own function with AC_DEFUN requiring a some minimal m4 concepts. But you should be able to find plenty of examples on how to access function arguments and return values.