detect.py 9.1 KB

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