Simple Second Expansion Help

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple Second Expansion Help

Anthony Clark
All,

Upon reading the official help for .SECONDEXPANSION: here,
https://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html,
My simple makefile below isn't doing what I expect - it could be from my
weird `$(eval ...)` use...

I'm expecting to be able to append to $(OBJECTS) in the `foo` rule and have
the final value be used in the `all` target. I know this might seem
anti-idiomatic as I see no reference for this technique anywhere.

---- Begin Makefile ----

.PHONY: foo all
.SECONDEXPANSION:

OBJECTS := obj/first

all: foo $$(OBJECTS) ; $(info OBJECTS=$(OBJECTS))

foo:
    @$(eval OBJECTS += obj/second)
    @echo "in foo, OBJECTS=$(OBJECTS)"

obj/%:
    @echo "obj rule => $@"

---- End Makefile ----

---- Begin Output ----

$ make
in foo, OBJECTS=obj/first obj/second
obj rule => obj/first
OBJECTS=obj/first obj/second

---- End Output ----

---- Begin Expected Output ----

$ make
in foo, OBJECTS=obj/first obj/second
obj rule => obj/first
obj rule => obj/second
OBJECTS=obj/first obj/second

---- End Expected Output ----

--
Anthony G. Clark
_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Simple Second Expansion Help

Paul Smith-20
On Thu, 2018-08-23 at 23:17 -0400, Anthony Clark wrote:
> all: foo $$(OBJECTS) ; $(info OBJECTS=$(OBJECTS))
>
> foo:
>     @$(eval OBJECTS += obj/second)
>     @echo "in foo, OBJECTS=$(OBJECTS)"

This won't work as you hope, because first make decides to rebuild the
"all" target (as it's the first target listed in the makefile).  In
order to build that target, it must expand the prerequisites so that it
can get a list of them.  At that time, the "foo" rule hasn't been
invoked yet so the eval for OBJECTS hasn't been run and $(OBJECTS)
expands to just "obj/first".

You may be assuming that make will walk through the prerequisites one
at a time and expand (and re-parse into more prerequisites) each one
then try to build it, before expanding (then re-parsing) the next
prerequisite, but that's not how it works.  The entire list of
prerequisites is expanded first, before any of them are built.

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make