Skip to content

Commit c20a84e

Browse files
authored
Merge pull request #1601 from Ivorforce/lto
Add lto scons option
2 parents f298ddd + 5f7cf05 commit c20a84e

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed

tools/android.py

+5
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,9 @@ def generate(env):
120120

121121
env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED"])
122122

123+
# Refer to https://github.com/godotengine/godot/blob/master/platform/android/detect.py
124+
# LTO benefits for Android (size, performance) haven't been clearly established yet.
125+
if env["lto"] == "auto":
126+
env["lto"] = "none"
127+
123128
common_compiler_flags.generate(env)

tools/common_compiler_flags.py

+30
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def exists(env):
2222

2323

2424
def generate(env):
25+
assert env["lto"] in ["thin", "full", "none"], "Unrecognized lto: {}".format(env["lto"])
26+
if env["lto"] != "none":
27+
print("Using LTO: " + env["lto"])
28+
2529
# Require C++17
2630
if env.get("is_msvc", False):
2731
env.Append(CXXFLAGS=["/std:c++17"])
@@ -64,6 +68,22 @@ def generate(env):
6468
env.Append(LINKFLAGS=["/OPT:REF"])
6569
elif env["optimize"] == "debug" or env["optimize"] == "none":
6670
env.Append(CCFLAGS=["/Od"])
71+
72+
if env["lto"] == "thin":
73+
if not env["use_llvm"]:
74+
print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
75+
env.Exit(255)
76+
77+
env.Append(CCFLAGS=["-flto=thin"])
78+
env.Append(LINKFLAGS=["-flto=thin"])
79+
elif env["lto"] == "full":
80+
if env["use_llvm"]:
81+
env.Append(CCFLAGS=["-flto"])
82+
env.Append(LINKFLAGS=["-flto"])
83+
else:
84+
env.AppendUnique(CCFLAGS=["/GL"])
85+
env.AppendUnique(ARFLAGS=["/LTCG"])
86+
env.AppendUnique(LINKFLAGS=["/LTCG"])
6787
else:
6888
if env["debug_symbols"]:
6989
# Adding dwarf-4 explicitly makes stacktraces work with clang builds,
@@ -91,3 +111,13 @@ def generate(env):
91111
env.Append(CCFLAGS=["-Og"])
92112
elif env["optimize"] == "none":
93113
env.Append(CCFLAGS=["-O0"])
114+
115+
if env["lto"] == "thin":
116+
if (env["platform"] == "windows" or env["platform"] == "linux") and not env["use_llvm"]:
117+
print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
118+
env.Exit(255)
119+
env.Append(CCFLAGS=["-flto=thin"])
120+
env.Append(LINKFLAGS=["-flto=thin"])
121+
elif env["lto"] == "full":
122+
env.Append(CCFLAGS=["-flto"])
123+
env.Append(LINKFLAGS=["-flto"])

tools/godotcpp.py

+8
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ def options(opts, env):
326326
("none", "custom", "debug", "speed", "speed_trace", "size"),
327327
)
328328
)
329+
opts.Add(
330+
EnumVariable(
331+
"lto",
332+
"Link-time optimization",
333+
"none",
334+
("none", "auto", "thin", "full"),
335+
)
336+
)
329337
opts.Add(BoolVariable("debug_symbols", "Build with debugging symbols", True))
330338
opts.Add(BoolVariable("dev_build", "Developer build with dev-only debugging code (DEV_ENABLED)", False))
331339
opts.Add(BoolVariable("verbose", "Enable verbose output for the compilation", False))

tools/ios.py

+5
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,9 @@ def generate(env):
9797

9898
env.Append(CPPDEFINES=["IOS_ENABLED", "UNIX_ENABLED"])
9999

100+
# Refer to https://github.com/godotengine/godot/blob/master/platform/ios/detect.py:
101+
# Disable by default as it makes linking in Xcode very slow.
102+
if env["lto"] == "auto":
103+
env["lto"] = "none"
104+
100105
common_compiler_flags.generate(env)

tools/linux.py

+4
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ def generate(env):
3939

4040
env.Append(CPPDEFINES=["LINUX_ENABLED", "UNIX_ENABLED"])
4141

42+
# Refer to https://github.com/godotengine/godot/blob/master/platform/linuxbsd/detect.py
43+
if env["lto"] == "auto":
44+
env["lto"] = "full"
45+
4246
common_compiler_flags.generate(env)

tools/macos.py

+5
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ def generate(env):
7373

7474
env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED"])
7575

76+
# Refer to https://github.com/godotengine/godot/blob/master/platform/macos/detect.py
77+
# LTO benefits for macOS (size, performance) haven't been clearly established yet.
78+
if env["lto"] == "auto":
79+
env["lto"] = "none"
80+
7681
common_compiler_flags.generate(env)

tools/web.py

+4
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ def generate(env):
5252

5353
env.Append(CPPDEFINES=["WEB_ENABLED", "UNIX_ENABLED"])
5454

55+
# Refer to https://github.com/godotengine/godot/blob/master/platform/web/detect.py
56+
if env["lto"] == "auto":
57+
env["lto"] = "full"
58+
5559
common_compiler_flags.generate(env)

tools/windows.py

+8
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,12 @@ def generate(env):
198198

199199
env.Append(CPPDEFINES=["WINDOWS_ENABLED"])
200200

201+
# Refer to https://github.com/godotengine/godot/blob/master/platform/windows/detect.py
202+
if env["lto"] == "auto":
203+
if env.get("is_msvc", False):
204+
# No LTO by default for MSVC, doesn't help.
205+
env["lto"] = "none"
206+
else: # Release
207+
env["lto"] = "full"
208+
201209
common_compiler_flags.generate(env)

0 commit comments

Comments
 (0)