I have a set of header files for a static library along with multiple implementations for different operating systems and build configurations - these include the 4 MSVC runtime library configurations for Windows (md, mdd, mt, mtd). I would like to create a CMake imported target which will select the correct library for the different CMake configs of the imported target (which is determined from CMAKE_MSVC_RUNTIME_LIBRARY
if it is set, otherwise some default).
If the IMPORTED_LOCATION
property were able to be set to a generator expression, I think this would be straight-forward, but as generator expressions are not permitted in this context, I am unsure how to approach doing this.
I cannot probe CMAKE_MSVC_RUNTIME_LIBRARY
with if()
statements, because it could expand to a string containing a generator expression.
The IMPORTED_LOCATION_{CONFIG}
properties are no help either, because they also do not accept generator expressions.
The best I can think of is: to have 4 separate targets which correspond to each runtime configuration, and use generator expressions in a target_link_libraries()
(which is valid) to select the correct one when linking against the imported target. Something like:
target_link_libraries(the_thing INTERFACE the_thing_$<IF:$<STREQUAL:${CMAKE_MSVC_RUNTIME_LIBRARY},MultiThreadedDebug>,mtd,$<IF:$<STREQUAL:${CMAKE_MSVC_RUNTIME_LIBRARY},MultiThreaded>,mt,$<IF:$<STREQUAL:${CMAKE_MSVC_RUNTIME_LIBRARY},MultiThreadedDebugDLL>,mdd,md>>>)
Any ideas?