detect.py 9.7 KB

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