detect.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import methods
  2. import os
  3. import sys
  4. from platform_methods import detect_arch
  5. from typing import TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from SCons import Environment
  8. def get_name():
  9. return "UWP"
  10. def can_build():
  11. if os.name == "nt":
  12. # building natively on windows!
  13. if os.getenv("VSINSTALLDIR"):
  14. if os.getenv("ANGLE_SRC_PATH") is None:
  15. return False
  16. return True
  17. return False
  18. def get_opts():
  19. return [
  20. ("msvc_version", "MSVC version to use (ignored if the VCINSTALLDIR environment variable is set)", None),
  21. ]
  22. def get_flags():
  23. return [
  24. ("arch", detect_arch()),
  25. ("tools", False),
  26. ("xaudio2", True),
  27. ("builtin_pcre2_with_jit", False),
  28. ]
  29. def configure(env: "Environment"):
  30. # Validate arch.
  31. supported_arches = ["x86_32", "x86_64", "arm32"]
  32. if env["arch"] not in supported_arches:
  33. print(
  34. 'Unsupported CPU architecture "%s" for UWP. Supported architectures are: %s.'
  35. % (env["arch"], ", ".join(supported_arches))
  36. )
  37. sys.exit()
  38. env.msvc = True
  39. ## Build type
  40. if env["target"] == "release":
  41. env.Append(CCFLAGS=["/MD"])
  42. env.Append(LINKFLAGS=["/SUBSYSTEM:WINDOWS"])
  43. if env["optimize"] != "none":
  44. env.Append(CCFLAGS=["/O2", "/GL"])
  45. env.Append(LINKFLAGS=["/LTCG"])
  46. elif env["target"] == "release_debug":
  47. env.Append(CCFLAGS=["/MD"])
  48. env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"])
  49. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  50. if env["optimize"] != "none":
  51. env.Append(CCFLAGS=["/O2", "/Zi"])
  52. elif env["target"] == "debug":
  53. env.Append(CCFLAGS=["/Zi"])
  54. env.Append(CCFLAGS=["/MDd"])
  55. env.Append(LINKFLAGS=["/SUBSYSTEM:CONSOLE"])
  56. env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
  57. env.Append(LINKFLAGS=["/DEBUG"])
  58. ## Compiler configuration
  59. env["ENV"] = os.environ
  60. vc_base_path = os.environ["VCTOOLSINSTALLDIR"] if "VCTOOLSINSTALLDIR" in os.environ else os.environ["VCINSTALLDIR"]
  61. # Force to use Unicode encoding
  62. env.AppendUnique(CCFLAGS=["/utf-8"])
  63. # ANGLE
  64. angle_root = os.environ["ANGLE_SRC_PATH"]
  65. env.Prepend(CPPPATH=[angle_root + "/include"])
  66. jobs = str(env.GetOption("num_jobs"))
  67. angle_build_cmd = (
  68. "msbuild.exe "
  69. + angle_root
  70. + "/winrt/10/src/angle.sln /nologo /v:m /m:"
  71. + jobs
  72. + " /p:Configuration=Release /p:Platform="
  73. )
  74. if os.path.isfile(f"{angle_root}/winrt/10/src/angle.sln"):
  75. env["build_angle"] = True
  76. ## Architecture
  77. arch = ""
  78. if str(os.getenv("Platform")).lower() == "arm":
  79. print("Compiled program architecture will be an ARM executable (forcing arch=arm32).")
  80. arch = "arm"
  81. env["arch"] = "arm32"
  82. env.Append(LINKFLAGS=["/MACHINE:ARM"])
  83. env.Append(LIBPATH=[vc_base_path + "lib/store/arm"])
  84. angle_build_cmd += "ARM"
  85. env.Append(LIBPATH=[angle_root + "/winrt/10/src/Release_ARM/lib"])
  86. else:
  87. compiler_version_str = methods.detect_visual_c_compiler_version(env["ENV"])
  88. if compiler_version_str == "amd64" or compiler_version_str == "x86_amd64":
  89. env["arch"] = "x86_64"
  90. print("Compiled program architecture will be a x64 executable (forcing arch=x86_64).")
  91. elif compiler_version_str == "x86" or compiler_version_str == "amd64_x86":
  92. env["arch"] = "x86_32"
  93. print("Compiled program architecture will be a x86 executable (forcing arch=x86_32).")
  94. else:
  95. print(
  96. "Failed to detect MSVC compiler architecture version... Defaulting to x86 32-bit executable settings"
  97. " (forcing arch=x86_32). Compilation attempt will continue, but SCons can not detect for what architecture"
  98. " this build is compiled for. You should check your settings/compilation setup."
  99. )
  100. env["arch"] = "x86_32"
  101. if env["arch"] == "x86_32":
  102. arch = "x86"
  103. angle_build_cmd += "Win32"
  104. env.Append(LINKFLAGS=["/MACHINE:X86"])
  105. env.Append(LIBPATH=[vc_base_path + "lib/store"])
  106. env.Append(LIBPATH=[angle_root + "/winrt/10/src/Release_Win32/lib"])
  107. else:
  108. arch = "x64"
  109. angle_build_cmd += "x64"
  110. env.Append(LINKFLAGS=["/MACHINE:X64"])
  111. env.Append(LIBPATH=[os.environ["VCINSTALLDIR"] + "lib/store/amd64"])
  112. env.Append(LIBPATH=[angle_root + "/winrt/10/src/Release_x64/lib"])
  113. env["PROGSUFFIX"] = "." + arch + env["PROGSUFFIX"]
  114. env["OBJSUFFIX"] = "." + arch + env["OBJSUFFIX"]
  115. env["LIBSUFFIX"] = "." + arch + env["LIBSUFFIX"]
  116. ## Compile flags
  117. env.Prepend(CPPPATH=["#platform/uwp", "#drivers/windows"])
  118. env.Append(CPPDEFINES=["UWP_ENABLED", "WINDOWS_ENABLED", "TYPED_METHOD_BIND"])
  119. env.Append(CPPDEFINES=["GLES_ENABLED", "GL_GLEXT_PROTOTYPES", "EGL_EGLEXT_PROTOTYPES", "ANGLE_ENABLED"])
  120. winver = "0x0602" # Windows 8 is the minimum target for UWP build
  121. env.Append(CPPDEFINES=[("WINVER", winver), ("_WIN32_WINNT", winver), "WIN32"])
  122. env.Append(CPPDEFINES=["__WRL_NO_DEFAULT_LIB__", ("PNG_ABORT", "abort")])
  123. env.Append(CPPFLAGS=["/AI", vc_base_path + "lib/store/references"])
  124. env.Append(CPPFLAGS=["/AI", vc_base_path + "lib/x86/store/references"])
  125. env.Append(
  126. CCFLAGS=(
  127. '/FS /MP /GS /wd"4453" /wd"28204" /wd"4291" /Zc:wchar_t /Gm- /fp:precise /errorReport:prompt /WX-'
  128. " /Zc:forScope /Gd /EHsc /nologo".split()
  129. )
  130. )
  131. env.Append(CPPDEFINES=["_UNICODE", "UNICODE", ("WINAPI_FAMILY", "WINAPI_FAMILY_APP")])
  132. env.Append(CXXFLAGS=["/ZW"])
  133. env.Append(
  134. CCFLAGS=[
  135. "/AI",
  136. vc_base_path + "\\vcpackages",
  137. "/AI",
  138. os.environ["WINDOWSSDKDIR"] + "\\References\\CommonConfiguration\\Neutral",
  139. ]
  140. )
  141. ## Link flags
  142. env.Append(
  143. LINKFLAGS=[
  144. "/MANIFEST:NO",
  145. "/NXCOMPAT",
  146. "/DYNAMICBASE",
  147. "/WINMD",
  148. "/APPCONTAINER",
  149. "/ERRORREPORT:PROMPT",
  150. "/NOLOGO",
  151. "/TLBID:1",
  152. '/NODEFAULTLIB:"kernel32.lib"',
  153. '/NODEFAULTLIB:"ole32.lib"',
  154. ]
  155. )
  156. LIBS = [
  157. "WindowsApp",
  158. "mincore",
  159. "ws2_32",
  160. "libANGLE",
  161. "libEGL",
  162. "libGLESv2",
  163. "bcrypt",
  164. ]
  165. env.Append(LINKFLAGS=[p + ".lib" for p in LIBS])
  166. # Incremental linking fix
  167. env["BUILDERS"]["ProgramOriginal"] = env["BUILDERS"]["Program"]
  168. env["BUILDERS"]["Program"] = methods.precious_program
  169. env.Append(BUILDERS={"ANGLE": env.Builder(action=angle_build_cmd)})