detect.py 9.3 KB

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