detect.py 20 KB

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