How to manage source file dependencies automatically and fast build for large C++ project?

Viewed 320

I am working on a c++ codebase using CMake with 500k+ lines of code. One problem I realize is that the (incremental) build speed is really slow.

Partly because the dependencies graph is not fine grained enough. E.g. many targets from target_link_libraries have unnecessary dependencies, e.g. linking(dependent on) objects(libraries) for a small part of the libraries but also containing many other unnecessary functions/classes to the target. So when a small change is made to to those dependent files, a lot of targets not needing the change get compiled again.

I thought about refactoring all the CMakeLists.txt such that each add_library is used for one file only and target_link_libraries only link necessary libraries. This way of refactoring is too time-consuming, not automatic enough and error-prone.

Can Bazel, Scons or other build system solve this problem?

related question: How to speed up Compile Time of my CMake enabled C++ Project?

1 Answers

Here are some things I used while moving 6M LOC codebase to CMake:

  1. Use LINK_DEPENDS_NO_SHARED (doc). You don't need to relink every dependent target if you only changed .cpp files.
  2. Use PUBLIC, PRIVATE and INTERFACE keywords correctly.
  3. Hide your symbols to improve link times.
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
  1. Build only subset of needed targets. This is applicable if you have some kind of plugin system and don't need all of plugins to be available at once.

Remember that if you change header files you MUST rebuild every dependent target, otherwise you risk may have ABI incompatibility. PIMPL idiom might help you there.

Related