Pass Variable to be Expanded in MAKEFLAGS

100 Views Asked by At

Given a Makefile like below, how can I set the value of FOOBAR to foo /home/scott using $MAKEFLAGS?

FOO=foo
FOOBAR=$(FOO) bar

$(info $$FOOBAR is [$(FOOBAR)])

I know I can do

$ make FOOBAR="\$(FOO) $HOME"
$FOOBAR is [foo /home/scott]

And this works fine

$ MAKEFLAGS="FOOBAR=foo\ $HOME" make
$FOOBAR is [foo /home/scott]

Interestingly, this doesn't work so well

$ MAKEFLAGS="FOOBAR='foo $HOME'" make
$FOOBAR is ['foo]

My goal is something like this, but try as I might I cannot make it happen

$ export MAKEFLAGS="FOOBAR=\$(FOO)\ $HOME"  # <-- this line is wrong
$ make
$FOOBAR is [foo /home/scott]  # <-- not the real output

Actual output of that is

$ export MAKEFLAGS="FOOBAR=\$(FOO)\ $HOME"
$ make
$FOOBAR is [/home/scott]

How can I use a variable to be substituted in a $MAKEFLAGS override?

(A POSIX standard solution is preferred, but a GNU-only solution would work.)

(Why do I need this? I want to override a variable used by Python which I am building with pyenv's python-build. I found that python-build exposes $MAKE_OPTS which turned out to work, but now I'm curious why my previous attempts have failed.)

1

There are 1 best solutions below

1
On BEST ANSWER

This is because MAKEFLAGS are immediately substituted, so before your FOO is even defined and as such it resolves to an empty string. If you wish to postpone resolution, you would need to double the dollar, e.g.:

$ export MAKEFLAGS="FOOBAR=\$\$(FOO)\ $HOME"
$ make
$FOOBAR is [foo /home/raspy]