我最近遇到了几个关于改善支持 CMake 的 C++ 项目的周转时间的特定方面的问题(例如“我应该在什么级别上分发我的构建过程?” https://stackoverflow.com/questions/37276312/at-what-level-should-i-distribute-my-build-process or “cmake重建_缓存just子目录?” https://stackoverflow.com/questions/35436945/cmake-rebuild-cache-for-just-a-subdirectory),我想知道是否有利用 CMake 提供的特定可能性的更一般的指导。如果可能没有跨平台编译时优化,我主要对基于 Visual Studio 或 GNU 工具链的方法感兴趣。
我已经了解并投资了一般推荐的领域来加速 C++ 构建:
-
更改/优化/微调工具链
-
优化您的代码库/软件架构(例如,通过减少依赖性并使用明确定义的子项目 - 单元测试)
-
投资更好的硬件(SSD、CPU、内存)
喜欢推荐的here https://stackoverflow.com/questions/373142/what-techniques-can-be-used-to-speed-up-c-compilation-times, here https://stackoverflow.com/questions/3396958/how-to-speed-up-g-compile-time-when-using-a-lot-of-templates or here https://stackoverflow.com/questions/6321679/effective-c-35-minimize-compilation-dependencies-between-files-is-it-still。所以我这个问题的重点是第一点。
另外我知道 CMake 的 Wiki 中可以找到的建议:
- CMake:使用所有核心进行构建 https://blog.kitware.com/cmake-building-with-all-your-cores/
- CMake 性能技巧 https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/Performance-Tips
前者只处理基础知识(并行 make),后者主要处理如何加速解析 CMake 文件。
只是为了让这个更具体一点,如果我以我的 CMake 为例here https://stackoverflow.com/questions/35436945/cmake-rebuild-cache-for-just-a-subdirectory/35826669#35826669使用 MSYS/GNU 的 100 个库我得到以下结果time
测量结果:
$ cmake --version
cmake version 3.5.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ time -p cmake -G "MSYS Makefiles" ..
-- The CXX compiler identification is GNU 4.8.1
...
-- Configuring done
-- Generating done
-- Build files have been written to: [...]
real 27.03
user 0.01
sys 0.03
$ time -p make -j8
...
[100%] Built target CMakeTest
real 113.11
user 8.82
sys 33.08
所以我总共有大约 140 秒的时间,我的目标 - 对于这个公认的非常简单的例子 - 是将其减少到我使用标准设置/工具获得的结果的 10-20% 左右。