防止变量在 Makefile 中被重写?

发布于 2024-10-12 11:12:05 字数 734 浏览 0 评论 0原文

我正在学习 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 技术交流群。

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

发布评论

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

评论(2

≈。彩虹 2024-10-19 11:12:05

?= 运算符仅定义未设置的变量。

VAR ?= value

到目前为止,最有用的方法是在 make 命令行上定义变量。这些变量在 make 期间变为只读。

$ cat Makefile
VAR := hello
$(error [${VAR}])

然后

$ make
Makefile:2: *** [hello].  Stop.

但这

$ make VAR='value overridden'
Makefile:2: *** [value overridden].  Stop.

建议您的代码片段 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.

VAR ?= value

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.

$ cat Makefile
VAR := hello
$(error [${VAR}])

then

$ make
Makefile:2: *** [hello].  Stop.

but

$ make VAR='value overridden'
Makefile:2: *** [value overridden].  Stop.

This suggests your snippet cd someapp-1.2.3 && $(MAKE) becomes $(MAKE) -C someapp-1.2.3 RANLIB=myval.

春夜浅 2024-10-19 11:12:05

使通过 make 命令行传递的变量覆盖 makefile 分配。 http://www.gnu.org/software/make/manual /make.html#Override-Directive

调用第三方 makefile,如下所示:

$ make ... RANLIB=myranlib CC=mycc

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:

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