7
votes

I'm seeing unexpected results for target-specfic variables in GNU make.

What I want is to set a target-specific variable that affects dependencies. I can use .SECONDEXPANSION to achieve that.

some-target: DEP := debug-dep

debug: some-target

.SECONDEXPANSION:
some-target: $$(DEP)
    @echo $^

debug-dep:

make debug prints debug-dep.


Now I read that make defines target-specific variables for descendant rules:

When you define a target-specific variable that variable value is also in effect for all prerequisites of this target, and all their prerequisites, etc.

But when I change my makefile to have the variable set on "parent" target:

debug: DEP := debug-dep

debug: some-target

.SECONDEXPANSION:
some-target: $$(DEP)
    @echo $^

debug-dep:

and do make debug I get a blank line.

This seems to contradict the documented behavior of target-specific variables. Is there something that I am missing?


This is sort of similar to make: Using target specific variables in prerequisites, but what I am doing isn't working.

1

1 Answers

5
votes

I believe the issue here is that target-specific variables are not set until the targets are being run (if you add @echo '$(DEP)' to your some-target body in that second case you will see that it is set) but that second expansion happens immediately after the initial read-in phase.

I was going to say that I'm actually surprised that this works in the first case at all (and speculate as to why) but then I pulled up the manual for a minute and while reading about .SECONDEXPANSION I found the following:

[T]he true power of this feature only becomes apparent when you discover that secondary expansions always take place within the scope of the automatic variables for that target. This means that you can use variables such as $@, $*, etc. during the second expansion and they will have their expected values, just as in the recipe. All you have to do is defer the expansion by escaping the $. Also, secondary expansion occurs for both explicit and implicit (pattern) rules.

Which explains your behaviour exactly. The expansion only sees variables set in the scope of the target and the prerequisite inheriting only happens at target evaluation time (since it depends on target prerequisites).