bug-autoconf
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting a default value for an AC_ARG_VAR


From: Nick Bowler
Subject: Re: Setting a default value for an AC_ARG_VAR
Date: Sun, 16 Dec 2018 17:00:49 -0500

On 12/16/18, Reuben Thomas <address@hidden> wrote:
> On Fri, 14 Dec 2018 at 14:51, Nick Bowler <address@hidden> wrote:
>
>>
>> If you want to set a value to a variable only if the variable is
>> unset, a typical way to do that in shell programming is:
>>
>>   : "${FOO=value-if-unset}"
>>
>> Or if you prefer also to set it the variable is _empty_ (i.e., including
>> the case where it is set to the empty string):
>>
>>   : "${FOO:=value-if-empty}"
>>
>
> So the answer seems to be "do it with shell". I still get confused (as
> here) with whether it's possible (or good style) to use shell directly,
> or whether there's something I should be doing with autoconf macros.

With Autoconf there are basically two distinct execution phases, and you
sometimes have choices about where your code lives:

  - You write m4 code, which runs on the developer's system, or
  - You write shell code, which runs on the user's system.

Or some combination of both.  Since "whether or not WORD_SIZE was set
in the user's configure environment" can't possibly be known by the
developer, you have to run it on the user system which means you have
to do it in shell.  Essentially everything you write in your configure.ac
which survives the m4 processing will end up in your configure script,
as shell program to be executed by the user.

Think of the Autoconf macros the same way you might think of standard
library functions in many programming languages: you can and probably
should use them when they help, otherwise it's totally fine to code
things up yourself.

Since the two forms above to conditionally assign variables are both
widely portable in practice and widely understood by shell programmers,
there's probably no advantage to Autoconf supplying a "standard" macro
to do it (AS_VAR_SET_IF could be used to do this too, but it's probably
overkill for your application).

> So in the end I have this:
>
> dnl Set size of word and address
> AC_ARG_VAR([WORD_SIZE],
>   [value of WORD_SIZE register [default: sizeof(long)]])
> AC_CHECK_SIZEOF([long])
> : "${WORD_SIZE=SIZEOF_LONG}"
> AC_ARG_VAR([ADDRESS_SIZE],
>   [value of ADDRESS_SIZE register [default: sizeof(size_t)]])
> AC_CHECK_SIZEOF([size_t])
> : "${ADDRESS_SIZE=SIZEOF_SIZE_T}"
>
> Does that look about right?

: "${WORD_SIZE=SIZEOF_LONG}" will, provided WORD_SIZE is previously
unset, set WORD_SIZE to the string SIZEOF_LONG.  That's probably
not what you wanted.  Try

  : "${WORD_SIZE=$SIZEOF_LONG}"

And likewise for the other assignment.

Hope that helps,
  Nick



reply via email to

[Prev in Thread] Current Thread [Next in Thread]