detect.py 9.0 KB

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