detect.py 9.9 KB

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