make macro=value and Submakes
A command-line variable definition such as
foo=bar overrides any
foo in a makefile. Some make
implementations (such as GNU make) propagate this
override to subsidiary invocations of make. Some other
implementations do not pass the substitution along to submakes.
$ cat Makefile
foo = foo
$ make foo=bar # GNU make 3.79.1
make: Entering directory `/home/adl'
make: Leaving directory `/home/adl'
$ pmake foo=bar # BSD make
You have a few possibilities if you do want the
to propagate to submakes. One is to use the -e
option, which causes all environment variables to have precedence over
the makefile macro definitions, and declare foo as an environment
$ env foo=bar make -e
The -e option is propagated to submakes automatically,
and since the environment is inherited between make
foo macro is overridden in
submakes as expected.
This syntax (
foo=bar make -e) is portable only when used
outside of a makefile, for instance from a script or from the
command line. When run inside a make rule, GNU
make 3.80 and prior versions forget to propagate the
-e option to submakes.
Moreover, using -e could have unexpected side effects if your
environment contains some other macros usually defined by the
makefile. (See also the note about
make -e and
Another way to propagate overrides to submakes is to do it
manually, from your makefile:
foo = foo
$(MAKE) foo=$(foo) two
You need to foresee all macros that a user might want to override if
you do that.