Skip to content

Commit 16df4bf

Browse files
committed
[SCons] Split targets.py, apply flags from tools
Split `targets` tool logic, moving all the compiler-specific flags to a new `common_compiler_flags.py` file, and everything else (CPPDEFINES, optimize option logic, dev build logic, etc) to the `godotcpp` tool. The default tools now apply the common compiler flags by importing the file and explicitly calling `configure`.
1 parent 620104e commit 16df4bf

9 files changed

+180
-180
lines changed

tools/android.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import my_spawn
4+
import common_compiler_flags
45
from SCons.Script import ARGUMENTS
56

67

@@ -118,3 +119,5 @@ def generate(env):
118119
env.Append(LINKFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"]])
119120

120121
env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED"])
122+
123+
common_compiler_flags.generate(env)

tools/common_compiler_flags.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
6+
def using_clang(env):
7+
return "clang" in os.path.basename(env["CC"])
8+
9+
10+
def is_vanilla_clang(env):
11+
if not using_clang(env):
12+
return False
13+
try:
14+
version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8")
15+
except (subprocess.CalledProcessError, OSError):
16+
print("Couldn't parse CXX environment variable to infer compiler version.")
17+
return False
18+
return not version.startswith("Apple")
19+
20+
21+
def exists(env):
22+
return True
23+
24+
25+
def generate(env):
26+
# Require C++17
27+
if env.get("is_msvc", False):
28+
env.Append(CXXFLAGS=["/std:c++17"])
29+
else:
30+
env.Append(CXXFLAGS=["-std=c++17"])
31+
32+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
33+
# saves around 20% of binary size and very significant build time.
34+
if env["disable_exceptions"]:
35+
if env.get("is_msvc", False):
36+
env.Append(CPPDEFINES=[("_HAS_EXCEPTIONS", 0)])
37+
else:
38+
env.Append(CXXFLAGS=["-fno-exceptions"])
39+
elif env.get("is_msvc", False):
40+
env.Append(CXXFLAGS=["/EHsc"])
41+
42+
if not env.get("is_msvc", False):
43+
if env["symbols_visibility"] == "visible":
44+
env.Append(CCFLAGS=["-fvisibility=default"])
45+
env.Append(LINKFLAGS=["-fvisibility=default"])
46+
elif env["symbols_visibility"] == "hidden":
47+
env.Append(CCFLAGS=["-fvisibility=hidden"])
48+
env.Append(LINKFLAGS=["-fvisibility=hidden"])
49+
50+
# Set optimize and debug_symbols flags.
51+
# "custom" means do nothing and let users set their own optimization flags.
52+
if env.get("is_msvc", False):
53+
if env["debug_symbols"]:
54+
env.Append(CCFLAGS=["/Zi", "/FS"])
55+
env.Append(LINKFLAGS=["/DEBUG:FULL"])
56+
57+
if env["optimize"] == "speed":
58+
env.Append(CCFLAGS=["/O2"])
59+
env.Append(LINKFLAGS=["/OPT:REF"])
60+
elif env["optimize"] == "speed_trace":
61+
env.Append(CCFLAGS=["/O2"])
62+
env.Append(LINKFLAGS=["/OPT:REF", "/OPT:NOICF"])
63+
elif env["optimize"] == "size":
64+
env.Append(CCFLAGS=["/O1"])
65+
env.Append(LINKFLAGS=["/OPT:REF"])
66+
elif env["optimize"] == "debug" or env["optimize"] == "none":
67+
env.Append(CCFLAGS=["/Od"])
68+
else:
69+
if env["debug_symbols"]:
70+
# Adding dwarf-4 explicitly makes stacktraces work with clang builds,
71+
# otherwise addr2line doesn't understand them.
72+
env.Append(CCFLAGS=["-gdwarf-4"])
73+
if env.dev_build:
74+
env.Append(CCFLAGS=["-g3"])
75+
else:
76+
env.Append(CCFLAGS=["-g2"])
77+
else:
78+
if using_clang(env) and not is_vanilla_clang(env):
79+
# Apple Clang, its linker doesn't like -s.
80+
env.Append(LINKFLAGS=["-Wl,-S", "-Wl,-x", "-Wl,-dead_strip"])
81+
else:
82+
env.Append(LINKFLAGS=["-s"])
83+
84+
if env["optimize"] == "speed":
85+
env.Append(CCFLAGS=["-O3"])
86+
# `-O2` is friendlier to debuggers than `-O3`, leading to better crash backtraces.
87+
elif env["optimize"] == "speed_trace":
88+
env.Append(CCFLAGS=["-O2"])
89+
elif env["optimize"] == "size":
90+
env.Append(CCFLAGS=["-Os"])
91+
elif env["optimize"] == "debug":
92+
env.Append(CCFLAGS=["-Og"])
93+
elif env["optimize"] == "none":
94+
env.Append(CCFLAGS=["-O0"])

tools/godotcpp.py

+67-33
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os, sys, platform
22

33
from SCons.Variables import EnumVariable, PathVariable, BoolVariable
4+
from SCons.Variables.BoolVariable import _text2bool
45
from SCons.Tool import Tool
56
from SCons.Builder import Builder
67
from SCons.Errors import UserError
8+
from SCons.Script import ARGUMENTS
9+
710

811
from binding_generator import scons_generate_bindings, scons_emit_files
912

@@ -14,6 +17,17 @@ def add_sources(sources, dir, extension):
1417
sources.append(dir + "/" + f)
1518

1619

20+
def get_cmdline_bool(option, default):
21+
"""We use `ARGUMENTS.get()` to check if options were manually overridden on the command line,
22+
and SCons' _text2bool helper to convert them to booleans, otherwise they're handled as strings.
23+
"""
24+
cmdline_val = ARGUMENTS.get(option)
25+
if cmdline_val is not None:
26+
return _text2bool(cmdline_val)
27+
else:
28+
return default
29+
30+
1731
def normalize_path(val, env):
1832
return val if os.path.isabs(val) else os.path.join(env.Dir("#").abspath, val)
1933

@@ -230,16 +244,23 @@ def options(opts, env):
230244
)
231245
)
232246

247+
opts.Add(
248+
EnumVariable(
249+
"optimize",
250+
"The desired optimization flags",
251+
"speed_trace",
252+
("none", "custom", "debug", "speed", "speed_trace", "size"),
253+
)
254+
)
255+
opts.Add(BoolVariable("debug_symbols", "Build with debugging symbols", True))
256+
opts.Add(BoolVariable("dev_build", "Developer build with dev-only debugging code (DEV_ENABLED)", False))
257+
233258
# Add platform options (custom tools can override platforms)
234259
for pl in sorted(set(platforms + custom_platforms)):
235260
tool = Tool(pl, toolpath=get_platform_tools_paths(env))
236261
if hasattr(tool, "options"):
237262
tool.options(opts)
238263

239-
# Targets flags tool (optimizations, debug symbols)
240-
target_tool = Tool("targets", toolpath=["tools"])
241-
target_tool.options(opts)
242-
243264

244265
def generate(env):
245266
# Default num_jobs to local cpu count if not user specified.
@@ -286,43 +307,56 @@ def generate(env):
286307

287308
print("Building for architecture " + env["arch"] + " on platform " + env["platform"])
288309

289-
if env.get("use_hot_reload") is None:
290-
env["use_hot_reload"] = env["target"] != "template_release"
291-
if env["use_hot_reload"]:
292-
env.Append(CPPDEFINES=["HOT_RELOAD_ENABLED"])
310+
# These defaults may be needed by platform tools
311+
env.use_hot_reload = env.get("use_hot_reload", env["target"] != "template_release")
312+
env.editor_build = env["target"] == "editor"
313+
env.dev_build = env["dev_build"]
314+
env.debug_features = env["target"] in ["editor", "template_debug"]
315+
316+
if env.dev_build:
317+
opt_level = "none"
318+
elif env.debug_features:
319+
opt_level = "speed_trace"
320+
else: # Release
321+
opt_level = "speed"
322+
323+
env["optimize"] = ARGUMENTS.get("optimize", opt_level)
324+
env["debug_symbols"] = get_cmdline_bool("debug_symbols", env.dev_build)
293325

294326
tool = Tool(env["platform"], toolpath=get_platform_tools_paths(env))
295327

296328
if tool is None or not tool.exists(env):
297329
raise ValueError("Required toolchain not found for platform " + env["platform"])
298330

299331
tool.generate(env)
300-
target_tool = Tool("targets", toolpath=["tools"])
301-
target_tool.generate(env)
302-
303-
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
304-
# saves around 20% of binary size and very significant build time.
305-
if env["disable_exceptions"]:
306-
if env.get("is_msvc", False):
307-
env.Append(CPPDEFINES=[("_HAS_EXCEPTIONS", 0)])
308-
else:
309-
env.Append(CXXFLAGS=["-fno-exceptions"])
310-
elif env.get("is_msvc", False):
311-
env.Append(CXXFLAGS=["/EHsc"])
312-
313-
if not env.get("is_msvc", False):
314-
if env["symbols_visibility"] == "visible":
315-
env.Append(CCFLAGS=["-fvisibility=default"])
316-
env.Append(LINKFLAGS=["-fvisibility=default"])
317-
elif env["symbols_visibility"] == "hidden":
318-
env.Append(CCFLAGS=["-fvisibility=hidden"])
319-
env.Append(LINKFLAGS=["-fvisibility=hidden"])
320-
321-
# Require C++17
322-
if env.get("is_msvc", False):
323-
env.Append(CXXFLAGS=["/std:c++17"])
332+
333+
if env.use_hot_reload:
334+
env.Append(CPPDEFINES=["HOT_RELOAD_ENABLED"])
335+
336+
if env.editor_build:
337+
env.Append(CPPDEFINES=["TOOLS_ENABLED"])
338+
339+
# Configuration of build targets:
340+
# - Editor or template
341+
# - Debug features (DEBUG_ENABLED code)
342+
# - Dev only code (DEV_ENABLED code)
343+
# - Optimization level
344+
# - Debug symbols for crash traces / debuggers
345+
# Keep this configuration in sync with SConstruct in upstream Godot.
346+
if env.debug_features:
347+
# DEBUG_ENABLED enables debugging *features* and debug-only code, which is intended
348+
# to give *users* extra debugging information for their game development.
349+
env.Append(CPPDEFINES=["DEBUG_ENABLED"])
350+
# In upstream Godot this is added in typedefs.h when DEBUG_ENABLED is set.
351+
env.Append(CPPDEFINES=["DEBUG_METHODS_ENABLED"])
352+
353+
if env.dev_build:
354+
# DEV_ENABLED enables *engine developer* code which should only be compiled for those
355+
# working on the engine itself.
356+
env.Append(CPPDEFINES=["DEV_ENABLED"])
324357
else:
325-
env.Append(CXXFLAGS=["-std=c++17"])
358+
# Disable assert() for production targets (only used in thirdparty code).
359+
env.Append(CPPDEFINES=["NDEBUG"])
326360

327361
if env["precision"] == "double":
328362
env.Append(CPPDEFINES=["REAL_T_IS_DOUBLE"])

tools/ios.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import subprocess
4+
import common_compiler_flags
45
from SCons.Variables import *
56

67
if sys.version_info < (3,):
@@ -104,3 +105,5 @@ def generate(env):
104105
env.Append(LINKFLAGS=["-isysroot", env["IOS_SDK_PATH"], "-F" + env["IOS_SDK_PATH"]])
105106

106107
env.Append(CPPDEFINES=["IOS_ENABLED", "UNIX_ENABLED"])
108+
109+
common_compiler_flags.generate(env)

tools/linux.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import common_compiler_flags
12
from SCons.Variables import *
23
from SCons.Tool import clang, clangxx
34

@@ -14,7 +15,7 @@ def generate(env):
1415
if env["use_llvm"]:
1516
clang.generate(env)
1617
clangxx.generate(env)
17-
elif env["use_hot_reload"]:
18+
elif env.use_hot_reload:
1819
# Required for extensions to truly unload.
1920
env.Append(CXXFLAGS=["-fno-gnu-unique"])
2021

@@ -37,3 +38,5 @@ def generate(env):
3738
env.Append(LINKFLAGS=["-march=rv64gc"])
3839

3940
env.Append(CPPDEFINES=["LINUX_ENABLED", "UNIX_ENABLED"])
41+
42+
common_compiler_flags.generate(env)

tools/macos.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import common_compiler_flags
34

45

56
def has_osxcross():
@@ -70,3 +71,5 @@ def generate(env):
7071
)
7172

7273
env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED"])
74+
75+
common_compiler_flags.generate(env)

0 commit comments

Comments
 (0)