Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 478
Alex Avatar asked Feb 09 '23 06:02

Alex


2 Answers

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.

like image 158
Brett Hale Avatar answered Mar 05 '23 19:03

Brett Hale


I took a deeper look to the question and at the moment this below is the syntax most clean, compact and with less redundancy (still too much IMO, I just missed one replace pasting here) I can get working for a simple enable option where I want only yes or no with a default and an error message:

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])
like image 38
Alex Avatar answered Mar 05 '23 19:03

Alex