detect.py 21 KB

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