Skip to content

Commit e49db7e

Browse files
committed
CMake: re-add /RTC1 removal fix
Add msvcHack.cmake to consolidate funky business Move the runtime library fix into the same location The RTC fix was present in 4.3 and below, it somehow got lost in a rebase perhaps.
1 parent f08e781 commit e49db7e

File tree

2 files changed

+101
-54
lines changed

2 files changed

+101
-54
lines changed

cmake/msvcHack.cmake

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#[=======================================================================[.rst:
2+
3+
msvcHack.cmake
4+
--------------
5+
6+
There are a couple of things that CMake does which mess up the build and are
7+
hard to work around. This cmake script is an attempt to document and work
8+
around those shortcomings.
9+
10+
MSVC Runtime Selection
11+
----------------------
12+
13+
There are two main ways to set the msvc runtime library;
14+
Using ``target_compile_options()`` to add the flags
15+
or using the ``CMAKE_MSVC_RUNTIME_LIBRARY`` property_ abstraction, introduced
16+
in CMake version 3.15 with the policy CMP0091_ to remove the flags from
17+
``CMAKE_<LANG>_FLAGS_<CONFIG>``.
18+
19+
Default: ``CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"``
20+
21+
This initializes each target's ``MSVC_RUNTIME_LIBRARY`` property at the time of
22+
target creation.
23+
24+
it is stated in the msvc_ documentation that: "All modules passed to a given
25+
invocation of the linker must have been compiled with the same runtime library
26+
compiler option (/MD, /MT, /LD)."
27+
28+
This creates a conundrum for us, the ``CMAKE_MSVC_RUNTIME_LIBRARY`` needs to be
29+
correct at the time the target is created, but we have no control over the
30+
consumers CMake scripts, and the per-target ``MSVC_RUNTIME_LIBRARY`` property
31+
is not transient.
32+
33+
It has been raised that not using ``CMAKE_MSVC_RUNTIME_LIBRARY`` can also cause
34+
issues_ when a dependency( independent to godot-cpp ) that doesn't set any
35+
runtime flags, which relies purely on the ``CMAKE_MSVC_RUNTIME_LIBRARY``
36+
variable will very likely not have the correct msvc runtime flags set.
37+
38+
So we'll set ``CMAKE_MSVC_RUNTIME_LIBRARY`` as CACHE STRING so that it will be
39+
available for consumer target definitions, but also be able to be overridden if
40+
needed.
41+
42+
Additionally we message consumers notifying them and pointing to this
43+
documentation.
44+
45+
.. _CMP0091:https://cmake.org/cmake/help/latest/policy/CMP0091.html
46+
.. _property:https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
47+
.. https://discourse.cmake.org/t/mt-staticrelease-doesnt-match-value-md-dynamicrelease/5428/4
48+
.. _msvc: https://learn.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library
49+
.. _issues: https://github.com/godotengine/godot-cpp/issues/1699
50+
51+
Run-Time Error Checks
52+
---------------------
53+
54+
The MSVC compiler flag_ /RTC[] is Used to enable and disable the run-time error
55+
checks feature, in conjunction with the runtime_checks pragma.
56+
57+
.. _flag: https://learn.microsoft.com/en-us/cpp/build/reference/rtc-run-time-error-checks?view=msvc-170
58+
59+
If enabled it triggers a stacktrace when a template_debug godot loads an
60+
gdextension. Unfortunately CMake adds this flag to Debug builds, and there
61+
is no simple solution for removing it. The solution here, is drawn from
62+
others experiences 1_, 2_.
63+
64+
.. _1: https://discourse.cmake.org/t/how-do-i-remove-compile-options-from-target/5965
65+
.. _2: https://stackoverflow.com/questions/74426638/how-to-remove-rtc1-from-specific-target-or-file-in-cmake
66+
67+
There are two locations where the RTC options appear, and they are both within
68+
the same macro in the file_: Platform/Windows-MSVC.cmake
69+
70+
.. _file: https://gitlab.kitware.com/cmake/cmake/-/blob/566e96d42db35e2c88ceb5d0f3de49736295c496/Modules/Platform/Windows-MSVC.cmake
71+
72+
FUTURE: CMake introduced the ``CMAKE_MSVC_RUNTIME_CHECKS`` option_ in v4.0
73+
.. _option: https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_CHECKS.html
74+
75+
]=======================================================================]
76+
77+
if(MSVC)
78+
message(
79+
STATUS
80+
"NOTE: We know its bad etiquette to set global build variables, however,\n"
81+
"\tCMAKE_MSVC_RUNTIME_LIBRARY needs to be set to something specific for godot-cpp, and\n"
82+
"\tCMAKE_CXX_FLAGS_DEBUG needs /RTC1 removed as it triggers a segfaut on when template_debug loads\n"
83+
"\tPlease ensure that any targets that are linking with godot-cpp are defined after godot-cpp\n"
84+
"\tFor more information please read godot-cpp/cmake/msvcHack.cmake"
85+
)
86+
87+
if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "v[0-9]+_clang_.*") # The same condition that Windows-MSVC.cmake uses
88+
else()
89+
string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
90+
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
91+
endif()
92+
93+
set(CMAKE_MSVC_RUNTIME_LIBRARY
94+
"MultiThreaded$<IF:$<BOOL:${GODOTCPP_DEBUG_CRT}>,DebugDLL,$<$<NOT:$<BOOL:${GODOTCPP_USE_STATIC_CPP}>>:DLL>>"
95+
CACHE STRING
96+
"Select the MSVC runtime library for use by compilers targeting the MSVC ABI."
97+
)
98+
endif()

cmake/windows.cmake

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,11 @@ project directive, it means that
1010
* ``CMAKE_CURRENT_SOURCE_DIR`` is the location of godot-cpp's CMakeLists.txt
1111
* ``CMAKE_SOURCE_DIR`` is the location where any prior ``project(...)``
1212
directive was
13-
14-
MSVC Runtime Selection
15-
----------------------
16-
17-
There are two main ways to set the msvc runtime library;
18-
Using ``target_compile_options()`` to add the flags
19-
or using the ``CMAKE_MSVC_RUNTIME_LIBRARY`` property_ abstraction, introduced
20-
in CMake version 3.15 with the policy CMP0091_ to remove the flags from
21-
``CMAKE_<LANG>_FLAGS_<CONFIG>``.
22-
23-
Default: ``CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"``
24-
25-
This initializes each target's ``MSVC_RUNTIME_LIBRARY`` property at the time of
26-
target creation.
27-
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-
32-
This creates a conundrum for us, the ``CMAKE_MSVC_RUNTIME_LIBRARY`` needs to be
33-
correct at the time the target is created, but we have no control over the
34-
consumers CMake scripts, and the per-target ``MSVC_RUNTIME_LIBRARY`` property
35-
is not transient.
36-
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.
45-
46-
Additionally we message consumers notifying them and pointing to this
47-
documentation.
48-
49-
.. _CMP0091:https://cmake.org/cmake/help/latest/policy/CMP0091.html
50-
.. _property:https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
51-
.. 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
54-
5513
]=======================================================================]
5614

15+
# MSVC compiler requires some changes to the defaults CMake sets up
16+
set(CMAKE_PROJECT_godot-cpp_INCLUDE cmake/msvcHack.cmake)
17+
5718
#[============================[ Windows Options ]============================]
5819
function(windows_options)
5920
#[[ Options from SCons
@@ -70,18 +31,6 @@ function(windows_options)
7031

7132
option(GODOTCPP_USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON)
7233
option(GODOTCPP_DEBUG_CRT "Compile with MSVC's debug CRT (/MDd)" OFF)
73-
74-
message(
75-
STATUS
76-
"If not already cached, setting CMAKE_MSVC_RUNTIME_LIBRARY.\n"
77-
"\tFor more information please read godot-cpp/cmake/windows.cmake"
78-
)
79-
80-
set(CMAKE_MSVC_RUNTIME_LIBRARY
81-
"MultiThreaded$<IF:$<BOOL:${GODOTCPP_DEBUG_CRT}>,DebugDLL,$<$<NOT:$<BOOL:${GODOTCPP_USE_STATIC_CPP}>>:DLL>>"
82-
CACHE STRING
83-
"Select the MSVC runtime library for use by compilers targeting the MSVC ABI."
84-
)
8534
endfunction()
8635

8736
#[===========================[ Target Generation ]===========================]

0 commit comments

Comments
 (0)