使 makefile 中的包含内容与当前文件的位置相关

发布于 12-14 19:21 字数 481 浏览 3 评论 0原文

序言:这个问题已经过时了。 NDK 构建/本机调试系统不再像这样工作了。


此问题直接相关。如何使 makefile 中的 include 指令的行为相对于当前执行 makefile 的位置。

假设当前路径是任意的并且您无法控制它。仅 makefile 位置已知。您的 makefile 不是 make 调用中的根文件 - 它已包含在内。 Android NDK 中正是如此。

是否有带有当前 makefile 名称或路径的内置变量(而不是根级 makefile)?我可以去掉文件名,只留下路径吗?在 Cygwin 上使用 make 3.81。

PREAMBLE: the question is wildly obsolete. The NDK build/native debugging system doesn't work like this anymore.


Directly related to this question. How can I make the include directive in makefiles behave relatively to the location of the currently executing makefile.

Assume that the current path is arbitrary and you have no control over it. Only the makefile location is known. Your makefile is not the root one from the make invokation - it's included. That's exactly how it is in Android NDK.

Is there a builtin variable with the current makefile's name or path (as opposed to the root level makefile)? Can I strip filename away from it, leaving just the path? Using make 3.81 on Cygwin.

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

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

发布评论

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

评论(4

注定孤独终老2024-12-21 19:21:54

您可以从 获取当前正在处理的 makefile 的名称MAKEFILE_LIST内置变量

鉴于当前的 makefile 是最后一个被包含的文件(换句话说,自当前脚本开始以来,您没有使用另一个 include 指令),路径脚本本身将是:

SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

现在您可以在同一目录中包含一个脚本(注意没有斜杠,它已经由 $(dir ...) 添加了):

include $(SELF_DIR)another.mk

注意:在 GNU Make 3.80 中没有lastword 内置函数。在这种情况下,您可以按如下方式实现它,将 $(lastword ...) 替换为 $(call lastword,...)

lastword = $(if $(firstword $1),$(word $(words $1),$1))

You can get the name of the makefile being currently processed from MAKEFILE_LIST builtin variable.

Given that the current makefile is the last one that has been included (in other words you didn't use another include directive since the beginning of the current script), the path to the script itself would be:

SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

Now you are able to include a script in the same directory as such (note an absence of slash, it has already been added by $(dir ...)):

include $(SELF_DIR)another.mk

Note: In GNU Make 3.80 there was no lastword builtin function. In that case you may implement it as follows replacing $(lastword ...) with $(call lastword,...):

lastword = $(if $(firstword $1),$(word $(words $1),$1))
听不够的曲调2024-12-21 19:21:54

是否存在具有当前 makefile 名称的内置变量?

是的,有,使用 ${CURDIR}。这是顶级 Makefile 所在的目录,因此您不需要从中删除任何内容。

http://www.gnu.org/software/make/manual/ make.html#递归

Is there a builtin variable with the current makefile's name?

Yes, there is, use ${CURDIR}. This is the directory where top-level Makefile is located, so you don't need to strip anything from it.

http://www.gnu.org/software/make/manual/make.html#Recursion

酒绊2024-12-21 19:21:54

我发现相对路径有效(GNUMake 3.81),但如果它们不适合您,请尝试以下操作:

include $(abspath ../whatever)

I find that relative paths work (GNUMake 3.81), but if they don't for you, try this:

include $(abspath ../whatever)
天赋异禀2024-12-21 19:21:54

虽然这是一个非常古老的线程,但是现在我们可以在调用 make 时使用 -I DIRECTORY, --include-dir=DIRECTORY
例如,如果根 Makefile 需要包含 ./sub/a.mk 并且 a.mk 使用相对于其自身目录的其他 make 文件,则可以像这样调用 make:

make -I ./sub <your Makefile target>

Although this is a very old thread, but now we can use -I DIRECTORY, --include-dir=DIRECTORY when calling make.
For example, if the root Makefile needs to include ./sub/a.mk and a.mk uses other make files relative to its own directory, you could call make like this:

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