使用ENV变量或默认值定义Makefile变量

时间:2022-10-05 18:38:02

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

4 个解决方案

#1


43  

To follow up on my comments above, here's an example:

为了跟进我上面的评论,这是一个例子:

T ?= foo
all:;: '$(T)'

Now if I run the various options it behaves as expected (I get foo only if I don't set T either on the command line or environment):

现在,如果我运行各种选项,它的行为与预期一致(只有在命令行或环境中没有设置T时才会得到foo):

$ make
: 'foo'

$ make T=bar
: 'bar'

$ T=bar make
: 'bar'

#2


13  

Variables specified on make command line override the values assigned in makefile:

make命令行中指定的变量会覆盖makefile中指定的值:

TMPDIR := "/tmp"
test:
    @echo $(TMPDIR)

And then:

接着:

make TMPDIR=whatever
whatever

It is generally bad practice for makefiles to depend on environment variables, this is why passing variables in make command line is preferred.

makefile依赖于环境变量通常是不好的做法,这就是首选在make命令行中传递变量的原因。


Another way is to use make or function:

另一种方法是使用make或function:

X := $(or ${X},${X},abc)

all :
    @echo ${X}

make
abc

X=def make 
def

Actually, you should just stick to using ?= assignment.

实际上,你应该坚持使用?=赋值。

#3


2  

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.

适用于两种情况:TMPDIR = new / path make和make TMPDIR = new / path。

#4


0  

One of the thing you could do is:

你可以做的一件事是:

TMPDIR := "/tmp"

ifdef $$TMPDIR
TMPDIR := $$TMPDIR
endif

test:
    echo $(TMPDIR)

#1


43  

To follow up on my comments above, here's an example:

为了跟进我上面的评论,这是一个例子:

T ?= foo
all:;: '$(T)'

Now if I run the various options it behaves as expected (I get foo only if I don't set T either on the command line or environment):

现在,如果我运行各种选项,它的行为与预期一致(只有在命令行或环境中没有设置T时才会得到foo):

$ make
: 'foo'

$ make T=bar
: 'bar'

$ T=bar make
: 'bar'

#2


13  

Variables specified on make command line override the values assigned in makefile:

make命令行中指定的变量会覆盖makefile中指定的值:

TMPDIR := "/tmp"
test:
    @echo $(TMPDIR)

And then:

接着:

make TMPDIR=whatever
whatever

It is generally bad practice for makefiles to depend on environment variables, this is why passing variables in make command line is preferred.

makefile依赖于环境变量通常是不好的做法,这就是首选在make命令行中传递变量的原因。


Another way is to use make or function:

另一种方法是使用make或function:

X := $(or ${X},${X},abc)

all :
    @echo ${X}

make
abc

X=def make 
def

Actually, you should just stick to using ?= assignment.

实际上,你应该坚持使用?=赋值。

#3


2  

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.

适用于两种情况:TMPDIR = new / path make和make TMPDIR = new / path。

#4


0  

One of the thing you could do is:

你可以做的一件事是:

TMPDIR := "/tmp"

ifdef $$TMPDIR
TMPDIR := $$TMPDIR
endif

test:
    echo $(TMPDIR)