help-make
[Top][All Lists]
Advanced

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

Re: Determine if variable exists on make side from sh side


From: Paul D. Smith
Subject: Re: Determine if variable exists on make side from sh side
Date: Mon, 10 Apr 2006 15:36:32 -0400

%% "PATTON, BILLY \(SBCSI\)" <address@hidden> writes:

  pb> Here is what I have:
  pb> all :
  pb>         @list=REFRESH+ldb+celltools+pub ;\
  pb>         if ! [ -z "$($list)" ] ; then \
  pb>                 echo "list = '$$list'"; \
  pb>         fi

Ouch.  You've simply got to remember: you cannot, cannot, cannot, EVER
reference make variables from the shell.

The behavior of make in this area is very simple; if you're confused
it's because you're trying to imagine that you can do things that you
can't do.


First, make expands the entire shell script according to make expansion
rules.  So, every "$" in the shell script is expanded.

Second, make gives the expanded string to the shell to execute.

Third, make waits for the shell to exit and determines whether it failed
or not by looking at the exit code.

That's it.  That's all there is.

So, just run through that processing in your mind to see what will
happen.  In the above example make will expand the string first; the
result of that will be this string:

    list=REFRESH+ldb+celltools+pub ;\
         if ! [ -z "" ] ; then \
                 echo "list = '$list'"; \
         fi

Here's why:

    - The first thing to be expanded is '$($list)'.  First make expands
      '$l' which is empty.  Adding that to 'ist' gives just 'ist'.  Then
      it expands '$(ist)' which is also empty.  So the result is empty.

    - The next thing to expand is '$$list', and here the $$ expands to a
      single "$", so the result of this is '$list'.

Now that string is passed to the shell.  You can figure out what happens
then.

  pb> I need to determine if the variable REFRESH+ldb+celltools+pub exists
  pb> in the make world.  This name is normally  generated for values passed
  pb> in to a define.

  pb> I believe that the -z will tell if a sh variable has no value

No, that's not correct.

-z will tell you if a STRING is empty, not if a variable has no value.
The string is expanded by the shell first, and if the result is an empty
string the test returns true otherwise it returns false.

See your shell documentation.

  pb> I need for sh functions to determine if a make variable exists.

First you have to define what "exists" means.  A variable can not exist
at all, it can exist but have the empty string as a value, or it can
exist and have some non-empty value.

Assuming what you want to check is whether the variable has some
non-empty value, then you can use -z.


Or, if you want to _make_ to test this rather than the shell, you can
use the make function $(if ...).

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist




reply via email to

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