detect.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path, get_compiler_version, is_vanilla_clang
  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 "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. def get_doc_classes():
  28. return [
  29. "EditorExportPlatformMacOS",
  30. ]
  31. def get_doc_path():
  32. return "doc_classes"
  33. def get_flags():
  34. return [
  35. ("arch", detect_arch()),
  36. ("use_volk", False),
  37. ]
  38. def get_mvk_sdk_path():
  39. def int_or_zero(i):
  40. try:
  41. return int(i)
  42. except:
  43. return 0
  44. def ver_parse(a):
  45. return [int_or_zero(i) for i in a.split(".")]
  46. dirname = os.path.expanduser("~/VulkanSDK")
  47. if not os.path.exists(dirname):
  48. return ""
  49. ver_num = ver_parse("0.0.0.0")
  50. files = os.listdir(dirname)
  51. lib_name_out = dirname
  52. for file in files:
  53. if os.path.isdir(os.path.join(dirname, file)):
  54. ver_comp = ver_parse(file)
  55. if ver_comp > ver_num:
  56. # Try new SDK location.
  57. lib_name = os.path.join(
  58. os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/macos-arm64_x86_64/"
  59. )
  60. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  61. ver_num = ver_comp
  62. lib_name_out = lib_name
  63. else:
  64. # Try old SDK location.
  65. lib_name = os.path.join(
  66. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/"
  67. )
  68. if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")):
  69. ver_num = ver_comp
  70. lib_name_out = lib_name
  71. return lib_name_out
  72. def configure(env: "Environment"):
  73. # Validate arch.
  74. supported_arches = ["x86_64", "arm64"]
  75. if env["arch"] not in supported_arches:
  76. print(
  77. 'Unsupported CPU architecture "%s" for macOS. Supported architectures are: %s.'
  78. % (env["arch"], ", ".join(supported_arches))
  79. )
  80. sys.exit()
  81. ## Build type
  82. if env["target"] == "template_release":
  83. if env["arch"] != "arm64":
  84. env.Prepend(CCFLAGS=["-msse2"])
  85. elif env.dev_build:
  86. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  87. ## Compiler configuration
  88. # Save this in environment for use by other modules
  89. if "OSXCROSS_ROOT" in os.environ:
  90. env["osxcross"] = True
  91. # CPU architecture.
  92. if env["arch"] == "arm64":
  93. print("Building for macOS 11.0+.")
  94. env.Append(ASFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  95. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  96. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  97. elif env["arch"] == "x86_64":
  98. print("Building for macOS 10.13+.")
  99. env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  100. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  101. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  102. cc_version = get_compiler_version(env)
  103. cc_version_major = cc_version["major"]
  104. cc_version_minor = cc_version["minor"]
  105. vanilla = is_vanilla_clang(env)
  106. # Workaround for Xcode 15 linker bug.
  107. if not vanilla and cc_version_major == 15 and cc_version_minor == 0:
  108. env.Prepend(LINKFLAGS=["-ld_classic"])
  109. env.Append(CCFLAGS=["-fobjc-arc"])
  110. if not "osxcross" in env: # regular native build
  111. if env["macports_clang"] != "no":
  112. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  113. mpclangver = env["macports_clang"]
  114. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  115. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  116. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  117. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  118. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  119. else:
  120. env["CC"] = "clang"
  121. env["CXX"] = "clang++"
  122. detect_darwin_sdk_path("macos", env)
  123. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  124. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  125. else: # osxcross build
  126. root = os.environ.get("OSXCROSS_ROOT", "")
  127. if env["arch"] == "arm64":
  128. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  129. else:
  130. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  131. ccache_path = os.environ.get("CCACHE")
  132. if ccache_path is None:
  133. env["CC"] = basecmd + "cc"
  134. env["CXX"] = basecmd + "c++"
  135. else:
  136. # there aren't any ccache wrappers available for macOS cross-compile,
  137. # to enable caching we need to prepend the path to the ccache binary
  138. env["CC"] = ccache_path + " " + basecmd + "cc"
  139. env["CXX"] = ccache_path + " " + basecmd + "c++"
  140. env["AR"] = basecmd + "ar"
  141. env["RANLIB"] = basecmd + "ranlib"
  142. env["AS"] = basecmd + "as"
  143. # LTO
  144. if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
  145. env["lto"] = "none"
  146. if env["lto"] != "none":
  147. if env["lto"] == "thin":
  148. env.Append(CCFLAGS=["-flto=thin"])
  149. env.Append(LINKFLAGS=["-flto=thin"])
  150. else:
  151. env.Append(CCFLAGS=["-flto"])
  152. env.Append(LINKFLAGS=["-flto"])
  153. # Sanitizers
  154. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  155. env.extra_suffix += ".san"
  156. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  157. if env["use_ubsan"]:
  158. env.Append(
  159. CCFLAGS=[
  160. "-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"
  161. ]
  162. )
  163. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  164. env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
  165. if env["use_asan"]:
  166. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  167. env.Append(LINKFLAGS=["-fsanitize=address"])
  168. if env["use_tsan"]:
  169. env.Append(CCFLAGS=["-fsanitize=thread"])
  170. env.Append(LINKFLAGS=["-fsanitize=thread"])
  171. if env["use_coverage"]:
  172. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  173. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  174. ## Dependencies
  175. if env["builtin_libtheora"] and env["arch"] == "x86_64":
  176. env["x86_libtheora_opt_gcc"] = True
  177. ## Flags
  178. env.Prepend(CPPPATH=["#platform/macos"])
  179. env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  180. env.Append(
  181. LINKFLAGS=[
  182. "-framework",
  183. "Cocoa",
  184. "-framework",
  185. "Carbon",
  186. "-framework",
  187. "AudioUnit",
  188. "-framework",
  189. "CoreAudio",
  190. "-framework",
  191. "CoreMIDI",
  192. "-framework",
  193. "IOKit",
  194. "-framework",
  195. "ForceFeedback",
  196. "-framework",
  197. "CoreVideo",
  198. "-framework",
  199. "AVFoundation",
  200. "-framework",
  201. "CoreMedia",
  202. "-framework",
  203. "QuartzCore",
  204. "-framework",
  205. "Security",
  206. ]
  207. )
  208. env.Append(LIBS=["pthread", "z"])
  209. if env["opengl3"]:
  210. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  211. if env["angle_libs"] != "":
  212. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  213. env.Append(LINKFLAGS=["-L" + env["angle_libs"]])
  214. env.Append(LINKFLAGS=["-lANGLE.macos." + env["arch"]])
  215. env.Append(LINKFLAGS=["-lEGL.macos." + env["arch"]])
  216. env.Append(LINKFLAGS=["-lGLES.macos." + env["arch"]])
  217. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  218. env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
  219. if env["vulkan"]:
  220. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  221. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "IOSurface"])
  222. if not env["use_volk"]:
  223. env.Append(LINKFLAGS=["-lMoltenVK"])
  224. mvk_found = False
  225. mvk_list = [get_mvk_sdk_path(), "/opt/homebrew/lib", "/usr/local/homebrew/lib", "/opt/local/lib"]
  226. if env["vulkan_sdk_path"] != "":
  227. mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"]))
  228. mvk_list.insert(
  229. 0,
  230. os.path.join(
  231. os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework/macos-arm64_x86_64/"
  232. ),
  233. )
  234. mvk_list.insert(
  235. 0,
  236. os.path.join(
  237. os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/"
  238. ),
  239. )
  240. for mvk_path in mvk_list:
  241. if mvk_path and os.path.isfile(os.path.join(mvk_path, "libMoltenVK.a")):
  242. mvk_found = True
  243. print("MoltenVK found at: " + mvk_path)
  244. env.Append(LINKFLAGS=["-L" + mvk_path])
  245. break
  246. if not mvk_found:
  247. print(
  248. "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
  249. )
  250. sys.exit(255)