detect.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. # Ensure intellisense tools like `compile_commands.json` play nice with MSVC syntax.
  296. env["CPPDEFPREFIX"] = "-D"
  297. env["INCPREFIX"] = "-I"
  298. env.AppendUnique(CPPDEFINES=[("alloca", "_alloca")])
  299. if env["silence_msvc"] and not env.GetOption("clean"):
  300. from tempfile import mkstemp
  301. # Ensure we have a location to write captured output to, in case of false positives.
  302. capture_path = methods.base_folder_path + "platform/windows/msvc_capture.log"
  303. with open(capture_path, "wt", encoding="utf-8"):
  304. pass
  305. old_spawn = env["SPAWN"]
  306. re_redirect_stream = re.compile(r"^[12]?>")
  307. re_cl_capture = re.compile(r"^.+\.(c|cc|cpp|cxx|c[+]{2})$", re.IGNORECASE)
  308. re_link_capture = re.compile(r'\s{3}\S.+\s(?:"[^"]+.lib"|\S+.lib)\s.+\s(?:"[^"]+.exp"|\S+.exp)')
  309. def spawn_capture(sh, escape, cmd, args, env):
  310. # We only care about cl/link, process everything else as normal.
  311. if args[0] not in ["cl", "link"]:
  312. return old_spawn(sh, escape, cmd, args, env)
  313. # Process as normal if the user is manually rerouting output.
  314. for arg in args:
  315. if re_redirect_stream.match(arg):
  316. return old_spawn(sh, escape, cmd, args, env)
  317. tmp_stdout, tmp_stdout_name = mkstemp()
  318. os.close(tmp_stdout)
  319. args.append(f">{tmp_stdout_name}")
  320. ret = old_spawn(sh, escape, cmd, args, env)
  321. try:
  322. with open(tmp_stdout_name, "r", encoding=sys.stdout.encoding, errors="replace") as tmp_stdout:
  323. lines = tmp_stdout.read().splitlines()
  324. os.remove(tmp_stdout_name)
  325. except OSError:
  326. pass
  327. # Early process no lines (OSError)
  328. if not lines:
  329. return ret
  330. is_cl = args[0] == "cl"
  331. content = ""
  332. caught = False
  333. for line in lines:
  334. # These conditions are far from all-encompassing, but are specialized
  335. # for what can be reasonably expected to show up in the repository.
  336. if not caught and (is_cl and re_cl_capture.match(line)) or (not is_cl and re_link_capture.match(line)):
  337. caught = True
  338. try:
  339. with open(capture_path, "a", encoding=sys.stdout.encoding) as log:
  340. log.write(line + "\n")
  341. except OSError:
  342. print_warning(f'Failed to log captured line: "{line}".')
  343. continue
  344. content += line + "\n"
  345. # Content remaining assumed to be an error/warning.
  346. if content:
  347. sys.stderr.write(content)
  348. return ret
  349. env["SPAWN"] = spawn_capture
  350. if env["debug_crt"]:
  351. # Always use dynamic runtime, static debug CRT breaks thread_local.
  352. env.AppendUnique(CCFLAGS=["/MDd"])
  353. else:
  354. if env["use_static_cpp"]:
  355. env.AppendUnique(CCFLAGS=["/MT"])
  356. else:
  357. env.AppendUnique(CCFLAGS=["/MD"])
  358. # MSVC incremental linking is broken and may _increase_ link time (GH-77968).
  359. if not env["incremental_link"]:
  360. env.Append(LINKFLAGS=["/INCREMENTAL:NO"])
  361. if env["arch"] == "x86_32":
  362. env["x86_libtheora_opt_vc"] = True
  363. env.Append(CCFLAGS=["/fp:strict"])
  364. env.AppendUnique(CCFLAGS=["/Gd", "/GR", "/nologo"])
  365. env.AppendUnique(CCFLAGS=["/utf-8"]) # Force to use Unicode encoding.
  366. # Once it was thought that only debug builds would be too large,
  367. # but this has recently stopped being true. See the mingw function
  368. # for notes on why this shouldn't be enabled for gcc
  369. env.AppendUnique(CCFLAGS=["/bigobj"])
  370. if vcvars_msvc_config: # should be automatic if SCons found it
  371. if os.getenv("WindowsSdkDir") is not None:
  372. env.Prepend(CPPPATH=[str(os.getenv("WindowsSdkDir")) + "/Include"])
  373. else:
  374. print_warning("Missing environment variable: WindowsSdkDir")
  375. validate_win_version(env)
  376. env.AppendUnique(
  377. CPPDEFINES=[
  378. "WINDOWS_ENABLED",
  379. "WASAPI_ENABLED",
  380. "WINMIDI_ENABLED",
  381. "TYPED_METHOD_BIND",
  382. "WIN32",
  383. "WINVER=%s" % env["target_win_version"],
  384. "_WIN32_WINNT=%s" % env["target_win_version"],
  385. ]
  386. )
  387. env.AppendUnique(CPPDEFINES=["NOMINMAX"]) # disable bogus min/max WinDef.h macros
  388. if env["arch"] == "x86_64":
  389. env.AppendUnique(CPPDEFINES=["_WIN64"])
  390. # Sanitizers
  391. prebuilt_lib_extra_suffix = ""
  392. if env["use_asan"]:
  393. env.extra_suffix += ".san"
  394. prebuilt_lib_extra_suffix = ".san"
  395. env.AppendUnique(CPPDEFINES=["SANITIZERS_ENABLED"])
  396. env.Append(CCFLAGS=["/fsanitize=address"])
  397. env.Append(LINKFLAGS=["/INFERASANLIBS"])
  398. ## Libs
  399. LIBS = [
  400. "winmm",
  401. "dsound",
  402. "kernel32",
  403. "ole32",
  404. "oleaut32",
  405. "sapi",
  406. "user32",
  407. "gdi32",
  408. "IPHLPAPI",
  409. "Shlwapi",
  410. "wsock32",
  411. "Ws2_32",
  412. "shell32",
  413. "advapi32",
  414. "dinput8",
  415. "dxguid",
  416. "imm32",
  417. "bcrypt",
  418. "Crypt32",
  419. "Avrt",
  420. "dwmapi",
  421. "dwrite",
  422. "wbemuuid",
  423. "ntdll",
  424. ]
  425. if env.debug_features:
  426. LIBS += ["psapi", "dbghelp"]
  427. if env["vulkan"]:
  428. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  429. if not env["use_volk"]:
  430. LIBS += ["vulkan"]
  431. if env["d3d12"]:
  432. check_d3d12_installed(env)
  433. env.AppendUnique(CPPDEFINES=["D3D12_ENABLED", "RD_ENABLED"])
  434. LIBS += ["dxgi", "dxguid"]
  435. LIBS += ["version"] # Mesa dependency.
  436. # Needed for avoiding C1128.
  437. if env["target"] == "release_debug":
  438. env.Append(CXXFLAGS=["/bigobj"])
  439. # PIX
  440. if env["arch"] not in ["x86_64", "arm64"] or env["pix_path"] == "" or not os.path.exists(env["pix_path"]):
  441. env["use_pix"] = False
  442. if env["use_pix"]:
  443. arch_subdir = "arm64" if env["arch"] == "arm64" else "x64"
  444. env.Append(LIBPATH=[env["pix_path"] + "/bin/" + arch_subdir])
  445. LIBS += ["WinPixEventRuntime"]
  446. env.Append(LIBPATH=[env["mesa_libs"] + "/bin"])
  447. LIBS += ["libNIR.windows." + env["arch"] + prebuilt_lib_extra_suffix]
  448. if env["opengl3"]:
  449. env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
  450. if env["angle_libs"] != "":
  451. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  452. env.Append(LIBPATH=[env["angle_libs"]])
  453. LIBS += [
  454. "libANGLE.windows." + env["arch"] + prebuilt_lib_extra_suffix,
  455. "libEGL.windows." + env["arch"] + prebuilt_lib_extra_suffix,
  456. "libGLES.windows." + env["arch"] + prebuilt_lib_extra_suffix,
  457. ]
  458. LIBS += ["dxgi", "d3d9", "d3d11"]
  459. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  460. if env["target"] in ["editor", "template_debug"]:
  461. LIBS += ["psapi", "dbghelp"]
  462. if env["use_llvm"]:
  463. LIBS += [f"clang_rt.builtins-{env['arch']}"]
  464. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  465. if vcvars_msvc_config:
  466. if os.getenv("WindowsSdkDir") is not None:
  467. env.Append(LIBPATH=[str(os.getenv("WindowsSdkDir")) + "/Lib"])
  468. else:
  469. print_warning("Missing environment variable: WindowsSdkDir")
  470. ## LTO
  471. if env["lto"] == "auto": # No LTO by default for MSVC, doesn't help.
  472. env["lto"] = "none"
  473. if env["lto"] != "none":
  474. if env["lto"] == "thin":
  475. if not env["use_llvm"]:
  476. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  477. sys.exit(255)
  478. env.AppendUnique(CCFLAGS=["-flto=thin"])
  479. elif env["use_llvm"]:
  480. env.AppendUnique(CCFLAGS=["-flto"])
  481. else:
  482. env.AppendUnique(CCFLAGS=["/GL"])
  483. if env["progress"]:
  484. env.AppendUnique(LINKFLAGS=["/LTCG:STATUS"])
  485. else:
  486. env.AppendUnique(LINKFLAGS=["/LTCG"])
  487. env.AppendUnique(ARFLAGS=["/LTCG"])
  488. if vcvars_msvc_config:
  489. env.Prepend(CPPPATH=[p for p in str(os.getenv("INCLUDE")).split(";")])
  490. env.Append(LIBPATH=[p for p in str(os.getenv("LIB")).split(";")])
  491. # Incremental linking fix
  492. env["BUILDERS"]["ProgramOriginal"] = env["BUILDERS"]["Program"]
  493. env["BUILDERS"]["Program"] = methods.precious_program
  494. env.Append(LINKFLAGS=["/NATVIS:platform\\windows\\godot.natvis"])
  495. if env["use_asan"]:
  496. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE_SANITIZERS)])
  497. else:
  498. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE)])
  499. def get_ar_version(env):
  500. ret = {
  501. "major": -1,
  502. "minor": -1,
  503. "patch": -1,
  504. "is_llvm": False,
  505. }
  506. try:
  507. output = (
  508. subprocess.check_output([env.subst(env["AR"]), "--version"], shell=(os.name == "nt"))
  509. .strip()
  510. .decode("utf-8")
  511. )
  512. except (subprocess.CalledProcessError, OSError):
  513. print_warning("Couldn't check version of `ar`.")
  514. return ret
  515. match = re.search(r"GNU ar(?: \(GNU Binutils\)| version) (\d+)\.(\d+)(?:\.(\d+))?", output)
  516. if match:
  517. ret["major"] = int(match[1])
  518. ret["minor"] = int(match[2])
  519. if match[3]:
  520. ret["patch"] = int(match[3])
  521. else:
  522. ret["patch"] = 0
  523. return ret
  524. match = re.search(r"LLVM version (\d+)\.(\d+)\.(\d+)", output)
  525. if match:
  526. ret["major"] = int(match[1])
  527. ret["minor"] = int(match[2])
  528. ret["patch"] = int(match[3])
  529. ret["is_llvm"] = True
  530. return ret
  531. print_warning("Couldn't parse version of `ar`.")
  532. return ret
  533. def get_is_ar_thin_supported(env):
  534. """Check whether `ar --thin` is supported. It is only supported since Binutils 2.38 or LLVM 14."""
  535. ar_version = get_ar_version(env)
  536. if ar_version["major"] == -1:
  537. return False
  538. if ar_version["is_llvm"]:
  539. return ar_version["major"] >= 14
  540. if ar_version["major"] == 2:
  541. return ar_version["minor"] >= 38
  542. print_warning("Unknown Binutils `ar` version.")
  543. return False
  544. WINPATHSEP_RE = re.compile(r"\\([^\"'\\]|$)")
  545. def tempfile_arg_esc_func(arg):
  546. from SCons.Subst import quote_spaces
  547. arg = quote_spaces(arg)
  548. # GCC requires double Windows slashes, let's use UNIX separator
  549. return WINPATHSEP_RE.sub(r"/\1", arg)
  550. def configure_mingw(env: "SConsEnvironment"):
  551. # Workaround for MinGW. See:
  552. # https://www.scons.org/wiki/LongCmdLinesOnWin32
  553. env.use_windows_spawn_fix()
  554. # HACK: For some reason, Windows-native shells have their MinGW tools
  555. # frequently fail as a result of parsing path separators incorrectly.
  556. # For some other reason, this issue is circumvented entirely if the
  557. # `mingw_prefix` bin is prepended to PATH.
  558. if os.sep == "\\":
  559. env.PrependENVPath("PATH", os.path.join(env["mingw_prefix"], "bin"))
  560. # In case the command line to AR is too long, use a response file.
  561. env["ARCOM_ORIG"] = env["ARCOM"]
  562. env["ARCOM"] = "${TEMPFILE('$ARCOM_ORIG', '$ARCOMSTR')}"
  563. env["TEMPFILESUFFIX"] = ".rsp"
  564. if os.name == "nt":
  565. env["TEMPFILEARGESCFUNC"] = tempfile_arg_esc_func
  566. ## Build type
  567. if not env["use_llvm"] and not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]):
  568. env["use_llvm"] = True
  569. if env["use_llvm"] and not try_cmd("clang --version", env["mingw_prefix"], env["arch"]):
  570. env["use_llvm"] = False
  571. if not env["use_llvm"] and try_cmd("gcc --version", env["mingw_prefix"], env["arch"], True):
  572. print("Detected GCC to be a wrapper for Clang.")
  573. env["use_llvm"] = True
  574. if 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 get_is_ar_thin_supported(env):
  663. env.Append(ARFLAGS=["--thin"])
  664. env.Append(CPPDEFINES=["WINDOWS_ENABLED", "WASAPI_ENABLED", "WINMIDI_ENABLED"])
  665. env.Append(
  666. CPPDEFINES=[
  667. ("WINVER", env["target_win_version"]),
  668. ("_WIN32_WINNT", env["target_win_version"]),
  669. ]
  670. )
  671. env.Append(
  672. LIBS=[
  673. "mingw32",
  674. "dsound",
  675. "ole32",
  676. "d3d9",
  677. "winmm",
  678. "gdi32",
  679. "iphlpapi",
  680. "shlwapi",
  681. "wsock32",
  682. "ws2_32",
  683. "kernel32",
  684. "oleaut32",
  685. "sapi",
  686. "dinput8",
  687. "dxguid",
  688. "ksuser",
  689. "imm32",
  690. "bcrypt",
  691. "crypt32",
  692. "avrt",
  693. "uuid",
  694. "dwmapi",
  695. "dwrite",
  696. "wbemuuid",
  697. "ntdll",
  698. ]
  699. )
  700. if env.debug_features:
  701. env.Append(LIBS=["psapi", "dbghelp"])
  702. if env["vulkan"]:
  703. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  704. if not env["use_volk"]:
  705. env.Append(LIBS=["vulkan"])
  706. if env["d3d12"]:
  707. check_d3d12_installed(env)
  708. env.AppendUnique(CPPDEFINES=["D3D12_ENABLED", "RD_ENABLED"])
  709. env.Append(LIBS=["dxgi", "dxguid"])
  710. # PIX
  711. if env["arch"] not in ["x86_64", "arm64"] or env["pix_path"] == "" or not os.path.exists(env["pix_path"]):
  712. env["use_pix"] = False
  713. if env["use_pix"]:
  714. arch_subdir = "arm64" if env["arch"] == "arm64" else "x64"
  715. env.Append(LIBPATH=[env["pix_path"] + "/bin/" + arch_subdir])
  716. env.Append(LIBS=["WinPixEventRuntime"])
  717. env.Append(LIBPATH=[env["mesa_libs"] + "/bin"])
  718. env.Append(LIBS=["libNIR.windows." + env["arch"]])
  719. env.Append(LIBS=["version"]) # Mesa dependency.
  720. if env["opengl3"]:
  721. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  722. if env["angle_libs"] != "":
  723. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  724. env.Append(LIBPATH=[env["angle_libs"]])
  725. env.Append(
  726. LIBS=[
  727. "EGL.windows." + env["arch"],
  728. "GLES.windows." + env["arch"],
  729. "ANGLE.windows." + env["arch"],
  730. ]
  731. )
  732. env.Append(LIBS=["dxgi", "d3d9", "d3d11"])
  733. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  734. env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
  735. def configure(env: "SConsEnvironment"):
  736. # Validate arch.
  737. supported_arches = ["x86_32", "x86_64", "arm32", "arm64"]
  738. validate_arch(env["arch"], get_name(), supported_arches)
  739. # At this point the env has been set up with basic tools/compilers.
  740. env.Prepend(CPPPATH=["#platform/windows"])
  741. if os.name == "nt":
  742. env["ENV"] = os.environ # this makes build less repeatable, but simplifies some things
  743. env["ENV"]["TMP"] = os.environ["TMP"]
  744. # First figure out which compiler, version, and target arch we're using
  745. if os.getenv("VCINSTALLDIR") and detect_build_env_arch() and not env["use_mingw"]:
  746. setup_msvc_manual(env)
  747. env.msvc = True
  748. vcvars_msvc_config = True
  749. elif env.get("MSVC_VERSION", "") and not env["use_mingw"]:
  750. setup_msvc_auto(env)
  751. env.msvc = True
  752. vcvars_msvc_config = False
  753. else:
  754. setup_mingw(env)
  755. env.msvc = False
  756. # Now set compiler/linker flags
  757. if env.msvc:
  758. configure_msvc(env, vcvars_msvc_config)
  759. else: # MinGW
  760. configure_mingw(env)
  761. def check_d3d12_installed(env):
  762. if not os.path.exists(env["mesa_libs"]):
  763. print_error(
  764. "The Direct3D 12 rendering driver requires dependencies to be installed.\n"
  765. "You can install them by running `python misc\\scripts\\install_d3d12_sdk_windows.py`.\n"
  766. "See the documentation for more information:\n\t"
  767. "https://docs.godotengine.org/en/latest/contributing/development/compiling/compiling_for_windows.html"
  768. )
  769. sys.exit(255)
  770. def validate_win_version(env):
  771. if int(env["target_win_version"], 16) < 0x0601:
  772. print_error("`target_win_version` should be 0x0601 or higher (Windows 7).")
  773. sys.exit(255)