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

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

问题描述

此问题直接相关

a>。如何使makefiles中的 include 指令与当前脚本的位置相对?



假设当前路径是任意的,您无法控制它。只有makefile位置是已知的。你的makefile不是根的 - 它包括。这就是它在Android NDK中的样子。



有一个内置变量与当前makefile的名称吗?我可以剥离文件名远离它,只留下路径?

解决方案

您可以从 MAKEFILE_LIST 内置变量。 / p>

由于当前makefile是最后一个已包含(换句话说,您没有使用另一个 include 指令,因为当前脚本的开头),脚本本身的路径是:

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

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

  include $(SELF_DIR)another.mk 

注意:在GNU Make 3.80中没有 lastword 内置函数。在这种情况下,你可以实现它如下用 $(call lastword,...)代替 $(lastword ...)

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


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

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.

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.

解决方案

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

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

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