是否可以在 Makefile 中设置环境变量 - 之后使用

发布于 2025-01-10 12:40:40 字数 509 浏览 0 评论 0原文

我试图在 Makefile 中设置一个环境变量,以便它可以在 sam shell 中作为 make 运行的另一个程序中使用,但在 make 运行之后。

更新:根据已接受的答案和评论,这是不可能的。

步骤:

  1. 运行make test设置环境:export TEST_ENV_ONE=OneString
  2. 运行另一个程序,可以读取TEST_ENV_ONE

尝试过这个:

不起作用:

test:
    export TEST_ENV_ONE=OneString
    $(shell export TEST_ENV_TWO=TwoString)

之后这是空的:

echo $TEST_ENV_ONE
echo $TEST_ENV_TWO

I am trying to set an Environment variable in a Makefile, so it can be used in another program running in the sam shell as make, but after make has run.

Update: This is not possible according to accepted answer with comments.

Steps:

  1. run make test setting env: export TEST_ENV_ONE=OneString
  2. run another program, that can read TEST_ENV_ONE

Tried this:

Not working:

test:
    export TEST_ENV_ONE=OneString
    $(shell export TEST_ENV_TWO=TwoString)

Afterwards this is empty:

echo $TEST_ENV_ONE
echo $TEST_ENV_TWO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南渊 2025-01-17 12:40:40

上面的 export TEST_ENV_ONE=OneString 正在专用 shell 中运行。后续命令在其他 shell 实例中运行。因此,它们不会继承环境变量 TEST_ENV_ONE


您可以使用顶级(即不在目标配方中)< makefile 中的 code>export 指令

export env_var := MyEnvVariable

.PHONY: all
all:
    echo env_var: $env_var

这样,变量 env_var 就会导出到将执行配方的 shell。

如果您使用上面的 makefile 运行 make

$ make
echo env_var: $env_var
env_var: MyEnvVariable

从输出中可以看到,运行 echo env_var: $env_var 的 shell 具有变量 env_var 在其环境中。

Your export TEST_ENV_ONE=OneString above is running in a dedicated shell. The subsequent commands run in other shell instances. Therefore, they don't inherit the environment variable TEST_ENV_ONE.


You could use a top-level (i.e., not in a target's recipe) export directive in the makefile:

export env_var := MyEnvVariable

.PHONY: all
all:
    echo env_var: $env_var

This way, the variable env_var is exported to the shells that will execute the recipes.

If you run make with the makefile above:

$ make
echo env_var: $env_var
env_var: MyEnvVariable

As you can see from the output, the shell that run echo env_var: $env_var had the variable env_var in its environment.

沙与沫 2025-01-17 12:40:40

如果您希望将环境变量导出到调用 make 的 shell,事情会有点困难,因为正如 ネroku 所解释的,您不能直接将环境变量从子进程 (make) 导出到它的父进程(调用 make 的 shell)。但是,如果您接受像这样调用 make:

eval "$(make -s)"

那么确实是可能的:只需 echo export VARIABLE1=VALUE1;导出变量2=值2; ... 在你的食谱中。警告:您还必须保证 make 在标准输入上不会回显任何其他内容。但您可以改用标准错误。示例:

$ cat Makefile
export TEST_ENV_ONE := OneString

all:
    printf 'make variable TEST_ENV_ONE = %s\n' "$(TEST_ENV_ONE)" 1>&2
    printf 'in-recipe shell variable TEST_ENV_ONE = %s\n' "$TEST_ENV_ONE" 1>&2
    printf 'export TEST_ENV_ONE="%s"\n' '$(TEST_ENV_ONE)'

$ unset TEST_ENV_ONE
$ printenv TEST_ENV_ONE
$ eval "$(make -s)"
make variable TEST_ENV_ONE = OneString
in-recipe shell variable TEST_ENV_ONE = OneString
$ printenv TEST_ENV_ONE
OneString

请注意,make 或多或少将环境变量视为make 变量。来自 GNU make 手册:

make 中的变量可以来自 make 运行的环境。
make 启动时看到的每个环境变量都是
转换为具有相同名称和值的 make 变量。
但是,在 makefile 中或使用命令进行显式赋值
论证,凌驾于环境之上。 (如果指定了“-e”标志,
然后环境中的值会覆盖 makefile 中的分配。
请参阅选项摘要。但这不是推荐的做法。)

因此,除非变量的值是 make 本身复杂计算的结果,否则获得相同结果的更自然的方法是在父 shell 中定义环境变量并使用它与 Makefile 中一样:

$ cat Makefile
all:
    printf 'make variable TEST_ENV_ONE = %s\n' "$(TEST_ENV_ONE)"
    printf 'in-recipe shell variable TEST_ENV_ONE = %s\n' "$TEST_ENV_ONE"

$ export TEST_ENV_ONE=OneString
$ make -s
make variable TEST_ENV_ONE = OneString
in-recipe shell variable TEST_ENV_ONE = OneString
$ printenv TEST_ENV_ONE
OneString

If you want the environment variables to be exported to the shell from which you invoked make things are a bit difficult because, as explained by ネロク, you cannot directly export environment variables from a child process (make) to its parent process (the shell from which you invoke make). But if you accept to invoke make like this:

eval "$(make -s)"

then it is indeed possible: just echo export VARIABLE1=VALUE1; export VARIABLE2=VALUE2; ... in your recipe. Warning: you will also have to guarantee that nothing else gets echoed by make on the standard input. But you can use the standard error, instead. Example:

$ cat Makefile
export TEST_ENV_ONE := OneString

all:
    printf 'make variable TEST_ENV_ONE = %s\n' "$(TEST_ENV_ONE)" 1>&2
    printf 'in-recipe shell variable TEST_ENV_ONE = %s\n' "$TEST_ENV_ONE" 1>&2
    printf 'export TEST_ENV_ONE="%s"\n' '$(TEST_ENV_ONE)'

$ unset TEST_ENV_ONE
$ printenv TEST_ENV_ONE
$ eval "$(make -s)"
make variable TEST_ENV_ONE = OneString
in-recipe shell variable TEST_ENV_ONE = OneString
$ printenv TEST_ENV_ONE
OneString

Note that make more or less considers environment variables as make variables. From GNU make manual:

Variables in make can come from the environment in which make is run.
Every environment variable that make sees when it starts up is
transformed into a make variable with the same name and value.
However, an explicit assignment in the makefile, or with a command
argument, overrides the environment. (If the ‘-e’ flag is specified,
then values from the environment override assignments in the makefile.
See Summary of Options. But this is not recommended practice.)

So, unless the value of your variable is the result of complex computations by make itself, a much more natural way to obtain the same result would be to define the environment variable in the parent shell and to use it as is in the Makefile:

$ cat Makefile
all:
    printf 'make variable TEST_ENV_ONE = %s\n' "$(TEST_ENV_ONE)"
    printf 'in-recipe shell variable TEST_ENV_ONE = %s\n' "$TEST_ENV_ONE"

$ export TEST_ENV_ONE=OneString
$ make -s
make variable TEST_ENV_ONE = OneString
in-recipe shell variable TEST_ENV_ONE = OneString
$ printenv TEST_ENV_ONE
OneString
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文