简体   繁体   English

取消makefile上的环境变量

[英]Unset an env variable on a makefile

I have a makefile that runs some other make target by first setting some variables: 我有一个makefile,它通过首先设置一些变量来运行其他make目标:

make -C somedir/ LE_VAR=/some/other/stuff LE_ANOTHER_VAR=/and/so/on

Now I need to unset LE_VAR (really unset, not just override the value with ""). 现在,我需要取消设置LE_VAR (确实取消设置,而不仅仅是用“”覆盖值)。

Is there any way to do it so on GNU Make 3.81 ? 有什么办法可以在GNU Make 3.81做到吗?

Thanks! 谢谢!

Assuming your makefile contains something like this to invoke a sub-make: 假设您的makefile包含类似这样的内容以调用子make:

submake:
         $(MAKE)

You need to modify the magical variable MAKEOVERRIDES , like this: 您需要修改魔术变量MAKEOVERRIDES ,如下所示:

MAKEOVERRIDES := $(filter-out LE_VAR=%,$(MAKEOVERRIDES))
unexport LE_VAR

submake:
        $(MAKE)

Check this out unexport variable . 检查这个unexport variable From gnu manual 从GNU手册

export variable

export variable-assignment

unexport variable

Tell make whether or not to export a particular variable to child processes

Refer https://www.gnu.org/software/make/manual/html_node/Quick-Reference.html 请参阅https://www.gnu.org/software/make/manual/html_node/Quick-Reference.html

Thank you very much for your replies, this was quite tricky. 非常感谢您的答复,这非常棘手。

When executing make, and setting vars in the parameters, like: 执行make并在参数中设置vars时,例如:

make -C le/path install ONEVAR=one OTHERVAR=two

We have both ONEVAR and OTHERVAR on the env and the subtasks ran by the first command. 我们在环境上同时拥有ONEVAR和OTHERVAR,子任务由第一个命令运行。 This kind of puzzled me because I added to the task (at le/path) to execute a simple bash script that only did: 这让我感到困惑,因为我添加了任务(在le / path处)以执行仅执行以下操作的简单bash脚本:

echo $ONEVAR
unset ONEVAR

And by my surprise the var $ONEVAR was actually "one" (so it was on the env) and the unset actually cleared it. 令我惊讶的是,变量$ ONEVAR实际上是“一个”(因此它在env上),而unset实际上清除了它。 But, adding an "echo $(ONEVAR)" on the makefile still outputs "one".. This is due to MAKEOVERRIDES, and in fact, as suggested by Communicating Options to a Sub-make : 但是,在makefile上添加“ echo $(ONEVAR)”仍然会输出“ one”。这是由于MAKEOVERRIDES所致,实际上,正如对Sub-make传达选项所建议的那样:

The command line variable definitions really appear in the variable MAKEOVERRIDES, and MAKEFLAGS contains a reference to this variable. 命令行变量定义确实出现在变量MAKEOVERRIDES中,并且MAKEFLAGS包含对此变量的引用。 If you do want to pass flags down normally, but don't want to pass down the command line variable definitions, you can reset MAKEOVERRIDES to empty, like this: MAKEOVERRIDES = 如果您确实希望正常传递标志,但又不想传递命令行变量定义,则可以将MAKEOVERRIDES重置为空,如下所示:MAKEOVERRIDES =

Or as MadScientist suggested above :) 或如MadScientist上面建议的那样:)

But this was not enough, since this var was still being passed to the other subtasks below (in this situation some nodejs modules that were being compiled on a local folder, and by bad luck, both a js file from phantomjs and some other makefiles where using a var with the same name (eg, $ONEVAR). 但这还不够,因为此var仍被传递给下面的其他子任务(在这种情况下,一些在本地文件夹中编译的nodejs模块,不幸的是,这是phantomjs的js文件和其他一些makefile,使用具有相同名称的变量(例如$ ONEVAR)。

unexport variable Tell make whether or not to export a particular variable to child processes. unexport变量告诉make是否将特定变量导出到子进程。 GNU Make Appendix A Quick Reference GNU附录A快速参考

What I did was: 我所做的是:

DESTDIR_BUFFER=$(DESTDIR)
MAKEOVERRIDES := $(filter-out DESTDIR=%,$(MAKEOVERRIDES))
unexport DESTDIR

And only then make npm install. 然后才安装npm。

At the end of this task I export DESTDIR with the value at DESTDIR_BUFFER and all the other consequent tasks still work. 在此任务结束时,我导出了具有DESTDIR_BUFFER值的DESTDIR,所有其他后续任务仍然有效。

Thanks a lot for your help! 非常感谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM