使 makefile 中的包含相对于文件的位置 [英] Make include in makefiles be relative to the file's location

查看:44
本文介绍了使 makefile 中的包含相对于文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题直接相关.如何使 makefile 中的 include 指令的行为相对于当前脚本的位置?

Directly related to this question. How can I make the include directive in makefiles behave relatively to the location of the current script?

假设 当前 路径是任意的,您无法控制它.只有生成文件的位置是已知的.您的 makefile 不是根文件 - 它已包含在内.在 Android NDK 中就是这样.

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 - it's included. That's exactly how it is in Android NDK.

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

Is there a builtin variable with the current makefile's name? Can I strip filename away from it, leaving just the path? Using make 3.81 on Cygwin.

推荐答案

可以从MAKEFILE_LIST 内置变量.

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

鉴于当前的makefile是最后一个被包含的(换句话说,你从当前脚本开始就没有使用另一个include指令),脚本本身的路径是:

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)))

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

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

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

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))

这篇关于使 makefile 中的包含相对于文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆