How to speed up the compilation time of my C ++ project with CMake support?

I came across several SO questions regarding specific aspects of improving the turn-on time of C ++ projects with C ++ support recently (for example, β€œAt what level should I distribute my build process?” Or β€œcmake rebuild_cache only for a subdirectory?” ) I was wondering if there is a more general guide using the specific features of CMake suggestions. If there is probably no cross-platform compile-time optimization, I'm mostly interested in the Visual Studio or GNU toochain approaches.

And I already know and invest in recommended areas to speed up C ++ builds:

  • Change / optimize / fine-tune toolchain

  • Optimize your base / software code architecture (for example, by reducing dependencies and using clearly defined subprojects - unit tests)

  • Invest in the best hardware (SSD, CPU, memory)

as recommended here , here or here . Therefore, my focus on this issue is in the first place.

Plus, I know the recommendations that can be found in the CMake Wiki:

The former simply handles the basics (parallel make), the later ones mainly process the parsing of CMake files.

To make this a little more specific, if I take the CMake example from here with 100 libraries using MSYS / GNU, I got the following time measurement results

 $ 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 

So, I have a total of ~ 140 seconds, and my goal - admittedly for this very simple example - is to get about 10-20% of what I get with the standard settings / tools.

+7
performance compilation cmake gnu-make nmake
source share
1 answer

Here I had good results using the CMake and Visual Studio or GNU tools:

  • Exchange GNU with Ninja . It is faster, automatically uses all available processor cores, and has good dependency management. Just know

    a.) You need to configure target dependencies in CMake correctly. If you get to the point where the assembly has a dependency on another artifact, it must wait until they are compiled (synchronization points).

     $ time -p cmake -G "Ninja" .. -- The CXX compiler identification is GNU 4.8.1 ... real 11.06 user 0.00 sys 0.00 $ time -p ninja ... [202/202] Linking CXX executable CMakeTest.exe real 40.31 user 0.01 sys 0.01 

    b.) Binding is always such a synchronization point. Therefore, you can use CMake Object Libraries more to reduce them, but that makes your CMake code a little ugly.

     $ time -p ninja ... [102/102] Linking CXX executable CMakeTest.exe real 27.62 user 0.00 sys 0.04 
  • Separate less frequently changed or stable parts of the code into separate CMake projects and use CMake ExternalProject_Add() or - if you, for example, switch to the binary delivery of some libraries - find_library() .

  • Think of a different set of compiler / linker options for your daily work (but only if you also have testing time / experience with the final build options).

    a.) Skip parts of optimization

    b.) Try incremental binding

  • If you often make changes to the CMake code itself, consider restoring CMake from sources optimized for your machine architecture. The officially distributed CMake binaries are just a compromise for working on any possible processor architecture.

    When I use MinGW64 / MSYS to restore CMake 3.5.2, for example,

     cmake -DCMAKE_BUILD_TYPE:STRING="Release" -DCMAKE_CXX_FLAGS:STRING="-march=native -m64 -Ofast -flto" -DCMAKE_EXE_LINKER_FLAGS:STRING="-Wl,--allow-multiple-definition" -G "MSYS Makefiles" .. 

    I can speed up the first part:

     $ time -p [...]/MSYS64/bin/cmake.exe -G "Ninja" .. real 6.46 user 0.03 sys 0.01 
  • If your I / O file is very slow, and since CMake works with dedicated binary output directories, use a RAM disk. If you are still using a hard drive, consider switching to a solid state drive.

  • Depending on your final output file, exchange a standard GNU linker with Gold Linker . Even faster than Gold Linker is one of the LLVM projects. You should check to see if it already supports the necessary features on your platform.

  • Use Clang / c2 instead of the Visual C ++ compiler. For the Visual C ++ compiler, performance recommendations are provided by the Visual C ++ team, see https://blogs.msdn.microsoft.com/vcblog/2016/10/26/recommendations-to-speed-c-builds-in- visual-studio /

  • Increadibuild may increase compilation time.

References

  • CMake: How to set dependencies Source, Library and CMakeLists.txt?
  • Replacing ld with gold - any experience?
  • Is the lld linker a replacement for ld and gold?
+7
source share

All Articles