Skip to content

Commit 7638b77

Browse files
committed
SCons: Disable C++ exception handling by default
Counterpart to godotengine/godot#80612.
1 parent 74b352e commit 7638b77

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

CMakeLists.txt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,22 @@ endif()
7272
# Input from user for GDExtension interface header and the API JSON file
7373
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE STRING "")
7474
set(GODOT_CUSTOM_API_FILE "" CACHE STRING "")
75-
set(FLOAT_PRECISION "single" CACHE STRING "")
76-
if ("${FLOAT_PRECISION}" STREQUAL "double")
77-
add_definitions(-DREAL_T_IS_DOUBLE)
78-
endif()
7975

8076
set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
8177
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
8278
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
8379
endif()
8480

81+
set(FLOAT_PRECISION "single" CACHE STRING "")
82+
if ("${FLOAT_PRECISION}" STREQUAL "double")
83+
add_definitions(-DREAL_T_IS_DOUBLE)
84+
endif()
85+
8586
set(GODOT_COMPILE_FLAGS )
8687

8788
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
8889
# using Visual Studio C++
89-
set(GODOT_COMPILE_FLAGS "/EHsc /utf-8") # /GF /MP
90+
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
9091

9192
if(CMAKE_BUILD_TYPE MATCHES Debug)
9293
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
@@ -107,6 +108,21 @@ else() # GCC/Clang
107108
endif(CMAKE_BUILD_TYPE MATCHES Debug)
108109
endif()
109110

111+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
112+
# saves around 20% of binary size and very significant build time (GH-80513).
113+
option(GODOT_DISABLE_EXCEPTIONS ON "Force disabling exception handling code")
114+
if (GODOT_DISABLE_EXCEPTIONS)
115+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
116+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
117+
else()
118+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
119+
endif()
120+
else()
121+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
122+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
123+
endif()
124+
endif()
125+
110126
# Generate source from the bindings file
111127
find_package(Python3 3.4 REQUIRED) # pathlib should be present
112128
if(GENERATE_TEMPLATE_GET_NODE)

SConstruct

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ opts.Add(
145145
default=env.get("generate_template_get_node", True),
146146
)
147147
)
148-
149148
opts.Add(BoolVariable(key="build_library", help="Build the godot-cpp library.", default=env.get("build_library", True)))
150149
opts.Add(
151150
EnumVariable(
@@ -155,6 +154,7 @@ opts.Add(
155154
allowed_values=("single", "double"),
156155
)
157156
)
157+
opts.Add(BoolVariable("disable_exceptions", "Force disabling exception handling code", True))
158158

159159
# compiledb
160160
opts.Add(
@@ -264,6 +264,16 @@ else:
264264
if env["precision"] == "double":
265265
env.Append(CPPDEFINES=["REAL_T_IS_DOUBLE"])
266266

267+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
268+
# saves around 20% of binary size and very significant build time (GH-80513).
269+
if env["disable_exceptions"]:
270+
if env.get("is_msvc", False):
271+
env.Append(CPPDEFINES=[("_HAS_EXCEPTIONS", 0)])
272+
else:
273+
env.Append(CCFLAGS=["-fno-exceptions"])
274+
elif env.get("is_msvc", False):
275+
env.Append(CCFLAGS=["/EHsc"])
276+
267277
# compile_commands.json
268278
if env.get("compiledb", False):
269279
env.Tool("compilation_db")

test/CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ set(GODOT_LINKER_FLAGS )
3636

3737
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
3838
# using Visual Studio C++
39-
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc /WX") # /GF /MP
39+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /WX") # /GF /MP
4040
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /DTYPED_METHOD_BIND")
4141

4242
if(CMAKE_BUILD_TYPE MATCHES Debug)
@@ -92,6 +92,21 @@ else()
9292
endif(CMAKE_BUILD_TYPE MATCHES Debug)
9393
endif()
9494

95+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
96+
# saves around 20% of binary size and very significant build time (GH-80513).
97+
option(GODOT_DISABLE_EXCEPTIONS ON "Force disabling exception handling code")
98+
if (GODOT_DISABLE_EXCEPTIONS)
99+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
100+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
101+
else()
102+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
103+
endif()
104+
else()
105+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
106+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
107+
endif()
108+
endif()
109+
95110
# Get Sources
96111
file(GLOB_RECURSE SOURCES src/*.c**)
97112
file(GLOB_RECURSE HEADERS include/*.h**)

tools/windows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def generate(env):
3131
env.Tool("mslink")
3232

3333
env.Append(CPPDEFINES=["TYPED_METHOD_BIND", "NOMINMAX"])
34-
env.Append(CCFLAGS=["/EHsc", "/utf-8"])
34+
env.Append(CCFLAGS=["/utf-8"])
3535
env.Append(LINKFLAGS=["/WX"])
3636

3737
if env["use_clang_cl"]:

0 commit comments

Comments
 (0)