Multi-Config Ninja Generator in CMake 3.17

February 3, 2020
CMake Logo

We are pleased to announce that a new multi-configuration variant of the Ninja generator will be available for all supported platforms in CMake 3.17. This feature has been requested in the past, and even attempted by external contributors, but not completed until now. With the Qt Project’s migration to CMake, we have finally implemented it to help with building simultaneous debug and release builds of Qt.

The Ninja Multi-Config generator generates .ninja files, just like the traditional single-config Ninja generator. However, it generates these files for multiple configurations, similar to the Visual Studio and XCode generators. This means that, for the first time ever, CMake supports multi-configuration builds on Linux and all other supported platforms. (Until the development of this feature, only Windows and MacOS could do multi-configuration builds using Visual Studio and XCode, respectively.)

When using the Ninja Multi-Config generator, multiple build-<Config>.ninja files are generated, one for each configuration in CMAKE_CONFIGURATION_TYPES. Select a configuration by running ninja -f build-<Config>.ninja <target>, which will build <target> in the selected configuration. And of course, you can also use cmake --build . --config <config> to build instead.

If you don’t want to run ninja with the -f argument, don’t worry. You can optionally specify a configuration to use for a file, in which case running ninja <target> will behave as if you ran ninja -f build-<DefaultConfig>.ninja <target>.

One of the more advanced features of the Ninja Multi-Config generator is cross-config mode. In this mode, you can use tools built in one configuration to run custom commands for another configuration. For example, you can run the release version of a code generation tool to generate some code, which will then be compiled in the debug configuration. This is useful for building targets in debug mode without having to use the slower debug versions of the tools used to generate the code.

The Ninja Multi-Config generator will be available in CMake 3.17. For details on how to use it, see the CMake documentation.

Leave a Reply