jfMR
jfMR

Reputation: 24738

Partially expanding recursively expanded variables

Consider the following makefile example:

all:;

varname := a

val = $($(varname)-value)

a-value := AAA
b-value := BBB

$(info val: $(val))
varname := b
$(info val: $(val))

As expected, running make on this makefile results in:

val: AAA
val: BBB

Since val is a recursively expanded variable, its expansion occurs when referencing (i.e., $(val)).

At the moment of defining the variable val, the variable a-value is not defined yet. Therefore, defining val as a simply expanded variable instead (i.e., with := instead of =):

val := $($(varname)-value) 

results in $($(varname)-value) being immediately expanded to an empty string, which, in turn, results in an empty $(val).

I would like to know whether there is a way to immediately expand varname in val's definition, but defer the expansion of the result until the moment in which val is referenced, as if val were actually defined in the makefile above as:

val = $(a-value) 

Upvotes: 0

Views: 94

Answers (1)

MadScientist
MadScientist

Reputation: 100816

Well, you could do this:

simplevar := $(varname)
val = $($(simplevar)-value)

Upvotes: 1

Related Questions