BUILD variables are simply expanded variables because they're assigned using
:= (and not
=), see the manual for more information. Basically the variable is assigned when the variable is defined, which is preventing you to do what you want.
But the target-specific variables should have precedence over the global ones, so overriding the
BUILD variable will work, since it will shadow the global
BUILD variable in the scope of the
debug: (and therefore the
X = release
debug: BUILD = debug
Y := $(X)
BUILD := $(Y)
Note that using
debug: Y = debug instead doesn't work since
BUILD itself is a simply expanded variable.
I'm not able to understand exactly why something like
debug: X := debug doesn't work. But I suppose that the target-specific variables are resolved after the global simply expanded variables. This also probably the reason why the target-specific
BUILD variable can shadow the global one in my solution.
Note that @jecxjo answer is partially wrong. What prevents you to override the assignment of
BUILD) is that they are assigned with
:= and not
=. I.e. your initial code using
= instead of
:= does exactly what you want:
X = release
debug: X = debug
# Note the '=' assignment instead of the ':='
Y = $(X)
BUILD = $(Y)
This is actually that change which solves your problem in his answer, not rewriting the targets. But since you mentioned that these assignments are included in another file that you cannot edit, this is not a solution.