Many thanks to you, Brian.

I'm not solving a real problem, I just found a corner case, which I think
should be fixed or documented.

In some cases, it is preferable to delay variable expansions in $(eval ...)
to speed up the parser, and $(foreach ...) can be used to introduce local
variables.

It seems that it's not hard to fix this bug and the fix does not break
existing scripts, I will send a patch when I have time.


-michael

On Fri, Oct 5, 2018 at 2:50 AM Brian Vandenberg <phantall+gnum...@gmail.com>
wrote:

> On Thu, Oct 4, 2018 at 7:53 AM Michael Builov <invalid.nore...@gnu.org>
> wrote:
> >
> > Follow-up Comment #3, bug #54727 (project make):
> >
> > It also possible to step on this "foreach + eval" bug not in a recipe.
>
> Agreed, I reproduced with the following:
>
> $ cat -n makefile
> 1  .RECIPEPREFIX := >
> 2  f:=g
> 3  asdf=$(foreach f,1,$(eval $1:$2:=$$f))
> 4  $(call asdf,A,M)
> 5  all:TMP:=$(call asdf,B,M)
> 6  all:A B
> 7  all A B:
> 8  >$(warning ${@}: M := $(value M))
> $ make --version | head -n1
> GNU Make 4.1
> $ make
> makefile:8: A: M := 1
> makefile:8: B: M := g
> makefile:8: all: M :=
>
> ... however, by changing $$f => $f the problem goes away.  On the one
> hand I agree it's inconsistent but on the other hand in these toy
> examples "$$f" isn't needed.  In the original problem you were trying
> to report is there any particular reason the $(foreach) variable's
> expansion was delayed?
>
> -brian
>
> >
> > Please consider the next example:
> >
> > ############
> > # define global variable
> > f := g
> >
> > # function for defining target-specific variables
> > # $1 - target
> > # $2 - variable
> > def_target_specific = $(foreach f,1,$(eval $1: $2:=$$f))
> >
> > # calling in global context: M=1
> > $(call def_target_specific,target_X,M)
> >
> > # calling in target-specific context: M=g
> > all: TMP := $(call def_target_specific,target_Y,M)
> >
> > all: target_X target_Y
> > target_X target_Y:; $(info $@: M=$M)
> > ############
> >
> > result:
> >
> > target_X: M=1
> > target_Y: M=g
> >
> >
> >
> > As we can see, the variable M has different values for target_X and
> target_Y,
> > which is not expected.
> >
> > Outer foreach variables cannot be used inside the eval context, if the
> call
> > comes from a target-specific variable definition.
> >
> >     _______________________________________________________
> >
> > Reply to this item at:
> >
> >   <https://savannah.gnu.org/bugs/?54727>
> >
> > _______________________________________________
> >   Message sent via Savannah
> >   https://savannah.gnu.org/
> >
> >
> > _______________________________________________
> > Bug-make mailing list
> > Bug-make@gnu.org
> > https://lists.gnu.org/mailman/listinfo/bug-make
>
> _______________________________________________
> Bug-make mailing list
> Bug-make@gnu.org
> https://lists.gnu.org/mailman/listinfo/bug-make
>
_______________________________________________
Bug-make mailing list
Bug-make@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-make

Reply via email to