Skip to content

Commit 112e41b

Browse files
authored
Merge pull request #1204 from akien-mga/scons-targets-sync-with-godot
SCons: Sync `targets.py` fully with upstream Godot
2 parents 74b352e + 600e749 commit 112e41b

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

tools/targets.py

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import os
2+
import subprocess
23
import sys
34
from SCons.Script import ARGUMENTS
45
from SCons.Variables import *
56
from SCons.Variables.BoolVariable import _text2bool
67

78

9+
# Helper methods
10+
11+
812
def get_cmdline_bool(option, default):
913
"""We use `ARGUMENTS.get()` to check if options were manually overridden on the command line,
1014
and SCons' _text2bool helper to convert them to booleans, otherwise they're handled as strings.
@@ -16,6 +20,24 @@ def get_cmdline_bool(option, default):
1620
return default
1721

1822

23+
def using_clang(env):
24+
return "clang" in os.path.basename(env["CC"])
25+
26+
27+
def is_vanilla_clang(env):
28+
if not using_clang(env):
29+
return False
30+
try:
31+
version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8")
32+
except (subprocess.CalledProcessError, OSError):
33+
print("Couldn't parse CXX environment variable to infer compiler version.")
34+
return False
35+
return not version.startswith("Apple")
36+
37+
38+
# Main tool definition
39+
40+
1941
def options(opts):
2042
opts.Add(
2143
EnumVariable(
@@ -34,19 +56,21 @@ def exists(env):
3456

3557

3658
def generate(env):
37-
env.dev_build = env["dev_build"]
38-
env.debug_features = env["target"] in ["editor", "template_debug"]
39-
env.editor_build = env["target"] == "editor"
59+
# Configuration of build targets:
60+
# - Editor or template
61+
# - Debug features (DEBUG_ENABLED code)
62+
# - Dev only code (DEV_ENABLED code)
63+
# - Optimization level
64+
# - Debug symbols for crash traces / debuggers
4065

41-
if env.editor_build:
42-
env.AppendUnique(CPPDEFINES=["TOOLS_ENABLED"])
66+
# Keep this configuration in sync with SConstruct in upstream Godot.
4367

44-
if env.debug_features:
45-
env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED", "DEBUG_METHODS_ENABLED"])
68+
env.editor_build = env["target"] == "editor"
69+
env.dev_build = env["dev_build"]
70+
env.debug_features = env["target"] in ["editor", "template_debug"]
4671

4772
if env.dev_build:
4873
opt_level = "none"
49-
env.AppendUnique(CPPDEFINES=["DEV_ENABLED"])
5074
elif env.debug_features:
5175
opt_level = "speed_trace"
5276
else: # Release
@@ -55,6 +79,26 @@ def generate(env):
5579
env["optimize"] = ARGUMENTS.get("optimize", opt_level)
5680
env["debug_symbols"] = get_cmdline_bool("debug_symbols", env.dev_build)
5781

82+
if env.editor_build:
83+
env.Append(CPPDEFINES=["TOOLS_ENABLED"])
84+
85+
if env.debug_features:
86+
# DEBUG_ENABLED enables debugging *features* and debug-only code, which is intended
87+
# to give *users* extra debugging information for their game development.
88+
env.Append(CPPDEFINES=["DEBUG_ENABLED"])
89+
# In upstream Godot this is added in typedefs.h when DEBUG_ENABLED is set.
90+
env.Append(CPPDEFINES=["DEBUG_METHODS_ENABLED"])
91+
92+
if env.dev_build:
93+
# DEV_ENABLED enables *engine developer* code which should only be compiled for those
94+
# working on the engine itself.
95+
env.Append(CPPDEFINES=["DEV_ENABLED"])
96+
else:
97+
# Disable assert() for production targets (only used in thirdparty code).
98+
env.Append(CPPDEFINES=["NDEBUG"])
99+
100+
# Set optimize and debug_symbols flags.
101+
# "custom" means do nothing and let users set their own optimization flags.
58102
if env.get("is_msvc", False):
59103
if env["debug_symbols"]:
60104
env.Append(CCFLAGS=["/Zi", "/FS"])
@@ -71,13 +115,21 @@ def generate(env):
71115
env.Append(LINKFLAGS=["/OPT:REF"])
72116
elif env["optimize"] == "debug" or env["optimize"] == "none":
73117
env.Append(CCFLAGS=["/Od"])
74-
75118
else:
76119
if env["debug_symbols"]:
120+
# Adding dwarf-4 explicitly makes stacktraces work with clang builds,
121+
# otherwise addr2line doesn't understand them.
122+
env.Append(CCFLAGS=["-gdwarf-4"])
77123
if env.dev_build:
78124
env.Append(CCFLAGS=["-g3"])
79125
else:
80126
env.Append(CCFLAGS=["-g2"])
127+
else:
128+
if using_clang(env) and not is_vanilla_clang(env):
129+
# Apple Clang, its linker doesn't like -s.
130+
env.Append(LINKFLAGS=["-Wl,-S", "-Wl,-x", "-Wl,-dead_strip"])
131+
else:
132+
env.Append(LINKFLAGS=["-s"])
81133

82134
if env["optimize"] == "speed":
83135
env.Append(CCFLAGS=["-O3"])

0 commit comments

Comments
 (0)