add_custom_target: DEPENDS takes union of all configurations
When using a conditional generator expression in the DEPENDS field of a custom target, both conditional branches are always taken.
Example:
add_custom_command(OUTPUT ${OUTPUT_DEBUG} COMMAND ${COMMAND_DEBUG})
add_custom_command(OUTPUT ${OUTPUT_RELEASE} COMMAND ${COMMAND_RELEASE})
add_custom_target(target ALL
DEPENDS
$<IF:$<CONFIG:Debug>,${OUTPUT_DEBUG},${OUTPUT_RELEASE}>
)
Whenever target
is built in debug, both OUTPUT_RELEASE
and OUTPUT_DEBUG
commands are triggered.
This also happens with the following reproducer:
cmake_minimum_required(VERSION 3.19)
project(test)
add_custom_command(OUTPUT debug COMMAND cmake -E touch debug)
add_custom_command(OUTPUT release COMMAND cmake -E touch release)
add_custom_target(target ALL
DEPENDS
$<$<CONFIG:Release>:release>
$<$<CONFIG:Debug>:debug>
)
Output:
$ cmake --build . --config Release
Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
Generating debug
Generating release
Building Custom Rule CMakeLists.txt
Building Custom Rule CMakeLists.txt