detect.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import os
  2. import sys
  3. from typing import TYPE_CHECKING
  4. from methods import detect_darwin_sdk_path, get_compiler_version, is_apple_clang, print_error, print_warning
  5. from platform_methods import detect_arch, detect_mvk, validate_arch
  6. if TYPE_CHECKING:
  7. from SCons.Script.SConscript import SConsEnvironment
  8. # To match other platforms
  9. STACK_SIZE = 8388608
  10. STACK_SIZE_SANITIZERS = 30 * 1024 * 1024
  11. def get_name():
  12. return "macOS"
  13. def can_build():
  14. if sys.platform == "darwin" or ("OSXCROSS_ROOT" in os.environ):
  15. return True
  16. return False
  17. def get_opts():
  18. from SCons.Variables import BoolVariable, EnumVariable
  19. return [
  20. ("osxcross_sdk", "OSXCross SDK version", "darwin16"),
  21. ("SWIFT_FRONTEND", "Path to the swift-frontend binary", ""),
  22. ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
  23. ("vulkan_sdk_path", "Path to the Vulkan SDK", ""),
  24. EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ["no", "5.0", "devel"], ignorecase=2),
  25. BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
  26. BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
  27. BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
  28. BoolVariable("use_coverage", "Use instrumentation codes in the binary (e.g. for code coverage)", False),
  29. ("angle_libs", "Path to the ANGLE static libraries", ""),
  30. (
  31. "bundle_sign_identity",
  32. "The 'Full Name', 'Common Name' or SHA-1 hash of the signing identity used to sign editor .app bundle.",
  33. "-",
  34. ),
  35. BoolVariable("generate_bundle", "Generate an APP bundle after building iOS/macOS binaries", False),
  36. ]
  37. def get_doc_classes():
  38. return [
  39. "EditorExportPlatformMacOS",
  40. ]
  41. def get_doc_path():
  42. return "doc_classes"
  43. def get_flags():
  44. return {
  45. "arch": detect_arch(),
  46. "use_volk": False,
  47. "metal": True,
  48. "supported": ["library", "metal", "mono"],
  49. }
  50. def configure(env: "SConsEnvironment"):
  51. # Validate arch.
  52. supported_arches = ["x86_64", "arm64"]
  53. validate_arch(env["arch"], get_name(), supported_arches)
  54. ## Compiler configuration
  55. # Save this in environment for use by other modules
  56. if "OSXCROSS_ROOT" in os.environ:
  57. env["osxcross"] = True
  58. # CPU architecture.
  59. if env["arch"] == "arm64":
  60. print("Building for macOS 11.0+.")
  61. env.Append(ASFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  62. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  63. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  64. elif env["arch"] == "x86_64":
  65. print("Building for macOS 10.13+.")
  66. env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  67. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  68. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  69. env.Append(CCFLAGS=["-ffp-contract=off"])
  70. env.Append(CCFLAGS=["-fobjc-arc"])
  71. cc_version = get_compiler_version(env)
  72. cc_version_major = cc_version["apple_major"]
  73. cc_version_minor = cc_version["apple_minor"]
  74. # Workaround for Xcode 15 linker bug.
  75. if is_apple_clang(env) and cc_version_major == 1500 and cc_version_minor == 0:
  76. env.Prepend(LINKFLAGS=["-ld_classic"])
  77. if env.dev_build:
  78. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  79. ccache_path = os.environ.get("CCACHE", "")
  80. if ccache_path != "":
  81. ccache_path = ccache_path + " "
  82. if "osxcross" not in env: # regular native build
  83. if env["macports_clang"] != "no":
  84. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  85. mpclangver = env["macports_clang"]
  86. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  87. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  88. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  89. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  90. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  91. else:
  92. env["CC"] = ccache_path + "clang"
  93. env["CXX"] = ccache_path + "clang++"
  94. detect_darwin_sdk_path("macos", env)
  95. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  96. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  97. else: # osxcross build
  98. root = os.environ.get("OSXCROSS_ROOT", "")
  99. if env["arch"] == "arm64":
  100. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  101. else:
  102. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  103. env["CC"] = ccache_path + basecmd + "cc"
  104. env["CXX"] = ccache_path + basecmd + "c++"
  105. env["AR"] = basecmd + "ar"
  106. env["RANLIB"] = basecmd + "ranlib"
  107. env["AS"] = basecmd + "as"
  108. # LTO
  109. if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
  110. env["lto"] = "none"
  111. if env["lto"] != "none":
  112. if env["lto"] == "thin":
  113. env.Append(CCFLAGS=["-flto=thin"])
  114. env.Append(LINKFLAGS=["-flto=thin"])
  115. else:
  116. env.Append(CCFLAGS=["-flto"])
  117. env.Append(LINKFLAGS=["-flto"])
  118. # Sanitizers
  119. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  120. env.extra_suffix += ".san"
  121. env.Append(CPPDEFINES=["SANITIZERS_ENABLED"])
  122. if env["use_ubsan"]:
  123. env.Append(
  124. CCFLAGS=[
  125. "-fsanitize=undefined,shift,shift-exponent,integer-divide-by-zero,unreachable,vla-bound,null,return,signed-integer-overflow,bounds,float-divide-by-zero,float-cast-overflow,nonnull-attribute,returns-nonnull-attribute,bool,enum,vptr,pointer-overflow,builtin"
  126. ]
  127. )
  128. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  129. env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
  130. if env["use_asan"]:
  131. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  132. env.Append(LINKFLAGS=["-fsanitize=address"])
  133. if env["use_tsan"]:
  134. env.Append(CCFLAGS=["-fsanitize=thread"])
  135. env.Append(LINKFLAGS=["-fsanitize=thread"])
  136. if env["library_type"] == "executable":
  137. env.Append(LINKFLAGS=["-Wl,-stack_size," + hex(STACK_SIZE_SANITIZERS)])
  138. elif env["library_type"] == "executable":
  139. env.Append(LINKFLAGS=["-Wl,-stack_size," + hex(STACK_SIZE)])
  140. if env["use_coverage"]:
  141. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  142. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  143. ## Dependencies
  144. if env["accesskit"]:
  145. if env["accesskit_sdk_path"] != "":
  146. env.Prepend(CPPPATH=[env["accesskit_sdk_path"] + "/include"])
  147. if env["arch"] == "arm64" or env["arch"] == "universal":
  148. env.Append(LINKFLAGS=["-L" + env["accesskit_sdk_path"] + "/lib/macos/arm64/static/"])
  149. if env["arch"] == "x86_64" or env["arch"] == "universal":
  150. env.Append(LINKFLAGS=["-L" + env["accesskit_sdk_path"] + "/lib/macos/x86_64/static/"])
  151. env.Append(LINKFLAGS=["-laccesskit"])
  152. else:
  153. env.Append(CPPDEFINES=["ACCESSKIT_DYNAMIC"])
  154. env.Append(CPPDEFINES=["ACCESSKIT_ENABLED"])
  155. if env["builtin_libtheora"] and env["arch"] == "x86_64":
  156. env["x86_libtheora_opt_gcc"] = True
  157. if env["sdl"]:
  158. env.Append(CPPDEFINES=["SDL_ENABLED"])
  159. env.Append(LINKFLAGS=["-framework", "ForceFeedback"])
  160. ## Flags
  161. env.Prepend(CPPPATH=["#platform/macos"])
  162. env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  163. env.Append(
  164. LINKFLAGS=[
  165. "-framework",
  166. "Cocoa",
  167. "-framework",
  168. "Carbon",
  169. "-framework",
  170. "AudioUnit",
  171. "-framework",
  172. "CoreAudio",
  173. "-framework",
  174. "CoreMIDI",
  175. "-framework",
  176. "IOKit",
  177. "-framework",
  178. "GameController",
  179. "-framework",
  180. "CoreHaptics",
  181. "-framework",
  182. "CoreVideo",
  183. "-framework",
  184. "AVFoundation",
  185. "-framework",
  186. "CoreMedia",
  187. "-framework",
  188. "QuartzCore",
  189. "-framework",
  190. "Security",
  191. "-framework",
  192. "UniformTypeIdentifiers",
  193. "-framework",
  194. "IOSurface",
  195. ]
  196. )
  197. env.Append(LIBS=["pthread", "z"])
  198. extra_frameworks = set()
  199. if env["opengl3"]:
  200. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  201. if env["angle_libs"] != "":
  202. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  203. env.Append(LINKFLAGS=["-L" + env["angle_libs"]])
  204. env.Append(LINKFLAGS=["-lANGLE.macos." + env["arch"]])
  205. env.Append(LINKFLAGS=["-lEGL.macos." + env["arch"]])
  206. env.Append(LINKFLAGS=["-lGLES.macos." + env["arch"]])
  207. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  208. env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
  209. if env["metal"] and env["arch"] != "arm64":
  210. print_warning("Target architecture '{}' does not support the Metal rendering driver".format(env["arch"]))
  211. env["metal"] = False
  212. if env["metal"]:
  213. env.AppendUnique(CPPDEFINES=["METAL_ENABLED", "RD_ENABLED"])
  214. extra_frameworks.add("Metal")
  215. extra_frameworks.add("MetalKit")
  216. extra_frameworks.add("MetalFX")
  217. env.Prepend(CPPPATH=["#thirdparty/spirv-cross"])
  218. if env["vulkan"]:
  219. env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  220. extra_frameworks.add("Metal")
  221. if not env["use_volk"]:
  222. env.Append(LINKFLAGS=["-lMoltenVK"])
  223. mvk_path = ""
  224. arch_variants = ["macos-arm64_x86_64", "macos-" + env["arch"]]
  225. for arch in arch_variants:
  226. mvk_path = detect_mvk(env, arch)
  227. if mvk_path != "":
  228. mvk_path = os.path.join(mvk_path, arch)
  229. break
  230. if mvk_path != "":
  231. env.Append(LINKFLAGS=["-L" + mvk_path])
  232. else:
  233. print_error(
  234. "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
  235. )
  236. sys.exit(255)
  237. if len(extra_frameworks) > 0:
  238. frameworks = [item for key in extra_frameworks for item in ["-framework", key]]
  239. env.Append(LINKFLAGS=frameworks)