Skip to content

Commit 27b2ba6

Browse files
committed
Merge pull request #104982 from Repiteo/scons/native-warn-optimize
SCons: Integrate `WARNLEVEL` & `OPTIMIZELEVEL`
2 parents 31a66d3 + 01f0bd3 commit 27b2ba6

File tree

2 files changed

+63
-72
lines changed

2 files changed

+63
-72
lines changed

SConstruct

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ from types import ModuleType
1515

1616
from SCons import __version__ as scons_raw_version
1717
from SCons.Builder import ListEmitter
18-
from SCons.Util import CLVar
1918

2019
# Explicitly resolve the helper modules, this is done to avoid clash with
2120
# modules of the same name that might be randomly added (e.g. someone adding
@@ -451,7 +450,7 @@ env.Prepend(CPPPATH=["#"])
451450

452451
# Allow marking includes as external/system to avoid raising warnings.
453452
env["_CCCOMCOM"] += " $_CPPEXTINCFLAGS"
454-
env["CPPEXTPATH"] = CLVar("")
453+
env["CPPEXTPATH"] = []
455454
if env.scons_version < (4, 2):
456455
env["_CPPEXTINCFLAGS"] = "${_concat(EXTINCPREFIX, CPPEXTPATH, EXTINCSUFFIX, __env__, RDirs, TARGET, SOURCE)}"
457456
else:
@@ -727,78 +726,75 @@ elif methods.using_clang(env) or methods.using_emcc(env):
727726
# Set optimize and debug_symbols flags.
728727
# "custom" means do nothing and let users set their own optimization flags.
729728
# Needs to happen after configure to have `env.msvc` defined.
729+
env.AppendUnique(CCFLAGS=["$OPTIMIZELEVEL"])
730730
if env.msvc:
731731
if env["debug_symbols"]:
732-
env.Append(CCFLAGS=["/Zi", "/FS"])
733-
env.Append(LINKFLAGS=["/DEBUG:FULL"])
732+
env.AppendUnique(CCFLAGS=["/Zi", "/FS"])
733+
env.AppendUnique(LINKFLAGS=["/DEBUG:FULL"])
734734
else:
735-
env.Append(LINKFLAGS=["/DEBUG:NONE"])
735+
env.AppendUnique(LINKFLAGS=["/DEBUG:NONE"])
736736

737737
if env["optimize"].startswith("speed"):
738-
env.Append(CCFLAGS=["/O2"])
739-
env.Append(LINKFLAGS=["/OPT:REF"])
738+
env["OPTIMIZELEVEL"] = "/O2"
739+
env.AppendUnique(LINKFLAGS=["/OPT:REF"])
740740
if env["optimize"] == "speed_trace":
741-
env.Append(LINKFLAGS=["/OPT:NOICF"])
741+
env.AppendUnique(LINKFLAGS=["/OPT:NOICF"])
742742
elif env["optimize"].startswith("size"):
743-
env.Append(CCFLAGS=["/O1"])
744-
env.Append(LINKFLAGS=["/OPT:REF"])
743+
env["OPTIMIZELEVEL"] = "/O1"
744+
env.AppendUnique(LINKFLAGS=["/OPT:REF"])
745745
if env["optimize"] == "size_extra":
746-
env.Append(CPPDEFINES=["SIZE_EXTRA"])
746+
env.AppendUnique(CPPDEFINES=["SIZE_EXTRA"])
747747
elif env["optimize"] == "debug" or env["optimize"] == "none":
748-
env.Append(CCFLAGS=["/Od"])
748+
env["OPTIMIZELEVEL"] = "/Od"
749749
else:
750750
if env["debug_symbols"]:
751751
if env["platform"] == "windows":
752752
if methods.using_clang(env):
753-
env.Append(CCFLAGS=["-gdwarf-4"]) # clang dwarf-5 symbols are broken on Windows.
753+
env.AppendUnique(CCFLAGS=["-gdwarf-4"]) # clang dwarf-5 symbols are broken on Windows.
754754
else:
755-
env.Append(CCFLAGS=["-gdwarf-5"]) # For gcc, only dwarf-5 symbols seem usable by libbacktrace.
755+
env.AppendUnique(CCFLAGS=["-gdwarf-5"]) # For gcc, only dwarf-5 symbols seem usable by libbacktrace.
756756
else:
757757
# Adding dwarf-4 explicitly makes stacktraces work with clang builds,
758758
# otherwise addr2line doesn't understand them
759-
env.Append(CCFLAGS=["-gdwarf-4"])
759+
env.AppendUnique(CCFLAGS=["-gdwarf-4"])
760760
if methods.using_emcc(env):
761761
# Emscripten only produces dwarf symbols when using "-g3".
762-
env.Append(CCFLAGS=["-g3"])
762+
env.AppendUnique(CCFLAGS=["-g3"])
763763
# Emscripten linker needs debug symbols options too.
764-
env.Append(LINKFLAGS=["-gdwarf-4"])
765-
env.Append(LINKFLAGS=["-g3"])
764+
env.AppendUnique(LINKFLAGS=["-gdwarf-4"])
765+
env.AppendUnique(LINKFLAGS=["-g3"])
766766
elif env.dev_build:
767-
env.Append(CCFLAGS=["-g3"])
767+
env.AppendUnique(CCFLAGS=["-g3"])
768768
else:
769-
env.Append(CCFLAGS=["-g2"])
769+
env.AppendUnique(CCFLAGS=["-g2"])
770770
if env["debug_paths_relative"]:
771771
# Remap absolute paths to relative paths for debug symbols.
772772
project_path = Dir("#").abspath
773-
env.Append(CCFLAGS=[f"-ffile-prefix-map={project_path}=."])
773+
env.AppendUnique(CCFLAGS=[f"-ffile-prefix-map={project_path}=."])
774774
else:
775775
if methods.is_apple_clang(env):
776776
# Apple Clang, its linker doesn't like -s.
777-
env.Append(LINKFLAGS=["-Wl,-S", "-Wl,-x", "-Wl,-dead_strip"])
777+
env.AppendUnique(LINKFLAGS=["-Wl,-S", "-Wl,-x", "-Wl,-dead_strip"])
778778
else:
779-
env.Append(LINKFLAGS=["-s"])
779+
env.AppendUnique(LINKFLAGS=["-s"])
780780

781781
# Linker needs optimization flags too, at least for Emscripten.
782782
# For other toolchains, this _may_ be useful for LTO too to disambiguate.
783+
env.AppendUnique(LINKFLAGS=["$OPTIMIZELEVEL"])
783784

784785
if env["optimize"] == "speed":
785-
env.Append(CCFLAGS=["-O3"])
786-
env.Append(LINKFLAGS=["-O3"])
786+
env["OPTIMIZELEVEL"] = "-O3"
787787
# `-O2` is friendlier to debuggers than `-O3`, leading to better crash backtraces.
788788
elif env["optimize"] == "speed_trace":
789-
env.Append(CCFLAGS=["-O2"])
790-
env.Append(LINKFLAGS=["-O2"])
789+
env["OPTIMIZELEVEL"] = "-O2"
791790
elif env["optimize"].startswith("size"):
792-
env.Append(CCFLAGS=["-Os"])
793-
env.Append(LINKFLAGS=["-Os"])
791+
env["OPTIMIZELEVEL"] = "-Os"
794792
if env["optimize"] == "size_extra":
795-
env.Append(CPPDEFINES=["SIZE_EXTRA"])
793+
env.AppendUnique(CPPDEFINES=["SIZE_EXTRA"])
796794
elif env["optimize"] == "debug":
797-
env.Append(CCFLAGS=["-Og"])
798-
env.Append(LINKFLAGS=["-Og"])
795+
env["OPTIMIZELEVEL"] = "-Og"
799796
elif env["optimize"] == "none":
800-
env.Append(CCFLAGS=["-O0"])
801-
env.Append(LINKFLAGS=["-O0"])
797+
env["OPTIMIZELEVEL"] = "-O0"
802798

803799
# Needs to happen after configure to handle "auto".
804800
if env["lto"] != "none":
@@ -839,6 +835,7 @@ elif env.msvc:
839835
env.Append(CXXFLAGS=["/EHsc"])
840836

841837
# Configure compiler warnings
838+
env.AppendUnique(CCFLAGS=["$WARNLEVEL"])
842839
if env.msvc and not methods.using_clang(env): # MSVC
843840
# Disable warnings which we don't plan to fix.
844841
disabled_warnings = [
@@ -856,19 +853,23 @@ if env.msvc and not methods.using_clang(env): # MSVC
856853
]
857854

858855
if env["warnings"] == "extra":
859-
env.Append(CCFLAGS=["/W4"] + disabled_warnings)
856+
env["WARNLEVEL"] = "/W4"
857+
env.AppendUnique(CCFLAGS=disabled_warnings)
860858
elif env["warnings"] == "all":
859+
env["WARNLEVEL"] = "/W3"
861860
# C4458 is like -Wshadow. Part of /W4 but let's apply it for the default /W3 too.
862-
env.Append(CCFLAGS=["/W3", "/w34458"] + disabled_warnings)
861+
env.AppendUnique(CCFLAGS=["/w34458"] + disabled_warnings)
863862
elif env["warnings"] == "moderate":
864-
env.Append(CCFLAGS=["/W2"] + disabled_warnings)
863+
env["WARNLEVEL"] = "/W2"
864+
env.AppendUnique(CCFLAGS=disabled_warnings)
865865
else: # 'no'
866+
env["WARNLEVEL"] = "/w"
866867
# C4267 is particularly finicky & needs to be explicitly disabled.
867-
env.Append(CCFLAGS=["/w", "/wd4267"])
868+
env.AppendUnique(CCFLAGS=["/wd4267"])
868869

869870
if env["werror"]:
870-
env.Append(CCFLAGS=["/WX"])
871-
env.Append(LINKFLAGS=["/WX"])
871+
env.AppendUnique(CCFLAGS=["/WX"])
872+
env.AppendUnique(LINKFLAGS=["/WX"])
872873

873874
else: # GCC, Clang
874875
common_warnings = []
@@ -887,45 +888,47 @@ else: # GCC, Clang
887888
# for putting them in `Set` or `Map`. We don't mind about unreliable ordering.
888889
common_warnings += ["-Wno-ordered-compare-function-pointers"]
889890

890-
# clang-cl will interpret `-Wall` as `-Weverything`, workaround with compatibility cast
891-
W_ALL = "-Wall" if not env.msvc else "-W3"
891+
# clang-cl will interpret `-Wall` as `-Weverything`, workaround with compatibility cast.
892+
env["WARNLEVEL"] = "-Wall" if not env.msvc else "-W3"
892893

893894
if env["warnings"] == "extra":
894-
env.Append(CCFLAGS=[W_ALL, "-Wextra", "-Wwrite-strings", "-Wno-unused-parameter"] + common_warnings)
895-
env.Append(CXXFLAGS=["-Wctor-dtor-privacy", "-Wnon-virtual-dtor"])
895+
env.AppendUnique(CCFLAGS=["-Wextra", "-Wwrite-strings", "-Wno-unused-parameter"] + common_warnings)
896+
env.AppendUnique(CXXFLAGS=["-Wctor-dtor-privacy", "-Wnon-virtual-dtor"])
896897
if methods.using_gcc(env):
897-
env.Append(
898+
env.AppendUnique(
898899
CCFLAGS=[
899900
"-Walloc-zero",
900901
"-Wduplicated-branches",
901902
"-Wduplicated-cond",
902903
"-Wstringop-overflow=4",
903904
]
904905
)
905-
env.Append(CXXFLAGS=["-Wplacement-new=1", "-Wvirtual-inheritance"])
906+
env.AppendUnique(CXXFLAGS=["-Wplacement-new=1", "-Wvirtual-inheritance"])
906907
# Need to fix a warning with AudioServer lambdas before enabling.
907908
# if cc_version_major != 9: # GCC 9 had a regression (GH-36325).
908909
# env.Append(CXXFLAGS=["-Wnoexcept"])
909910
if cc_version_major >= 9:
910-
env.Append(CCFLAGS=["-Wattribute-alias=2"])
911+
env.AppendUnique(CCFLAGS=["-Wattribute-alias=2"])
911912
if cc_version_major >= 11: # Broke on MethodBind templates before GCC 11.
912-
env.Append(CCFLAGS=["-Wlogical-op"])
913+
env.AppendUnique(CCFLAGS=["-Wlogical-op"])
913914
elif methods.using_clang(env) or methods.using_emcc(env):
914-
env.Append(CCFLAGS=["-Wimplicit-fallthrough"])
915+
env.AppendUnique(CCFLAGS=["-Wimplicit-fallthrough"])
915916
elif env["warnings"] == "all":
916-
env.Append(CCFLAGS=[W_ALL] + common_warnings)
917+
env.AppendUnique(CCFLAGS=common_warnings)
917918
elif env["warnings"] == "moderate":
918-
env.Append(CCFLAGS=[W_ALL, "-Wno-unused"] + common_warnings)
919+
env.AppendUnique(CCFLAGS=["-Wno-unused"] + common_warnings)
919920
else: # 'no'
920-
env.Append(CCFLAGS=["-w"])
921+
env["WARNLEVEL"] = "-w"
921922

922923
if env["werror"]:
923-
env.Append(CCFLAGS=["-Werror"])
924+
env.AppendUnique(CCFLAGS=["-Werror"])
924925

925926
# Configure external includes.
926927
if env.msvc:
927-
if cc_version_major < 16 or (cc_version_major == 16 and cc_version_minor < 10):
928-
env.AppendUnique(CCFLAGS=["/experimental:external"])
928+
if not methods.using_clang(env):
929+
if cc_version_major < 16 or (cc_version_major == 16 and cc_version_minor < 10):
930+
env.AppendUnique(CCFLAGS=["/experimental:external"])
931+
env.AppendUnique(CCFLAGS=["/external:anglebrackets"])
929932
env.AppendUnique(CCFLAGS=["/external:W0"])
930933
env["EXTINCPREFIX"] = "/external:I"
931934
env["EXTINCSUFFIX"] = ""

methods.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,19 @@ def redirect_emitter(target, source, env):
108108
def disable_warnings(self):
109109
# 'self' is the environment
110110
if self.msvc and not using_clang(self):
111-
# We have to remove existing warning level defines before appending /w,
112-
# otherwise we get: "warning D9025 : overriding '/W3' with '/w'"
113-
WARN_FLAGS = ["/Wall", "/W4", "/W3", "/W2", "/W1", "/W0"]
114-
self["CCFLAGS"] = [x for x in self["CCFLAGS"] if x not in WARN_FLAGS]
115-
self["CFLAGS"] = [x for x in self["CFLAGS"] if x not in WARN_FLAGS]
116-
self["CXXFLAGS"] = [x for x in self["CXXFLAGS"] if x not in WARN_FLAGS]
117-
self.AppendUnique(CCFLAGS=["/w"])
111+
self["WARNLEVEL"] = "/w"
118112
else:
119-
self.AppendUnique(CCFLAGS=["-w"])
113+
self["WARNLEVEL"] = "-w"
120114

121115

122116
def force_optimization_on_debug(self):
123117
# 'self' is the environment
124118
if self["target"] == "template_release":
125119
return
126-
127-
if self.msvc:
128-
# We have to remove existing optimization level defines before appending /O2,
129-
# otherwise we get: "warning D9025 : overriding '/0d' with '/02'"
130-
self["CCFLAGS"] = [x for x in self["CCFLAGS"] if not x.startswith("/O")]
131-
self["CFLAGS"] = [x for x in self["CFLAGS"] if not x.startswith("/O")]
132-
self["CXXFLAGS"] = [x for x in self["CXXFLAGS"] if not x.startswith("/O")]
133-
self.AppendUnique(CCFLAGS=["/O2"])
120+
elif self.msvc:
121+
self["OPTIMIZELEVEL"] = "/O2"
134122
else:
135-
self.AppendUnique(CCFLAGS=["-O3"])
123+
self["OPTIMIZELEVEL"] = "-O3"
136124

137125

138126
def add_module_version_string(self, s):

0 commit comments

Comments
 (0)