target_link_libraries: docs do not explicitly forbid debug/optimized keywords in generator expressions
I am having trouble using a list with a conditional generator expression inside target link libraries. The generator expression seems to expand incorrectly.
In short, I have a list of libraries that is found by a 3rd party CMake find module. The list is composed of a number of elements each preceded by “optimized” or “debug”, depending on if there are built for release or debug. There is no problem if I add this list of libraries in target_link_libraries
without conditional generator expression. But strange things start to happen if I add a <$BOOL:...>
expression. Suddenly our application started to get a dependency to a library called optimized.lib
.
I tried to make a self contained minimal example:
cmake_minimum_required(VERSION 3.12)
project(expansion_error LANGUAGES CXX)
add_executable(my_exe main.cpp)
option(USE_FOOLIB "Use foo.lib" ON)
option(USE_BARLIB "Use bar.lib" ON)
list(APPEND FOO_LIBRARIES optimized foo)
list(APPEND FOO_LIBRARIES debug foo_d)
target_link_libraries(my_exe
PUBLIC
$<$<BOOL:${USE_FOOLIB}>:${FOO_LIBRARIES}>
$<$<BOOL:${USE_BARLIB}>:bar>
)
But when I run this script using CMake 3.12.2, it expands to something even worse:
$<1:optimized;foo.lib;foo_d>.lib;bar.lib
--- for debug builds
$<1:optimized;foo.lib;>.lib;bar.lib
--- for release builds
If I try to put the generator expression inside double quotes like this:
target_link_libraries(my_exe
PUBLIC
"$<$<BOOL:${USE_FOOLIB}>:${FOO_LIBRARIES}>"
"$<$<BOOL:${USE_BARLIB}>:bar>"
)
The generated expansion is optimized.lib;foo.lib;debug.lib;foo_d.lib;bar.lib
for both debug and release builds