CMake: Fix selection of MSVC Runtime compile flags

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.
pull/1663/head
Samuel Nicholas 2024-11-30 12:19:41 +10:30
parent bb567060f4
commit 732df06a81
1 changed files with 49 additions and 3 deletions

View File

@ -5,8 +5,50 @@ Windows
This file contains functions for options and configuration for targeting the
Windows platform
]=======================================================================]
MSVC Runtime Selection
----------------------
There are two main ways to set the msvc runtime library;
Using ``target_compile_options()`` to add the flags
or using the ``CMAKE_MSVC_RUNTIME_LIBRARY`` property_ abstraction, introduced
in CMake version 3.15 with the policy CMP0091_ to remove the flags from
``CMAKE_<LANG>_FLAGS_<CONFIG>``.
Default: ``CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"``
This initializes each target's ``MSVC_RUNTIME_LIBRARY`` property at the time of
target creation.
This creates a conundrum for us, the ``CMAKE_MSVC_RUNTIME_LIBRARY`` needs to be
correct at the time the target is created, but we have no control over the
consumers CMake scripts, and the per-target ``MSVC_RUNTIME_LIBRARY`` property
is not transient.
We need ``CMAKE_MSVC_RUNTIME_LIBRARY`` to be ``"$<1:>"`` to ensure it
will not add any flags. And then use ``target_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.
.. _CMP0091:https://cmake.org/cmake/help/latest/policy/CMP0091.html
.. _property:https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
.. https://discourse.cmake.org/t/mt-staticrelease-doesnt-match-value-md-dynamicrelease/5428/4
]=======================================================================]
if( PROJECT_NAME ) # we are not the top level if this is true
if( DEFINED CMAKE_MSVC_RUNTIME_LIBRARY )
# Warning that we are clobbering the variable.
message( WARNING "setting CMAKE_MSVC_RUNTIME_LIBRARY to \"$<1:>\"")
else( )
# Notification that we are setting the variable
message( STATUS "setting CMAKE_MSVC_RUNTIME_LIBRARY to \"$<1:>\"")
endif()
endif()
set( CMAKE_MSVC_RUNTIME_LIBRARY "$<1:>" CACHE INTERNAL "Select the MSVC runtime library for use by compilers targeting the MSVC ABI." )
#[============================[ Windows Options ]============================]
function( windows_options )
option( GODOT_USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON )
@ -15,6 +57,7 @@ function( windows_options )
endfunction()
#[===========================[ Target Generation ]===========================]
function( windows_generate TARGET_NAME )
set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
set( IS_CLANG "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
@ -26,8 +69,6 @@ function( windows_generate TARGET_NAME )
set_target_properties( ${TARGET_NAME}
PROPERTIES
PDB_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR}/bin>"
INTERFACE_MSVC_RUNTIME_LIBRARY
"$<IF:${DEBUG_CRT},MultiThreadedDebugDLL,$<IF:${STATIC_CPP},MultiThreaded,MultiThreadedDLL>>"
)
target_compile_definitions( ${TARGET_NAME}
@ -39,6 +80,11 @@ function( windows_generate TARGET_NAME )
>
)
target_compile_options( ${TARGET_NAME}
PUBLIC
$<${IS_MSVC}:$<IF:${DEBUG_CRT},/MDd,$<IF:${STATIC_CPP},/MT,/MD>>>
)
target_link_options( ${TARGET_NAME}
PUBLIC