1

Example:

var1 = x

dep : $(var1)
     echo ($^ -> $@)

This will show x -> dep, but what I really want to show is var1 -> dep.

I basically want to sprinkle this echo all over the rules so I can make sense of a dependency graph. Nothing else worked.

SFbay007
  • 1,917
  • 1
  • 20
  • 39

1 Answers1

0

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
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • 1
    Thanks for you answer Joseph. I am sure there are ways to do what I am asking by adding more code. But my main point was to do that using only one statement. We have a huge complex makefile system with many parts and files sprinkled. I want to understand the dependencies better without adding too much code. I just wanted to paste same statement every where there is a recipe and be done with it. – SFbay007 Jan 12 '19 at 18:26