detect.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. import os
  2. import re
  3. import subprocess
  4. import sys
  5. from typing import TYPE_CHECKING
  6. import methods
  7. from methods import print_error, print_warning
  8. from platform_methods import detect_arch, validate_arch
  9. if TYPE_CHECKING:
  10. from SCons.Script.SConscript import SConsEnvironment
  11. # To match other platforms
  12. STACK_SIZE = 8388608
  13. STACK_SIZE_SANITIZERS = 30 * 1024 * 1024
  14. def get_name():
  15. return "Windows"
  16. def try_cmd(test, prefix, arch, check_clang=False):
  17. archs = ["x86_64", "x86_32", "arm64", "arm32"]
  18. if arch:
  19. archs = [arch]
  20. for a in archs:
  21. try:
  22. out = subprocess.Popen(
  23. get_mingw_bin_prefix(prefix, a) + test,
  24. shell=True,
  25. stderr=subprocess.PIPE,
  26. stdout=subprocess.PIPE,
  27. )
  28. outs, errs = out.communicate()
  29. if out.returncode == 0:
  30. if check_clang and not outs.startswith(b"clang"):
  31. return False
  32. return True
  33. except Exception:
  34. pass
  35. return False
  36. def can_build():
  37. if os.name == "nt":
  38. # Building natively on Windows
  39. # If VCINSTALLDIR is set in the OS environ, use traditional Godot logic to set up MSVC
  40. if os.getenv("VCINSTALLDIR"): # MSVC, manual setup
  41. return True
  42. # Otherwise, let SCons find MSVC if installed, or else MinGW.
  43. # Since we're just returning True here, if there's no compiler
  44. # installed, we'll get errors when it tries to build with the
  45. # null compiler.
  46. return True
  47. if os.name == "posix":
  48. # Cross-compiling with MinGW-w64 (old MinGW32 is not supported)
  49. prefix = os.getenv("MINGW_PREFIX", "")
  50. if try_cmd("gcc --version", prefix, "") or try_cmd("clang --version", prefix, ""):
  51. return True
  52. return False
  53. def get_mingw_bin_prefix(prefix, arch):
  54. bin_prefix = (os.path.normpath(os.path.join(prefix, "bin")) + os.sep) if prefix else ""
  55. ARCH_PREFIXES = {
  56. "x86_64": "x86_64-w64-mingw32-",
  57. "x86_32": "i686-w64-mingw32-",
  58. "arm32": "armv7-w64-mingw32-",
  59. "arm64": "aarch64-w64-mingw32-",
  60. }
  61. arch_prefix = ARCH_PREFIXES[arch] if arch else ""
  62. return bin_prefix + arch_prefix
  63. def get_detected(env: "SConsEnvironment", tool: str) -> str:
  64. checks = [
  65. get_mingw_bin_prefix(env["mingw_prefix"], env["arch"]) + tool,
  66. get_mingw_bin_prefix(env["mingw_prefix"], "") + tool,
  67. ]
  68. return str(env.Detect(checks))
  69. def detect_build_env_arch():
  70. msvc_target_aliases = {
  71. "amd64": "x86_64",
  72. "i386": "x86_32",
  73. "i486": "x86_32",
  74. "i586": "x86_32",
  75. "i686": "x86_32",
  76. "x86": "x86_32",
  77. "x64": "x86_64",
  78. "x86_64": "x86_64",
  79. "arm": "arm32",
  80. "arm64": "arm64",
  81. "aarch64": "arm64",
  82. }
  83. if os.getenv("VCINSTALLDIR") or os.getenv("VCTOOLSINSTALLDIR"):
  84. if os.getenv("Platform"):
  85. msvc_arch = os.getenv("Platform").lower()
  86. if msvc_arch in msvc_target_aliases.keys():
  87. return msvc_target_aliases[msvc_arch]
  88. if os.getenv("VSCMD_ARG_TGT_ARCH"):
  89. msvc_arch = os.getenv("VSCMD_ARG_TGT_ARCH").lower()
  90. if msvc_arch in msvc_target_aliases.keys():
  91. return msvc_target_aliases[msvc_arch]
  92. # Pre VS 2017 checks.
  93. if os.getenv("VCINSTALLDIR"):
  94. PATH = os.getenv("PATH").upper()
  95. VCINSTALLDIR = os.getenv("VCINSTALLDIR").upper()
  96. path_arch = {
  97. "BIN\\x86_ARM;": "arm32",
  98. "BIN\\amd64_ARM;": "arm32",
  99. "BIN\\x86_ARM64;": "arm64",
  100. "BIN\\amd64_ARM64;": "arm64",
  101. "BIN\\x86_amd64;": "a86_64",
  102. "BIN\\amd64;": "x86_64",
  103. "BIN\\amd64_x86;": "x86_32",
  104. "BIN;": "x86_32",
  105. }
  106. for path, arch in path_arch.items():
  107. final_path = VCINSTALLDIR + path
  108. if final_path in PATH:
  109. return arch
  110. # VS 2017 and newer.
  111. if os.getenv("VCTOOLSINSTALLDIR"):
  112. host_path_index = os.getenv("PATH").upper().find(os.getenv("VCTOOLSINSTALLDIR").upper() + "BIN\\HOST")
  113. if host_path_index > -1:
  114. first_path_arch = os.getenv("PATH")[host_path_index:].split(";")[0].rsplit("\\", 1)[-1].lower()
  115. if first_path_arch in msvc_target_aliases.keys():
  116. return msvc_target_aliases[first_path_arch]
  117. msys_target_aliases = {
  118. "mingw32": "x86_32",
  119. "mingw64": "x86_64",
  120. "ucrt64": "x86_64",
  121. "clang64": "x86_64",
  122. "clang32": "x86_32",
  123. "clangarm64": "arm64",
  124. }
  125. if os.getenv("MSYSTEM"):
  126. msys_arch = os.getenv("MSYSTEM").lower()
  127. if msys_arch in msys_target_aliases.keys():
  128. return msys_target_aliases[msys_arch]
  129. return ""
  130. def get_opts():
  131. from SCons.Variables import BoolVariable, EnumVariable
  132. mingw = os.getenv("MINGW_PREFIX", "")
  133. # Direct3D 12 SDK dependencies folder.
  134. d3d12_deps_folder = os.getenv("LOCALAPPDATA")
  135. if d3d12_deps_folder:
  136. d3d12_deps_folder = os.path.join(d3d12_deps_folder, "Godot", "build_deps")
  137. else:
  138. # Cross-compiling, the deps install script puts things in `bin`.
  139. # Getting an absolute path to it is a bit hacky in Python.
  140. try:
  141. import inspect
  142. caller_frame = inspect.stack()[1]
  143. caller_script_dir = os.path.dirname(os.path.abspath(caller_frame[1]))
  144. d3d12_deps_folder = os.path.join(caller_script_dir, "bin", "build_deps")
  145. except Exception: # Give up.
  146. d3d12_deps_folder = ""
  147. return [
  148. ("mingw_prefix", "MinGW prefix", mingw),
  149. # Targeted Windows version: 7 (and later), minimum supported version
  150. # XP support dropped after EOL due to missing API for IPv6 and other issues
  151. # Vista support dropped after EOL due to GH-10243
  152. (
  153. "target_win_version",
  154. "Targeted Windows version, >= 0x0601 (Windows 7)",
  155. "0x0601",
  156. ),
  157. EnumVariable("windows_subsystem", "Windows subsystem", "gui", ("gui", "console")),
  158. (
  159. "msvc_version",
  160. "MSVC version to use. Ignored if VCINSTALLDIR is set in shell env.",
  161. None,
  162. ),
  163. BoolVariable("use_mingw", "Use the Mingw compiler, even if MSVC is installed.", False),
  164. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  165. BoolVariable("use_static_cpp", "Link MinGW/MSVC C++ runtime libraries statically", True),
  166. BoolVariable("use_asan", "Use address sanitizer (ASAN)", False),
  167. BoolVariable("use_ubsan", "Use LLVM compiler undefined behavior sanitizer (UBSAN)", False),
  168. BoolVariable("debug_crt", "Compile with MSVC's debug CRT (/MDd)", False),
  169. BoolVariable("incremental_link", "Use MSVC incremental linking. May increase or decrease build times.", False),
  170. BoolVariable("silence_msvc", "Silence MSVC's cl/link stdout bloat, redirecting any errors to stderr.", True),
  171. ("angle_libs", "Path to the ANGLE static libraries", ""),
  172. # Direct3D 12 support.
  173. (
  174. "mesa_libs",
  175. "Path to the MESA/NIR static libraries (required for D3D12)",
  176. os.path.join(d3d12_deps_folder, "mesa"),
  177. ),
  178. (
  179. "agility_sdk_path",
  180. "Path to the Agility SDK distribution (optional for D3D12)",
  181. os.path.join(d3d12_deps_folder, "agility_sdk"),
  182. ),
  183. BoolVariable(
  184. "agility_sdk_multiarch",
  185. "Whether the Agility SDK DLLs will be stored in arch-specific subdirectories",
  186. False,
  187. ),
  188. BoolVariable("use_pix", "Use PIX (Performance tuning and debugging for DirectX 12) runtime", False),
  189. (
  190. "pix_path",
  191. "Path to the PIX runtime distribution (optional for D3D12)",
  192. os.path.join(d3d12_deps_folder, "pix"),
  193. ),
  194. ]
  195. def get_doc_classes():
  196. return [
  197. "EditorExportPlatformWindows",
  198. ]
  199. def get_doc_path():
  200. return "doc_classes"
  201. def get_flags():
  202. arch = detect_build_env_arch() or detect_arch()
  203. return {
  204. "arch": arch,
  205. "supported": ["d3d12", "mono", "xaudio2"],
  206. }
  207. def setup_msvc_manual(env: "SConsEnvironment"):
  208. """Running from VCVARS environment"""
  209. env_arch = detect_build_env_arch()
  210. if env["arch"] != env_arch:
  211. print_error(
  212. "Arch argument (%s) is not matching Native/Cross Compile Tools Prompt/Developer Console (or Visual Studio settings) that is being used to run SCons (%s).\n"
  213. "Run SCons again without arch argument (example: scons p=windows) and SCons will attempt to detect what MSVC compiler will be executed and inform you."
  214. % (env["arch"], env_arch)
  215. )
  216. sys.exit(255)
  217. print("Using VCVARS-determined MSVC, arch %s" % (env_arch))
  218. def setup_msvc_auto(env: "SConsEnvironment"):
  219. """Set up MSVC using SCons's auto-detection logic"""
  220. # If MSVC_VERSION is set by SCons, we know MSVC is installed.
  221. # But we may want a different version or target arch.
  222. # Valid architectures for MSVC's TARGET_ARCH:
  223. # ['amd64', 'emt64', 'i386', 'i486', 'i586', 'i686', 'ia64', 'itanium', 'x86', 'x86_64', 'arm', 'arm64', 'aarch64']
  224. # Our x86_64 and arm64 are the same, and we need to map the 32-bit
  225. # architectures to other names since MSVC isn't as explicit.
  226. # The rest we don't need to worry about because they are
  227. # aliases or aren't supported by Godot (itanium & ia64).
  228. msvc_arch_aliases = {"x86_32": "x86", "arm32": "arm"}
  229. if env["arch"] in msvc_arch_aliases.keys():
  230. env["TARGET_ARCH"] = msvc_arch_aliases[env["arch"]]
  231. else:
  232. env["TARGET_ARCH"] = env["arch"]
  233. # The env may have already been set up with default MSVC tools, so
  234. # reset a few things so we can set it up with the tools we want.
  235. # (Ideally we'd decide on the tool config before configuring any
  236. # environment, and just set the env up once, but this function runs
  237. # on an existing env so this is the simplest way.)
  238. env["MSVC_SETUP_RUN"] = False # Need to set this to re-run the tool
  239. env["MSVS_VERSION"] = None
  240. env["MSVC_VERSION"] = None
  241. if "msvc_version" in env:
  242. env["MSVC_VERSION"] = env["msvc_version"]
  243. env.Tool("msvc")
  244. env.Tool("mssdk") # we want the MS SDK
  245. # Re-add potentially overwritten flags.
  246. env.AppendUnique(CCFLAGS=env.get("ccflags", "").split())
  247. env.AppendUnique(CXXFLAGS=env.get("cxxflags", "").split())
  248. env.AppendUnique(CFLAGS=env.get("cflags", "").split())
  249. env.AppendUnique(RCFLAGS=env.get("rcflags", "").split())
  250. # Note: actual compiler version can be found in env['MSVC_VERSION'], e.g. "14.1" for VS2015
  251. print("Using SCons-detected MSVC version %s, arch %s" % (env["MSVC_VERSION"], env["arch"]))
  252. def setup_mingw(env: "SConsEnvironment"):
  253. """Set up env for use with mingw"""
  254. env_arch = detect_build_env_arch()
  255. if os.getenv("MSYSTEM") == "MSYS":
  256. print_error(
  257. "Running from base MSYS2 console/environment, use target specific environment instead (e.g., mingw32, mingw64, clang32, clang64)."
  258. )
  259. sys.exit(255)
  260. if env_arch != "" and env["arch"] != env_arch:
  261. print_error(
  262. "Arch argument (%s) is not matching MSYS2 console/environment that is being used to run SCons (%s).\n"
  263. "Run SCons again without arch argument (example: scons p=windows) and SCons will attempt to detect what MSYS2 compiler will be executed and inform you."
  264. % (env["arch"], env_arch)
  265. )
  266. sys.exit(255)
  267. if not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]) and not try_cmd(
  268. "clang --version", env["mingw_prefix"], env["arch"]
  269. ):
  270. print_error("No valid compilers found, use MINGW_PREFIX environment variable to set MinGW path.")
  271. sys.exit(255)
  272. env.Tool("mingw")
  273. env.AppendUnique(CCFLAGS=env.get("ccflags", "").split())
  274. env.AppendUnique(RCFLAGS=env.get("rcflags", "").split())
  275. print("Using MinGW, arch %s" % (env["arch"]))
  276. def configure_msvc(env: "SConsEnvironment", vcvars_msvc_config):
  277. """Configure env to work with MSVC"""
  278. ## Build type
  279. # TODO: Re-evaluate the need for this / streamline with common config.
  280. if env["target"] == "template_release":
  281. env.Append(LINKFLAGS=["/ENTRY:mainCRTStartup"])
  282. if env["windows_subsystem"] == "gui":
  283. env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"])
  284. else:
  285. env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"])
  286. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  287. ## Compile/link flags
  288. if env["use_llvm"]:
  289. env["CC"] = "clang-cl"
  290. env["CXX"] = "clang-cl"
  291. env["LINK"] = "lld-link"
  292. env["AR"] = "llvm-lib"
  293. env.AppendUnique(CPPDEFINES=["R128_STDC_ONLY"])
  294. env.extra_suffix = ".llvm" + env.extra_suffix
  295. if env["silence_msvc"] and not env.GetOption("clean"):
  296. from tempfile import mkstemp
  297. # Ensure we have a location to write captured output to, in case of false positives.
  298. capture_path = methods.base_folder_path + "platform/windows/msvc_capture.log"
  299. with open(capture_path, "wt", encoding="utf-8"):
  300. pass
  301. old_spawn = env["SPAWN"]
  302. re_redirect_stream = re.compile(r"^[12]?>")
  303. re_cl_capture = re.compile(r"^.+\.(c|cc|cpp|cxx|c[+]{2})$", re.IGNORECASE)
  304. re_link_capture = re.compile(r'\s{3}\S.+\s(?:"[^"]+.lib"|\S+.lib)\s.+\s(?:"[^"]+.exp"|\S+.exp)')
  305. def spawn_capture(sh, escape, cmd, args, env):
  306. # We only care about cl/link, process everything else as normal.
  307. if args[0] not in ["cl", "link"]:
  308. return old_spawn(sh, escape, cmd, args, env)
  309. # Process as normal if the user is manually rerouting output.
  310. for arg in args:
  311. if re_redirect_stream.match(arg):
  312. return old_spawn(sh, escape, cmd, args, env)
  313. tmp_stdout, tmp_stdout_name = mkstemp()
  314. os.close(tmp_stdout)
  315. args.append(f">{tmp_stdout_name}")
  316. ret = old_spawn(sh, escape, cmd, args, env)
  317. try:
  318. with open(tmp_stdout_name, "r", encoding=sys.stdout.encoding, errors="replace") as tmp_stdout:
  319. lines = tmp_stdout.read().splitlines()
  320. os.remove(tmp_stdout_name)
  321. except OSError:
  322. pass
  323. # Early process no lines (OSError)
  324. if not lines:
  325. return ret
  326. is_cl = args[0] == "cl"
  327. content = ""
  328. caught = False
  329. for line in lines:
  330. # These conditions are far from all-encompassing, but are specialized
  331. # for what can be reasonably expected to show up in the repository.
  332. if not caught and (is_cl and re_cl_capture.match(line)) or (not is_cl and re_link_capture.match(line)):
  333. caught = True
  334. try:
  335. with open(capture_path, "a", encoding=sys.stdout.encoding) as log:
  336. log.write(line + "\n")
  337. except OSError:
  338. print_warning(f'Failed to log captured line: "{line}".')
  339. continue
  340. content += line + "\n"
  341. # Content remaining assumed to be an error/warning.
  342. if content:
  343. sys.stderr.write(content)
  344. return ret
  345. env["SPAWN"] = spawn_capture
  346. if env["debug_crt"]:
  347. # Always use dynamic runtime, static debug CRT breaks thread_local.
  348. env.AppendUnique(CCFLAGS=["/MDd"])
  349. else:
  350. if env["use_static_cpp"]:
  351. env.AppendUnique(CCFLAGS=["/MT"])
  352. else:
  353. env.AppendUnique(CCFLAGS=["/MD"])
  354. # MSVC incremental linking is broken and may _increase_ link time (GH-77968).
  355. if not env["incremental_link"]:
  356. env.Append(LINKFLAGS=["/INCREMENTAL:NO"])
  357. if env["arch"] == "x86_32":
  358. env["x86_libtheora_opt_vc"] = True
  359. env.Append(CCFLAGS=["/fp:strict"])
  360. env.AppendUnique(CCFLAGS=["/Gd", "/GR", "/nologo"])
  361. env.AppendUnique(CCFLAGS=["/utf-8"]) # Force to use Unicode encoding.
  362. # Once it was thought that only debug builds would be too large,
  363. # but this has recently stopped being true. See the mingw function
  364. # for notes on why this shouldn't be enabled for gcc
  365. env.AppendUnique(CCFLAGS=["/bigobj"])
  366. if vcvars_msvc_config: # should be automatic if SCons found it
  367. if os.getenv("WindowsSdkDir") is not None:
  368. env.Prepend(CPPPATH=[str(os.getenv("WindowsSdkDir")) + "/Include"])
  369. else:
  370. print_warning("Missing environment variable: WindowsSdkDir")
  371. validate_win_version(env)
  372. env.AppendUnique(
  373. CPPDEFINES=[
  374. "WINDOWS_ENABLED",
  375. "WASAPI_ENABLED",
  376. "WINMIDI_ENABLED",
  377. "TYPED_METHOD_BIND",
  378. "WIN32",
  379. "WINVER=%s" % env["target_win_version"],
  380. "_WIN32_WINNT=%s" % env["target_win_version"],
  381. ]
  382. )
  383. env.AppendUnique(CPPDEFINES=["NOMINMAX"]) # disable bogus min/max WinDef.h macros
  384. if env["arch"] == "x86_64":
  385. env.AppendUnique(CPPDEFINES=["_WIN64"])
  386. # Sanitizers
  387. prebuilt_lib_extra_suffix = ""
  388. if env["use_asan"]:
  389. env.extra_suffix += ".san"
  390. prebuilt_lib_extra_suffix = ".san"
  391. env.AppendUnique(CPPDEFINES=["SANITIZERS_ENABLED"])
  392. env.Append(CCFLAGS=["/fsanitize=address"])
  393. env.Append(LINKFLAGS=["/INFERASANLIBS"])
  394. ## Libs
  395. LIBS = [
  396. "winmm",
  397. "dsound",
  398. "kernel32",
  399. "ole32",
  400. "oleaut32",
  401. "sapi",
  402. "user32",
  403. "gdi32",
  404. "IPHLPAPI",
  405. "Shlwapi",
  406. "wsock32",
  407. "Ws2_32",
  408. "shell32",
  409. "advapi32",
  410. "dinput8",
  411. "dxguid",
  412. "imm32",
  413. "bcrypt",
  414. "Crypt32",
  415. "Avrt",
  416. "dwmapi",
  417. "dwrite",
  418. "wbemuuid",
  419. "ntdll",
  420. ]
  421. if env.debug_features:
  422. LIBS += ["psapi", "dbghelp"]
  423. if env["vulkan"]:
  424. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  425. if not env["use_volk"]:
  426. LIBS += ["vulkan"]
  427. if env["d3d12"]:
  428. check_d3d12_installed(env)
  429. env.AppendUnique(CPPDEFINES=["D3D12_ENABLED", "RD_ENABLED"])
  430. LIBS += ["dxgi", "dxguid"]
  431. LIBS += ["version"] # Mesa dependency.
  432. # Needed for avoiding C1128.
  433. if env["target"] == "release_debug":
  434. env.Append(CXXFLAGS=["/bigobj"])
  435. # PIX
  436. if env["arch"] not in ["x86_64", "arm64"] or env["pix_path"] == "" or not os.path.exists(env["pix_path"]):
  437. env["use_pix"] = False
  438. if env["use_pix"]:
  439. arch_subdir = "arm64" if env["arch"] == "arm64" else "x64"
  440. env.Append(LIBPATH=[env["pix_path"] + "/bin/" + arch_subdir])
  441. LIBS += ["WinPixEventRuntime"]
  442. env.Append(LIBPATH=[env["mesa_libs"] + "/bin"])
  443. LIBS += ["libNIR.windows." + env["arch"] + prebuilt_lib_extra_suffix]
  444. if env["opengl3"]:
  445. env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
  446. if env["angle_libs"] != "":
  447. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  448. env.Append(LIBPATH=[env["angle_libs"]])
  449. LIBS += [
  450. "libANGLE.windows." + env["arch"] + prebuilt_lib_extra_suffix,
  451. "libEGL.windows." + env["arch"] + prebuilt_lib_extra_suffix,
  452. "libGLES.windows." + env["arch"] + prebuilt_lib_extra_suffix,
  453. ]
  454. LIBS += ["dxgi", "d3d9", "d3d11"]
  455. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  456. if env["target"] in ["editor", "template_debug"]:
  457. LIBS += ["psapi", "dbghelp"]
  458. if env["use_llvm"]:
  459. LIBS += [f"clang_rt.builtins-{env['arch']}"]
  460. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  461. if vcvars_msvc_config:
  462. if os.getenv("WindowsSdkDir") is not None:
  463. env.Append(LIBPATH=[str(os.getenv("WindowsSdkDir")) + "/Lib"])
  464. else:
  465. print_warning("Missing environment variable: WindowsSdkDir")
  466. ## LTO
  467. if env["lto"] == "auto": # No LTO by default for MSVC, doesn't help.
  468. env["lto"] = "none"
  469. if env["lto"] != "none":
  470. if env["lto"] == "thin":
  471. if not env["use_llvm"]:
  472. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  473. sys.exit(255)
  474. env.AppendUnique(CCFLAGS=["-flto=thin"])
  475. elif env["use_llvm"]:
  476. env.AppendUnique(CCFLAGS=["-flto"])
  477. else:
  478. env.AppendUnique(CCFLAGS=["/GL"])
  479. if env["progress"]:
  480. env.AppendUnique(LINKFLAGS=["/LTCG:STATUS"])
  481. else:
  482. env.AppendUnique(LINKFLAGS=["/LTCG"])
  483. env.AppendUnique(ARFLAGS=["/LTCG"])
  484. if vcvars_msvc_config:
  485. env.Prepend(CPPPATH=[p for p in str(os.getenv("INCLUDE")).split(";")])
  486. env.Append(LIBPATH=[p for p in str(os.getenv("LIB")).split(";")])
  487. # Incremental linking fix
  488. env["BUILDERS"]["ProgramOriginal"] = env["BUILDERS"]["Program"]
  489. env["BUILDERS"]["Program"] = methods.precious_program
  490. env.Append(LINKFLAGS=["/NATVIS:platform\\windows\\godot.natvis"])
  491. if env["use_asan"]:
  492. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE_SANITIZERS)])
  493. else:
  494. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE)])
  495. def get_ar_version(env):
  496. ret = {
  497. "major": -1,
  498. "minor": -1,
  499. "patch": -1,
  500. "is_llvm": False,
  501. }
  502. try:
  503. output = (
  504. subprocess.check_output([env.subst(env["AR"]), "--version"], shell=(os.name == "nt"))
  505. .strip()
  506. .decode("utf-8")
  507. )
  508. except (subprocess.CalledProcessError, OSError):
  509. print_warning("Couldn't check version of `ar`.")
  510. return ret
  511. match = re.search(r"GNU ar(?: \(GNU Binutils\)| version) (\d+)\.(\d+)(?:\.(\d+))?", output)
  512. if match:
  513. ret["major"] = int(match[1])
  514. ret["minor"] = int(match[2])
  515. if match[3]:
  516. ret["patch"] = int(match[3])
  517. else:
  518. ret["patch"] = 0
  519. return ret
  520. match = re.search(r"LLVM version (\d+)\.(\d+)\.(\d+)", output)
  521. if match:
  522. ret["major"] = int(match[1])
  523. ret["minor"] = int(match[2])
  524. ret["patch"] = int(match[3])
  525. ret["is_llvm"] = True
  526. return ret
  527. print_warning("Couldn't parse version of `ar`.")
  528. return ret
  529. def get_is_ar_thin_supported(env):
  530. """Check whether `ar --thin` is supported. It is only supported since Binutils 2.38 or LLVM 14."""
  531. ar_version = get_ar_version(env)
  532. if ar_version["major"] == -1:
  533. return False
  534. if ar_version["is_llvm"]:
  535. return ar_version["major"] >= 14
  536. if ar_version["major"] == 2:
  537. return ar_version["minor"] >= 38
  538. print_warning("Unknown Binutils `ar` version.")
  539. return False
  540. WINPATHSEP_RE = re.compile(r"\\([^\"'\\]|$)")
  541. def tempfile_arg_esc_func(arg):
  542. from SCons.Subst import quote_spaces
  543. arg = quote_spaces(arg)
  544. # GCC requires double Windows slashes, let's use UNIX separator
  545. return WINPATHSEP_RE.sub(r"/\1", arg)
  546. def configure_mingw(env: "SConsEnvironment"):
  547. # Workaround for MinGW. See:
  548. # https://www.scons.org/wiki/LongCmdLinesOnWin32
  549. env.use_windows_spawn_fix()
  550. # HACK: For some reason, Windows-native shells have their MinGW tools
  551. # frequently fail as a result of parsing path separators incorrectly.
  552. # For some other reason, this issue is circumvented entirely if the
  553. # `mingw_prefix` bin is prepended to PATH.
  554. if os.sep == "\\":
  555. env.PrependENVPath("PATH", os.path.join(env["mingw_prefix"], "bin"))
  556. # In case the command line to AR is too long, use a response file.
  557. env["ARCOM_ORIG"] = env["ARCOM"]
  558. env["ARCOM"] = "${TEMPFILE('$ARCOM_ORIG', '$ARCOMSTR')}"
  559. env["TEMPFILESUFFIX"] = ".rsp"
  560. if os.name == "nt":
  561. env["TEMPFILEARGESCFUNC"] = tempfile_arg_esc_func
  562. ## Build type
  563. if not env["use_llvm"] and not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]):
  564. env["use_llvm"] = True
  565. if env["use_llvm"] and not try_cmd("clang --version", env["mingw_prefix"], env["arch"]):
  566. env["use_llvm"] = False
  567. if not env["use_llvm"] and try_cmd("gcc --version", env["mingw_prefix"], env["arch"], True):
  568. print("Detected GCC to be a wrapper for Clang.")
  569. env["use_llvm"] = True
  570. # TODO: Re-evaluate the need for this / streamline with common config.
  571. if env["target"] == "template_release":
  572. if env["arch"] != "arm64":
  573. env.Append(CCFLAGS=["-msse2"])
  574. elif env.dev_build:
  575. # Allow big objects. It's supposed not to have drawbacks but seems to break
  576. # GCC LTO, so enabling for debug builds only (which are not built with LTO
  577. # and are the only ones with too big objects).
  578. env.Append(CCFLAGS=["-Wa,-mbig-obj"])
  579. if env["windows_subsystem"] == "gui":
  580. env.Append(LINKFLAGS=["-Wl,--subsystem,windows"])
  581. else:
  582. env.Append(LINKFLAGS=["-Wl,--subsystem,console"])
  583. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  584. ## Compiler configuration
  585. if env["arch"] == "x86_32":
  586. if env["use_static_cpp"]:
  587. env.Append(LINKFLAGS=["-static"])
  588. env.Append(LINKFLAGS=["-static-libgcc"])
  589. env.Append(LINKFLAGS=["-static-libstdc++"])
  590. else:
  591. if env["use_static_cpp"]:
  592. env.Append(LINKFLAGS=["-static"])
  593. if env["arch"] in ["x86_32", "x86_64"]:
  594. env["x86_libtheora_opt_gcc"] = True
  595. env.Append(CCFLAGS=["-ffp-contract=off"])
  596. if env["use_llvm"]:
  597. env["CC"] = get_detected(env, "clang")
  598. env["CXX"] = get_detected(env, "clang++")
  599. env["AR"] = get_detected(env, "ar")
  600. env["RANLIB"] = get_detected(env, "ranlib")
  601. env.Append(ASFLAGS=["-c"])
  602. env.extra_suffix = ".llvm" + env.extra_suffix
  603. else:
  604. env["CC"] = get_detected(env, "gcc")
  605. env["CXX"] = get_detected(env, "g++")
  606. env["AR"] = get_detected(env, "gcc-ar" if os.name != "nt" else "ar")
  607. env["RANLIB"] = get_detected(env, "gcc-ranlib")
  608. env["RC"] = get_detected(env, "windres")
  609. ARCH_TARGETS = {
  610. "x86_32": "pe-i386",
  611. "x86_64": "pe-x86-64",
  612. "arm32": "armv7-w64-mingw32",
  613. "arm64": "aarch64-w64-mingw32",
  614. }
  615. env.AppendUnique(RCFLAGS=f"--target={ARCH_TARGETS[env['arch']]}")
  616. env["AS"] = get_detected(env, "as")
  617. env["OBJCOPY"] = get_detected(env, "objcopy")
  618. env["STRIP"] = get_detected(env, "strip")
  619. ## LTO
  620. if env["lto"] == "auto": # Full LTO for production with MinGW.
  621. env["lto"] = "full"
  622. if env["lto"] != "none":
  623. if env["lto"] == "thin":
  624. if not env["use_llvm"]:
  625. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  626. sys.exit(255)
  627. env.Append(CCFLAGS=["-flto=thin"])
  628. env.Append(LINKFLAGS=["-flto=thin"])
  629. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  630. env.Append(CCFLAGS=["-flto"])
  631. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  632. else:
  633. env.Append(CCFLAGS=["-flto"])
  634. env.Append(LINKFLAGS=["-flto"])
  635. if env["use_asan"]:
  636. env.Append(LINKFLAGS=["-Wl,--stack," + str(STACK_SIZE_SANITIZERS)])
  637. else:
  638. env.Append(LINKFLAGS=["-Wl,--stack," + str(STACK_SIZE)])
  639. ## Compile flags
  640. validate_win_version(env)
  641. if not env["use_llvm"]:
  642. env.Append(CCFLAGS=["-mwindows"])
  643. if env["use_asan"] or env["use_ubsan"]:
  644. if not env["use_llvm"]:
  645. print("GCC does not support sanitizers on Windows.")
  646. sys.exit(255)
  647. if env["arch"] not in ["x86_32", "x86_64"]:
  648. print("Sanitizers are only supported for x86_32 and x86_64.")
  649. sys.exit(255)
  650. env.extra_suffix += ".san"
  651. env.AppendUnique(CPPDEFINES=["SANITIZERS_ENABLED"])
  652. san_flags = []
  653. if env["use_asan"]:
  654. san_flags.append("-fsanitize=address")
  655. if env["use_ubsan"]:
  656. san_flags.append("-fsanitize=undefined")
  657. # Disable the vptr check since it gets triggered on any COM interface calls.
  658. san_flags.append("-fno-sanitize=vptr")
  659. env.Append(CFLAGS=san_flags)
  660. env.Append(CCFLAGS=san_flags)
  661. env.Append(LINKFLAGS=san_flags)
  662. if env["use_llvm"] and os.name == "nt" and methods._colorize:
  663. env.Append(CCFLAGS=["$(-fansi-escape-codes$)", "$(-fcolor-diagnostics$)"])
  664. if get_is_ar_thin_supported(env):
  665. env.Append(ARFLAGS=["--thin"])
  666. env.Append(CPPDEFINES=["WINDOWS_ENABLED", "WASAPI_ENABLED", "WINMIDI_ENABLED"])
  667. env.Append(
  668. CPPDEFINES=[
  669. ("WINVER", env["target_win_version"]),
  670. ("_WIN32_WINNT", env["target_win_version"]),
  671. ]
  672. )
  673. env.Append(
  674. LIBS=[
  675. "mingw32",
  676. "dsound",
  677. "ole32",
  678. "d3d9",
  679. "winmm",
  680. "gdi32",
  681. "iphlpapi",
  682. "shlwapi",
  683. "wsock32",
  684. "ws2_32",
  685. "kernel32",
  686. "oleaut32",
  687. "sapi",
  688. "dinput8",
  689. "dxguid",
  690. "ksuser",
  691. "imm32",
  692. "bcrypt",
  693. "crypt32",
  694. "avrt",
  695. "uuid",
  696. "dwmapi",
  697. "dwrite",
  698. "wbemuuid",
  699. "ntdll",
  700. ]
  701. )
  702. if env.debug_features:
  703. env.Append(LIBS=["psapi", "dbghelp"])
  704. if env["vulkan"]:
  705. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  706. if not env["use_volk"]:
  707. env.Append(LIBS=["vulkan"])
  708. if env["d3d12"]:
  709. check_d3d12_installed(env)
  710. env.AppendUnique(CPPDEFINES=["D3D12_ENABLED", "RD_ENABLED"])
  711. env.Append(LIBS=["dxgi", "dxguid"])
  712. # PIX
  713. if env["arch"] not in ["x86_64", "arm64"] or env["pix_path"] == "" or not os.path.exists(env["pix_path"]):
  714. env["use_pix"] = False
  715. if env["use_pix"]:
  716. arch_subdir = "arm64" if env["arch"] == "arm64" else "x64"
  717. env.Append(LIBPATH=[env["pix_path"] + "/bin/" + arch_subdir])
  718. env.Append(LIBS=["WinPixEventRuntime"])
  719. env.Append(LIBPATH=[env["mesa_libs"] + "/bin"])
  720. env.Append(LIBS=["libNIR.windows." + env["arch"]])
  721. env.Append(LIBS=["version"]) # Mesa dependency.
  722. if env["opengl3"]:
  723. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  724. if env["angle_libs"] != "":
  725. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  726. env.Append(LIBPATH=[env["angle_libs"]])
  727. env.Append(
  728. LIBS=[
  729. "EGL.windows." + env["arch"],
  730. "GLES.windows." + env["arch"],
  731. "ANGLE.windows." + env["arch"],
  732. ]
  733. )
  734. env.Append(LIBS=["dxgi", "d3d9", "d3d11"])
  735. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  736. env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
  737. def configure(env: "SConsEnvironment"):
  738. # Validate arch.
  739. supported_arches = ["x86_32", "x86_64", "arm32", "arm64"]
  740. validate_arch(env["arch"], get_name(), supported_arches)
  741. # At this point the env has been set up with basic tools/compilers.
  742. env.Prepend(CPPPATH=["#platform/windows"])
  743. if os.name == "nt":
  744. env["ENV"] = os.environ # this makes build less repeatable, but simplifies some things
  745. env["ENV"]["TMP"] = os.environ["TMP"]
  746. # First figure out which compiler, version, and target arch we're using
  747. if os.getenv("VCINSTALLDIR") and detect_build_env_arch() and not env["use_mingw"]:
  748. setup_msvc_manual(env)
  749. env.msvc = True
  750. vcvars_msvc_config = True
  751. elif env.get("MSVC_VERSION", "") and not env["use_mingw"]:
  752. setup_msvc_auto(env)
  753. env.msvc = True
  754. vcvars_msvc_config = False
  755. else:
  756. setup_mingw(env)
  757. env.msvc = False
  758. # Now set compiler/linker flags
  759. if env.msvc:
  760. configure_msvc(env, vcvars_msvc_config)
  761. else: # MinGW
  762. configure_mingw(env)
  763. def check_d3d12_installed(env):
  764. if not os.path.exists(env["mesa_libs"]):
  765. print_error(
  766. "The Direct3D 12 rendering driver requires dependencies to be installed.\n"
  767. "You can install them by running `python misc\\scripts\\install_d3d12_sdk_windows.py`.\n"
  768. "See the documentation for more information:\n\t"
  769. "https://docs.godotengine.org/en/latest/contributing/development/compiling/compiling_for_windows.html"
  770. )
  771. sys.exit(255)
  772. def validate_win_version(env):
  773. if int(env["target_win_version"], 16) < 0x0601:
  774. print_error("`target_win_version` should be 0x0601 or higher (Windows 7).")
  775. sys.exit(255)