detect.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. import methods
  2. import os
  3. import subprocess
  4. import sys
  5. from platform_methods import detect_arch
  6. from typing import TYPE_CHECKING
  7. if TYPE_CHECKING:
  8. from SCons import Environment
  9. # To match other platforms
  10. STACK_SIZE = 8388608
  11. def is_active():
  12. return True
  13. def get_name():
  14. return "Windows"
  15. def try_cmd(test, prefix, arch):
  16. if arch:
  17. try:
  18. out = subprocess.Popen(
  19. get_mingw_bin_prefix(prefix, arch) + test,
  20. shell=True,
  21. stderr=subprocess.PIPE,
  22. stdout=subprocess.PIPE,
  23. )
  24. out.communicate()
  25. if out.returncode == 0:
  26. return True
  27. except Exception:
  28. pass
  29. else:
  30. for a in ["x86_64", "x86_32", "arm64", "arm32"]:
  31. try:
  32. out = subprocess.Popen(
  33. get_mingw_bin_prefix(prefix, a) + test,
  34. shell=True,
  35. stderr=subprocess.PIPE,
  36. stdout=subprocess.PIPE,
  37. )
  38. out.communicate()
  39. if out.returncode == 0:
  40. return True
  41. except Exception:
  42. pass
  43. return False
  44. def can_build():
  45. if os.name == "nt":
  46. # Building natively on Windows
  47. # If VCINSTALLDIR is set in the OS environ, use traditional Godot logic to set up MSVC
  48. if os.getenv("VCINSTALLDIR"): # MSVC, manual setup
  49. return True
  50. # Otherwise, let SCons find MSVC if installed, or else MinGW.
  51. # Since we're just returning True here, if there's no compiler
  52. # installed, we'll get errors when it tries to build with the
  53. # null compiler.
  54. return True
  55. if os.name == "posix":
  56. # Cross-compiling with MinGW-w64 (old MinGW32 is not supported)
  57. prefix = os.getenv("MINGW_PREFIX", "")
  58. if try_cmd("gcc --version", prefix, "") or try_cmd("clang --version", prefix, ""):
  59. return True
  60. return False
  61. def get_mingw_bin_prefix(prefix, arch):
  62. if not prefix:
  63. mingw_bin_prefix = ""
  64. elif prefix[-1] != "/":
  65. mingw_bin_prefix = prefix + "/bin/"
  66. else:
  67. mingw_bin_prefix = prefix + "bin/"
  68. if arch == "x86_64":
  69. mingw_bin_prefix += "x86_64-w64-mingw32-"
  70. elif arch == "x86_32":
  71. mingw_bin_prefix += "i686-w64-mingw32-"
  72. elif arch == "arm32":
  73. mingw_bin_prefix += "armv7-w64-mingw32-"
  74. elif arch == "arm64":
  75. mingw_bin_prefix += "aarch64-w64-mingw32-"
  76. return mingw_bin_prefix
  77. def detect_build_env_arch():
  78. msvc_target_aliases = {
  79. "amd64": "x86_64",
  80. "i386": "x86_32",
  81. "i486": "x86_32",
  82. "i586": "x86_32",
  83. "i686": "x86_32",
  84. "x86": "x86_32",
  85. "x64": "x86_64",
  86. "x86_64": "x86_64",
  87. "arm": "arm32",
  88. "arm64": "arm64",
  89. "aarch64": "arm64",
  90. }
  91. if os.getenv("VCINSTALLDIR") or os.getenv("VCTOOLSINSTALLDIR"):
  92. if os.getenv("Platform"):
  93. msvc_arch = os.getenv("Platform").lower()
  94. if msvc_arch in msvc_target_aliases.keys():
  95. return msvc_target_aliases[msvc_arch]
  96. if os.getenv("VSCMD_ARG_TGT_ARCH"):
  97. msvc_arch = os.getenv("VSCMD_ARG_TGT_ARCH").lower()
  98. if msvc_arch in msvc_target_aliases.keys():
  99. return msvc_target_aliases[msvc_arch]
  100. # Pre VS 2017 checks.
  101. if os.getenv("VCINSTALLDIR"):
  102. PATH = os.getenv("PATH").upper()
  103. VCINSTALLDIR = os.getenv("VCINSTALLDIR").upper()
  104. path_arch = {
  105. "BIN\\x86_ARM;": "arm32",
  106. "BIN\\amd64_ARM;": "arm32",
  107. "BIN\\x86_ARM64;": "arm64",
  108. "BIN\\amd64_ARM64;": "arm64",
  109. "BIN\\x86_amd64;": "a86_64",
  110. "BIN\\amd64;": "x86_64",
  111. "BIN\\amd64_x86;": "x86_32",
  112. "BIN;": "x86_32",
  113. }
  114. for path, arch in path_arch.items():
  115. final_path = VCINSTALLDIR + path
  116. if final_path in PATH:
  117. return arch
  118. # VS 2017 and newer.
  119. if os.getenv("VCTOOLSINSTALLDIR"):
  120. host_path_index = os.getenv("PATH").upper().find(os.getenv("VCTOOLSINSTALLDIR").upper() + "BIN\\HOST")
  121. if host_path_index > -1:
  122. first_path_arch = os.getenv("PATH").split(";")[0].rsplit("\\", 1)[-1].lower()
  123. return msvc_target_aliases[first_path_arch]
  124. msys_target_aliases = {
  125. "mingw32": "x86_32",
  126. "mingw64": "x86_64",
  127. "ucrt64": "x86_64",
  128. "clang64": "x86_64",
  129. "clang32": "x86_32",
  130. "clangarm64": "arm64",
  131. }
  132. if os.getenv("MSYSTEM"):
  133. msys_arch = os.getenv("MSYSTEM").lower()
  134. if msys_arch in msys_target_aliases.keys():
  135. return msys_target_aliases[msys_arch]
  136. return ""
  137. def get_opts():
  138. from SCons.Variables import BoolVariable, EnumVariable
  139. mingw = os.getenv("MINGW_PREFIX", "")
  140. return [
  141. ("mingw_prefix", "MinGW prefix", mingw),
  142. # Targeted Windows version: 7 (and later), minimum supported version
  143. # XP support dropped after EOL due to missing API for IPv6 and other issues
  144. # Vista support dropped after EOL due to GH-10243
  145. (
  146. "target_win_version",
  147. "Targeted Windows version, >= 0x0601 (Windows 7)",
  148. "0x0601",
  149. ),
  150. EnumVariable("windows_subsystem", "Windows subsystem", "gui", ("gui", "console")),
  151. (
  152. "msvc_version",
  153. "MSVC version to use. Ignored if VCINSTALLDIR is set in shell env.",
  154. None,
  155. ),
  156. BoolVariable("use_mingw", "Use the Mingw compiler, even if MSVC is installed.", False),
  157. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  158. BoolVariable("use_static_cpp", "Link MinGW/MSVC C++ runtime libraries statically", True),
  159. BoolVariable("use_asan", "Use address sanitizer (ASAN)", False),
  160. BoolVariable("debug_crt", "Compile with MSVC's debug CRT (/MDd)", False),
  161. ]
  162. def get_doc_classes():
  163. return [
  164. "EditorExportPlatformWindows",
  165. ]
  166. def get_doc_path():
  167. return "doc_classes"
  168. def get_flags():
  169. arch = detect_build_env_arch() or detect_arch()
  170. return [
  171. ("arch", arch),
  172. ]
  173. def build_res_file(target, source, env):
  174. arch_aliases = {
  175. "x86_32": "pe-i386",
  176. "x86_64": "pe-x86-64",
  177. "arm32": "armv7-w64-mingw32",
  178. "arm64": "aarch64-w64-mingw32",
  179. }
  180. cmdbase = "windres --include-dir . --target=" + arch_aliases[env["arch"]]
  181. mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
  182. for x in range(len(source)):
  183. ok = True
  184. # Try prefixed executable (MinGW on Linux).
  185. cmd = mingw_bin_prefix + cmdbase + " -i " + str(source[x]) + " -o " + str(target[x])
  186. try:
  187. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  188. if len(out[1]):
  189. ok = False
  190. except Exception:
  191. ok = False
  192. # Try generic executable (MSYS2).
  193. if not ok:
  194. cmd = cmdbase + " -i " + str(source[x]) + " -o " + str(target[x])
  195. try:
  196. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  197. if len(out[1]):
  198. return -1
  199. except Exception:
  200. return -1
  201. return 0
  202. def setup_msvc_manual(env):
  203. """Running from VCVARS environment"""
  204. env_arch = detect_build_env_arch()
  205. if env["arch"] != env_arch:
  206. print(
  207. """
  208. 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).
  209. 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.
  210. """
  211. % (env["arch"], env_arch)
  212. )
  213. sys.exit(200)
  214. print("Found MSVC, arch %s" % (env_arch))
  215. def setup_msvc_auto(env):
  216. """Set up MSVC using SCons's auto-detection logic"""
  217. # If MSVC_VERSION is set by SCons, we know MSVC is installed.
  218. # But we may want a different version or target arch.
  219. # Valid architectures for MSVC's TARGET_ARCH:
  220. # ['amd64', 'emt64', 'i386', 'i486', 'i586', 'i686', 'ia64', 'itanium', 'x86', 'x86_64', 'arm', 'arm64', 'aarch64']
  221. # Our x86_64 and arm64 are the same, and we need to map the 32-bit
  222. # architectures to other names since MSVC isn't as explicit.
  223. # The rest we don't need to worry about because they are
  224. # aliases or aren't supported by Godot (itanium & ia64).
  225. msvc_arch_aliases = {"x86_32": "x86", "arm32": "arm"}
  226. if env["arch"] in msvc_arch_aliases.keys():
  227. env["TARGET_ARCH"] = msvc_arch_aliases[env["arch"]]
  228. else:
  229. env["TARGET_ARCH"] = env["arch"]
  230. # The env may have already been set up with default MSVC tools, so
  231. # reset a few things so we can set it up with the tools we want.
  232. # (Ideally we'd decide on the tool config before configuring any
  233. # environment, and just set the env up once, but this function runs
  234. # on an existing env so this is the simplest way.)
  235. env["MSVC_SETUP_RUN"] = False # Need to set this to re-run the tool
  236. env["MSVS_VERSION"] = None
  237. env["MSVC_VERSION"] = None
  238. if "msvc_version" in env:
  239. env["MSVC_VERSION"] = env["msvc_version"]
  240. env.Tool("msvc")
  241. env.Tool("mssdk") # we want the MS SDK
  242. # Note: actual compiler version can be found in env['MSVC_VERSION'], e.g. "14.1" for VS2015
  243. print("Found MSVC version %s, arch %s" % (env["MSVC_VERSION"], env["arch"]))
  244. def setup_mingw(env):
  245. """Set up env for use with mingw"""
  246. env_arch = detect_build_env_arch()
  247. if os.getenv("MSYSTEM") == "MSYS":
  248. print(
  249. """
  250. Running from base MSYS2 console/environment, use target specific environment instead (e.g., mingw32, mingw64, clang32, clang64).
  251. """
  252. )
  253. sys.exit(201)
  254. if env_arch != "" and env["arch"] != env_arch:
  255. print(
  256. """
  257. Arch argument (%s) is not matching MSYS2 console/environment that is being used to run SCons (%s).
  258. 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.
  259. """
  260. % (env["arch"], env_arch)
  261. )
  262. sys.exit(202)
  263. if not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]) and not try_cmd(
  264. "clang --version", env["mingw_prefix"], env["arch"]
  265. ):
  266. print(
  267. """
  268. No valid compilers found, use MINGW_PREFIX environment variable to set MinGW path.
  269. """
  270. )
  271. sys.exit(202)
  272. print("Using MinGW, arch %s" % (env["arch"]))
  273. def configure_msvc(env, vcvars_msvc_config):
  274. """Configure env to work with MSVC"""
  275. ## Build type
  276. # TODO: Re-evaluate the need for this / streamline with common config.
  277. if env["target"] == "template_release":
  278. env.Append(LINKFLAGS=["/ENTRY:mainCRTStartup"])
  279. if env["windows_subsystem"] == "gui":
  280. env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"])
  281. else:
  282. env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"])
  283. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  284. ## Compile/link flags
  285. if env["debug_crt"]:
  286. # Always use dynamic runtime, static debug CRT breaks thread_local.
  287. env.AppendUnique(CCFLAGS=["/MDd"])
  288. else:
  289. if env["use_static_cpp"]:
  290. env.AppendUnique(CCFLAGS=["/MT"])
  291. else:
  292. env.AppendUnique(CCFLAGS=["/MD"])
  293. if env["arch"] == "x86_32":
  294. env["x86_libtheora_opt_vc"] = True
  295. env.AppendUnique(CCFLAGS=["/Gd", "/GR", "/nologo"])
  296. env.AppendUnique(CCFLAGS=["/utf-8"]) # Force to use Unicode encoding.
  297. env.AppendUnique(CXXFLAGS=["/TP"]) # assume all sources are C++
  298. # Once it was thought that only debug builds would be too large,
  299. # but this has recently stopped being true. See the mingw function
  300. # for notes on why this shouldn't be enabled for gcc
  301. env.AppendUnique(CCFLAGS=["/bigobj"])
  302. if vcvars_msvc_config: # should be automatic if SCons found it
  303. if os.getenv("WindowsSdkDir") is not None:
  304. env.Prepend(CPPPATH=[os.getenv("WindowsSdkDir") + "/Include"])
  305. else:
  306. print("Missing environment variable: WindowsSdkDir")
  307. env.AppendUnique(
  308. CPPDEFINES=[
  309. "WINDOWS_ENABLED",
  310. "WASAPI_ENABLED",
  311. "WINMIDI_ENABLED",
  312. "TYPED_METHOD_BIND",
  313. "WIN32",
  314. "MSVC",
  315. "WINVER=%s" % env["target_win_version"],
  316. "_WIN32_WINNT=%s" % env["target_win_version"],
  317. ]
  318. )
  319. env.AppendUnique(CPPDEFINES=["NOMINMAX"]) # disable bogus min/max WinDef.h macros
  320. if env["arch"] == "x86_64":
  321. env.AppendUnique(CPPDEFINES=["_WIN64"])
  322. ## Libs
  323. LIBS = [
  324. "winmm",
  325. "dsound",
  326. "kernel32",
  327. "ole32",
  328. "oleaut32",
  329. "sapi",
  330. "user32",
  331. "gdi32",
  332. "IPHLPAPI",
  333. "Shlwapi",
  334. "wsock32",
  335. "Ws2_32",
  336. "shell32",
  337. "advapi32",
  338. "dinput8",
  339. "dxguid",
  340. "imm32",
  341. "bcrypt",
  342. "Avrt",
  343. "dwmapi",
  344. "dwrite",
  345. "wbemuuid",
  346. ]
  347. if env.debug_features:
  348. LIBS += ["psapi", "dbghelp"]
  349. if env["vulkan"]:
  350. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED"])
  351. if not env["use_volk"]:
  352. LIBS += ["vulkan"]
  353. if env["opengl3"]:
  354. env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
  355. LIBS += ["opengl32"]
  356. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  357. if vcvars_msvc_config:
  358. if os.getenv("WindowsSdkDir") is not None:
  359. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  360. else:
  361. print("Missing environment variable: WindowsSdkDir")
  362. ## LTO
  363. if env["lto"] == "auto": # No LTO by default for MSVC, doesn't help.
  364. env["lto"] = "none"
  365. if env["lto"] != "none":
  366. if env["lto"] == "thin":
  367. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  368. sys.exit(255)
  369. env.AppendUnique(CCFLAGS=["/GL"])
  370. env.AppendUnique(ARFLAGS=["/LTCG"])
  371. if env["progress"]:
  372. env.AppendUnique(LINKFLAGS=["/LTCG:STATUS"])
  373. else:
  374. env.AppendUnique(LINKFLAGS=["/LTCG"])
  375. if vcvars_msvc_config:
  376. env.Prepend(CPPPATH=[p for p in os.getenv("INCLUDE").split(";")])
  377. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  378. # Sanitizers
  379. if env["use_asan"]:
  380. env.extra_suffix += ".san"
  381. env.Append(LINKFLAGS=["/INFERASANLIBS"])
  382. env.Append(CCFLAGS=["/fsanitize=address"])
  383. # Incremental linking fix
  384. env["BUILDERS"]["ProgramOriginal"] = env["BUILDERS"]["Program"]
  385. env["BUILDERS"]["Program"] = methods.precious_program
  386. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE)])
  387. def configure_mingw(env):
  388. # Workaround for MinGW. See:
  389. # https://www.scons.org/wiki/LongCmdLinesOnWin32
  390. env.use_windows_spawn_fix()
  391. ## Build type
  392. if not env["use_llvm"] and not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]):
  393. env["use_llvm"] = True
  394. if env["use_llvm"] and not try_cmd("clang --version", env["mingw_prefix"], env["arch"]):
  395. env["use_llvm"] = False
  396. # TODO: Re-evaluate the need for this / streamline with common config.
  397. if env["target"] == "template_release":
  398. env.Append(CCFLAGS=["-msse2"])
  399. elif env.dev_build:
  400. # Allow big objects. It's supposed not to have drawbacks but seems to break
  401. # GCC LTO, so enabling for debug builds only (which are not built with LTO
  402. # and are the only ones with too big objects).
  403. env.Append(CCFLAGS=["-Wa,-mbig-obj"])
  404. if env["windows_subsystem"] == "gui":
  405. env.Append(LINKFLAGS=["-Wl,--subsystem,windows"])
  406. else:
  407. env.Append(LINKFLAGS=["-Wl,--subsystem,console"])
  408. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  409. ## Compiler configuration
  410. if os.name != "nt":
  411. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  412. if env["arch"] == "x86_32":
  413. if env["use_static_cpp"]:
  414. env.Append(LINKFLAGS=["-static"])
  415. env.Append(LINKFLAGS=["-static-libgcc"])
  416. env.Append(LINKFLAGS=["-static-libstdc++"])
  417. else:
  418. if env["use_static_cpp"]:
  419. env.Append(LINKFLAGS=["-static"])
  420. if env["arch"] in ["x86_32", "x86_64"]:
  421. env["x86_libtheora_opt_gcc"] = True
  422. mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
  423. if env["use_llvm"]:
  424. env["CC"] = mingw_bin_prefix + "clang"
  425. env["CXX"] = mingw_bin_prefix + "clang++"
  426. if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
  427. env["AS"] = mingw_bin_prefix + "as"
  428. if try_cmd("ar --version", env["mingw_prefix"], env["arch"]):
  429. env["AR"] = mingw_bin_prefix + "ar"
  430. if try_cmd("ranlib --version", env["mingw_prefix"], env["arch"]):
  431. env["RANLIB"] = mingw_bin_prefix + "ranlib"
  432. env.extra_suffix = ".llvm" + env.extra_suffix
  433. else:
  434. env["CC"] = mingw_bin_prefix + "gcc"
  435. env["CXX"] = mingw_bin_prefix + "g++"
  436. if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
  437. env["AS"] = mingw_bin_prefix + "as"
  438. if try_cmd("gcc-ar --version", env["mingw_prefix"], env["arch"]):
  439. env["AR"] = mingw_bin_prefix + "gcc-ar"
  440. if try_cmd("gcc-ranlib --version", env["mingw_prefix"], env["arch"]):
  441. env["RANLIB"] = mingw_bin_prefix + "gcc-ranlib"
  442. ## LTO
  443. if env["lto"] == "auto": # Full LTO for production with MinGW.
  444. env["lto"] = "full"
  445. if env["lto"] != "none":
  446. if env["lto"] == "thin":
  447. if not env["use_llvm"]:
  448. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  449. sys.exit(255)
  450. env.Append(CCFLAGS=["-flto=thin"])
  451. env.Append(LINKFLAGS=["-flto=thin"])
  452. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  453. env.Append(CCFLAGS=["-flto"])
  454. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  455. else:
  456. env.Append(CCFLAGS=["-flto"])
  457. env.Append(LINKFLAGS=["-flto"])
  458. env.Append(LINKFLAGS=["-Wl,--stack," + str(STACK_SIZE)])
  459. ## Compile flags
  460. if not env["use_llvm"]:
  461. env.Append(CCFLAGS=["-mwindows"])
  462. env.Append(CPPDEFINES=["WINDOWS_ENABLED", "WASAPI_ENABLED", "WINMIDI_ENABLED"])
  463. env.Append(
  464. CPPDEFINES=[
  465. ("WINVER", env["target_win_version"]),
  466. ("_WIN32_WINNT", env["target_win_version"]),
  467. ]
  468. )
  469. env.Append(
  470. LIBS=[
  471. "mingw32",
  472. "dsound",
  473. "ole32",
  474. "d3d9",
  475. "winmm",
  476. "gdi32",
  477. "iphlpapi",
  478. "shlwapi",
  479. "wsock32",
  480. "ws2_32",
  481. "kernel32",
  482. "oleaut32",
  483. "sapi",
  484. "dinput8",
  485. "dxguid",
  486. "ksuser",
  487. "imm32",
  488. "bcrypt",
  489. "avrt",
  490. "uuid",
  491. "dwmapi",
  492. "dwrite",
  493. "wbemuuid",
  494. ]
  495. )
  496. if env.debug_features:
  497. env.Append(LIBS=["psapi", "dbghelp"])
  498. if env["vulkan"]:
  499. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  500. if not env["use_volk"]:
  501. env.Append(LIBS=["vulkan"])
  502. if env["opengl3"]:
  503. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  504. env.Append(LIBS=["opengl32"])
  505. env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
  506. # resrc
  507. env.Append(BUILDERS={"RES": env.Builder(action=build_res_file, suffix=".o", src_suffix=".rc")})
  508. def configure(env: "Environment"):
  509. # Validate arch.
  510. supported_arches = ["x86_32", "x86_64", "arm32", "arm64"]
  511. if env["arch"] not in supported_arches:
  512. print(
  513. 'Unsupported CPU architecture "%s" for Windows. Supported architectures are: %s.'
  514. % (env["arch"], ", ".join(supported_arches))
  515. )
  516. sys.exit()
  517. # At this point the env has been set up with basic tools/compilers.
  518. env.Prepend(CPPPATH=["#platform/windows"])
  519. if os.name == "nt":
  520. env["ENV"] = os.environ # this makes build less repeatable, but simplifies some things
  521. env["ENV"]["TMP"] = os.environ["TMP"]
  522. # First figure out which compiler, version, and target arch we're using
  523. if os.getenv("VCINSTALLDIR") and detect_build_env_arch() and not env["use_mingw"]:
  524. setup_msvc_manual(env)
  525. env.msvc = True
  526. vcvars_msvc_config = True
  527. elif env.get("MSVC_VERSION", "") and not env["use_mingw"]:
  528. setup_msvc_auto(env)
  529. env.msvc = True
  530. vcvars_msvc_config = False
  531. else:
  532. setup_mingw(env)
  533. env.msvc = False
  534. # Now set compiler/linker flags
  535. if env.msvc:
  536. configure_msvc(env, vcvars_msvc_config)
  537. else: # MinGW
  538. configure_mingw(env)