Barry
Barry

Reputation: 303097

makefile read reused variable inside recipe

In trying to implement nonrecursive make, I have a Rules.mk which looks like:

############
# Enter Stack
############
sp              := $(sp).x
dirstack_$(sp)  := $(d)
d               := $(dir)

.. setup things like OBJECTS_$(d), DEPS_$(d), TARGET_$(d), etc ...

############
# Exit Stack
############
-include        $(DEPS_$(d))
d               := $(dirstack_$(sp))
sp              := $(basename $(sp))

One of the variables I wanted to set was:

INCLUDE_PATH_$(d) := -Isomething -Isomething/else ...

To be used in the compilation rule:

$(OBJDIR_$(d))/%.o : $(d)/%.cpp $(OBJDIR_$(d))/%.d
    $(CC) $(CFLAGS) $(INCLUDE_PATH_$(d)) -o $@ -c $<

But this doesn't work - $(INCLUDE_PATH_$(d)) doesn't get expanded until later - when $(d) is no longer has the value I need it to have in order for this to work. What's the way for me to do this properly?

Upvotes: 1

Views: 142

Answers (1)

user657267
user657267

Reputation: 21000

You could use a target-specific variable

$(OBJDIR_$d)/%.o : INCLUDES := $(INCLUDE_PATH_$d)
$(OBJDIR_$d)/%.o : $d/%.cpp $(OBJDIR_$d)/%.d
    $(CC) $(CFLAGS) $(INCLUDES) -o $@ -c $<

The following is perhaps more standard / flexible (assuming CPPFLAGS isn't set to recursively expand) although it depends on your needs

$(OBJDIR_$d)/%.o : CPPFLAGS += $(INCLUDE_PATH_$d)
$(OBJDIR_$d)/%.o : $d/%.cpp $(OBJDIR_$d)/%.d
    $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<

Upvotes: 1

Related Questions