help-make
[Top][All Lists]
Advanced

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

Variable definition in eval'd function


From: Bryan Ischo
Subject: Variable definition in eval'd function
Date: Wed, 10 Oct 2007 03:48:54 -0400 (EDT)
User-agent: SquirrelMail/1.4.8-4.fc5

Hi all.  I am having a heck of a time getting the $(eval) function to do
what I want with respect to a per-evaluation variable definition.

Here is a small test makefile that demonstrates what I am trying to do:

--- begin Makefile ---

FRAGMENTS = foo bar

define PROCESS_FRAGMENT

-include $(1).mk

.PHONY: $(1)
$(1):
        @echo $$@
        @echo $$(VARIABLE)

endef

$(foreach i, $(FRAGMENTS), $(eval $(call PROCESS_FRAGMENT,$(i))))

--- end Makefile ---

To explain what is happening here ... for each word in the FRAGMENTS
variable, I am calling the PROCESS_FRAGMENT template, and the resulting
text is then being evaluated as Makefile text.  Since FRAGMENTS contains
two words, the template is thus being called and then evaluated for the
words 'foo' and 'bar', in succession.

The template itself does two things:

1) includes a makefile with the same name as the template argument

2) defines a rule for a phony target with the same filename as the
template argument, and with no dependencies, and with two commands: first,
echo the target filename, and second, echo the value of the VARIABLE
variable

OK, so in foo.mk I have just the text "VARIABLE = foo_variable" and in
bar.mk I have the text "VARIABLE = bar_variable".

What I expect to end up with is two rules that look like this:

.PHONY: foo
foo:
        @echo foo
        @echo foo_variable

.PHONY: bar
bar:
        @echo bar
        @echo bar_variable

The problem is, that the $$(VARIABLE) variable reference doesn't do what I
expect.  It is being set to "bar_variable" for both the 'foo' rule and the
'bar' rule.  Clearly the value of the VARIABLE variable at the time that
the rule is being evaluated is the value it was given by the final call to
the template, not the value given in the 'current' call to the template.

I have tried using $(VARIABLE) but I then get an empty string when the foo
rule is evaluated, and the value "foo_variable" when the bar rule is
evaluated.

I have also added this text to the PROCESS_FRAGMENT template to see what
would happen:

.PHONY: $$(VARIABLE)
$$(VARIABLE):
         @echo $$@

The amazing thing is this DOES seem to work as I expected; I end up with
the following two additional rules:

.PHONY: foo_variable
foo_variable:
         @echo foo_variable

.PHONY: bar_variable
bar_variable:
         @echo bar_variable

So it seems that for some reason, variables that are the part of a rule's
commands are not expanded in the same way as variables that are not part
of a rule's commands.  Is there a reason for this?  And, is there a way to
'workaround' this behavior, so that I can have the commands of a rule be
the result of a variable evaluation, as well as the target name and
dependencies?

Thank you!
Bryan

------------------------------------------------------------------------
Bryan Ischo                address@hidden            2001 Mazda 626 GLX
Hamilton, New Zealand      http://www.ischo.com     RedHat Fedora Core 5
------------------------------------------------------------------------






reply via email to

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