Skip to content

Commit fa65b70

Browse files
committed
Merge pull request #98105 from Repiteo/scons/mingw-shenanigans
SCons: Extend `MinGW` support & checks
2 parents 8d03a46 + d4dddd0 commit fa65b70

File tree

2 files changed

+51
-88
lines changed

2 files changed

+51
-88
lines changed

platform/windows/detect.py

Lines changed: 48 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ def can_build():
6868

6969

7070
def get_mingw_bin_prefix(prefix, arch):
71-
if not prefix:
72-
mingw_bin_prefix = ""
73-
elif prefix[-1] != "/":
74-
mingw_bin_prefix = prefix + "/bin/"
75-
else:
76-
mingw_bin_prefix = prefix + "bin/"
71+
bin_prefix = (os.path.normpath(os.path.join(prefix, "bin")) + os.sep) if prefix else ""
72+
ARCH_PREFIXES = {
73+
"x86_64": "x86_64-w64-mingw32-",
74+
"x86_32": "i686-w64-mingw32-",
75+
"arm32": "armv7-w64-mingw32-",
76+
"arm64": "aarch64-w64-mingw32-",
77+
}
78+
arch_prefix = ARCH_PREFIXES[arch] if arch else ""
79+
return bin_prefix + arch_prefix
7780

78-
if arch == "x86_64":
79-
mingw_bin_prefix += "x86_64-w64-mingw32-"
80-
elif arch == "x86_32":
81-
mingw_bin_prefix += "i686-w64-mingw32-"
82-
elif arch == "arm32":
83-
mingw_bin_prefix += "armv7-w64-mingw32-"
84-
elif arch == "arm64":
85-
mingw_bin_prefix += "aarch64-w64-mingw32-"
8681

87-
return mingw_bin_prefix
82+
def get_detected(env: "SConsEnvironment", tool: str) -> str:
83+
checks = [
84+
get_mingw_bin_prefix(env["mingw_prefix"], env["arch"]) + tool,
85+
get_mingw_bin_prefix(env["mingw_prefix"], "") + tool,
86+
]
87+
return str(env.Detect(checks))
8888

8989

9090
def detect_build_env_arch():
@@ -245,41 +245,6 @@ def get_flags():
245245
}
246246

247247

248-
def build_res_file(target, source, env: "SConsEnvironment"):
249-
arch_aliases = {
250-
"x86_32": "pe-i386",
251-
"x86_64": "pe-x86-64",
252-
"arm32": "armv7-w64-mingw32",
253-
"arm64": "aarch64-w64-mingw32",
254-
}
255-
cmdbase = "windres --include-dir . --target=" + arch_aliases[env["arch"]]
256-
257-
mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
258-
259-
for x in range(len(source)):
260-
ok = True
261-
# Try prefixed executable (MinGW on Linux).
262-
cmd = mingw_bin_prefix + cmdbase + " -i " + str(source[x]) + " -o " + str(target[x])
263-
try:
264-
out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
265-
if len(out[1]):
266-
ok = False
267-
except Exception:
268-
ok = False
269-
270-
# Try generic executable (MSYS2).
271-
if not ok:
272-
cmd = cmdbase + " -i " + str(source[x]) + " -o " + str(target[x])
273-
try:
274-
out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
275-
if len(out[1]):
276-
return -1
277-
except Exception:
278-
return -1
279-
280-
return 0
281-
282-
283248
def setup_msvc_manual(env: "SConsEnvironment"):
284249
"""Running from VCVARS environment"""
285250

@@ -361,6 +326,10 @@ def setup_mingw(env: "SConsEnvironment"):
361326
print_error("No valid compilers found, use MINGW_PREFIX environment variable to set MinGW path.")
362327
sys.exit(255)
363328

329+
env.Tool("mingw")
330+
env.AppendUnique(CCFLAGS=env.get("ccflags", "").split())
331+
env.AppendUnique(RCFLAGS=env.get("rcflags", "").split())
332+
364333
print("Using MinGW, arch %s" % (env["arch"]))
365334

366335

@@ -706,6 +675,13 @@ def configure_mingw(env: "SConsEnvironment"):
706675
# https://www.scons.org/wiki/LongCmdLinesOnWin32
707676
env.use_windows_spawn_fix()
708677

678+
# HACK: For some reason, Windows-native shells have their MinGW tools
679+
# frequently fail as a result of parsing path separators incorrectly.
680+
# For some other reason, this issue is circumvented entirely if the
681+
# `mingw_prefix` bin is prepended to PATH.
682+
if os.sep == "\\":
683+
env.PrependENVPath("PATH", os.path.join(env["mingw_prefix"], "bin"))
684+
709685
# In case the command line to AR is too long, use a response file.
710686
env["ARCOM_ORIG"] = env["ARCOM"]
711687
env["ARCOM"] = "${TEMPFILE('$ARCOM_ORIG', '$ARCOMSTR')}"
@@ -760,29 +736,31 @@ def configure_mingw(env: "SConsEnvironment"):
760736

761737
env.Append(CCFLAGS=["-ffp-contract=off"])
762738

763-
mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
764-
765739
if env["use_llvm"]:
766-
env["CC"] = mingw_bin_prefix + "clang"
767-
env["CXX"] = mingw_bin_prefix + "clang++"
768-
if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
769-
env["AS"] = mingw_bin_prefix + "as"
770-
env.Append(ASFLAGS=["-c"])
771-
if try_cmd("ar --version", env["mingw_prefix"], env["arch"]):
772-
env["AR"] = mingw_bin_prefix + "ar"
773-
if try_cmd("ranlib --version", env["mingw_prefix"], env["arch"]):
774-
env["RANLIB"] = mingw_bin_prefix + "ranlib"
740+
env["CC"] = get_detected(env, "clang")
741+
env["CXX"] = get_detected(env, "clang++")
742+
env["AR"] = get_detected(env, "ar")
743+
env["RANLIB"] = get_detected(env, "ranlib")
744+
env.Append(ASFLAGS=["-c"])
775745
env.extra_suffix = ".llvm" + env.extra_suffix
776746
else:
777-
env["CC"] = mingw_bin_prefix + "gcc"
778-
env["CXX"] = mingw_bin_prefix + "g++"
779-
if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
780-
env["AS"] = mingw_bin_prefix + "as"
781-
ar = "ar" if os.name == "nt" else "gcc-ar"
782-
if try_cmd(f"{ar} --version", env["mingw_prefix"], env["arch"]):
783-
env["AR"] = mingw_bin_prefix + ar
784-
if try_cmd("gcc-ranlib --version", env["mingw_prefix"], env["arch"]):
785-
env["RANLIB"] = mingw_bin_prefix + "gcc-ranlib"
747+
env["CC"] = get_detected(env, "gcc")
748+
env["CXX"] = get_detected(env, "g++")
749+
env["AR"] = get_detected(env, "gcc-ar" if os.name != "nt" else "ar")
750+
env["RANLIB"] = get_detected(env, "gcc-ranlib")
751+
752+
env["RC"] = get_detected(env, "windres")
753+
ARCH_TARGETS = {
754+
"x86_32": "pe-i386",
755+
"x86_64": "pe-x86-64",
756+
"arm32": "armv7-w64-mingw32",
757+
"arm64": "aarch64-w64-mingw32",
758+
}
759+
env.AppendUnique(RCFLAGS=f"--target={ARCH_TARGETS[env['arch']]}")
760+
761+
env["AS"] = get_detected(env, "as")
762+
env["OBJCOPY"] = get_detected(env, "objcopy")
763+
env["STRIP"] = get_detected(env, "strip")
786764

787765
## LTO
788766

@@ -924,9 +902,6 @@ def configure_mingw(env: "SConsEnvironment"):
924902

925903
env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
926904

927-
# resrc
928-
env.Append(BUILDERS={"RES": env.Builder(action=build_res_file, suffix=".o", src_suffix=".rc")})
929-
930905

931906
def configure(env: "SConsEnvironment"):
932907
# Validate arch.

platform/windows/platform_windows_builders.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@
22

33
import os
44

5-
from detect import get_mingw_bin_prefix, try_cmd
6-
75

86
def make_debug_mingw(target, source, env):
97
dst = str(target[0])
108
# Force separate debug symbols if executable size is larger than 1.9 GB.
119
if env["separate_debug_symbols"] or os.stat(dst).st_size >= 2040109465:
12-
mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
13-
if try_cmd("objcopy --version", env["mingw_prefix"], env["arch"]):
14-
os.system(mingw_bin_prefix + "objcopy --only-keep-debug {0} {0}.debugsymbols".format(dst))
15-
else:
16-
os.system("objcopy --only-keep-debug {0} {0}.debugsymbols".format(dst))
17-
if try_cmd("strip --version", env["mingw_prefix"], env["arch"]):
18-
os.system(mingw_bin_prefix + "strip --strip-debug --strip-unneeded {0}".format(dst))
19-
else:
20-
os.system("strip --strip-debug --strip-unneeded {0}".format(dst))
21-
if try_cmd("objcopy --version", env["mingw_prefix"], env["arch"]):
22-
os.system(mingw_bin_prefix + "objcopy --add-gnu-debuglink={0}.debugsymbols {0}".format(dst))
23-
else:
24-
os.system("objcopy --add-gnu-debuglink={0}.debugsymbols {0}".format(dst))
10+
os.system("{0} --only-keep-debug {1} {1}.debugsymbols".format(env["OBJCOPY"], dst))
11+
os.system("{0} --strip-debug --strip-unneeded {1}".format(env["STRIP"], dst))
12+
os.system("{0} --add-gnu-debuglink={1}.debugsymbols {1}".format(env["OBJCOPY"], dst))

0 commit comments

Comments
 (0)