How to set child process' environment variable in Makefile

前端 未结 4 1677
半阙折子戏
半阙折子戏 2020-11-28 05:08

I would like to change this Makefile:

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

boot:
    @supervisor         \\
      --harmony         \\
             


        
相关标签:
4条回答
  • 2020-11-28 05:18

    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:

    test: NODE_ENV = test
    

    to this:

    test: export NODE_ENV = test
    

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

    0 讨论(0)
  • 2020-11-28 05:19

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

    export MY_VAR = foo  # Available for all targets
    

    Or export variables for a specific target (target-specific variables):

    my-target: export MY_VAR_1 = foo
    my-target: export MY_VAR_2 = bar
    my-target: export MY_VAR_3 = baz
    
    my-target: dependency_1 dependency_2
      echo do something
    

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

    .EXPORT_ALL_VARIABLES:
    
    MY_VAR_1 = foo
    MY_VAR_2 = bar
    MY_VAR_3 = baz
    
    test:
      @echo $$MY_VAR_1 $$MY_VAR_2 $$MY_VAR_3
    

    see .EXPORT_ALL_VARIABLES

    0 讨论(0)
  • 2020-11-28 05:24

    I would re-write the original target test, taking care the needed variable is defined IN THE SAME SUB-PROCESS as the application to launch:

    test:
        ( NODE_ENV=test mocha --harmony --reporter spec test )
    
    0 讨论(0)
  • 2020-11-28 05:29

    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.

    SHELL := /bin/bash
    
    .PHONY: test tests
    test tests:
        PATH=./node_modules/.bin/:$$PATH \
        JSCOVERAGE=1 \
        nodeunit tests/
    
    0 讨论(0)
提交回复
热议问题