Skip to content

Commit 3eb39bd

Browse files
committed
Update copy of godot-cpp's build tools
From godot-cpp commit f2b521f55a0a2ba7080418ba898b97028da6134a
1 parent 1106ba6 commit 3eb39bd

File tree

11 files changed

+492
-74
lines changed

11 files changed

+492
-74
lines changed

SConstruct

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ opts.Add(
125125
env['target'] = 'template_release'
126126

127127

128+
# default debug_symbols off
129+
if 'debug_symbols' not in opts.args:
130+
opts.args['debug_symbols'] = 'false'
131+
132+
128133
# Targets flags tool (optimizations, debug symbols)
129134
target_tool = Tool("targets", toolpath=["tools/build/platform"])
130135
target_tool.options(opts)
@@ -139,7 +144,9 @@ build_utils.process_arch(env)
139144

140145

141146
# godot-cpp's linux toolchain config needs this set
142-
env['use_hot_reload'] = False
147+
env.use_hot_reload = False
148+
env['disable_exceptions'] = False
149+
env['symbols_visibility'] = 'hidden'
143150

144151

145152
tool = Tool(env["platform"], toolpath=["tools/build/platform"])

tools/build/platform/android.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
import os
22
import sys
3+
4+
import common_compiler_flags
35
import my_spawn
4-
from SCons.Script import ARGUMENTS
56

67

78
def options(opts):
89
opts.Add(
910
"android_api_level",
1011
"Target Android API level",
11-
"18" if "32" in ARGUMENTS.get("arch", "arm64") else "21",
12+
"21",
1213
)
1314
opts.Add(
14-
"ANDROID_NDK_ROOT",
15-
"Path to your Android NDK installation. By default, uses ANDROID_NDK_ROOT from your defined environment variables.",
16-
os.environ.get("ANDROID_NDK_ROOT", None),
15+
"ANDROID_HOME",
16+
"Path to your Android SDK installation. By default, uses ANDROID_HOME from your defined environment variables.",
17+
os.environ.get("ANDROID_HOME", os.environ.get("ANDROID_SDK_ROOT")),
1718
)
1819

1920

2021
def exists(env):
21-
return "ANDROID_NDK_ROOT" in os.environ or "ANDROID_NDK_ROOT" in ARGUMENTS
22+
return get_android_ndk_root(env) is not None
23+
24+
25+
# This must be kept in sync with the value in https://github.com/godotengine/godot/blob/master/platform/android/detect.py#L58.
26+
def get_ndk_version():
27+
return "23.2.8568313"
28+
29+
30+
def get_android_ndk_root(env):
31+
if env["ANDROID_HOME"]:
32+
return env["ANDROID_HOME"] + "/ndk/" + get_ndk_version()
33+
else:
34+
return os.environ.get("ANDROID_NDK_ROOT")
2235

2336

2437
def generate(env):
25-
if "ANDROID_NDK_ROOT" not in env:
38+
if get_android_ndk_root(env) is None:
2639
raise ValueError(
27-
"To build for Android, ANDROID_NDK_ROOT must be defined. Please set ANDROID_NDK_ROOT to the root folder of your Android NDK installation."
40+
"To build for Android, the path to the NDK must be defined. Please set ANDROID_HOME to the root folder of your Android SDK installation."
2841
)
2942

3043
if env["arch"] not in ("arm64", "x86_64", "arm32", "x86_32"):
@@ -35,14 +48,12 @@ def generate(env):
3548
my_spawn.configure(env)
3649

3750
# Validate API level
38-
api_level = int(env["android_api_level"])
39-
if "64" in env["arch"] and api_level < 21:
40-
print("WARN: 64-bit Android architectures require an API level of at least 21; setting android_api_level=21")
51+
if int(env["android_api_level"]) < 21:
52+
print("WARNING: minimum supported Android target api is 21. Forcing target api 21.")
4153
env["android_api_level"] = "21"
42-
api_level = 21
4354

4455
# Setup toolchain
45-
toolchain = env["ANDROID_NDK_ROOT"] + "/toolchains/llvm/prebuilt/"
56+
toolchain = get_android_ndk_root(env) + "/toolchains/llvm/prebuilt/"
4657
if sys.platform == "win32" or sys.platform == "msys":
4758
toolchain += "windows"
4859
import platform as pltfm
@@ -54,6 +65,12 @@ def generate(env):
5465
elif sys.platform == "darwin":
5566
toolchain += "darwin-x86_64"
5667
env.Append(LINKFLAGS=["-shared"])
68+
69+
if not os.path.exists(toolchain):
70+
print("ERROR: Could not find NDK toolchain at " + toolchain + ".")
71+
print("Make sure NDK version " + get_ndk_version() + " is installed.")
72+
env.Exit(1)
73+
5774
env.PrependENVPath("PATH", toolchain + "/bin") # This does nothing half of the time, but we'll put it here anyways
5875

5976
# Get architecture info
@@ -102,3 +119,5 @@ def generate(env):
102119
env.Append(LINKFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"]])
103120

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

0 commit comments

Comments
 (0)