如何收集大型Makefile项目的所有依赖关系

如何收集大型Makefile项目的所有依赖关系

本文介绍了如何收集大型Makefile项目的所有依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个大型的C项目中,我有一个顶级的Makefile和许多子Makefile在不同的子目录中。我需要收集编译的所有依赖关系。为此,我将 -MMD 添加到 CFLAGS 并获取一堆 .d 依赖文件。



这些 .d 文件分散在子目录中。此外,依赖关系有时被编写为绝对路径,有时作为与编译目录相关的路径,有时包含符号链接。我编写了一个脚本,用于查找所有.d文件,遍历其目录,并解析所有找到的路径。这有效,但有数以万计的依赖关系文件,这个依赖关系集合持续大约与编译相同的时间! (这是等待太久:))

有没有更快的方法来获取单个文件中的所有依赖关系?这是ANSI C,GCC和Linux如果重要的话。 -MM 将所有依赖关系发送到标准输出。

  gcc -MM ... file.c>> $(top)/all.d 

如果后期处理是在一个文件中收集输出的唯一原因,则可以使用管道过滤输出

  gcc -MM ... file.c | sh filter.sh> file.d 

并将依赖项文件分开。



如果某个本地包含文件( defs.h )或主要源文件的路径很重要,则可以强制gcc包含通过给出适当的 -I 选项,例如

  gcc -MM -I $(top)/ path / to ... $(top)/ path / to / file .c>> $(top)/all.d 

  gcc -MM -I $(top)/ path / to ... $(top)/path/to/file.c | sh filter.sh> file.d 

而不是

  file.o:file.c defs.h 

gcc会发出

  file.o:/absolute/path/to/file.c / absolute / path / to /defs.h 

这当然也适用于相对路径。


In a large C project, I have a top Makefile and many sub-Makefiles in different subdirectories. I need to collect all dependencies of the compilation. For that, I add -MMD to CFLAGS and get a bunch of .d dependency files.

These .d files are scattered in the subdirectories. Also, the dependencies are written sometimes as absolute paths, sometimes as paths relevant to the compilation directory, and sometimes containing symbolic links. I have written a script which finds all .d files, traverses their directories, and resolves all found paths. This works, but with tens of thousands of dependency files this dependency collection lasts about the same time as the compilation! (which is too long to wait :) )

Is there a faster way to get all dependencies in a single file? This is ANSI C, GCC and Linux if that matters. Thanks in advance.

解决方案

Instead of -MMD, you can use -MM, which sends the dependencies to standard output.

You can then collect all the output to some dependency file in the top level directory with

gcc -MM ... file.c >>$(top)/all.d

If post processing is the only reason for collecting the output in one file, you can filter the output with a pipe

gcc -MM ... file.c | sh filter.sh >file.d

and keep the dependency files separate.

If the path to some local include file (defs.h) or the main source is important, you can force gcc to include a path by giving the appropriate -I option, e.g.

gcc -MM -I$(top)/path/to ... $(top)/path/to/file.c >>$(top)/all.d

or

gcc -MM -I$(top)/path/to ... $(top)/path/to/file.c | sh filter.sh >file.d

Instead of

file.o: file.c defs.h

gcc will emit

file.o: /absolute/path/to/file.c /absolute/path/to/defs.h

This works with relative paths as well, of course.

这篇关于如何收集大型Makefile项目的所有依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:21