help-make
[Top][All Lists]
Advanced

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

RE: variable expansion (immediate vs deferred)


From: Dave Korn
Subject: RE: variable expansion (immediate vs deferred)
Date: Thu, 24 Jan 2008 17:41:17 -0000

On 15 March 2008 05:06, chandan wrote:

> Consider the following makefile:
> 
> ########################
> LIST := one two three
> 
> all : $(LIST)

  Expanded immediately: so the prerequisites are "one two three".  Changing
$(LIST) later will not change the prerequisites of "all".

>        @echo $^;

  This is the list of prerequisites.  It is "one two three", because those are
the prerequisites of all.  They do not change when you later reassign LIST.

>        @for i in $(LIST); do \

  But this is $(LIST), expansion deferred until the command is run, by which
time it holds the value "four five six".

>                echo -n "$$i "; \
>        done
>        @echo;
> 
> .PHONY : $(LIST)

  This makes one, two and three phony targets.

> one:
> 
> two:
> 
> three:
> 
> LIST := four five six

  This reassigns the value of LIST.

> #########################
> 
> The output would be:
> $ make
> one two three
> four five six

  Yes, that is correct.

> The GNU Make manual (section 3.9) has the following ....
> ******************************************************************
> A rule is always expanded the same way, regardless of the form:
>    immediate : immediate ; deferred
>    deffered
> ******************************************************************
> and this ...
> ******************************************************************
> We say that expansion is deffered if expansion is not performed
> immediately. Expansion of deffered construct is not performed
> until either the construct appears later in an immediate context,
> or until  the second phase.
> ******************************************************************

  Yes, it does.

> In the above makefile $(LIST) appears in the prerequiste list of both
> "all" and ".PHONY" which happen to be expanded in an immediate
> context. The output shows that $(LIST) (within commands section) is
> expanded in the second phase i.e target-update phase (even though
> $(LIST) appears later in an immediate context).

  Yes, that's exactly in accordance with 

>    immediate : immediate ; deferred
>    deffered

as you quoted above; you have to imagine that the rule in your makefile looks
like this:

> all : one two three
>        @echo $^;
>        @for i in $(LIST); do \
>                echo -n "$$i "; \
>        done
>        @echo;

and by the time the command is executed, the contents of LIST are "four five
six".

> Quoting from section 5.1.2 of GNU Make manual ...
> ******************************************************************
> The other ways in which make processes commands is by expanding
> any variable references in them. This occurs after make has finished
> reading all the makefiles and the target is determined to be out of date;
> *******************************************************************
> Considering this statement the output is correct.

  Yes, it is.
 
> Can anybody clear this ambiguity.

  What ambiguity?  You never said anything about what you expected to happen
differently!

  Both sections say the same thing and both sections claim that the output is
correct and it is.  Hopefully the explanation above makes it clearer to you.

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....





reply via email to

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