Ninja Multi-Config: Provide a way to exclude building a configuration for a target when using cross config mode
Consider the project
cmake_minimum_required(VERSION 3.21.0)
set(CMAKE_CONFIGURATION_TYPES "Release;Debug" CACHE STRING "")
if(CMAKE_GENERATOR STREQUAL "Ninja Multi-Config")
set(CMAKE_DEFAULT_BUILD_TYPE "Release" CACHE STRING "")
set(CMAKE_CROSS_CONFIGS "all" CACHE STRING "")
set(CMAKE_DEFAULT_CONFIGS "all" CACHE STRING "")
endif()
project(proj LANGUAGES CXX)
set(source_path "${CMAKE_CURRENT_BINARY_DIR}/source.cpp")
file(WRITE "${source_path}" "void foo() {}")
add_library(Core SHARED "${source_path}")
Configuring with cmake .. -G"Ninja Multi-Config"
and running ninja -v
will build the Core
target for both Release
and Debug
.
There is currently no way to tell CMake not to generate build rules for a specific configuration of a target.
I propose introducing a new property called CROSS_CONFIGS
which can be set to a subset of CMAKE_CONFIGURATION_TYPES
.
E.g. with
set_target_properties(Core PROPERTIES CROSS_CONFIGS "Debug")
calling either ninja
or ninja Core
will only build the Core
library for 'Debug'.
Trying to call ninja Core:Release
would be an error, because CMake would not generate any build rules for a Release
library.
The property should also work for add_custom_target
and any add_custom_command
s that are driven by the target.
The proposed feature is different from #20923 (closed) because it's not about excluding a configuration from the all
target, but not generating build rules at all.