detect.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. BoolVariable("incremental_link", "Use MSVC incremental linking. May increase or decrease build times.", False),
  160. ]
  161. def get_doc_classes():
  162. return [
  163. "EditorExportPlatformWindows",
  164. ]
  165. def get_doc_path():
  166. return "doc_classes"
  167. def get_flags():
  168. arch = detect_build_env_arch() or detect_arch()
  169. return [
  170. ("arch", arch),
  171. ]
  172. def build_res_file(target, source, env):
  173. arch_aliases = {
  174. "x86_32": "pe-i386",
  175. "x86_64": "pe-x86-64",
  176. "arm32": "armv7-w64-mingw32",
  177. "arm64": "aarch64-w64-mingw32",
  178. }
  179. cmdbase = "windres --include-dir . --target=" + arch_aliases[env["arch"]]
  180. mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
  181. for x in range(len(source)):
  182. ok = True
  183. # Try prefixed executable (MinGW on Linux).
  184. cmd = mingw_bin_prefix + cmdbase + " -i " + str(source[x]) + " -o " + str(target[x])
  185. try:
  186. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  187. if len(out[1]):
  188. ok = False
  189. except Exception:
  190. ok = False
  191. # Try generic executable (MSYS2).
  192. if not ok:
  193. cmd = cmdbase + " -i " + str(source[x]) + " -o " + str(target[x])
  194. try:
  195. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  196. if len(out[1]):
  197. return -1
  198. except Exception:
  199. return -1
  200. return 0
  201. def setup_msvc_manual(env):
  202. """Running from VCVARS environment"""
  203. env_arch = detect_build_env_arch()
  204. if env["arch"] != env_arch:
  205. print(
  206. """
  207. 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).
  208. 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.
  209. """
  210. % (env["arch"], env_arch)
  211. )
  212. sys.exit(200)
  213. print("Found MSVC, arch %s" % (env_arch))
  214. def setup_msvc_auto(env):
  215. """Set up MSVC using SCons's auto-detection logic"""
  216. # If MSVC_VERSION is set by SCons, we know MSVC is installed.
  217. # But we may want a different version or target arch.
  218. # Valid architectures for MSVC's TARGET_ARCH:
  219. # ['amd64', 'emt64', 'i386', 'i486', 'i586', 'i686', 'ia64', 'itanium', 'x86', 'x86_64', 'arm', 'arm64', 'aarch64']
  220. # Our x86_64 and arm64 are the same, and we need to map the 32-bit
  221. # architectures to other names since MSVC isn't as explicit.
  222. # The rest we don't need to worry about because they are
  223. # aliases or aren't supported by Godot (itanium & ia64).
  224. msvc_arch_aliases = {"x86_32": "x86", "arm32": "arm"}
  225. if env["arch"] in msvc_arch_aliases.keys():
  226. env["TARGET_ARCH"] = msvc_arch_aliases[env["arch"]]
  227. else:
  228. env["TARGET_ARCH"] = env["arch"]
  229. # The env may have already been set up with default MSVC tools, so
  230. # reset a few things so we can set it up with the tools we want.
  231. # (Ideally we'd decide on the tool config before configuring any
  232. # environment, and just set the env up once, but this function runs
  233. # on an existing env so this is the simplest way.)
  234. env["MSVC_SETUP_RUN"] = False # Need to set this to re-run the tool
  235. env["MSVS_VERSION"] = None
  236. env["MSVC_VERSION"] = None
  237. if "msvc_version" in env:
  238. env["MSVC_VERSION"] = env["msvc_version"]
  239. env.Tool("msvc")
  240. env.Tool("mssdk") # we want the MS SDK
  241. # Note: actual compiler version can be found in env['MSVC_VERSION'], e.g. "14.1" for VS2015
  242. print("Found MSVC version %s, arch %s" % (env["MSVC_VERSION"], env["arch"]))
  243. def setup_mingw(env):
  244. """Set up env for use with mingw"""
  245. env_arch = detect_build_env_arch()
  246. if os.getenv("MSYSTEM") == "MSYS":
  247. print(
  248. """
  249. Running from base MSYS2 console/environment, use target specific environment instead (e.g., mingw32, mingw64, clang32, clang64).
  250. """
  251. )
  252. sys.exit(201)
  253. if env_arch != "" and env["arch"] != env_arch:
  254. print(
  255. """
  256. Arch argument (%s) is not matching MSYS2 console/environment that is being used to run SCons (%s).
  257. 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.
  258. """
  259. % (env["arch"], env_arch)
  260. )
  261. sys.exit(202)
  262. if not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]) and not try_cmd(
  263. "clang --version", env["mingw_prefix"], env["arch"]
  264. ):
  265. print(
  266. """
  267. No valid compilers found, use MINGW_PREFIX environment variable to set MinGW path.
  268. """
  269. )
  270. sys.exit(202)
  271. print("Using MinGW, arch %s" % (env["arch"]))
  272. def configure_msvc(env, vcvars_msvc_config):
  273. """Configure env to work with MSVC"""
  274. ## Build type
  275. # TODO: Re-evaluate the need for this / streamline with common config.
  276. if env["target"] == "template_release":
  277. env.Append(LINKFLAGS=["/ENTRY:mainCRTStartup"])
  278. if env["windows_subsystem"] == "gui":
  279. env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"])
  280. else:
  281. env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"])
  282. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  283. ## Compile/link flags
  284. if env["debug_crt"]:
  285. # Always use dynamic runtime, static debug CRT breaks thread_local.
  286. env.AppendUnique(CCFLAGS=["/MDd"])
  287. else:
  288. if env["use_static_cpp"]:
  289. env.AppendUnique(CCFLAGS=["/MT"])
  290. else:
  291. env.AppendUnique(CCFLAGS=["/MD"])
  292. # MSVC incremental linking is broken and may _increase_ link time (GH-77968).
  293. if not env["incremental_link"]:
  294. env.Append(LINKFLAGS=["/INCREMENTAL:NO"])
  295. if env["arch"] == "x86_32":
  296. env["x86_libtheora_opt_vc"] = True
  297. env.AppendUnique(CCFLAGS=["/Gd", "/GR", "/nologo"])
  298. env.AppendUnique(CCFLAGS=["/utf-8"]) # Force to use Unicode encoding.
  299. env.AppendUnique(CXXFLAGS=["/TP"]) # assume all sources are C++
  300. # Once it was thought that only debug builds would be too large,
  301. # but this has recently stopped being true. See the mingw function
  302. # for notes on why this shouldn't be enabled for gcc
  303. env.AppendUnique(CCFLAGS=["/bigobj"])
  304. if vcvars_msvc_config: # should be automatic if SCons found it
  305. if os.getenv("WindowsSdkDir") is not None:
  306. env.Prepend(CPPPATH=[os.getenv("WindowsSdkDir") + "/Include"])
  307. else:
  308. print("Missing environment variable: WindowsSdkDir")
  309. env.AppendUnique(
  310. CPPDEFINES=[
  311. "WINDOWS_ENABLED",
  312. "WASAPI_ENABLED",
  313. "WINMIDI_ENABLED",
  314. "TYPED_METHOD_BIND",
  315. "WIN32",
  316. "MSVC",
  317. "WINVER=%s" % env["target_win_version"],
  318. "_WIN32_WINNT=%s" % env["target_win_version"],
  319. ]
  320. )
  321. env.AppendUnique(CPPDEFINES=["NOMINMAX"]) # disable bogus min/max WinDef.h macros
  322. if env["arch"] == "x86_64":
  323. env.AppendUnique(CPPDEFINES=["_WIN64"])
  324. ## Libs
  325. LIBS = [
  326. "winmm",
  327. "dsound",
  328. "kernel32",
  329. "ole32",
  330. "oleaut32",
  331. "sapi",
  332. "user32",
  333. "gdi32",
  334. "IPHLPAPI",
  335. "Shlwapi",
  336. "wsock32",
  337. "Ws2_32",
  338. "shell32",
  339. "advapi32",
  340. "dinput8",
  341. "dxguid",
  342. "imm32",
  343. "bcrypt",
  344. "Crypt32",
  345. "Avrt",
  346. "dwmapi",
  347. "dwrite",
  348. "wbemuuid",
  349. ]
  350. if env.debug_features:
  351. LIBS += ["psapi", "dbghelp"]
  352. if env["vulkan"]:
  353. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED"])
  354. if not env["use_volk"]:
  355. LIBS += ["vulkan"]
  356. if env["opengl3"]:
  357. env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
  358. LIBS += ["opengl32"]
  359. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  360. if vcvars_msvc_config:
  361. if os.getenv("WindowsSdkDir") is not None:
  362. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  363. else:
  364. print("Missing environment variable: WindowsSdkDir")
  365. ## LTO
  366. if env["lto"] == "auto": # No LTO by default for MSVC, doesn't help.
  367. env["lto"] = "none"
  368. if env["lto"] != "none":
  369. if env["lto"] == "thin":
  370. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  371. sys.exit(255)
  372. env.AppendUnique(CCFLAGS=["/GL"])
  373. env.AppendUnique(ARFLAGS=["/LTCG"])
  374. if env["progress"]:
  375. env.AppendUnique(LINKFLAGS=["/LTCG:STATUS"])
  376. else:
  377. env.AppendUnique(LINKFLAGS=["/LTCG"])
  378. if vcvars_msvc_config:
  379. env.Prepend(CPPPATH=[p for p in os.getenv("INCLUDE").split(";")])
  380. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  381. # Sanitizers
  382. if env["use_asan"]:
  383. env.extra_suffix += ".san"
  384. env.Append(LINKFLAGS=["/INFERASANLIBS"])
  385. env.Append(CCFLAGS=["/fsanitize=address"])
  386. # Incremental linking fix
  387. env["BUILDERS"]["ProgramOriginal"] = env["BUILDERS"]["Program"]
  388. env["BUILDERS"]["Program"] = methods.precious_program
  389. env.Append(LINKFLAGS=["/NATVIS:platform\windows\godot.natvis"])
  390. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE)])
  391. def configure_mingw(env):
  392. # Workaround for MinGW. See:
  393. # https://www.scons.org/wiki/LongCmdLinesOnWin32
  394. env.use_windows_spawn_fix()
  395. ## Build type
  396. if not env["use_llvm"] and not try_cmd("gcc --version", env["mingw_prefix"], env["arch"]):
  397. env["use_llvm"] = True
  398. if env["use_llvm"] and not try_cmd("clang --version", env["mingw_prefix"], env["arch"]):
  399. env["use_llvm"] = False
  400. # TODO: Re-evaluate the need for this / streamline with common config.
  401. if env["target"] == "template_release":
  402. env.Append(CCFLAGS=["-msse2"])
  403. elif env.dev_build:
  404. # Allow big objects. It's supposed not to have drawbacks but seems to break
  405. # GCC LTO, so enabling for debug builds only (which are not built with LTO
  406. # and are the only ones with too big objects).
  407. env.Append(CCFLAGS=["-Wa,-mbig-obj"])
  408. if env["windows_subsystem"] == "gui":
  409. env.Append(LINKFLAGS=["-Wl,--subsystem,windows"])
  410. else:
  411. env.Append(LINKFLAGS=["-Wl,--subsystem,console"])
  412. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  413. ## Compiler configuration
  414. if os.name != "nt":
  415. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  416. if env["arch"] == "x86_32":
  417. if env["use_static_cpp"]:
  418. env.Append(LINKFLAGS=["-static"])
  419. env.Append(LINKFLAGS=["-static-libgcc"])
  420. env.Append(LINKFLAGS=["-static-libstdc++"])
  421. else:
  422. if env["use_static_cpp"]:
  423. env.Append(LINKFLAGS=["-static"])
  424. if env["arch"] in ["x86_32", "x86_64"]:
  425. env["x86_libtheora_opt_gcc"] = True
  426. mingw_bin_prefix = get_mingw_bin_prefix(env["mingw_prefix"], env["arch"])
  427. if env["use_llvm"]:
  428. env["CC"] = mingw_bin_prefix + "clang"
  429. env["CXX"] = mingw_bin_prefix + "clang++"
  430. if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
  431. env["AS"] = mingw_bin_prefix + "as"
  432. if try_cmd("ar --version", env["mingw_prefix"], env["arch"]):
  433. env["AR"] = mingw_bin_prefix + "ar"
  434. if try_cmd("ranlib --version", env["mingw_prefix"], env["arch"]):
  435. env["RANLIB"] = mingw_bin_prefix + "ranlib"
  436. env.extra_suffix = ".llvm" + env.extra_suffix
  437. else:
  438. env["CC"] = mingw_bin_prefix + "gcc"
  439. env["CXX"] = mingw_bin_prefix + "g++"
  440. if try_cmd("as --version", env["mingw_prefix"], env["arch"]):
  441. env["AS"] = mingw_bin_prefix + "as"
  442. if try_cmd("gcc-ar --version", env["mingw_prefix"], env["arch"]):
  443. env["AR"] = mingw_bin_prefix + "gcc-ar"
  444. if try_cmd("gcc-ranlib --version", env["mingw_prefix"], env["arch"]):
  445. env["RANLIB"] = mingw_bin_prefix + "gcc-ranlib"
  446. ## LTO
  447. if env["lto"] == "auto": # Full LTO for production with MinGW.
  448. env["lto"] = "full"
  449. if env["lto"] != "none":
  450. if env["lto"] == "thin":
  451. if not env["use_llvm"]:
  452. print("ThinLTO is only compatible with LLVM, use `use_llvm=yes` or `lto=full`.")
  453. sys.exit(255)
  454. env.Append(CCFLAGS=["-flto=thin"])
  455. env.Append(LINKFLAGS=["-flto=thin"])
  456. elif not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  457. env.Append(CCFLAGS=["-flto"])
  458. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  459. else:
  460. env.Append(CCFLAGS=["-flto"])
  461. env.Append(LINKFLAGS=["-flto"])
  462. env.Append(LINKFLAGS=["-Wl,--stack," + str(STACK_SIZE)])
  463. ## Compile flags
  464. if not env["use_llvm"]:
  465. env.Append(CCFLAGS=["-mwindows"])
  466. env.Append(CPPDEFINES=["WINDOWS_ENABLED", "WASAPI_ENABLED", "WINMIDI_ENABLED"])
  467. env.Append(
  468. CPPDEFINES=[
  469. ("WINVER", env["target_win_version"]),
  470. ("_WIN32_WINNT", env["target_win_version"]),
  471. ]
  472. )
  473. env.Append(
  474. LIBS=[
  475. "mingw32",
  476. "dsound",
  477. "ole32",
  478. "d3d9",
  479. "winmm",
  480. "gdi32",
  481. "iphlpapi",
  482. "shlwapi",
  483. "wsock32",
  484. "ws2_32",
  485. "kernel32",
  486. "oleaut32",
  487. "sapi",
  488. "dinput8",
  489. "dxguid",
  490. "ksuser",
  491. "imm32",
  492. "bcrypt",
  493. "crypt32",
  494. "avrt",
  495. "uuid",
  496. "dwmapi",
  497. "dwrite",
  498. "wbemuuid",
  499. ]
  500. )
  501. if env.debug_features:
  502. env.Append(LIBS=["psapi", "dbghelp"])
  503. if env["vulkan"]:
  504. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  505. if not env["use_volk"]:
  506. env.Append(LIBS=["vulkan"])
  507. if env["opengl3"]:
  508. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  509. env.Append(LIBS=["opengl32"])
  510. env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
  511. # resrc
  512. env.Append(BUILDERS={"RES": env.Builder(action=build_res_file, suffix=".o", src_suffix=".rc")})
  513. def configure(env: "Environment"):
  514. # Validate arch.
  515. supported_arches = ["x86_32", "x86_64", "arm32", "arm64"]
  516. if env["arch"] not in supported_arches:
  517. print(
  518. 'Unsupported CPU architecture "%s" for Windows. Supported architectures are: %s.'
  519. % (env["arch"], ", ".join(supported_arches))
  520. )
  521. sys.exit()
  522. # At this point the env has been set up with basic tools/compilers.
  523. env.Prepend(CPPPATH=["#platform/windows"])
  524. if os.name == "nt":
  525. env["ENV"] = os.environ # this makes build less repeatable, but simplifies some things
  526. env["ENV"]["TMP"] = os.environ["TMP"]
  527. # First figure out which compiler, version, and target arch we're using
  528. if os.getenv("VCINSTALLDIR") and detect_build_env_arch() and not env["use_mingw"]:
  529. setup_msvc_manual(env)
  530. env.msvc = True
  531. vcvars_msvc_config = True
  532. elif env.get("MSVC_VERSION", "") and not env["use_mingw"]:
  533. setup_msvc_auto(env)
  534. env.msvc = True
  535. vcvars_msvc_config = False
  536. else:
  537. setup_mingw(env)
  538. env.msvc = False
  539. # Now set compiler/linker flags
  540. if env.msvc:
  541. configure_msvc(env, vcvars_msvc_config)
  542. else: # MinGW
  543. configure_mingw(env)