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.
CMake has a bug report named Support for precompiled headers opened 14 years ago.
In his C++Now2017: Effective CMake talk Daniel Pfeifer mentioned that he had a prototype adding precompiled headers support, namely CMake MR 984. At 1h:19m:18s we can see how precompiled headers as usage requirements would look in CMake:
target_precompile_headers(Foo PUBLIC "foo.h" PRIVATE <unordered_map> )
His prototype had only support for Visual Studio and Xcode generators, nothing for Ninja or Makefiles.
Based on his work I've completed CMake MR 3553, which also adds support for Ninja and Makefiles generators.
Precompiled headers support in compilers can be split into two categories:
With both styles supported in CMake, I did a CMake build of Qt Creator on my Windows 10 machine running on a Lenovo AMD Ryzen CPU:
The results in a chart:
Because every target will get its own precompiled header, the build directory size have changed a bit:
The code that I had to write to add precompiled headers support in Qt Creator's CMake files is here.
Note for ccache users: you need to set up ccache sloppiness.
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:
#include "source_file1.cpp" #include "source_file2.cpp" /*...*/ #include "source_file8.cpp"
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:
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.
Download the latest release here: www.qt.io/download.
Qt 5.12 was developed with a strong focus on quality and is a long-term-supported (LTS) release that will be supported for 3 years.
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Näytä tämä julkaisu Instagramissa.
Want to build something for tomorrow, join #QtPeople today! We have loads of cool jobs you don’t want to miss! http://qt.io/careers #builtwithQt #software #developers #coding #framework #tool #tooling #C++ #QML #engineers #sales #tech #technology #UI #UX #CX #Qt #Qtdev #global #openpositions #careers #job
Henkilön Qt (@theqtcompany) jakama julkaisu