如何在Makefile中设置环境变量

时间:2021-07-18 11:35:10

I would like to change this Makefile:

我想更改这个Makefile:

SHELL := /bin/bash
PATH  := node_modules/.bin:$(PATH)

boot:
    @supervisor         \
      --harmony         \
      --watch etc,lib       \
      --extensions js,json      \
      --no-restart-on error     \
        lib

test:
    NODE_ENV=test mocha         \
      --harmony             \
      --reporter spec       \
        test

clean:
    @rm -rf node_modules

.PHONY: test clean

to:

至:

SHELL := /bin/bash
PATH  := node_modules/.bin:$(PATH)

boot:
    @supervisor         \
      --harmony         \
      --watch etc,lib       \
      --extensions js,json      \
      --no-restart-on error     \
        lib

test: NODE_ENV=test
test:
    mocha                   \
      --harmony             \
      --reporter spec       \
        test

clean:
    @rm -rf node_modules

.PHONY: test clean

Unfortunately the second one does not work (the node process still runs with the default NODE_ENV.

不幸的是,第二个不起作用(节点进程仍然使用默认的NODE_ENV运行。

What did I miss?

我错过了什么?

3 个解决方案

#1


95  

Make variables are not exported into the environment of processes make invokes... by default. However you can use make's export to force them to do so. Change:

默认情况下,Make变量不会导出到进程调用环境中。但是,您可以使用make的导出来强制它们这样做。更改:

test: NODE_ENV = test

to this:

对此:

test: export NODE_ENV = test

(assuming you have a sufficiently modern version of GNU make).

(假设你有一个足够现代的GNU make版本)。

#2


11  

As MadScientist pointed out, you can export individual variables with:

正如MadScientist指出的那样,您可以使用以下方法导出单个变量:

export MY_VAR = foo

You can also specify the .EXPORT_ALL_VARIABLES target to—you guessed it!—EXPORT ALL THE THINGS!!!:

您也可以指定.EXPORT_ALL_VARIABLES目标 - 您猜对了! - 出口所有事情!!!:

.EXPORT_ALL_VARIABLES:

MY_VAR = foo

test:
  @echo $$MY_VAR

see .EXPORT_ALL_VARIABLES

请参阅.EXPORT_ALL_VARIABLES

#3


7  

I only needed the environment variables locally to invoke my test command, here's an example setting multiple environment vars in a bash shell, and escaping the dollar sign in make.

我只需要本地环境变量来调用我的测试命令,这里是一个在bash shell中设置多个环境变量并在make中转义美元符号的示例。

SHELL := /bin/bash

.PHONY: test tests
test tests:
    PATH=./node_modules/.bin/:$$PATH \
    JSCOVERAGE=1 \
    nodeunit tests/

#1


95  

Make variables are not exported into the environment of processes make invokes... by default. However you can use make's export to force them to do so. Change:

默认情况下,Make变量不会导出到进程调用环境中。但是,您可以使用make的导出来强制它们这样做。更改:

test: NODE_ENV = test

to this:

对此:

test: export NODE_ENV = test

(assuming you have a sufficiently modern version of GNU make).

(假设你有一个足够现代的GNU make版本)。

#2


11  

As MadScientist pointed out, you can export individual variables with:

正如MadScientist指出的那样,您可以使用以下方法导出单个变量:

export MY_VAR = foo

You can also specify the .EXPORT_ALL_VARIABLES target to—you guessed it!—EXPORT ALL THE THINGS!!!:

您也可以指定.EXPORT_ALL_VARIABLES目标 - 您猜对了! - 出口所有事情!!!:

.EXPORT_ALL_VARIABLES:

MY_VAR = foo

test:
  @echo $$MY_VAR

see .EXPORT_ALL_VARIABLES

请参阅.EXPORT_ALL_VARIABLES

#3


7  

I only needed the environment variables locally to invoke my test command, here's an example setting multiple environment vars in a bash shell, and escaping the dollar sign in make.

我只需要本地环境变量来调用我的测试命令,这里是一个在bash shell中设置多个环境变量并在make中转义美元符号的示例。

SHELL := /bin/bash

.PHONY: test tests
test tests:
    PATH=./node_modules/.bin/:$$PATH \
    JSCOVERAGE=1 \
    nodeunit tests/