detect.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import methods
  2. import os
  3. # To match other platforms
  4. STACK_SIZE = 8388608
  5. def is_active():
  6. return True
  7. def get_name():
  8. return "Windows"
  9. def can_build():
  10. if os.name == "nt":
  11. # Building natively on Windows
  12. # If VCINSTALLDIR is set in the OS environ, use traditional Godot logic to set up MSVC
  13. if os.getenv("VCINSTALLDIR"): # MSVC, manual setup
  14. return True
  15. # Otherwise, let SCons find MSVC if installed, or else Mingw.
  16. # Since we're just returning True here, if there's no compiler
  17. # installed, we'll get errors when it tries to build with the
  18. # null compiler.
  19. return True
  20. if os.name == "posix":
  21. # Cross-compiling with MinGW-w64 (old MinGW32 is not supported)
  22. mingw32 = "i686-w64-mingw32-"
  23. mingw64 = "x86_64-w64-mingw32-"
  24. if os.getenv("MINGW32_PREFIX"):
  25. mingw32 = os.getenv("MINGW32_PREFIX")
  26. if os.getenv("MINGW64_PREFIX"):
  27. mingw64 = os.getenv("MINGW64_PREFIX")
  28. test = "gcc --version > /dev/null 2>&1"
  29. if os.system(mingw64 + test) == 0 or os.system(mingw32 + test) == 0:
  30. return True
  31. return False
  32. def get_opts():
  33. from SCons.Variables import BoolVariable, EnumVariable
  34. mingw32 = ""
  35. mingw64 = ""
  36. if os.name == "posix":
  37. mingw32 = "i686-w64-mingw32-"
  38. mingw64 = "x86_64-w64-mingw32-"
  39. if os.getenv("MINGW32_PREFIX"):
  40. mingw32 = os.getenv("MINGW32_PREFIX")
  41. if os.getenv("MINGW64_PREFIX"):
  42. mingw64 = os.getenv("MINGW64_PREFIX")
  43. return [
  44. ("mingw_prefix_32", "MinGW prefix (Win32)", mingw32),
  45. ("mingw_prefix_64", "MinGW prefix (Win64)", mingw64),
  46. # Targeted Windows version: 7 (and later), minimum supported version
  47. # XP support dropped after EOL due to missing API for IPv6 and other issues
  48. # Vista support dropped after EOL due to GH-10243
  49. ("target_win_version", "Targeted Windows version, >= 0x0601 (Windows 7)", "0x0601"),
  50. EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")),
  51. EnumVariable("windows_subsystem", "Windows subsystem", "default", ("default", "console", "gui")),
  52. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  53. ("msvc_version", "MSVC version to use. Ignored if VCINSTALLDIR is set in shell env.", None),
  54. BoolVariable("use_mingw", "Use the Mingw compiler, even if MSVC is installed. Only used on Windows.", False),
  55. BoolVariable("use_llvm", "Use the LLVM compiler", False),
  56. BoolVariable("use_thinlto", "Use ThinLTO", False),
  57. ]
  58. def get_flags():
  59. return []
  60. def build_res_file(target, source, env):
  61. if env["bits"] == "32":
  62. cmdbase = env["mingw_prefix_32"]
  63. else:
  64. cmdbase = env["mingw_prefix_64"]
  65. cmdbase = cmdbase + "windres --include-dir . "
  66. import subprocess
  67. for x in range(len(source)):
  68. cmd = cmdbase + "-i " + str(source[x]) + " -o " + str(target[x])
  69. try:
  70. out = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  71. if len(out[1]):
  72. return 1
  73. except:
  74. return 1
  75. return 0
  76. def setup_msvc_manual(env):
  77. """Set up env to use MSVC manually, using VCINSTALLDIR"""
  78. if env["bits"] != "default":
  79. print(
  80. """
  81. Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console
  82. (or Visual Studio settings) that is being used to run SCons. As a consequence, bits argument is disabled. Run scons again without bits
  83. argument (example: scons p=windows) and SCons will attempt to detect what MSVC compiler will be executed and inform you.
  84. """
  85. )
  86. raise SCons.Errors.UserError("Bits argument should not be used when using VCINSTALLDIR")
  87. # Force bits arg
  88. # (Actually msys2 mingw can support 64-bit, we could detect that)
  89. env["bits"] = "32"
  90. env["x86_libtheora_opt_vc"] = True
  91. # find compiler manually
  92. compiler_version_str = methods.detect_visual_c_compiler_version(env["ENV"])
  93. print("Found MSVC compiler: " + compiler_version_str)
  94. # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writing)... vc compiler for 64bit can not compile _asm
  95. if compiler_version_str == "amd64" or compiler_version_str == "x86_amd64":
  96. env["bits"] = "64"
  97. env["x86_libtheora_opt_vc"] = False
  98. print("Compiled program architecture will be a 64 bit executable (forcing bits=64).")
  99. elif compiler_version_str == "x86" or compiler_version_str == "amd64_x86":
  100. print("Compiled program architecture will be a 32 bit executable. (forcing bits=32).")
  101. else:
  102. print(
  103. "Failed to manually detect MSVC compiler architecture version... Defaulting to 32bit executable settings"
  104. " (forcing bits=32). Compilation attempt will continue, but SCons can not detect for what architecture this"
  105. " build is compiled for. You should check your settings/compilation setup, or avoid setting VCINSTALLDIR."
  106. )
  107. def setup_msvc_auto(env):
  108. """Set up MSVC using SCons's auto-detection logic"""
  109. # If MSVC_VERSION is set by SCons, we know MSVC is installed.
  110. # But we may want a different version or target arch.
  111. # The env may have already been set up with default MSVC tools, so
  112. # reset a few things so we can set it up with the tools we want.
  113. # (Ideally we'd decide on the tool config before configuring any
  114. # environment, and just set the env up once, but this function runs
  115. # on an existing env so this is the simplest way.)
  116. env["MSVC_SETUP_RUN"] = False # Need to set this to re-run the tool
  117. env["MSVS_VERSION"] = None
  118. env["MSVC_VERSION"] = None
  119. env["TARGET_ARCH"] = None
  120. if env["bits"] != "default":
  121. env["TARGET_ARCH"] = {"32": "x86", "64": "x86_64"}[env["bits"]]
  122. if env.has_key("msvc_version"):
  123. env["MSVC_VERSION"] = env["msvc_version"]
  124. env.Tool("msvc")
  125. env.Tool("mssdk") # we want the MS SDK
  126. # Note: actual compiler version can be found in env['MSVC_VERSION'], e.g. "14.1" for VS2015
  127. # Get actual target arch into bits (it may be "default" at this point):
  128. if env["TARGET_ARCH"] in ("amd64", "x86_64"):
  129. env["bits"] = "64"
  130. else:
  131. env["bits"] = "32"
  132. print("Found MSVC version %s, arch %s, bits=%s" % (env["MSVC_VERSION"], env["TARGET_ARCH"], env["bits"]))
  133. if env["TARGET_ARCH"] in ("amd64", "x86_64"):
  134. env["x86_libtheora_opt_vc"] = False
  135. def setup_mingw(env):
  136. """Set up env for use with mingw"""
  137. # Nothing to do here
  138. print("Using MinGW")
  139. pass
  140. def configure_msvc(env, manual_msvc_config):
  141. """Configure env to work with MSVC"""
  142. # Build type
  143. if env["tests"]:
  144. env["windows_subsystem"] = "console"
  145. elif env["windows_subsystem"] == "default":
  146. # Default means we use console for debug, gui for release.
  147. if "debug" in env["target"]:
  148. env["windows_subsystem"] = "console"
  149. else:
  150. env["windows_subsystem"] = "gui"
  151. if env["target"] == "release":
  152. if env["optimize"] == "speed": # optimize for speed (default)
  153. env.Append(CCFLAGS=["/O2"])
  154. else: # optimize for size
  155. env.Append(CCFLAGS=["/O1"])
  156. env.Append(LINKFLAGS=["/ENTRY:mainCRTStartup"])
  157. env.Append(LINKFLAGS=["/OPT:REF"])
  158. elif env["target"] == "release_debug":
  159. if env["optimize"] == "speed": # optimize for speed (default)
  160. env.Append(CCFLAGS=["/O2"])
  161. else: # optimize for size
  162. env.Append(CCFLAGS=["/O1"])
  163. env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"])
  164. env.Append(LINKFLAGS=["/OPT:REF"])
  165. elif env["target"] == "debug":
  166. env.AppendUnique(CCFLAGS=["/Z7", "/Od", "/EHsc"])
  167. env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"])
  168. env.Append(LINKFLAGS=["/DEBUG"])
  169. if env["debug_symbols"] == "full" or env["debug_symbols"] == "yes":
  170. env.AppendUnique(CCFLAGS=["/Z7"])
  171. env.AppendUnique(LINKFLAGS=["/DEBUG"])
  172. if env["windows_subsystem"] == "gui":
  173. env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"])
  174. else:
  175. env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"])
  176. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  177. ## Compile/link flags
  178. env.AppendUnique(CCFLAGS=["/MT", "/Gd", "/GR", "/nologo"])
  179. # Force to use Unicode encoding
  180. env.AppendUnique(CCFLAGS=["/utf-8"])
  181. env.AppendUnique(CXXFLAGS=["/TP"]) # assume all sources are C++
  182. if manual_msvc_config: # should be automatic if SCons found it
  183. if os.getenv("WindowsSdkDir") is not None:
  184. env.Prepend(CPPPATH=[os.getenv("WindowsSdkDir") + "/Include"])
  185. else:
  186. print("Missing environment variable: WindowsSdkDir")
  187. env.AppendUnique(
  188. CPPDEFINES=[
  189. "WINDOWS_ENABLED",
  190. "WASAPI_ENABLED",
  191. "WINMIDI_ENABLED",
  192. "TYPED_METHOD_BIND",
  193. "WIN32",
  194. "MSVC",
  195. "WINVER=%s" % env["target_win_version"],
  196. "_WIN32_WINNT=%s" % env["target_win_version"],
  197. ]
  198. )
  199. env.AppendUnique(CPPDEFINES=["NOMINMAX"]) # disable bogus min/max WinDef.h macros
  200. if env["bits"] == "64":
  201. env.AppendUnique(CPPDEFINES=["_WIN64"])
  202. ## Libs
  203. LIBS = [
  204. "winmm",
  205. "dsound",
  206. "kernel32",
  207. "ole32",
  208. "oleaut32",
  209. "user32",
  210. "gdi32",
  211. "IPHLPAPI",
  212. "Shlwapi",
  213. "wsock32",
  214. "Ws2_32",
  215. "shell32",
  216. "advapi32",
  217. "dinput8",
  218. "dxguid",
  219. "imm32",
  220. "bcrypt",
  221. "Avrt",
  222. "dwmapi",
  223. ]
  224. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED"])
  225. if not env["builtin_vulkan"]:
  226. LIBS += ["vulkan"]
  227. else:
  228. LIBS += ["cfgmgr32"]
  229. # env.AppendUnique(CPPDEFINES = ['OPENGL_ENABLED'])
  230. LIBS += ["opengl32"]
  231. env.Append(LINKFLAGS=[p + env["LIBSUFFIX"] for p in LIBS])
  232. if manual_msvc_config:
  233. if os.getenv("WindowsSdkDir") is not None:
  234. env.Append(LIBPATH=[os.getenv("WindowsSdkDir") + "/Lib"])
  235. else:
  236. print("Missing environment variable: WindowsSdkDir")
  237. ## LTO
  238. if env["use_lto"]:
  239. env.AppendUnique(CCFLAGS=["/GL"])
  240. env.AppendUnique(ARFLAGS=["/LTCG"])
  241. if env["progress"]:
  242. env.AppendUnique(LINKFLAGS=["/LTCG:STATUS"])
  243. else:
  244. env.AppendUnique(LINKFLAGS=["/LTCG"])
  245. if manual_msvc_config:
  246. env.Prepend(CPPPATH=[p for p in os.getenv("INCLUDE").split(";")])
  247. env.Append(LIBPATH=[p for p in os.getenv("LIB").split(";")])
  248. # Incremental linking fix
  249. env["BUILDERS"]["ProgramOriginal"] = env["BUILDERS"]["Program"]
  250. env["BUILDERS"]["Program"] = methods.precious_program
  251. env.AppendUnique(LINKFLAGS=["/STACK:" + str(STACK_SIZE)])
  252. def configure_mingw(env):
  253. # Workaround for MinGW. See:
  254. # http://www.scons.org/wiki/LongCmdLinesOnWin32
  255. env.use_windows_spawn_fix()
  256. ## Build type
  257. if env["tests"]:
  258. env["windows_subsystem"] = "console"
  259. elif env["windows_subsystem"] == "default":
  260. # Default means we use console for debug, gui for release.
  261. if "debug" in env["target"]:
  262. env["windows_subsystem"] = "console"
  263. else:
  264. env["windows_subsystem"] = "gui"
  265. if env["target"] == "release":
  266. env.Append(CCFLAGS=["-msse2"])
  267. if env["optimize"] == "speed": # optimize for speed (default)
  268. if env["bits"] == "64":
  269. env.Append(CCFLAGS=["-O3"])
  270. else:
  271. env.Append(CCFLAGS=["-O2"])
  272. else: # optimize for size
  273. env.Prepend(CCFLAGS=["-Os"])
  274. if env["debug_symbols"] == "yes":
  275. env.Prepend(CCFLAGS=["-g1"])
  276. if env["debug_symbols"] == "full":
  277. env.Prepend(CCFLAGS=["-g2"])
  278. elif env["target"] == "release_debug":
  279. env.Append(CCFLAGS=["-O2"])
  280. env.Append(CPPDEFINES=["DEBUG_ENABLED"])
  281. if env["debug_symbols"] == "yes":
  282. env.Prepend(CCFLAGS=["-g1"])
  283. if env["debug_symbols"] == "full":
  284. env.Prepend(CCFLAGS=["-g2"])
  285. if env["optimize"] == "speed": # optimize for speed (default)
  286. env.Append(CCFLAGS=["-O2"])
  287. else: # optimize for size
  288. env.Prepend(CCFLAGS=["-Os"])
  289. elif env["target"] == "debug":
  290. env.Append(CCFLAGS=["-g3"])
  291. env.Append(CPPDEFINES=["DEBUG_ENABLED"])
  292. if env["windows_subsystem"] == "gui":
  293. env.Append(LINKFLAGS=["-Wl,--subsystem,windows"])
  294. else:
  295. env.Append(LINKFLAGS=["-Wl,--subsystem,console"])
  296. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  297. ## Compiler configuration
  298. if os.name != "nt":
  299. env["PROGSUFFIX"] = env["PROGSUFFIX"] + ".exe" # for linux cross-compilation
  300. if env["bits"] == "default":
  301. if os.name == "nt":
  302. env["bits"] = "64" if "PROGRAMFILES(X86)" in os.environ else "32"
  303. else: # default to 64-bit on Linux
  304. env["bits"] = "64"
  305. mingw_prefix = ""
  306. if env["bits"] == "32":
  307. env.Append(LINKFLAGS=["-static"])
  308. env.Append(LINKFLAGS=["-static-libgcc"])
  309. env.Append(LINKFLAGS=["-static-libstdc++"])
  310. mingw_prefix = env["mingw_prefix_32"]
  311. else:
  312. env.Append(LINKFLAGS=["-static"])
  313. mingw_prefix = env["mingw_prefix_64"]
  314. if env["use_llvm"]:
  315. env["CC"] = mingw_prefix + "clang"
  316. env["AS"] = mingw_prefix + "as"
  317. env["CXX"] = mingw_prefix + "clang++"
  318. env["AR"] = mingw_prefix + "ar"
  319. env["RANLIB"] = mingw_prefix + "ranlib"
  320. env["LINK"] = mingw_prefix + "clang++"
  321. else:
  322. env["CC"] = mingw_prefix + "gcc"
  323. env["AS"] = mingw_prefix + "as"
  324. env["CXX"] = mingw_prefix + "g++"
  325. env["AR"] = mingw_prefix + "gcc-ar"
  326. env["RANLIB"] = mingw_prefix + "gcc-ranlib"
  327. env["LINK"] = mingw_prefix + "g++"
  328. env["x86_libtheora_opt_gcc"] = True
  329. if env["use_lto"]:
  330. if not env["use_llvm"] and env.GetOption("num_jobs") > 1:
  331. env.Append(CCFLAGS=["-flto"])
  332. env.Append(LINKFLAGS=["-flto=" + str(env.GetOption("num_jobs"))])
  333. else:
  334. if env["use_thinlto"]:
  335. env.Append(CCFLAGS=["-flto=thin"])
  336. env.Append(LINKFLAGS=["-flto=thin"])
  337. else:
  338. env.Append(CCFLAGS=["-flto"])
  339. env.Append(LINKFLAGS=["-flto"])
  340. env.Append(LINKFLAGS=["-Wl,--stack," + str(STACK_SIZE)])
  341. ## Compile flags
  342. env.Append(CCFLAGS=["-mwindows"])
  343. env.Append(CPPDEFINES=["WINDOWS_ENABLED", "WASAPI_ENABLED", "WINMIDI_ENABLED"])
  344. env.Append(CPPDEFINES=[("WINVER", env["target_win_version"]), ("_WIN32_WINNT", env["target_win_version"])])
  345. env.Append(
  346. LIBS=[
  347. "mingw32",
  348. "dsound",
  349. "ole32",
  350. "d3d9",
  351. "winmm",
  352. "gdi32",
  353. "iphlpapi",
  354. "shlwapi",
  355. "wsock32",
  356. "ws2_32",
  357. "kernel32",
  358. "oleaut32",
  359. "dinput8",
  360. "dxguid",
  361. "ksuser",
  362. "imm32",
  363. "bcrypt",
  364. "avrt",
  365. "uuid",
  366. "dwmapi",
  367. ]
  368. )
  369. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  370. if not env["builtin_vulkan"]:
  371. env.Append(LIBS=["vulkan"])
  372. else:
  373. env.Append(LIBS=["cfgmgr32"])
  374. ## TODO !!! Re-enable when OpenGLES Rendering Device is implemented !!!
  375. # env.Append(CPPDEFINES=['OPENGL_ENABLED'])
  376. env.Append(LIBS=["opengl32"])
  377. env.Append(CPPDEFINES=["MINGW_ENABLED", ("MINGW_HAS_SECURE_API", 1)])
  378. # resrc
  379. env.Append(BUILDERS={"RES": env.Builder(action=build_res_file, suffix=".o", src_suffix=".rc")})
  380. def configure(env):
  381. # At this point the env has been set up with basic tools/compilers.
  382. env.Prepend(CPPPATH=["#platform/windows"])
  383. print("Configuring for Windows: target=%s, bits=%s" % (env["target"], env["bits"]))
  384. if os.name == "nt":
  385. env["ENV"] = os.environ # this makes build less repeatable, but simplifies some things
  386. env["ENV"]["TMP"] = os.environ["TMP"]
  387. # First figure out which compiler, version, and target arch we're using
  388. if os.getenv("VCINSTALLDIR") and not env["use_mingw"]:
  389. # Manual setup of MSVC
  390. setup_msvc_manual(env)
  391. env.msvc = True
  392. manual_msvc_config = True
  393. elif env.get("MSVC_VERSION", "") and not env["use_mingw"]:
  394. setup_msvc_auto(env)
  395. env.msvc = True
  396. manual_msvc_config = False
  397. else:
  398. setup_mingw(env)
  399. env.msvc = False
  400. # Now set compiler/linker flags
  401. if env.msvc:
  402. configure_msvc(env, manual_msvc_config)
  403. else: # MinGW
  404. configure_mingw(env)