target_link_libraries: $<$<CONFIG:Foo>:Bar> for INTERFACE libs broken multi-config generators
While trying to work-around #20682 (closed) I fell into the following pit:
cmake_minimum_required(VERSION 3.17.0)
project(buggetybug)
add_executable(exe main.cpp)
add_library(debug_stuff_lib INTERFACE)
target_sources(debug_stuff_lib INTERFACE debug_stuff.cpp)
add_library(release_stuff_lib INTERFACE)
target_sources(release_stuff_lib INTERFACE release_stuff.cpp)
target_link_libraries(exe PRIVATE
$<$<CONFIG:Debug>:debug_stuff_lib>
$<$<CONFIG:Release>:release_stuff_lib>)
configured with
cmake -G"Ninja Multi-Config" -DCMAKE_PREFIX_PATH=~/Qt/5.14/lib/cmake '-DCMAKE_CONFIGURATION_TYPES=Debug;Release'
does not use release_stuff.cpp
in the Release configuration:
cmake --build . --clean-first --parallel --config Debug ; cmake --build . --clean-first --parallel --config Release
[1/1] Cleaning all built files...
Cleaning... 3 files.
[1/3] Building CXX object CMakeFiles/exe.dir/Debug/debug_stuff.cpp.o
[2/3] Building CXX object CMakeFiles/exe.dir/Debug/main.cpp.o
[3/3] Linking CXX executable Debug/exe
[1/1] Cleaning all built files...
Cleaning... 3 files.
[1/3] Building CXX object CMakeFiles/exe.dir/Release/debug_stuff.cpp.o
[2/3] Building CXX object CMakeFiles/exe.dir/Release/main.cpp.o
[3/3] Linking CXX executable Release/exe
Switching the order of configurations leads to always using release_stuff.cpp:
cmake -G"Ninja Multi-Config" -DCMAKE_PREFIX_PATH=~/Qt/5.14/lib/cmake '-DCMAKE_CONFIGURATION_TYPES=Release;Debug'
...
Cleaning... 2 files.
[1/3] Building CXX object CMakeFiles/exe.dir/Debug/release_stuff.cpp.o
[2/3] Building CXX object CMakeFiles/exe.dir/Debug/main.cpp.o
[3/3] Linking CXX executable Debug/exe
[1/1] Cleaning all built files...
Cleaning... 3 files.
[1/3] Building CXX object CMakeFiles/exe.dir/Release/release_stuff.cpp.o
[2/3] Building CXX object CMakeFiles/exe.dir/Release/main.cpp.o
[3/3] Linking CXX executable Release/exe
Looks like the generator expression in target_link_libraries
always tests for the "main config".
Example attached: multicfgbug.tar.gz