help-make
[Top][All Lists]
Advanced

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

Re: Evaluation of variables inside make rules' dependencies


From: Philip Guenther
Subject: Re: Evaluation of variables inside make rules' dependencies
Date: Sun, 6 Mar 2011 21:00:21 -0800

On Sun, Mar 6, 2011 at 6:06 PM, Dmitry N. Mikushin <address@hidden> wrote:
> I'm trying to understand the behavior of the following example:
>
> %.a: LIBNAME = $(shell echo $(notdir $(basename $@)) | sed s/lib// | \
>        tr '[a-z]' '[A-Z]' | tr '/' '_')
> %.a: SOURCES = $($(LIBNAME)_SOURCES)
> %.a: OBJECTS = $(addprefix $(STORE)/, $(addsuffix .o, $(SOURCES)))
> %.a: DEPLIBS = $($(LIBNAME)_DEPLIBS)
> %.a: DEFINES += $($(shell echo $(1) | tr '[a-z]' '[A-Z]' | tr '/' 
> '_')_DEFINES)
> %.a: INCLUDES += $($(shell echo $(1) | tr '[a-z]' '[A-Z]' | tr '/'
> '_')_INCLUDES)
> %.a: dirs $(OBJECTS)
>       address@hidden Packing static library $@ $(OBJECTS) ...
>       address@hidden(AR) $(ARPARAMS) $@ $(OBJECTS)
>
> The result is:
> ============
>
> $(OBJECTS) seems to be empty inside line %.a: dirs $(OBJECTS),
...
> So what's the difference between evaluating variable in dependency
> line and in command?

There are several differences.  The first is that the former is
expanded immediately while the latter is deferred until the rule is
run.  There are others.  For example, to quote the manual:

----
6.11 Target-specific Variable Values
....
This feature allows you to define different values for the same variable,
based on the target that `make' is currently building.  As with
automatic variables, these values are only available within the context
of a target's recipe (and in other target-specific assignments).
----

I.e., they aren't available for immediate expansion.  There's a
workaround: turn on second-expansion and target-specific variables can
be referenced during the second expansion (but not during the first,
still).

I.e., add the line
  .SECONDEXPANSION:

above this in the Makefile and then use $$(OBJECTS) instead of
$(OBJECTS) in the dependency line.

Beware: there may be other changes that you need to make when you add
.SECONDEXPANSION.  Read the manual and then think about what variables
contain and whether a second expansion will affect things...


Philip Guenther



reply via email to

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