Firstly, to work in my environment your makefile needs two slight modifications: replace the parentheses in echo
with quotes, and add a rule for x
, to give:
var1 = x
dep: $(var1)
echo "$^ -> $@"
x:
There appears to be no method to recover the unexpanded form of a variable name in a recipe. My suggested solution/work-around is twofold: cache the variable name in another variable, and as per MadScientist's comment use $(warning ...)
. For example, the makefile:
var1 = x
dep: var1_=var1
dep: $(var1)
@echo "$^ -> $@"
@echo "$(var1_) -> $@"
$(warning "Info: Using $^ to build $@")
$(warning "Info: Using $(var1) to build $@")
$(warning "Info: Using $(var1_) to build $@")
$(warning "Info: Using $($(var1_)) to build $@")
x:
will give:
Makefile:7: "Info: Using x to build dep"
Makefile:8: "Info: Using x to build dep"
Makefile:9: "Info: Using var1 to build dep"
Makefile:10: "Info: Using x to build dep"
x -> dep
var1 -> dep
The var1_
variable needs to be defined on a separate line (see https://stackoverflow.com/a/20714468/318716), which doesn't appear to be well-documented. And the order of the output is interesting.
Edit: A more simple variant of this idea is to replace:
var1 = x
dep: var1_=var1
with just:
var1 = x
var1_ = var1