2024-06-20 06:38:54 +00:00
|
|
|
import os
|
2022-06-06 13:09:32 +00:00
|
|
|
import sys
|
2024-06-24 17:20:55 +00:00
|
|
|
|
2024-02-14 14:32:27 +00:00
|
|
|
import common_compiler_flags
|
2024-06-24 17:20:55 +00:00
|
|
|
import my_spawn
|
|
|
|
from SCons.Tool import mingw, msvc
|
|
|
|
from SCons.Variables import BoolVariable
|
2022-06-06 13:09:32 +00:00
|
|
|
|
|
|
|
|
2024-06-25 14:20:59 +00:00
|
|
|
def silence_msvc(env):
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
# Ensure we have a location to write captured output to, in case of false positives.
|
|
|
|
capture_path = os.path.join(os.path.dirname(__file__), "..", "msvc_capture.log")
|
|
|
|
with open(capture_path, "wt", encoding="utf-8"):
|
|
|
|
pass
|
|
|
|
|
|
|
|
old_spawn = env["SPAWN"]
|
|
|
|
re_redirect_stream = re.compile(r"^[12]?>")
|
|
|
|
re_cl_capture = re.compile(r"^.+\.(c|cc|cpp|cxx|c[+]{2})$", re.IGNORECASE)
|
|
|
|
re_link_capture = re.compile(r'\s{3}\S.+\s(?:"[^"]+.lib"|\S+.lib)\s.+\s(?:"[^"]+.exp"|\S+.exp)')
|
|
|
|
|
|
|
|
def spawn_capture(sh, escape, cmd, args, env):
|
|
|
|
# We only care about cl/link, process everything else as normal.
|
|
|
|
if args[0] not in ["cl", "link"]:
|
|
|
|
return old_spawn(sh, escape, cmd, args, env)
|
|
|
|
|
|
|
|
# Process as normal if the user is manually rerouting output.
|
|
|
|
for arg in args:
|
|
|
|
if re_redirect_stream.match(arg):
|
|
|
|
return old_spawn(sh, escape, cmd, args, env)
|
|
|
|
|
|
|
|
tmp_stdout, tmp_stdout_name = tempfile.mkstemp()
|
|
|
|
os.close(tmp_stdout)
|
|
|
|
args.append(f">{tmp_stdout_name}")
|
|
|
|
ret = old_spawn(sh, escape, cmd, args, env)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(tmp_stdout_name, "r", encoding=sys.stdout.encoding, errors="replace") as tmp_stdout:
|
|
|
|
lines = tmp_stdout.read().splitlines()
|
|
|
|
os.remove(tmp_stdout_name)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Early process no lines (OSError)
|
|
|
|
if not lines:
|
|
|
|
return ret
|
|
|
|
|
|
|
|
is_cl = args[0] == "cl"
|
|
|
|
content = ""
|
|
|
|
caught = False
|
|
|
|
for line in lines:
|
|
|
|
# These conditions are far from all-encompassing, but are specialized
|
|
|
|
# for what can be reasonably expected to show up in the repository.
|
|
|
|
if not caught and (is_cl and re_cl_capture.match(line)) or (not is_cl and re_link_capture.match(line)):
|
|
|
|
caught = True
|
|
|
|
try:
|
|
|
|
with open(capture_path, "a", encoding=sys.stdout.encoding) as log:
|
|
|
|
log.write(line + "\n")
|
|
|
|
except OSError:
|
|
|
|
print(f'WARNING: Failed to log captured line: "{line}".')
|
|
|
|
continue
|
|
|
|
content += line + "\n"
|
|
|
|
# Content remaining assumed to be an error/warning.
|
|
|
|
if content:
|
|
|
|
sys.stderr.write(content)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
env["SPAWN"] = spawn_capture
|
|
|
|
|
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
def options(opts):
|
2024-06-20 06:38:54 +00:00
|
|
|
mingw = os.getenv("MINGW_PREFIX", "")
|
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
opts.Add(BoolVariable("use_mingw", "Use the MinGW compiler instead of MSVC - only effective on Windows", False))
|
2022-07-09 14:24:32 +00:00
|
|
|
opts.Add(BoolVariable("use_clang_cl", "Use the clang driver instead of MSVC - only effective on Windows", False))
|
2023-04-02 14:46:40 +00:00
|
|
|
opts.Add(BoolVariable("use_static_cpp", "Link MinGW/MSVC C++ runtime libraries statically", True))
|
2024-06-25 14:20:59 +00:00
|
|
|
opts.Add(BoolVariable("silence_msvc", "Silence MSVC's cl/link stdout bloat, redirecting errors to stderr.", True))
|
2024-06-20 06:38:54 +00:00
|
|
|
opts.Add(BoolVariable("use_llvm", "Use the LLVM compiler", False))
|
|
|
|
opts.Add("mingw_prefix", "MinGW prefix", mingw)
|
2022-06-06 13:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def exists(env):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def generate(env):
|
2022-07-04 15:50:54 +00:00
|
|
|
if not env["use_mingw"] and msvc.exists(env):
|
2022-06-06 13:09:32 +00:00
|
|
|
if env["arch"] == "x86_64":
|
|
|
|
env["TARGET_ARCH"] = "amd64"
|
2024-06-20 06:38:54 +00:00
|
|
|
elif env["arch"] == "arm64":
|
|
|
|
env["TARGET_ARCH"] = "arm64"
|
|
|
|
elif env["arch"] == "arm32":
|
|
|
|
env["TARGET_ARCH"] = "arm"
|
2022-06-06 13:09:32 +00:00
|
|
|
elif env["arch"] == "x86_32":
|
|
|
|
env["TARGET_ARCH"] = "x86"
|
2024-06-20 06:38:54 +00:00
|
|
|
|
|
|
|
env["MSVC_SETUP_RUN"] = False # Need to set this to re-run the tool
|
|
|
|
env["MSVS_VERSION"] = None
|
|
|
|
env["MSVC_VERSION"] = None
|
|
|
|
|
2022-07-04 15:50:54 +00:00
|
|
|
env["is_msvc"] = True
|
2022-12-20 14:22:29 +00:00
|
|
|
|
|
|
|
# MSVC, linker, and archiver.
|
2022-07-04 15:50:54 +00:00
|
|
|
msvc.generate(env)
|
2024-06-20 06:38:54 +00:00
|
|
|
env.Tool("msvc")
|
2022-12-20 14:22:29 +00:00
|
|
|
env.Tool("mslib")
|
|
|
|
env.Tool("mslink")
|
|
|
|
|
2022-09-11 11:12:44 +00:00
|
|
|
env.Append(CPPDEFINES=["TYPED_METHOD_BIND", "NOMINMAX"])
|
2023-08-16 09:47:19 +00:00
|
|
|
env.Append(CCFLAGS=["/utf-8"])
|
2022-06-06 13:09:32 +00:00
|
|
|
env.Append(LINKFLAGS=["/WX"])
|
2022-09-11 17:25:22 +00:00
|
|
|
|
2022-07-09 14:24:32 +00:00
|
|
|
if env["use_clang_cl"]:
|
|
|
|
env["CC"] = "clang-cl"
|
|
|
|
env["CXX"] = "clang-cl"
|
2022-06-06 13:09:32 +00:00
|
|
|
|
2023-04-02 14:46:40 +00:00
|
|
|
if env["use_static_cpp"]:
|
|
|
|
env.Append(CCFLAGS=["/MT"])
|
|
|
|
else:
|
|
|
|
env.Append(CCFLAGS=["/MD"])
|
|
|
|
|
2024-06-25 14:20:59 +00:00
|
|
|
if env["silence_msvc"] and not env.GetOption("clean"):
|
|
|
|
silence_msvc(env)
|
|
|
|
|
2024-06-20 06:38:54 +00:00
|
|
|
elif (sys.platform == "win32" or sys.platform == "msys") and not env["mingw_prefix"]:
|
2022-06-06 13:09:32 +00:00
|
|
|
env["use_mingw"] = True
|
2022-07-04 15:50:54 +00:00
|
|
|
mingw.generate(env)
|
2022-06-06 13:09:32 +00:00
|
|
|
# Don't want lib prefixes
|
|
|
|
env["IMPLIBPREFIX"] = ""
|
|
|
|
env["SHLIBPREFIX"] = ""
|
|
|
|
# Want dll suffix
|
|
|
|
env["SHLIBSUFFIX"] = ".dll"
|
2023-04-02 14:46:40 +00:00
|
|
|
|
|
|
|
env.Append(CCFLAGS=["-Wwrite-strings"])
|
|
|
|
env.Append(LINKFLAGS=["-Wl,--no-undefined"])
|
|
|
|
if env["use_static_cpp"]:
|
|
|
|
env.Append(
|
|
|
|
LINKFLAGS=[
|
|
|
|
"-static",
|
|
|
|
"-static-libgcc",
|
|
|
|
"-static-libstdc++",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
# Long line hack. Use custom spawn, quick AR append (to avoid files with the same names to override each other).
|
|
|
|
my_spawn.configure(env)
|
|
|
|
|
|
|
|
else:
|
|
|
|
env["use_mingw"] = True
|
|
|
|
# Cross-compilation using MinGW
|
2024-06-20 06:38:54 +00:00
|
|
|
prefix = ""
|
|
|
|
if env["mingw_prefix"]:
|
|
|
|
prefix = env["mingw_prefix"] + "/bin/"
|
|
|
|
|
|
|
|
if env["arch"] == "x86_64":
|
|
|
|
prefix += "x86_64"
|
|
|
|
elif env["arch"] == "arm64":
|
|
|
|
prefix += "aarch64"
|
|
|
|
elif env["arch"] == "arm32":
|
|
|
|
prefix += "armv7"
|
|
|
|
elif env["arch"] == "x86_32":
|
|
|
|
prefix += "i686"
|
|
|
|
|
|
|
|
if env["use_llvm"]:
|
|
|
|
env["CXX"] = prefix + "-w64-mingw32-clang++"
|
|
|
|
env["CC"] = prefix + "-w64-mingw32-clang"
|
|
|
|
env["AR"] = prefix + "-w64-mingw32-llvm-ar"
|
|
|
|
env["RANLIB"] = prefix + "-w64-mingw32-ranlib"
|
|
|
|
env["LINK"] = prefix + "-w64-mingw32-clang"
|
|
|
|
else:
|
|
|
|
env["CXX"] = prefix + "-w64-mingw32-g++"
|
|
|
|
env["CC"] = prefix + "-w64-mingw32-gcc"
|
|
|
|
env["AR"] = prefix + "-w64-mingw32-gcc-ar"
|
|
|
|
env["RANLIB"] = prefix + "-w64-mingw32-ranlib"
|
|
|
|
env["LINK"] = prefix + "-w64-mingw32-g++"
|
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
# Want dll suffix
|
|
|
|
env["SHLIBSUFFIX"] = ".dll"
|
|
|
|
|
2023-04-02 14:46:40 +00:00
|
|
|
env.Append(CCFLAGS=["-Wwrite-strings"])
|
|
|
|
env.Append(LINKFLAGS=["-Wl,--no-undefined"])
|
|
|
|
if env["use_static_cpp"]:
|
|
|
|
env.Append(
|
|
|
|
LINKFLAGS=[
|
|
|
|
"-static",
|
|
|
|
"-static-libgcc",
|
|
|
|
"-static-libstdc++",
|
|
|
|
]
|
|
|
|
)
|
2024-06-20 06:38:54 +00:00
|
|
|
if env["use_llvm"]:
|
|
|
|
env.Append(LINKFLAGS=["-lstdc++"])
|
|
|
|
|
|
|
|
if sys.platform == "win32" or sys.platform == "msys":
|
|
|
|
my_spawn.configure(env)
|
2023-07-22 12:46:14 +00:00
|
|
|
|
|
|
|
env.Append(CPPDEFINES=["WINDOWS_ENABLED"])
|
2024-02-14 14:32:27 +00:00
|
|
|
|
2024-09-21 10:52:50 +00:00
|
|
|
# Refer to https://github.com/godotengine/godot/blob/master/platform/windows/detect.py
|
|
|
|
if env["lto"] == "auto":
|
|
|
|
if env.get("is_msvc", False):
|
|
|
|
# No LTO by default for MSVC, doesn't help.
|
|
|
|
env["lto"] = "none"
|
|
|
|
else: # Release
|
|
|
|
env["lto"] = "full"
|
|
|
|
|
2024-02-14 14:32:27 +00:00
|
|
|
common_compiler_flags.generate(env)
|