As you might have heard Qt is moving away from qmake in favor of CMake. Some people have raised concerns with the fact that CMake generates projects that build in longer time than qmake.
For the last couple of weeks I have been busy with improving CMake upstream in regards to compilation speed.
Qmake can make use of precompiled headers, while CMake doesn't have support for them natively. It's enough to search after "precompiled headers CMake" in your favorite search engine to find tens of projects that provide this functionatily via a CMake module.
While working on adding support for precompiled headers I learned how to add source files internally in the CMake project. Therefore it was easy to come up with unity (jumbo) source files, which include the original source files in batches in form of:
The CMake MR 3611 adds support for Unity (Jumbo) Builds in CMake.
With precompiled headers one could just change a few CMake lines and get a 20-40% speed increase without changing any source files. With unity builds it's not always the case. ODR (One Definition Rule) errors are popping up everywhere.
Qt Creator's source code is not compatible with unity builds. I took Speedcrunch for testing.
Speedcrunch also doesn't work out of the box with unity builds. Patch can be found here.
Setting up Unity builds with CMake is as easy as passing -DCMAKE_UNITY_BUILD=ON in the CMake command line. The default batch size is 8.
I configured Speedcrunch in Release mode, and compiled only the speedcrunch target, and got the results:
MinGW GCC 8.1.0 – 29.33% speed-up
Visual C++ 2017 – 53.50% speed-up
Clang-cl 8.0.0 – 23.61% speed-up
The results in a chart:
The build directory size is a bit smaller with Unity builds, because of less object files created.
Both features are under code review on Kitware side. We can do something about the build times now, until C++20 Modules will change the game.