automake
[Top][All Lists]
Advanced

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

Re: Automake Conditional and ARG_VAR Question


From: Ralf Wildenhues
Subject: Re: Automake Conditional and ARG_VAR Question
Date: Thu, 6 May 2010 19:23:48 +0200
User-agent: Mutt/1.5.20 (2009-10-28)

Hello David,

* David Bond wrote on Tue, Apr 27, 2010 at 07:54:02PM CEST:
> I'm working on a project that we are converting to autotools and I
> had a question. If this is the wrong place to ask these please
> direct me to the right location.

This is the right place to ask, but we might be a little slow (or a lot,
even) in answering; in this case I plainly overlooked your mail.  Sorry
about that.

> Our project has a particle feature with three posible states:
> STATIC, DYNAMIC, or NONE
> 
> Ideally we would like the build to be like:
> ./configure STATE=STATIC ; make
> 
> What I want this to do is, depending on the configure time STATE
> value giveni, generate a Makefile that executes Make code specific
> for that STATE. There should also be a default STATE in case this
> command line option not be given. i.e. DYNAMIC. This is what I
> believe should accomplish
> this:
> 
> configure.ac
> ---------------------------------------
> AM_CONDITIONAL([STATE_DYNAMIC],[test x$STATE = xDYNAMIC])
> AM_CONDITIONAL([STATE_STATIC],[test x$STATE = xSTATIC])
> 
> AC_ARG_VAR([STATE],[The STATE STATIC DYNAMIC or NONE.])
> AC_SUBST([STATE],[DYNAMIC])

Almost.  This last line overrides $STATE to be set to DYNAMIC no matter
what you passed on the configure command line.  Replace it with these
two lines:
  : ${STATE=DYNAMIC}
  AC_SUBST([STATE])

and it will do what you want.

> Also, what is the reason for appending an x in
> front of test varibles. I saw that in the make manual along with
> several examples.

Shell syntax oddities, and portability issues.  If the value of the
variable might begin with a hyphen/minus, then without the x, the 'test'
builtin may mistake its first argument for an operator.  If your $STATE
variable might be empty, then without the x, the number of arguments
passed to 'test' is wrong, and you will get an error.  This can however
also be avoided by double-quoting the expansion:
  test "$STATE" = DYNAMIC

which BTW is needed anyway if the variable might expand to more than one
word.  So, the safe idiom to protect against all three is something like
  test x"$STATE" = xDYNAMIC

(where of course, putting the x outside of the double-quotes is a mere
stylistic decision).

Hope that helps.

Cheers,
Ralf




reply via email to

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