防止变量在 Makefile 中被重写?
我正在学习 Makefile 和应用程序交叉编译,并且我想找到一种方法来防止在 Makefile 中重新定义变量。
作为一个例子,我编写了一个顶级 Makefile /usr/src/someapp.mk ,它将...
1) 从网络下载应用程序的源代码到 /usr/src/someapp-1.2.3/
2) 解压源,并且
3) 运行其原始 /usr/src/someapp-1.2.3/Makefile
由于原始 Makefile 是作为独立脚本编写的,因此它将在不知不觉中(重新)定义通常的嫌疑对象(CC、AR/RANLIB、等)与本地值,有效地删除我在顶级 Makefile 中选择的值:
#My top-level /usr/src/someapp.mk:
all:
wget someapp-1.2.3.tar.gz
tar xzvf someapp-1.2.3.tar.gz
cd someapp-1.2.3 && $(MAKE)
#Original Makefile from www /usr/src/someapp-1.2.3/Makefile:
#RANLIB locally redefined... :-/
RANLIB=ranlib
all:
$(RANLIB)...
我希望用户能够下载原始源代码,而不是提供我自己的修改版本:有没有办法让“ make" 要么忽略本地重新定义变量,要么动态重写它们?
谢谢。
I'm learning about Makefiles and application cross-compiling, and I'd like to find a way to prevent variables from being redefined in Makefiles further down the chain.
As an example, I write a top-level Makefile /usr/src/someapp.mk that will...
1) download the application's source code from the web into /usr/src/someapp-1.2.3/
2) untar the source, and
3) run its original /usr/src/someapp-1.2.3/Makefile
Since the original Makefile was written as a stand-alone script, it will unknowningly (re)define the usual suspects (CC, AR/RANLIB, etc.) with local values, effectively erasing the value I chose in the top-level Makefile:
#My top-level /usr/src/someapp.mk:
all:
wget someapp-1.2.3.tar.gz
tar xzvf someapp-1.2.3.tar.gz
cd someapp-1.2.3 && $(MAKE)
#Original Makefile from www /usr/src/someapp-1.2.3/Makefile:
#RANLIB locally redefined... :-/
RANLIB=ranlib
all:
$(RANLIB)...
I'd like users to be able to download the original source code instead of providing my own, modified version: Is there a way to have "make" either ignore when variables are redefined locally or rewrite them on the fly?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
?=
运算符仅定义未设置的变量。到目前为止,最有用的方法是在 make 命令行上定义变量。这些变量在 make 期间变为只读。
然后
但这
建议您的代码片段
cd someapp-1.2.3 && $(MAKE)
变为$(MAKE) -C someapp-1.2.3 RANLIB=myval
。The
?=
operator will only define a variable if it is unset.By far the most useful way though is to define variables on the make command line. Such variables become read-only for the duration of the make.
then
but
This suggests your snippet
cd someapp-1.2.3 && $(MAKE)
becomes$(MAKE) -C someapp-1.2.3 RANLIB=myval
.使通过 make 命令行传递的变量覆盖 makefile 分配。 http://www.gnu.org/software/make/manual /make.html#Override-Directive
调用第三方 makefile,如下所示:
Make variables passed through make command line override makefile assignments. http://www.gnu.org/software/make/manual/make.html#Override-Directive
Invoke that third-party makefile as follows: