Skip to content

Commit d4dddd0

Browse files
committed
SCons: Extend MinGW support & checks
1 parent 92e51fc commit d4dddd0

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

@@ -716,6 +685,13 @@ def configure_mingw(env: "SConsEnvironment"):
716685
# https://www.scons.org/wiki/LongCmdLinesOnWin32
717686
env.use_windows_spawn_fix()
718687

688+
# HACK: For some reason, Windows-native shells have their MinGW tools
689+
# frequently fail as a result of parsing path separators incorrectly.
690+
# For some other reason, this issue is circumvented entirely if the
691+
# `mingw_prefix` bin is prepended to PATH.
692+
if os.sep == "\\":
693+
env.PrependENVPath("PATH", os.path.join(env["mingw_prefix"], "bin"))
694+
719695
# In case the command line to AR is too long, use a response file.
720696
env["ARCOM_ORIG"] = env["ARCOM"]
721697
env["ARCOM"] = "${TEMPFILE('$ARCOM_ORIG', '$ARCOMSTR')}"
@@ -770,29 +746,31 @@ def configure_mingw(env: "SConsEnvironment"):
770746

771747
env.Append(CCFLAGS=["-ffp-contract=off"])
772748

773-
mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
774-
775749
if env["use_llvm"]:
776-
env["CC"] = mingw_bin_prefix + "clang"
777-
env["CXX"] = mingw_bin_prefix + "clang++"
778-
if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
779-
env["AS"] = mingw_bin_prefix + "as"
780-
env.Append(ASFLAGS=["-c"])
781-
if try_cmd("ar --version", env["mingw_prefix"], env["arch"]):
782-
env["AR"] = mingw_bin_prefix + "ar"
783-
if try_cmd("ranlib --version", env["mingw_prefix"], env["arch"]):
784-
env["RANLIB"] = mingw_bin_prefix + "ranlib"
750+
env["CC"] = get_detected(env, "clang")
751+
env["CXX"] = get_detected(env, "clang++")
752+
env["AR"] = get_detected(env, "ar")
753+
env["RANLIB"] = get_detected(env, "ranlib")
754+
env.Append(ASFLAGS=["-c"])
785755
env.extra_suffix = ".llvm" + env.extra_suffix
786756
else:
787-
env["CC"] = mingw_bin_prefix + "gcc"
788-
env["CXX"] = mingw_bin_prefix + "g++"
789-
if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
790-
env["AS"] = mingw_bin_prefix + "as"
791-
ar = "ar" if os.name == "nt" else "gcc-ar"
792-
if try_cmd(f"{ar} --version", env["mingw_prefix"], env["arch"]):
793-
env["AR"] = mingw_bin_prefix + ar
794-
if try_cmd("gcc-ranlib --version", env["mingw_prefix"], env["arch"]):
795-
env["RANLIB"] = mingw_bin_prefix + "gcc-ranlib"
757+
env["CC"] = get_detected(env, "gcc")
758+
env["CXX"] = get_detected(env, "g++")
759+
env["AR"] = get_detected(env, "gcc-ar" if os.name != "nt" else "ar")
760+
env["RANLIB"] = get_detected(env, "gcc-ranlib")
761+
762+
env["RC"] = get_detected(env, "windres")
763+
ARCH_TARGETS = {
764+
"x86_32": "pe-i386",
765+
"x86_64": "pe-x86-64",
766+
"arm32": "armv7-w64-mingw32",
767+
"arm64": "aarch64-w64-mingw32",
768+
}
769+
env.AppendUnique(RCFLAGS=f"--target={ARCH_TARGETS[env['arch']]}")
770+
771+
env["AS"] = get_detected(env, "as")
772+
env["OBJCOPY"] = get_detected(env, "objcopy")
773+
env["STRIP"] = get_detected(env, "strip")
796774

797775
## LTO
798776

@@ -944,9 +922,6 @@ def configure_mingw(env: "SConsEnvironment"):
944922

945923
env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
946924

947-
# resrc
948-
env.Append(BUILDERS={"RES": env.Builder(action=build_res_file, suffix=".o", src_suffix=".rc")})
949-
950925

951926
def configure(env: "SConsEnvironment"):
952927
# 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)