help-make
[Top][All Lists]
Advanced

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

Re: Conditionals, step one


From: Paul Smith
Subject: Re: Conditionals, step one
Date: Thu, 16 Sep 2010 15:27:24 -0400

On Thu, 2010-09-16 at 05:56 -0700, Radly wrote:
> OS = $OSTYPE
> 
> ifeq ($(value OS), darwin10.0)
>   TXT = GOT_IT
> else
>   TXT = MISSED_IT
> endif
> 
> all:
>         @echo OS = $(value OS)
>         @echo $(TXT)

Wow, what an interesting combination of incorrectnesses :-)

First, as Derek says, this line:

> OS = $OSTYPE

is wrong.  This uses shell variable syntax.  A make variable reference
would look like $(OSTYPE) or ${OSTYPE}.  If $OSTYPE were to be
interpreted by make it would be the value of the variable $O, plus the
static string "TYPE".  Since O is presumably unset, the result would be
TYPE.

Make always imports all your shell environment variables and makes them
available as make variables (but you still always have to use make
syntax to reference them) so if you use $(OSTYPE) you will get what you
want.

However, that's not the end.

Next, you use this:

> ifeq ($(value OS), darwin10.0)

This is wrong as well: the $(value ...) function returns the _unexpanded
text_ of the variable, so in this case the unexpanded text of the
variable OS is the literal string "$OSTYPE".  So here, you're comparing
the string "darwin10.0" with the string "$OSTYPE" and obviously that
will NEVER be true.

Then finally, you have this:

>         @echo OS = $(value OS)

it SEEMS like this is printing the right thing, so you're confused about
why the ifeq is not succeeding.  But what's really going on here?  Again
you're using the $(value ..) function so this expands to the literal
string "$OSTYPE".

Then you invoke the shell and ask it to run the command:

        echo OS = $OSTYPE

The shell sees that command and expands the shell variable "OSTYPE"
which expands to what you expected, and confusion ensues.


So, the short take-aways here are:
     1. Always use make variable reference syntax when working with
        variables inside a makefile: $(OSTYPE) not $OSTYPE
     2. Never use the $(value ..) function unless you really need it...
        and here's a hint, you'll probably never need it.  That function
        is useful only in a very small, very obscure set of situations.

The right way to write your makefile would be something like:

        ifeq ($(OSTYPE),darwin10.0)
          TXT = GOT_IT
        else
          TXT = MISSED_IT
        endif

        all:
                @echo OS = $(OSTYPE)
                @echo $(TXT)

However, assuming you're doing a lot of variable assignment I recommend
a very different method.  Instead of big if-else-endif statements, just
use include files with suffixes for each supported OSTYPE.  Something
like:

        include $(firstword $(wildcard osinfo.$(OSTYPE)) osinfo.notype)

        all:
                @echo OS = $(OSTYPE)
                @echo $(TXT)

Now create a file for darwin10.0:

        $ cat osinfo.darwin10.0
        TXT = GOT_IT

and another file for "unknown OSTYPE":

        $ cat osinfo.notype
        TXT = MISSED_IT

Season to taste.




reply via email to

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