Skip to content

Commit 847dca4

Browse files
authored
Merge pull request godotengine#1701 from enetheru/msvc_runtime
CMake: Fix for godotengine#1699 msvc runtime selection issues
2 parents f398ebb + 18a926e commit 847dca4

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

cmake/windows.cmake

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#[=======================================================================[.rst:
22
Windows
33
-------
4-
54
This file contains functions for options and configuration for targeting the
65
Windows platform
76
7+
Because this file is included into the top level CMakelists.txt before the
8+
project directive, it means that
9+
10+
* ``CMAKE_CURRENT_SOURCE_DIR`` is the location of godot-cpp's CMakeLists.txt
11+
* ``CMAKE_SOURCE_DIR`` is the location where any prior ``project(...)``
12+
directive was
13+
814
MSVC Runtime Selection
915
----------------------
1016
@@ -19,48 +25,50 @@ Default: ``CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"
1925
This initializes each target's ``MSVC_RUNTIME_LIBRARY`` property at the time of
2026
target creation.
2127
28+
it is stated in the msvc_ documentation that: "All modules passed to a given
29+
invocation of the linker must have been compiled with the same runtime library
30+
compiler option (/MD, /MT, /LD)."
31+
2232
This creates a conundrum for us, the ``CMAKE_MSVC_RUNTIME_LIBRARY`` needs to be
2333
correct at the time the target is created, but we have no control over the
2434
consumers CMake scripts, and the per-target ``MSVC_RUNTIME_LIBRARY`` property
2535
is not transient.
2636
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.
37+
It has been raised that not using ``CMAKE_MSVC_RUNTIME_LIBRARY`` can also cause
38+
issues_ when a dependency( independent to godot-cpp ) that doesn't set any
39+
runtime flags, which relies purely on the ``CMAKE_MSVC_RUNTIME_LIBRARY``
40+
variable will very likely not have the correct msvc runtime flags set.
41+
42+
So we'll set ``CMAKE_MSVC_RUNTIME_LIBRARY`` as CACHE STRING so that it will be
43+
available for consumer target definitions, but also be able to be overridden if
44+
needed.
3045
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.
46+
Additionally we message consumers notifying them and pointing to this
47+
documentation.
3448
3549
.. _CMP0091:https://cmake.org/cmake/help/latest/policy/CMP0091.html
3650
.. _property:https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
3751
.. https://discourse.cmake.org/t/mt-staticrelease-doesnt-match-value-md-dynamicrelease/5428/4
52+
.. _msvc: https://learn.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library
53+
.. _issues: https://github.com/godotengine/godot-cpp/issues/1699
3854
3955
]=======================================================================]
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." )
50-
51-
#[============================[ Windows Options ]============================]
5256
function( windows_options )
53-
5457
option( GODOT_USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON )
55-
5658
option( GODOT_DEBUG_CRT "Compile with MSVC's debug CRT (/MDd)" OFF )
5759

60+
message( STATUS "If not already cached, setting CMAKE_MSVC_RUNTIME_LIBRARY.\n"
61+
"\tFor more information please read godot-cpp/cmake/windows.cmake")
62+
63+
set( CMAKE_MSVC_RUNTIME_LIBRARY
64+
"MultiThreaded$<IF:$<BOOL:${GODOT_DEBUG_CRT}>,DebugDLL,$<$<NOT:$<BOOL:${GODOT_USE_STATIC_CPP}>>:DLL>>"
65+
CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI.")
5866
endfunction()
5967

68+
6069
#[===========================[ Target Generation ]===========================]
6170
function( windows_generate )
6271
set( STATIC_CPP "$<BOOL:${GODOT_USE_STATIC_CPP}>")
63-
set( DEBUG_CRT "$<BOOL:${GODOT_DEBUG_CRT}>" )
6472

6573
set_target_properties( ${TARGET_NAME}
6674
PROPERTIES
@@ -76,11 +84,6 @@ function( windows_generate )
7684
>
7785
)
7886

79-
target_compile_options( ${TARGET_NAME}
80-
PUBLIC
81-
$<${IS_MSVC}:$<IF:${DEBUG_CRT},/MDd,$<IF:${STATIC_CPP},/MT,/MD>>>
82-
)
83-
8487
target_link_options( ${TARGET_NAME}
8588
PUBLIC
8689

0 commit comments

Comments
 (0)