help-make
[Top][All Lists]
Advanced

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

Re: How to delay a variable's setting


From: Paul D. Smith
Subject: Re: How to delay a variable's setting
Date: Mon, 19 Mar 2001 18:07:43 -0500

%% Bernie Zelitch <address@hidden> writes:

  bz> If I run MS nmake on the following, the output is
  bz> 1
  bz> 2
  bz> ***********************
  bz> all : yyy xxx
  bz> var=1
  bz> yyy :
  bz>     @echo $(var)
  bz> var=2

  bz> xxx :
  bz>     @echo $(var)
  bz> ***********************

This is not correct (at least, not for the POSIX definition of make or
any other standard make).

Very odd.  I don't have nmake so I don't know what the rules are, but I
find this behavior bizzare.

  bz> However, with gmake, the output is
  bz> 2
  bz> 2

This is correct.

  bz> Apparently, the two makes have two different ideas of when the
  bz> variable value should be captured.

Sort of.

Really, the difference is when the variables are _expanded_.

Make doesn't expand variables within a rule script until it actually
wants to run that rule.  This doesn't happen, of course, until after the
entire makefile has been completely read in and make starts its second
phase of processing.

By that time, the second setting of the variable value has already
occurred, so when it runs either rule the value is "2".

How to do what you want depends largely on what version of GNU make
you're using and how portable you want it to be.

A relatively portable method is to use constructed variable names, like
this:

  all: yyy xxx

  yyy_var = 1
  xxx_var = 2

  yyy :
        echo $(address@hidden)
  xxx :
        echo $(address@hidden)

(of course you can move the variable assignments anywhere you like.

A less portable method that will only work with new versions of GNU
make would be using target-specific variables:

  all: yyy xxx

  var = 1
  yyy : local_var := $(var)
  yyy :
        @echo $(local_var)

  var = 2
  xxx : local_var := $(var)
  xxx :
        @echo $(local_var)

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://www.paulandlesley.org/gmake/
 "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]