detect.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path
  4. def is_active():
  5. return True
  6. def get_name():
  7. return "OSX"
  8. def can_build():
  9. if sys.platform == "darwin" or ("OSXCROSS_ROOT" in os.environ):
  10. return True
  11. return False
  12. def get_opts():
  13. from SCons.Variables import BoolVariable, EnumVariable
  14. return [
  15. ("osxcross_sdk", "OSXCross SDK version", "darwin16"),
  16. ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
  17. ("vulkan_sdk_path", "Path to the Vulkan SDK", ""),
  18. EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ("no", "5.0", "devel")),
  19. BoolVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", True),
  20. BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False),
  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_flags():
  27. return [
  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. files = os.listdir(dirname)
  40. ver_file = "0.0.0.0"
  41. ver_num = ver_parse(ver_file)
  42. for file in files:
  43. if os.path.isdir(os.path.join(dirname, file)):
  44. ver_comp = ver_parse(file)
  45. lib_name = os.path.join(
  46. os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/libMoltenVK.a"
  47. )
  48. if os.path.isfile(lib_name) and ver_comp > ver_num:
  49. ver_num = ver_comp
  50. ver_file = file
  51. return os.path.join(os.path.join(dirname, ver_file), "MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/")
  52. def configure(env):
  53. ## Build type
  54. if env["target"] == "release":
  55. if env["optimize"] == "speed": # optimize for speed (default)
  56. env.Prepend(CCFLAGS=["-O3", "-fomit-frame-pointer", "-ftree-vectorize"])
  57. elif env["optimize"] == "size": # optimize for size
  58. env.Prepend(CCFLAGS=["-Os", "-ftree-vectorize"])
  59. if env["arch"] != "arm64":
  60. env.Prepend(CCFLAGS=["-msse2"])
  61. if env["debug_symbols"]:
  62. env.Prepend(CCFLAGS=["-g2"])
  63. elif env["target"] == "release_debug":
  64. if env["optimize"] == "speed": # optimize for speed (default)
  65. env.Prepend(CCFLAGS=["-O2"])
  66. elif env["optimize"] == "size": # optimize for size
  67. env.Prepend(CCFLAGS=["-Os"])
  68. if env["debug_symbols"]:
  69. env.Prepend(CCFLAGS=["-g2"])
  70. elif env["target"] == "debug":
  71. env.Prepend(CCFLAGS=["-g3"])
  72. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  73. ## Architecture
  74. # Mac OS X no longer runs on 32-bit since 10.7 which is unsupported since 2014
  75. # As such, we only support 64-bit
  76. env["bits"] = "64"
  77. ## Compiler configuration
  78. # Save this in environment for use by other modules
  79. if "OSXCROSS_ROOT" in os.environ:
  80. env["osxcross"] = True
  81. if env["arch"] == "arm64":
  82. print("Building for macOS 11.0+, platform arm64.")
  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. else:
  87. print("Building for macOS 10.12+, platform x86_64.")
  88. env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  89. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  90. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.12"])
  91. env.Append(CCFLAGS=["-fobjc-arc"])
  92. if not "osxcross" in env: # regular native build
  93. if env["macports_clang"] != "no":
  94. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  95. mpclangver = env["macports_clang"]
  96. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  97. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  98. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  99. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  100. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  101. else:
  102. env["CC"] = "clang"
  103. env["CXX"] = "clang++"
  104. detect_darwin_sdk_path("osx", env)
  105. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  106. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  107. else: # osxcross build
  108. root = os.environ.get("OSXCROSS_ROOT", 0)
  109. if env["arch"] == "arm64":
  110. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  111. else:
  112. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  113. ccache_path = os.environ.get("CCACHE")
  114. if ccache_path is None:
  115. env["CC"] = basecmd + "cc"
  116. env["CXX"] = basecmd + "c++"
  117. else:
  118. # there aren't any ccache wrappers available for OS X cross-compile,
  119. # to enable caching we need to prepend the path to the ccache binary
  120. env["CC"] = ccache_path + " " + basecmd + "cc"
  121. env["CXX"] = ccache_path + " " + basecmd + "c++"
  122. env["AR"] = basecmd + "ar"
  123. env["RANLIB"] = basecmd + "ranlib"
  124. env["AS"] = basecmd + "as"
  125. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  126. env.extra_suffix += ".san"
  127. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  128. if env["use_ubsan"]:
  129. env.Append(
  130. CCFLAGS=[
  131. "-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"
  132. ]
  133. )
  134. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  135. env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
  136. if env["use_asan"]:
  137. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  138. env.Append(LINKFLAGS=["-fsanitize=address"])
  139. if env["use_tsan"]:
  140. env.Append(CCFLAGS=["-fsanitize=thread"])
  141. env.Append(LINKFLAGS=["-fsanitize=thread"])
  142. if env["use_coverage"]:
  143. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  144. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  145. ## Dependencies
  146. if env["builtin_libtheora"]:
  147. if env["arch"] != "arm64":
  148. env["x86_libtheora_opt_gcc"] = True
  149. ## Flags
  150. env.Prepend(CPPPATH=["#platform/osx"])
  151. env.Append(CPPDEFINES=["OSX_ENABLED", "UNIX_ENABLED", "APPLE_STYLE_KEYS", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
  152. env.Append(
  153. LINKFLAGS=[
  154. "-framework",
  155. "Cocoa",
  156. "-framework",
  157. "Carbon",
  158. "-framework",
  159. "AudioUnit",
  160. "-framework",
  161. "CoreAudio",
  162. "-framework",
  163. "CoreMIDI",
  164. "-framework",
  165. "IOKit",
  166. "-framework",
  167. "ForceFeedback",
  168. "-framework",
  169. "CoreVideo",
  170. "-framework",
  171. "AVFoundation",
  172. "-framework",
  173. "CoreMedia",
  174. ]
  175. )
  176. env.Append(LIBS=["pthread", "z"])
  177. if env["opengl3"]:
  178. env.Append(CPPDEFINES=["GLES_ENABLED", "GLES3_ENABLED"])
  179. env.Append(CCFLAGS=["-Wno-deprecated-declarations"]) # Disable deprecation warnings
  180. env.Append(LINKFLAGS=["-framework", "OpenGL"])
  181. env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
  182. if env["vulkan"]:
  183. env.Append(CPPDEFINES=["VULKAN_ENABLED"])
  184. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "QuartzCore", "-framework", "IOSurface"])
  185. if not env["use_volk"]:
  186. env.Append(LINKFLAGS=["-lMoltenVK"])
  187. mvk_found = False
  188. if env["vulkan_sdk_path"] != "":
  189. mvk_path = os.path.join(
  190. os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework/macos-arm64_x86_64/"
  191. )
  192. if os.path.isfile(os.path.join(mvk_path, "libMoltenVK.a")):
  193. mvk_found = True
  194. env.Append(LINKFLAGS=["-L" + mvk_path])
  195. if not mvk_found:
  196. mvk_path = get_mvk_sdk_path()
  197. if os.path.isfile(os.path.join(mvk_path, "libMoltenVK.a")):
  198. mvk_found = True
  199. env.Append(LINKFLAGS=["-L" + mvk_path])
  200. if not mvk_found:
  201. print(
  202. "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
  203. )
  204. sys.exit(255)