detect.py 9.5 KB

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