[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gmake: testing if a var is [un]defined
From: |
John Graham-Cumming |
Subject: |
Re: gmake: testing if a var is [un]defined |
Date: |
Fri, 14 Jan 2005 06:49:16 -0500 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040208 Thunderbird/0.5 Mnenhy/0.6.0.104 |
Ernest wrote:
Consider a variable called FOO...is there a more efficient/quick/simple way
to test if this variable is defined (vs. undefined) at *runtime* than as
follows? Note that "defined" *can* mean an empty value ("FOO=").
# echo "no" if FOO is undefined
# or "yes" if FOO is defined
foo:
echo $(if $(findstring undefined,$(origin FOO)),no,yes)
That looks like a perfectly good way to do it. You could use filter
instead of findstring. And you could even defined a function so that
it's clear what's going on:
defined = $(if $(filter undefined,$(origin $1)),no,yes)
foo: ; @echo $(call defined,FOO)
You might find it's better to have 'defined' return an empty string for
'no', and a non-empty string for 'yes' if you plan to use the result of
'defined' in a $(if):
defined = $(filter-out undefined,$(origin $1))
foo: ; @echo $(if $(call defined,FOO),foo is defined,foo is undefined)
John.
--
John Graham-Cumming
jgc@jgc.org
Home: http://www.jgc.org/
Work: http://www.electric-cloud.com/
POPFile: http://getpopfile.org/