编写一个Makefile,使其被其他Makefile包含 [英] Writing a Makefile to be includable by other Makefiles

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

问题描述

背景

我有一个(大型)项目A和一个(大型)项目B,因此A依赖于B.

I have a (large) project A and a (large) project B, such that A depends on B.

我想拥有两个单独的生成文件-一个用于项目A,一个用于项目B-以提高性能和可维护性.

I would like to have two separate makefiles -- one for project A and one for project B -- for performance and maintainability.

基于评论

Based on the comments to an earlier question, I have decided to entirely rewrite B's makefile such that A's makefile can include it. This will avoid the evils of recursive make: allow parallelism, not remake unnecessarily, improve performance, etc.

当前解决方案

我可以通过在顶部添加(在包含其他任何内容之前)找到当前正在执行的makefile的目录.

I can find the directory of the currently executing makefile by including at the top (before any other includes).

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

我将每个目标写为

$(TOP)/some-target: $(TOP)/some-src

并更改任何必要的shell命令,例如find dirfind $(TOP)/dir.

and making changes to any necessary shell commands, e.g. find dir to find $(TOP)/dir.

虽然可以解决问题,但它有两个缺点:

While this solves the problems it has a couple disadvantages:

  1. 目标和规则更长,可读性更差. (这是不可避免的.模块化是有代价的.)

  1. Targets and rules are longer and a little less readable. (This is likely unavoidable. Modularity has a price).

使用gcc -M自动生成依赖项需要进行后处理才能在任意位置添加$(TOP).

Using gcc -M to auto-generate dependencies requires post-processing to add $(TOP) everywhere.

这是编写其他人可以include d的makefile的常用方法吗?

Is this the usual way to write makefiles that can be included by others?

推荐答案

如果用通常"来表示最常见",那么答案是否".人们最常做的就是临时修改includee,以免名称与includer冲突.

If by "usual" you mean, "most common", then the answer is "no". The most common thing people do, is to improvise some changes to the includee so the names do not clash with the includer.

但是,您所做的是好的设计".

What you did, however, is "good design".

事实上,我对您的设计更进一步.

In fact, I take your design even futher.

我计算了一个目录堆栈,如果包含是递归的,则在解析makefile树时,需要将当前目录保留在堆栈中. $D是当前目录-人们键入的内容比$(TOP)/短,

I compute a stack of directories, if the inclusion is recursive, you need to keep the current directories on a stack as you parse the makefile tree. $D is the current directory - shorter for people to type than $(TOP)/,

,而我将包含在一切中的 加上$D/,所以您就有了变量:

and I prepend everything in the includee, with $D/, so you have variables:

$D/FOOBAR := 

和虚假目标:

$D/phony:

这篇关于编写一个Makefile,使其被其他Makefile包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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