Kenenbek Arzymatov
Kenenbek Arzymatov

Reputation: 9119

Multiple target specific variables in Makefile

Make can't define multiple target-specific variables.

But there is a macros from this answer that can solve the problem.

assign-vars = $(foreach A,$2,$(eval $1: $A))

But probably it can't set variable that depends on another variable. For example:

assign-vars = $(foreach A,$2,$(eval $1: $A))


multiple_variable = a=10 \
                    b=$(shell echo $$(( $(a)-1 )) )

$(call assign-vars, print, $(multiple_variable))

print:
    @echo a=$(a)
    @echo b=$(b)

I expected to see b=9 as a result from print target but I get b=-1.

How can I fix it?

Upvotes: 0

Views: 1384

Answers (2)

Vroomfondel
Vroomfondel

Reputation: 2898

If you rewrite it to:

define newline


endef

assign-vars = $(eval $1: $(subst $(newline),$(newline)$1 :,$2))

define print_vars :=
a=10
b=$(shell echo $$(( $(a)-1 )) )
endef

that should do the trick.

EDIT: I only see now, that you are trying to calculate a target specific variable from anothen one. I don't think that will work, as I doubt that there is a specified order in which target specific variables are assigned. So a may or may not be defined when b receives its value.

Upvotes: 1

MadScientist
MadScientist

Reputation: 100836

The problem is that the entire contents of multiple_variable is expanded first, before the for-loop runs. So, it's not possible to refer to earlier variables in the assignment of later variables: they won't be set yet.

When you run:

$(call assign-vars, print, $(multiple_variable))

the first thing make does, is expand the variable multiple_variable: That expands to this:

a=10 b=-1

because when make goes to invoke the shell, the make variable a has not been set yet and so it runs echo $(( -1 )).

I don't really know of a good way to do what you appear to want to do... although I'm not sure I understand what you want to do anyway. It seems like you should take a step back and reconsider your requirements.

Upvotes: 1

Related Questions