autotools is there a short for AC_ARG_ENABLE action-if-given
Asked Answered
L

3

5

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
Laural answered 19/8, 2015 at 10:15 Comment(0)
D
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.

Deweydewhirst answered 20/8, 2015 at 1:27 Comment(3)
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)Laural
There's a lot of redundancy on the variable names, prone to errors (IMO)Laural
@Laural - the learning curve for the autotools is sometimes frustratingly steep. The Autotools Mythbuster 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.Deweydewhirst
L
4

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])
Laural answered 20/8, 2015 at 19:49 Comment(2)
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.Deweydewhirst
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.Mistakable
E
3

You don't need to re-invent the wheel here at all. Instead of rolling your own code or clearing the default code via [], just use the default code by leaving the action-if-given and action-if-not-given sections unset. The other "trick" here is to use a different test form for options that are enabled by default vs ones that are disabled by default (!= xno instead of = xyes).

Use this for defaulting to truthy:

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

Or for if you want the default to be falsey use this arrangement instead:

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

Either way you then use in your Makefile.am with:

if WELCOME
...
endif
Engud answered 12/3 at 10:19 Comment(2)
Thanks, if it works it's very neat, I haven't worked on an autoconf project in a long time, but if I get the chance I'll try itLaural
Usually the implicit staff is very hard to learn, thanks very much for this posting. I would never learn about this by reading the books.Pandanus

© 2022 - 2024 — McMap. All rights reserved.