包含的Makefile的父目录 [英] Included Makefile's parent directory

查看:257
本文介绍了包含的Makefile的父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个顶级Makefile可以包含的文件中描述每个submake的依赖项.这是为了允许进行递归的make设置(具有实例变量和相对路径的所有功能),但允许在顶层make中描述所有依赖项,以提高编译速度和并行性.

I want to describe each submake's dependencies in a file that a top-level Makefile can include. This is to allows for a recursive make setup (with all of the power of instanced variables and relative pathing) but with all dependencies described in a top-level make to increase compile speed and parallelism.

例如,假设我们有一个看起来像这样的目录树:

For instance, let's assume we have a directory tree that looks like this:

project/
|-- lib1
|   |-- Makefile
|   `-- Makefile.reg
|-- lib2
|   |-- Makefile
|   `-- Makefile.reg
|-- Makefile
`-- Makefile.reg

project/lib1/Makefile.reg文件的文件可能如下所示:

The file for project/lib1/Makefile.reg file may look like this:

REG := lib1
include ../Makefile.reg

project/lib2/Makefile.reg文件的文件可能如下所示:

The file for project/lib2/Makefile.reg file may look like this:

REG := lib2
DEP := lib1
include ../Makefile.reg

文件project/Makefile.reg如下所示:

$(REG)_DIR = ????
$(REG): $(DEP)
  $(MAKE) -C $($@_DIR)

最后,顶级project/Makefile看起来像这样:

And finally, the top-level project/Makefile would look like this:

include $(shell find . -name "Makefile.reg")

现在,顶级Makefile具有每个目标的所有依赖项信息,并且可以智能地调用递归make并利用完全并行性,同时保持依赖项树不变.

Now, the top-level Makefile has all of the dependency information for every target and can intelligently call recursive make and utilize full parallelism while keeping the dependency tree intact.

问题是我不确定如何让project/Makefile.reg知道当前子品牌的Makefile.reg的路径是什么. make将始终从顶级目录中调用,因此$(shell pwd)始终将报告project/.一种解决方案是包括这样的答案,但是我希望每个答案都可以Makefile.reg仅指定目标和可选的依赖项列表.

The issue is that I'm not sure how to let project/Makefile.reg know what the current submake's Makefile.reg's path is. make will always be called from the top-level directory, so $(shell pwd) will always report project/. One solution would be to include the line from this SO answer, however I would like each Makefile.reg to only specify a target and an optional dependency list.

是否有一种方法可以将目录路径发现包含在公用的project/Makefile.reg中,而不是在每个子make Makefile.reg中都添加新行?换句话说,包含的makefile可以推断出父makefile的目录吗?在MAKEFILE_LIST变量的某些不同解析中,我看到了潜力.

Is there a way to include the directory path discovery in the common project/Makefile.reg rather than putting a new line in every single submake Makefile.reg? In other words, can an included makefile deduce the parent makefile's directory? I see potential in some different parsing of the MAKEFILE_LIST variable.

推荐答案

如链接票证中答案中所包括的文档摘要所示. MAKEFILE_LIST的值随生产进行更新.列表中的最新条目是当前的makefile,它使列表中倒数第二个条目成为之前的最近包含的makefile.如果您愿意断言主project/Makefile.reg仅会包含在子目录makefile中,那么它可以简单地检查MAKEFILE_LIST中的该条目.

As the snippet of documentation included in the answer on your linked ticket indicates. The value of MAKEFILE_LIST is updated as make goes. The latest entry in the list is the current makefile which makes the penultimate entry in the list the most recently included makefile before that. If you are willing to assert that the main project/Makefile.reg will only ever be included from a sub-directory makefile then it could simply examine that entry in MAKEFILE_LIST.

或者,您可以简单地定义一个罐头食谱在主Makefile中,然后在每个项目的makefile中调用它以定义适当的目标.

Alternatively you could simply define a canned recipe in the main Makefile and call it in each project makefile to define the appropriate targets.

不幸的是,make的确使倒数第二个进入变得比愉快更难.但是以下方法应该起作用:

Unfortunately make does make getting the penultimate entry a little more difficult than is pleasant. But the following should work:

FOO=a b c d e f g
BAR=h i j k l m n
BAZ=o p q r s t u
QUX=v w x y z 1 2

penultimateword = $(wordlist $(words $1),$(words $1), x $1)

REG=FOO
$(REG)_DIR= $(call penultimateword,$($(REG)))
$(info REG_DIR=$($(REG)_DIR))

REG=BAR
$(REG)_DIR= $(call penultimateword,$($(REG)))
$(info REG_DIR=$($(REG)_DIR))

REG=BAZ
$(REG)_DIR= $(call penultimateword,$($(REG)))
$(info REG_DIR=$($(REG)_DIR))

REG=QUX
$(REG)_DIR= $(call penultimateword,$($(REG)))
$(info REG_DIR=$($(REG)_DIR))

all: ;

上面的灵感来自神奇的 GMSL chop函数.

Inspiration for the above came from the chop function from the fantastic GMSL.

这篇关于包含的Makefile的父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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