CMake: Fix selection of MSVC Runtime compile flags#1663
Conversation
8a743fc to
fa1497a
Compare
|
OK, to verify that I have this one right this time, I have compiled the test project, on windows with the following configurations. scons verbose=yes target=template_debug scons verbose=yes target=template_debug debug_crt=yes scons verbose=yes target=template_debug use_static_cpp=no cmake --fresh .. cmake --fresh .. -DGODOT_DEBUG_CRT=YES cmake --fresh .. -DGODOT_USE_STATIC_CPP=NO Cleaned up the compile logs, and compared them in this spreadsheet: While there are still discrepancies between the builds, they are down to other factors. I've also got an external project replicating the test folder, but entirely separated from the sources so it has to fetch and consume godot-cpp independently. This also correctly picks up the options. |
My last attempt at solving this was not correct. I have left lots of comments in the source detailing the issue as it will effect consumers.
fa1497a to
732df06
Compare
My last attempt at solving this was not correct, and I apologise for that.
MSVC Runtime Selection
There are two main ways to set the msvc runtime library; Using
target_compile_options()to add the flags or using theCMAKE_MSVC_RUNTIME_LIBRARYproperty abstraction1, introduced in CMake version 3.15 with the policy CMP00912 toremove the flags from
CMAKE_<LANG>_FLAGS_<CONFIG>.Default:
CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"This initializes each target's
MSVC_RUNTIME_LIBRARYproperty at the time of target creation.This creates a conundrum for us, the
CMAKE_MSVC_RUNTIME_LIBRARYneeds to be correct at the time the target is created, but we have no control over the consumers CMake scripts, and the per-targetMSVC_RUNTIME_LIBRARYpropertyis not transient, that is, it doesn't flow to dependent targets.
The previous attempt using INTERFACE_MSVC_RUNTIME_LIBRARY was a complete brain fail on my part, the INTERFACE_ prefix is not a universal concept like I thought it was, this property will do nothing.
We need
CMAKE_MSVC_RUNTIME_LIBRARYto be"$<1:>"3 to ensure it will not add any flags. And then usetarget_compile_options()so that our flags will propagate to consumers.In the interests of playing nicely we detect whether we are being consumed and notify the consumer that we are setting
CMAKE_MSVC_RUNTIME_LIBRARY, that dependent targets rely on it, and point them to these comments as to why.Further information from the cmake forum
Footnotes
https://cmake.org/cmake/help/latest/policy/CMP0091.html ↩
https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html ↩
This is a generator expression that results in the empty string. ↩