Skip to content

Commit 10ed66f

Browse files
committed
SCons: Add emitter to declutter build objects
1 parent 0028fd6 commit 10ed66f

File tree

7 files changed

+50
-11
lines changed

7 files changed

+50
-11
lines changed

SConstruct

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from importlib.util import module_from_spec, spec_from_file_location
1414
from types import ModuleType
1515

1616
from SCons import __version__ as scons_raw_version
17+
from SCons.Builder import ListEmitter
1718

1819
# Explicitly resolve the helper modules, this is done to avoid clash with
1920
# modules of the same name that might be randomly added (e.g. someone adding
@@ -236,6 +237,13 @@ opts.Add(BoolVariable("engine_update_check", "Enable engine update checks in the
236237
opts.Add(BoolVariable("steamapi", "Enable minimal SteamAPI integration for usage time tracking (editor only)", False))
237238
opts.Add("cache_path", "Path to a directory where SCons cache files will be stored. No value disables the cache.", "")
238239
opts.Add("cache_limit", "Max size (in GiB) for the SCons cache. 0 means no limit.", "0")
240+
opts.Add(
241+
BoolVariable(
242+
"redirect_build_objects",
243+
"Enable redirecting built objects/libraries to `bin/obj/` to declutter the repository.",
244+
True,
245+
)
246+
)
239247

240248
# Thirdparty libraries
241249
opts.Add(BoolVariable("builtin_brotli", "Use the built-in Brotli library", True))
@@ -1052,6 +1060,14 @@ if env["ninja"]:
10521060
if env["threads"]:
10531061
env.Append(CPPDEFINES=["THREADS_ENABLED"])
10541062

1063+
# Ensure build objects are put in their own folder if `redirect_build_objects` is enabled.
1064+
env.Prepend(LIBEMITTER=[methods.redirect_emitter])
1065+
env.Prepend(SHLIBEMITTER=[methods.redirect_emitter])
1066+
for key in (emitters := env.StaticObject.builder.emitter):
1067+
emitters[key] = ListEmitter([methods.redirect_emitter] + env.Flatten(emitters[key]))
1068+
for key in (emitters := env.SharedObject.builder.emitter):
1069+
emitters[key] = ListEmitter([methods.redirect_emitter] + env.Flatten(emitters[key]))
1070+
10551071
# Build subdirs, the build order is dependent on link order.
10561072
Export("env")
10571073

methods.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from misc.utility.color import print_error, print_info, print_warning
1717

1818
# Get the "Godot" folder name ahead of time
19-
base_folder_path = str(os.path.abspath(Path(__file__).parent)) + "/"
20-
base_folder_only = os.path.basename(os.path.normpath(base_folder_path))
19+
base_folder = Path(__file__).resolve().parent
2120

2221
compiler_version_cache = None
2322

@@ -83,6 +82,29 @@ def add_source_files(self, sources, files, allow_gen=False):
8382
return True
8483

8584

85+
def redirect_emitter(target, source, env):
86+
"""
87+
Emitter to automatically redirect object/library build files to the `bin/obj` directory,
88+
retaining subfolder structure. External build files will attempt to retain subfolder
89+
structure relative to their environment's parent directory, sorted under `bin/obj/external`.
90+
If `redirect_build_objects` is `False`, or an external build file isn't relative to the
91+
passed environment, this emitter does nothing.
92+
"""
93+
if not env["redirect_build_objects"]:
94+
return target, source
95+
96+
redirected_targets = []
97+
for item in target:
98+
if base_folder in (path := Path(item.get_abspath()).resolve()).parents:
99+
item = env.File(f"#bin/obj/{path.relative_to(base_folder)}")
100+
elif (alt_base := Path(env.Dir(".").get_abspath()).resolve().parent) in path.parents:
101+
item = env.File(f"#bin/obj/external/{path.relative_to(alt_base)}")
102+
else:
103+
print_warning(f'Failed to redirect "{path}"')
104+
redirected_targets.append(item)
105+
return redirected_targets, source
106+
107+
86108
def disable_warnings(self):
87109
# 'self' is the environment
88110
if self.msvc and not using_clang(self):
@@ -150,7 +172,7 @@ def get_version_info(module_version_string="", silent=False):
150172

151173

152174
def get_git_info():
153-
os.chdir(base_folder_path)
175+
os.chdir(base_folder)
154176

155177
# Parse Git hash if we're in a Git repo.
156178
git_hash = ""
@@ -775,7 +797,7 @@ def show_progress(env):
775797
if env["ninja"]:
776798
return
777799

778-
NODE_COUNT_FILENAME = f"{base_folder_path}.scons_node_count"
800+
NODE_COUNT_FILENAME = base_folder / ".scons_node_count"
779801

780802
class ShowProgress:
781803
def __init__(self):

modules/SCsub

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ for name, path in env.module_list.items():
101101
if env["tests"]:
102102

103103
def modules_tests_builder(target, source, env):
104-
headers = sorted([os.path.relpath(src.path, methods.base_folder_path).replace("\\", "/") for src in source])
104+
headers = sorted([os.path.relpath(src.path, methods.base_folder).replace("\\", "/") for src in source])
105105
with methods.generated_wrapper(str(target[0])) as file:
106106
for header in headers:
107107
file.write(f'#include "{header}"\n')

platform/android/SCsub

+2-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ env_thirdparty.disable_warnings()
4444
thirdparty_obj = env_thirdparty.SharedObject("#thirdparty/misc/ifaddrs-android.cc")
4545
android_objects.append(thirdparty_obj)
4646

47-
lib = env_android.add_shared_library("#bin/libgodot", [android_objects], SHLIBSUFFIX=env["SHLIBSUFFIX"])
47+
lib = env_android.add_shared_library("libgodot", android_objects)
4848

4949
# Needed to force rebuilding the platform files when the thirdparty code is updated.
5050
env.Depends(lib, thirdparty_obj)
@@ -78,9 +78,7 @@ if lib_arch_dir != "":
7878
lib_tools_dir = ""
7979

8080
out_dir = "#platform/android/java/lib/libs/" + lib_tools_dir + lib_type_dir + "/" + lib_arch_dir
81-
env_android.Command(
82-
out_dir + "/libgodot_android.so", "#bin/libgodot" + env["SHLIBSUFFIX"], Move("$TARGET", "$SOURCE")
83-
)
81+
env_android.Command(out_dir + "/libgodot_android.so", lib, Move("$TARGET", "$SOURCE"))
8482

8583
stl_lib_path = (
8684
str(env["ANDROID_NDK_ROOT"]) + "/sources/cxx-stl/llvm-libc++/libs/" + lib_arch_dir + "/libc++_shared.so"

platform/web/detect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def library_emitter(target, source, env):
9898

9999

100100
def configure(env: "SConsEnvironment"):
101-
env.Append(LIBEMITTER=library_emitter)
101+
env.Append(LIBEMITTER=[library_emitter])
102102

103103
# Validate arch.
104104
supported_arches = ["wasm32"]

platform/windows/SCsub

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ from pathlib import Path
88

99
import platform_windows_builders
1010

11+
from methods import redirect_emitter
12+
1113
sources = []
1214

1315
common_win = [
@@ -49,6 +51,7 @@ def arrange_program_clean(prog):
4951
Clean(prog, extra_files_to_clean)
5052

5153

54+
env["BUILDERS"]["RES"].emitter = redirect_emitter
5255
res_file = "godot_res.rc"
5356
res_target = "godot_res" + env["OBJSUFFIX"]
5457
res_obj = env.RES(res_target, res_file)

platform/windows/detect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def configure_msvc(env: "SConsEnvironment"):
278278
from tempfile import mkstemp
279279

280280
# Ensure we have a location to write captured output to, in case of false positives.
281-
capture_path = methods.base_folder_path + "platform/windows/msvc_capture.log"
281+
capture_path = methods.base_folder / "platform" / "windows" / "msvc_capture.log"
282282
with open(capture_path, "wt", encoding="utf-8"):
283283
pass
284284

0 commit comments

Comments
 (0)