帮助 rake 依赖关系映射

发布于 2024-08-23 11:28:32 字数 964 浏览 6 评论 0原文

我正在为 C++ 项目编写 Rakefile。我希望它自动识别#includes,强制重建依赖于更改的源文件的目标文件。我有一个可行的解决方案,但我认为它可以更好。我正在寻找以下建议:

  • 改进我的功能的建议 为
  • 我完成工作的库、gems 或工具
  • 链接到很酷的 C++ Rakefiles,我应该检查一下它们可以做类似的事情

这是我到目前为止所拥有的。它是一个返回给定源文件的依赖项列表的函数。我输入给定目标文件的源文件,并且我想要一个文件列表,这些文件将迫使我重建我的目标文件。

def find_deps( file )
  deps = Array.new
  # Find all include statements
  cmd = "grep -r -h -E \"#include\" #{file}"
  includes = `#{cmd}`
  includes.each do |line|
    dep = line[ /\.\/(\w+\/)*\w+\.(cpp|h|hpp)/ ]
    unless dep.nil?
      deps << dep # Add the dependency to the list
      deps += find_deps( dep )
    end
  end
  return deps
end

我应该注意到,我的所有包含现在看起来都是这样的:

#include "./Path/From/Top/Level/To/My/File.h" // For top-level files like main.cpp 
#include "../../../Path/From/Top/To/My/File.h" // Otherwise

请注意,我在项目中使用双引号,并在外部库包含中使用尖括号。我愿意接受有关其他方法的建议,这些方法可以使我的生活更轻松地进行包含路径。

I'm writing a Rakefile for a C++ project. I want it to identify #includes automatically, forcing the rebuilding of object files that depend on changed source files. I have a working solution, but I think it can be better. I'm looking for suggestions for:

  • Suggestions for improving my function
  • Libraries, gems, or tools that do the work for me
  • Links to cool C++ Rakefiles that I should check out that do similar things

Here's what I have so far. It's a function that returns the list of dependencies given a source file. I feed in the source file for a given object file, and I want a list of files that will force me to rebuild my object file.

def find_deps( file )
  deps = Array.new
  # Find all include statements
  cmd = "grep -r -h -E \"#include\" #{file}"
  includes = `#{cmd}`
  includes.each do |line|
    dep = line[ /\.\/(\w+\/)*\w+\.(cpp|h|hpp)/ ]
    unless dep.nil?
      deps << dep # Add the dependency to the list
      deps += find_deps( dep )
    end
  end
  return deps
end

I should note that all of my includes look like this right now:

#include "./Path/From/Top/Level/To/My/File.h" // For top-level files like main.cpp 
#include "../../../Path/From/Top/To/My/File.h" // Otherwise

Note that I'm using double quotes for includes within my project and angle brackets for external library includes. I'm open to suggestions on alternative ways to do my include pathing that make my life easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韬韬不绝 2024-08-30 11:28:32

请使用 gcc 命令生成 Make 依赖项列表,并解析该列表:

g++ -M -MM -MF - inputfile.cpp

有关详细信息,请参阅 man gccinfo gcc

Use the gcc command to generate a Make dependency list instead, and parse that:

g++ -M -MM -MF - inputfile.cpp

See man gcc or info gcc for details.

一世旳自豪 2024-08-30 11:28:32

我确信对于 #include 指令中的内容有不同的思想流派。我建议不要将整个路径放入 #includes 中。相反,在编译命令中设置正确的包含路径(使用 -I)。这使得将来重新定位文件变得更容易并且更具可读性(在我看来)。这听起来可能微不足道,但随着项目的发展进行重组的能力绝对是有价值的。

使用预处理器(请参阅@greyfade)生成依赖项列表的优点是它将根据您的包含目录扩展标头路径。


更新:另请参阅Rakefile 文档的导入依赖项部分< /a> 用于读取 makefile 依赖格式的库。

I'm sure there are different schools of thought with respect to what to put in #include directives. I advise against putting the whole path in your #includes. Instead, set up the proper include paths in your compile command (with -I). This makes it easier to relocate files in the future and more readable (in my opinion). It may sound minor, but the ability to reorganize as a project evolves is definitely valuable.

Using the preprocessor (see @greyfade) to generate the dependency list has the advantage that it will expand the header paths for you based on your include dirs.


Update: see also the Importing Dependencies section of the Rakefile doc for a library that reads the makefile dependency format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文