List Info

Thread: Re: Understanding $(eval)




Re: Understanding $(eval)
user name
2007-09-10 23:26:51
On Mon, 2007-09-10 at 17:11 -0400, Bryan Ischo wrote:
> Whenever gnu make encounters the invocation of an
$(eval) function, it
> will evaluate the text therein as make syntax.  Here is
an excerpt from
> the example from the info page:
> 
>      define PROGRAM_template
>       $(1): $$($(1)_OBJ) $$($(1)_LIBS:%=-l%)
>       ALL_OBJS   += $$($(1)_OBJS)
>      endef
> 
>      $(foreach prog,$(PROGRAMS),$(eval $(call
PROGRAM_template,$(prog))))

> But, in actuality, if you don't include the $(eval)
invocation, then make
> does not process the text that resulted from the
$(call) as make syntax. 
> I don't know what it does with it exactly, but it
doesn't parse it as make
> syntax (when I invoke make on such a construct (a
$(call) without an
> $(eval) around it), I get an error like this:
call.mk:9: *** multiple
> target patterns.  Stop.).

It does TRY to parse the result as make syntax.  However,
it's a feature
(or limitation, perhaps) of the make parser that a logical
line in the
makefile can only expand to a single line.  You cannot have
a line in
the makefile expand into multiple lines.  It just doesn't
work; make
will try to treat the results as a single "line"
containing newline
characters, where the newlines are treated like any other
character
rather than like line separators.

> I find this even more confusing when I consider that
$(eval) seems to be a
> no-op when used like this:
> 
> $(eval FOO := bar)
> 
> This has exactly the same effect as this:
> 
> FOO := bar

In this case, since the thing eval is expanding is a single
line, they
are exactly equivalent.

> Is there some other use of $(eval) that I am missing? 
Is there some
> aspect to how make must process makefile commands that
I am not
> understanding that makes $(eval) necessary?

Originally when I set out to implement the feature provided
by eval,
this is exactly how I intended to do it: remove the
restriction that a
single line of a makefile must expand to a single line, and
allow the
expansion to create multiple lines which would then
themselves be
parsed.  This turned out to be annoyingly tricky, although
of course it
would be doable: it's just programming.  In fact, it could
STILL be done
if we wanted to do it.

However, I then decided to just use an $(eval ...) function
since it was
simpler to implement, AND it does provide one very powerful
feature you
can't get using the original method; consider this:

	all: foo ; echo $(FOO)

	foo: ; $(eval FOO = foo)

Obviously this is a contrived example, but the ability to
perform make
operations such as setting make variable from inside command
scripts can
provide a lot of power.

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


_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make

Re: Understanding $(eval)
country flaguser name
United States
2007-09-11 00:06:24
Paul Smith wrote:

> Originally when I set out to implement the feature
provided by eval,
> this is exactly how I intended to do it: remove the
restriction that a
> single line of a makefile must expand to a single line,
and allow the
> expansion to create multiple lines which would then
themselves be
> parsed.  This turned out to be annoyingly tricky,
although of course it
> would be doable: it's just programming.  In fact, it
could STILL be done
> if we wanted to do it.

Thank you for the explanation, it is very helpful in sanity
checking my
understanding of eval and its use.  Perhaps someday the make
code base
will be altered to allow multi-line text replacements, in
which case most
uses of $(eval) will be harmless but unnecessary.

> However, I then decided to just use an $(eval ...)
function since it was
> simpler to implement, AND it does provide one very
powerful feature you
> can't get using the original method; consider this:
>
> 	all: foo ; echo $(FOO)
>
> 	foo: ; $(eval FOO = foo)
>
> Obviously this is a contrived example, but the ability
to perform make
> operations such as setting make variable from inside
command scripts can
> provide a lot of power.

$(eval) is an *incredibly* useful function and I am just
really happy that
gnu make has added it, despite its (small) warts.  I am
writing a
reasonably gnarly makefile using $(eval) and $(call)
extensively and some
things aren't quite working like I expect, probably because
of bugs in my
makefile code.  But I feel like the structure of the thing
is really nice,
and I would never be able to do things as cleanly without
$(eval) and
$(call).

Thanks again, and best wishes,
Bryan






_______________________________________________
Help-make mailing list
Help-makegnu.org
http:
//lists.gnu.org/mailman/listinfo/help-make

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )