detect.py 21 KB

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