Support adding configuration specific custom targets and dependencies when using Ninja Multi-Config
It would be useful to allow creation of ninja targets that are specific to a certain configuration.
One can then run add_custom_target
commands or add_custom_command
commands limited to a certain configuration.
Sample project showcasing the syntax:
cmake_minimum_required(VERSION 3.18)
set(CMAKE_CROSS_CONFIGS "all" CACHE STRING "" FORCE)
set(CMAKE_DEFAULT_CONFIGS "all" CACHE STRING "" FORCE)
list(GET CMAKE_CONFIGURATION_TYPES 0 first_config_type)
set(CMAKE_DEFAULT_BUILD_TYPE "${first_config_type}" CACHE STRING "")
project(test_proj CXX)
set(source_file "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp")
file(WRITE "${source_file}" "int main(int argc, char** argv) {return 0;}")
add_executable(app "${source_file}")
### Simple case
foreach(config ${CMAKE_CONFIGURATION_TYPES})
add_custom_target(app_per_config_${config}
COMMAND ${CMAKE_COMMAND} -E echo "${config} specific command"
)
add_dependencies(app_per_config_${config} app:${config}) # <--------------------- new syntax
endforeach()
### More complicated case with add_custom_command
foreach(config ${CMAKE_CONFIGURATION_TYPES})
add_custom_target(app_prepare_${config})
add_dependencies(app_prepare_${config} app:${config}) # <------------------------- new syntax
set(script_timestamp_file "${CMAKE_CURRENT_BINARY_DIR}/script_timestamp_${config}.txt")
add_custom_command(
OUTPUT "${script_timestamp_file}"
DEPENDS
app_prepare_${config}
COMMAND
${CMAKE_COMMAND} -E touch "${script_timestamp_file}"
VERBATIM
)
add_custom_target(app_post_per_config DEPENDS "${script_timestamp_file}")
add_dependencies(app_post_per_config app)
endforeach()
This is slightly related to #12877 (closed) because it would allow creating custom commands targeting a specific configuration.