From the docs:
Target-specific variables have the same priority as any other makefile variable. Variables provided on the command line (and in the environment if the '-e' option is in force) will take precedence. Specifying the 'override' directive will allow the target-specific variable value to be preferred.
So, a simple makefile, like:
# A pattern-specific variable assignment.
% : foo += file
all : x ;
# Target is a double-colon w/o dependencies, so Make will ALWAYS run its commands.
x ::
@echo '$(foo)'
Running, we get:
# Override makefile-level variables, with a command-line assignment.
$ make foo=cmd
cmd cmd cmd
# Set the value in the environment, And tell Make to prefer it over any makefile-level definitions.
$ foo=env make --environment-overrides
env file file
Returning now, to the quote above, from the documentation:
Variables provided on the command line (and in the environment if the '-e' option is in force) will take precedence.
It seems, that using either:
- Command-line assignment.
- Environment-set variables, AND using
-e
(--environment-overrides
).
Have both the same effect, i.e. overrides the file-level (makefile) variable.
But, the results differ greatly. Remember that the value given in the command-line was: cmd
, and the value given in the environment was: env
.
Now, compare the values, given for a command-line override vs. an environment override:
cmd cmd cmd
(for command-line override).env file file
(for environment override).
So, whereas for command-line, Make repeats the same value, i.e. cmd
, 3 times, for environment-override, the situation is different. That is, Make will "repeat" the environment-level value: env
only 1 time, and then repeats - none other - than the overridden file-level value: file
.
Now, not only is the situation completely different for an "override" from command-line vs. an "override" from the environment, which is strange by itself, the problem here is much bigger.
Since, Make rules to give "priority" for a command-line (or environment) value, why does it insist to append "other" values (as in the case of environment-override, where Make appends "file file"), or in the case of a command-line override (where Make repeats the same value ***3* times). Seriously?
How does it make sense at all? And what is the justification for these inconsistent and strange results?
env file file
result makes sense to me; thecmd cmd cmd
result makes no sense. I am having to guess at why there are two additions (rather than just one) to the basicfoo
macro, too. I'll observe that if you happen to have a filex
in your directory, the echo command won't be executed. – Jonathan Lefflerall
target and one from thex
target, as both match%
. – Toby Speightcmd cmd cmd
is "potentially correct". – Ji Chacmd cmd cmd
is the easy case.env file file
is the odd one. Answering. – Etan Reisner