Reputation: 18102
I am trying to do a simple thing:
TMPDIR ?= /tmp
test:
@echo $(TMPDIR)
This works if I run:
$ make test
/tmp
It also works if I run:
$ make test -e TMPDIR=~/tmp
/home/user/tmp
What can I do to also have it works for:
$ TMPDIR=~/tmp make test
/home/user/tmp
Upvotes: 146
Views: 122702
Reputation: 100856
To follow up on my comments above, here's an example:
T ?= foo
all:
$(info T is $(T))
Now if I run the Makefile in various ways, it behaves as we expect (I get foo
only if I don't set T
either on the command line or environment):
$ make
T is foo
$ make T=bar
T is bar
$ T=bar make
T is bar
Upvotes: 241
Reputation: 136266
Variables specified on make
command line override the values assigned in makefile:
TMPDIR := "/tmp"
test:
@echo $(TMPDIR)
And then:
make TMPDIR=whatever
whatever
It is generally considered a bad practice for makefiles to depend on environment variables because that may lead to non-reproducible builds. This is why passing variable overrides in make
command line explicitly is recommended.
Upvotes: 66
Reputation: 166419
Here is a simple solution:
SHELL := env TMPDIR=$(TMPDIR) $(SHELL)
TMPDIR ?= "/tmp"
all:
@echo $(TMPDIR)
which works for both scenarios: TMPDIR=new/path make
and make TMPDIR=new/path
.
Upvotes: 10
Reputation: 18102
One of the thing you could do is:
TMPDIR := "/tmp"
ifdef $$TMPDIR
TMPDIR := $$TMPDIR
endif
test:
echo $(TMPDIR)
Upvotes: 2