detect.py 9.3 KB

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