On Sat, May 24, 2025 at 06:56:42PM +0200, R. Diez wrote:
> The configure.ac code is complicated, but basically it boils down to
> this (simplified):
> 
> AC_ARG_ENABLE(some-option,
>   AS_HELP_STRING([--enable-some-option=[[[yes/no/auto]]]],
>                  [blah blah (default is $someDefault)])
> 
> The trouble is, when I run "./configure --help", the $someDefault
> variable appears as a literal, that is, it is not being substituted. I
> tried with an M4 macro instead of a shell variable, to no avail.

M4 macros are fine but as documented, AS_HELP_STRING does not expand its
second argument, so you need to be careful about quoting when you use it.
For example:

  % cat >configure.ac <<'EOF'
  AC_INIT([test], [0])
   
  m4_define([someDefault], [yes])
  AC_ARG_ENABLE([some-option],
    [AS_HELP_STRING([--enable-some-option],
      [blah blah (default is ]m4_defn([someDefault])[)])])
EOF

  % autoconf && ./configure --help | grep some-option
    --enable-some-option    blah blah (default is yes)

> After a while struggling with this weird Autoconf behaviour, I came up
> with this test:
> 
> AS_IF([test "a" = "a"], [
>   AC_MSG_NOTICE([true branch])
>   
> AC_ARG_ENABLE([argname-true-branch],AS_HELP_STRING([--enable-argname-true-branch],[Help
>  string for the true branch]))
> ], [
>   AC_MSG_NOTICE([false branch])
>   
> AC_ARG_ENABLE([argname-false-branch],AS_HELP_STRING([--enable-argname-false-branch],[Help
>  string for the false branch]))
> ])
> 
> The output when running "./configure --help" is:
> --enable-argname-true-branch  Help string for the true branch
> --enable-argname-false-branch Help string for the false branch
[...]
> My guess is that "./configure --help" is not evaluating the script
> properly, but it is just grepping all AC_ARG_ENABLE occurrences and
> doing some minimal M4 parsing which does not include normal variable
> or M4 macro substitution.

All the help text is generated statically at m4 time and is included
into the configure script as a quoted here-document.  So the shell will
not expand anything found within the help text at configure time.

If the defaults can be determined by m4 then you can use m4 to generate
the help string.  Note that AS_IF is not an m4 conditional, both the
true and false "branches" are included in the configure script, so both
AC_ARG_EANBLE macros will be expanded by m4 (and thus all their m4 side
effects will be performed).

Otherwise, just write some text which is vague about the default.
Something like "default: auto" is usually fine.

Hope that helps,
  Nick

Reply via email to