55This file contains functions for options and configuration for targeting the
66Windows platform
77
8+ MSVC Runtime Selection
9+ ----------------------
10+
11+ There are two main ways to set the msvc runtime library;
12+ Using ``target_compile_options()`` to add the flags
13+ or using the ``CMAKE_MSVC_RUNTIME_LIBRARY`` property_ abstraction, introduced
14+ in CMake version 3.15 with the policy CMP0091_ to remove the flags from
15+ ``CMAKE_<LANG>_FLAGS_<CONFIG>``.
16+
17+ Default: ``CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"``
18+
19+ This initializes each target's ``MSVC_RUNTIME_LIBRARY`` property at the time of
20+ target creation.
21+
22+ This creates a conundrum for us, the ``CMAKE_MSVC_RUNTIME_LIBRARY`` needs to be
23+ correct at the time the target is created, but we have no control over the
24+ consumers CMake scripts, and the per-target ``MSVC_RUNTIME_LIBRARY`` property
25+ is not transient.
26+
27+ We need ``CMAKE_MSVC_RUNTIME_LIBRARY`` to be ``"$<1:>"`` to ensure it
28+ will not add any flags. And then use ``target_compile_options()`` so that our
29+ flags will propagate to consumers.
30+
31+ In the interests of playing nicely we detect whether we are being consumed
32+ and notify the consumer that we are setting ``CMAKE_MSVC_RUNTIME_LIBRARY``,
33+ that dependent targets rely on it, and point them to these comments as to why.
34+
35+ .. _CMP0091:https://cmake.org/cmake/help/latest/policy/CMP0091.html
36+ .. _property:https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
37+ .. https://discourse.cmake.org/t/mt-staticrelease-doesnt-match-value-md-dynamicrelease/5428/4
38+
839]=======================================================================]
40+ if ( PROJECT_NAME ) # we are not the top level if this is true
41+ if ( DEFINED CMAKE_MSVC_RUNTIME_LIBRARY )
42+ # Warning that we are clobbering the variable.
43+ message ( WARNING "setting CMAKE_MSVC_RUNTIME_LIBRARY to \" $<1:>\" " )
44+ else ( )
45+ # Notification that we are setting the variable
46+ message ( STATUS "setting CMAKE_MSVC_RUNTIME_LIBRARY to \" $<1:>\" " )
47+ endif ()
48+ endif ()
49+ set ( CMAKE_MSVC_RUNTIME_LIBRARY "$<1:>" CACHE INTERNAL "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." )
950
51+ #[============================[ Windows Options ]============================]
1052function ( windows_options )
1153
1254 option ( GODOT_USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON )
@@ -15,6 +57,7 @@ function( windows_options )
1557
1658endfunction ()
1759
60+ #[===========================[ Target Generation ]===========================]
1861function ( windows_generate TARGET_NAME )
1962 set ( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
2063 set ( IS_CLANG "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
@@ -26,8 +69,6 @@ function( windows_generate TARGET_NAME )
2669 set_target_properties ( ${TARGET_NAME}
2770 PROPERTIES
2871 PDB_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR} /bin>"
29- INTERFACE_MSVC_RUNTIME_LIBRARY
30- "$<IF:${DEBUG_CRT} ,MultiThreadedDebugDLL,$<IF:${STATIC_CPP} ,MultiThreaded,MultiThreadedDLL>>"
3172 )
3273
3374 target_compile_definitions ( ${TARGET_NAME}
@@ -39,6 +80,11 @@ function( windows_generate TARGET_NAME )
3980 >
4081 )
4182
83+ target_compile_options ( ${TARGET_NAME}
84+ PUBLIC
85+ $<${IS_MSVC} :$<IF:${DEBUG_CRT} ,/MDd,$<IF:${STATIC_CPP} ,/MT,/MD >>>
86+ )
87+
4288 target_link_options ( ${TARGET_NAME}
4389 PUBLIC
4490
0 commit comments