如何在Makefile中获取脚本? [英] How to source a script in a Makefile?
问题描述
是否有更好的方法从makefile中获取设置环境变量的脚本?
Is there a better way to source a script, which sets env vars, from within a makefile?
FLAG ?= 0
ifeq ($(FLAG),0)
export FLAG=1
/bin/myshell -c '<source scripts here> ; $(MAKE) $@'
else
...targets...
endif
推荐答案
要回答以下问题:您不能.
基本问题是子进程不能更改父进程的环境.外壳程序通过在source
时不分叉新进程来解决此问题,而只是在外壳程序的当前版本中运行这些命令.效果很好,但make
不是/bin/sh
(或脚本所用的外壳),并且不理解该语言(除了它们的共同点).
The basic issue is that a child process can not alter the parent's environment. The shell gets around this by not forking a new process when source
'ing, but just running those commands in the current incarnation of the shell. That works fine, but make
is not /bin/sh
(or whatever shell your script is for) and does not understand that language (aside from the bits they have in common).
Chris Dodd和Foo Bah解决了一个可能的解决方法,因此我建议另一个(假设您正在运行GNU make):将Shell脚本后处理为make兼容的文本并包含结果:
Chris Dodd and Foo Bah have addressed one possible workaround, so I'll suggest another (assuming you are running GNU make): post-process the shell script into make compatible text and include the result:
shell-variable-setter.make: shell-varaible-setter.sh
postprocess.py @^
# ...
else
include shell-variable-setter.make
endif
锻炼时还留下
杂乱无章的细节.
messy details left as an exercise.
这篇关于如何在Makefile中获取脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!