detect.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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
  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. "supported": ["mono"],
  44. }
  45. def configure(env: "SConsEnvironment"):
  46. # Validate arch.
  47. supported_arches = ["x86_64", "arm64"]
  48. if env["arch"] not in supported_arches:
  49. print_error(
  50. 'Unsupported CPU architecture "%s" for macOS. Supported architectures are: %s.'
  51. % (env["arch"], ", ".join(supported_arches))
  52. )
  53. sys.exit(255)
  54. ## Build type
  55. if env["target"] == "template_release":
  56. if env["arch"] != "arm64":
  57. env.Prepend(CCFLAGS=["-msse2"])
  58. elif env.dev_build:
  59. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  60. ## Compiler configuration
  61. # Save this in environment for use by other modules
  62. if "OSXCROSS_ROOT" in os.environ:
  63. env["osxcross"] = True
  64. # CPU architecture.
  65. if env["arch"] == "arm64":
  66. print("Building for macOS 11.0+.")
  67. env.Append(ASFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  68. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  69. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  70. elif env["arch"] == "x86_64":
  71. print("Building for macOS 10.13+.")
  72. env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  73. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  74. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  75. env.Append(CCFLAGS=["-ffp-contract=off"])
  76. cc_version = get_compiler_version(env)
  77. cc_version_major = cc_version["apple_major"]
  78. cc_version_minor = cc_version["apple_minor"]
  79. vanilla = is_vanilla_clang(env)
  80. # Workaround for Xcode 15 linker bug.
  81. if not vanilla and cc_version_major == 1500 and cc_version_minor == 0:
  82. env.Prepend(LINKFLAGS=["-ld_classic"])
  83. env.Append(CCFLAGS=["-fobjc-arc"])
  84. if "osxcross" not in env: # regular native build
  85. if env["macports_clang"] != "no":
  86. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  87. mpclangver = env["macports_clang"]
  88. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  89. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  90. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  91. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  92. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  93. else:
  94. env["CC"] = "clang"
  95. env["CXX"] = "clang++"
  96. detect_darwin_sdk_path("macos", env)
  97. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  98. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  99. else: # osxcross build
  100. root = os.environ.get("OSXCROSS_ROOT", "")
  101. if env["arch"] == "arm64":
  102. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  103. else:
  104. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  105. ccache_path = os.environ.get("CCACHE")
  106. if ccache_path is None:
  107. env["CC"] = basecmd + "cc"
  108. env["CXX"] = basecmd + "c++"
  109. else:
  110. # there aren't any ccache wrappers available for macOS cross-compile,
  111. # to enable caching we need to prepend the path to the ccache binary
  112. env["CC"] = ccache_path + " " + basecmd + "cc"
  113. env["CXX"] = ccache_path + " " + basecmd + "c++"
  114. env["AR"] = basecmd + "ar"
  115. env["RANLIB"] = basecmd + "ranlib"
  116. env["AS"] = basecmd + "as"
  117. # LTO
  118. if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
  119. env["lto"] = "none"
  120. if env["lto"] != "none":
  121. if env["lto"] == "thin":
  122. env.Append(CCFLAGS=["-flto=thin"])
  123. env.Append(LINKFLAGS=["-flto=thin"])
  124. else:
  125. env.Append(CCFLAGS=["-flto"])
  126. env.Append(LINKFLAGS=["-flto"])
  127. # Sanitizers
  128. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  129. env.extra_suffix += ".san"
  130. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  131. if env["use_ubsan"]:
  132. env.Append(
  133. CCFLAGS=[
  134. "-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"
  135. ]
  136. )
  137. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  138. env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
  139. if env["use_asan"]:
  140. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  141. env.Append(LINKFLAGS=["-fsanitize=address"])
  142. if env["use_tsan"]:
  143. env.Append(CCFLAGS=["-fsanitize=thread"])
  144. env.Append(LINKFLAGS=["-fsanitize=thread"])
  145. if env["use_coverage"]:
  146. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  147. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  148. ## Dependencies
  149. if env["builtin_libtheora"] and env["arch"] == "x86_64":
  150. env["x86_libtheora_opt_gcc"] = True
  151. ## Flags
  152. env.Prepend(CPPPATH=["#platform/macos"])
  153. env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  154. env.Append(
  155. LINKFLAGS=[
  156. "-framework",
  157. "Cocoa",
  158. "-framework",
  159. "Carbon",
  160. "-framework",
  161. "AudioUnit",
  162. "-framework",
  163. "CoreAudio",
  164. "-framework",
  165. "CoreMIDI",
  166. "-framework",
  167. "IOKit",
  168. "-framework",
  169. "GameController",
  170. "-framework",
  171. "CoreHaptics",
  172. "-framework",
  173. "CoreVideo",
  174. "-framework",
  175. "AVFoundation",
  176. "-framework",
  177. "CoreMedia",
  178. "-framework",
  179. "QuartzCore",
  180. "-framework",
  181. "Security",
  182. ]
  183. )
  184. env.Append(LIBS=["pthread", "z"])
  185. if env["opengl3"]:
  186. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  187. if env["angle_libs"] != "":
  188. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  189. env.Append(LINKFLAGS=["-L" + env["angle_libs"]])
  190. env.Append(LINKFLAGS=["-lANGLE.macos." + env["arch"]])
  191. env.Append(LINKFLAGS=["-lEGL.macos." + env["arch"]])
  192. env.Append(LINKFLAGS=["-lGLES.macos." + env["arch"]])
  193. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  194. env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
  195. if env["vulkan"]:
  196. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  197. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "IOSurface"])
  198. if not env["use_volk"]:
  199. env.Append(LINKFLAGS=["-lMoltenVK"])
  200. mvk_path = ""
  201. arch_variants = ["macos-arm64_x86_64", "macos-" + env["arch"]]
  202. for arch in arch_variants:
  203. mvk_path = detect_mvk(env, arch)
  204. if mvk_path != "":
  205. mvk_path = os.path.join(mvk_path, arch)
  206. break
  207. if mvk_path != "":
  208. env.Append(LINKFLAGS=["-L" + mvk_path])
  209. else:
  210. print_error(
  211. "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
  212. )
  213. sys.exit(255)