4

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
Alex
  • 3,264
  • 1
  • 25
  • 40

2 Answers2

5

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.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • Thanks, I thought, for a simple case where only yes or no are needed there must be already a macro... thought wrong! Or I'm really bad at searching. I think I'll have a lot of those so I better find the minimal/cleanest (maybe I can wrap the case in a loop... will see) – Alex Aug 20 '15 at 01:58
  • There's a lot of redundancy on the variable names, prone to errors (IMO) – Alex Aug 20 '15 at 01:59
  • @Alex - the learning curve for the autotools is sometimes frustratingly steep. The [Autotools Mythbuster](https://autotools.io/index.html) is the best *modern* tutorial I know of. Apart from its frustrating site navigation. It even covers some `M4sh` basics if you want to write your own macros. – Brett Hale Aug 20 '15 at 02:03
3

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])
Alex
  • 3,264
  • 1
  • 25
  • 40
  • Still, this is *much* more readable than the original. And theres' no reason why you can't change `$enable_form_helper` before the `AM_CONDITIONAL` if other options change or override this option. – Brett Hale Aug 21 '15 at 11:08
  • Thank you Alex. Like you, I still find this very annoying! I've never been an m4 expert, but maybe it's time to dig in. There has to be a cleaner solution. – Daniel Santos Apr 02 '19 at 01:41