Skip to content

Commit

Permalink
SCons: List .gen.cpp sources explicitly to avoid globbing errors
Browse files Browse the repository at this point in the history
Whenever we change the name (or remove) generated cpp files with the `.gen.cpp`
extension, users run into build issues when switching between branches (i.e.
switching before and after the name change/removal). This is because we glob
`*.cpp` so if a now-obsolete file from a previous build is present, we'll
include it too, potentially leading to bugs or compilation failure (due to
missing headers or invalid code).

So globbing patterns in `add_source_files` will now skip files ending with
`.gen.cpp`, which should instead be passed explicitly where they're used.

(cherry picked from commit c133480)
  • Loading branch information
akien-mga committed Oct 16, 2021
1 parent f28199f commit a5093d6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ env.core_sources += thirdparty_obj
# Godot source files

env.add_source_files(env.core_sources, "*.cpp")
env.add_source_files(env.core_sources, "script_encryption_key.gen.cpp")

# Certificates
env.Depends(
Expand Down
1 change: 1 addition & 0 deletions editor/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ if env["tools"]:
env.CommandNoCache("#editor/builtin_fonts.gen.h", flist, run_in_subprocess(editor_builders.make_fonts_header))

env.add_source_files(env.editor_sources, "*.cpp")
env.add_source_files(env.editor_sources, "register_exporters.gen.cpp")

SConscript("collada/SCsub")
SConscript("doc/SCsub")
Expand Down
11 changes: 4 additions & 7 deletions main/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ env.main_sources = []
env.add_source_files(env.main_sources, "*.cpp")

# Order matters here. Higher index controller database files write on top of lower index database files.
controller_databases = ["#main/gamecontrollerdb.txt", "#main/godotcontrollerdb.txt"]
controller_databases = ["gamecontrollerdb.txt", "godotcontrollerdb.txt"]

env.Depends("#main/default_controller_mappings.gen.cpp", controller_databases)
env.CommandNoCache(
"#main/default_controller_mappings.gen.cpp",
gensource = env.CommandNoCache(
"default_controller_mappings.gen.cpp",
controller_databases,
run_in_subprocess(main_builders.make_default_controller_mappings),
)

# Don't warn about duplicate entry here, we need it registered manually for first build,
# even if later builds will pick it up twice due to above *.cpp globbing.
env.add_source_files(env.main_sources, "#main/default_controller_mappings.gen.cpp", warn_duplicates=False)
env.add_source_files(env.main_sources, gensource)

env.Depends("#main/splash.gen.h", "#main/splash.png")
env.CommandNoCache("#main/splash.gen.h", "#main/splash.png", run_in_subprocess(main_builders.make_splash))
Expand Down
13 changes: 8 additions & 5 deletions methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from SCons.Variables.BoolVariable import _text2bool


def add_source_files(self, sources, files, warn_duplicates=True):
def add_source_files(self, sources, files):
# Convert string to list of absolute paths (including expanding wildcard)
if isbasestring(files):
# Keep SCons project-absolute path as they are (no wildcard support)
Expand All @@ -21,17 +21,20 @@ def add_source_files(self, sources, files, warn_duplicates=True):
return
files = [files]
else:
# Exclude .gen.cpp files from globbing, to avoid including obsolete ones.
# They should instead be added manually.
skip_gen_cpp = "*" in files
dir_path = self.Dir(".").abspath
files = sorted(glob.glob(dir_path + "/" + files))
if skip_gen_cpp:
files = [f for f in files if not f.endswith(".gen.cpp")]

# Add each path as compiled Object following environment (self) configuration
for path in files:
obj = self.Object(path)
if obj in sources:
if warn_duplicates:
print('WARNING: Object "{}" already included in environment sources.'.format(obj))
else:
continue
print('WARNING: Object "{}" already included in environment sources.'.format(obj))
continue
sources.append(obj)


Expand Down

0 comments on commit a5093d6

Please sign in to comment.