detect.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import os
  2. import sys
  3. from methods import detect_darwin_sdk_path, get_compiler_version, is_vanilla_clang
  4. from platform_methods import detect_arch, detect_mvk
  5. from typing import TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from SCons import Environment
  8. def get_name():
  9. return "macOS"
  10. def can_build():
  11. if sys.platform == "darwin" or ("OSXCROSS_ROOT" in os.environ):
  12. return True
  13. return False
  14. def get_opts():
  15. from SCons.Variables import BoolVariable, EnumVariable
  16. return [
  17. ("osxcross_sdk", "OSXCross SDK version", "darwin16"),
  18. ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
  19. ("vulkan_sdk_path", "Path to the Vulkan SDK", ""),
  20. EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ("no", "5.0", "devel")),
  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. ("angle_libs", "Path to the ANGLE static libraries", ""),
  26. (
  27. "bundle_sign_identity",
  28. "The 'Full Name', 'Common Name' or SHA-1 hash of the signing identity used to sign editor .app bundle.",
  29. "-",
  30. ),
  31. BoolVariable("generate_bundle", "Generate an APP bundle after building iOS/macOS binaries", False),
  32. ]
  33. def get_doc_classes():
  34. return [
  35. "EditorExportPlatformMacOS",
  36. ]
  37. def get_doc_path():
  38. return "doc_classes"
  39. def get_flags():
  40. return [
  41. ("arch", detect_arch()),
  42. ("use_volk", False),
  43. ]
  44. def configure(env: "Environment"):
  45. # Validate arch.
  46. supported_arches = ["x86_64", "arm64"]
  47. if env["arch"] not in supported_arches:
  48. print(
  49. 'Unsupported CPU architecture "%s" for macOS. Supported architectures are: %s.'
  50. % (env["arch"], ", ".join(supported_arches))
  51. )
  52. sys.exit()
  53. ## Build type
  54. if env["target"] == "template_release":
  55. if env["arch"] != "arm64":
  56. env.Prepend(CCFLAGS=["-msse2"])
  57. elif env.dev_build:
  58. env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
  59. ## Compiler configuration
  60. # Save this in environment for use by other modules
  61. if "OSXCROSS_ROOT" in os.environ:
  62. env["osxcross"] = True
  63. # CPU architecture.
  64. if env["arch"] == "arm64":
  65. print("Building for macOS 11.0+.")
  66. env.Append(ASFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  67. env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  68. env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=11.0"])
  69. elif env["arch"] == "x86_64":
  70. print("Building for macOS 10.13+.")
  71. env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  72. env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  73. env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
  74. cc_version = get_compiler_version(env)
  75. cc_version_major = cc_version["apple_major"]
  76. cc_version_minor = cc_version["apple_minor"]
  77. vanilla = is_vanilla_clang(env)
  78. # Workaround for Xcode 15 linker bug.
  79. if not vanilla and cc_version_major == 1500 and cc_version_minor == 0:
  80. env.Prepend(LINKFLAGS=["-ld_classic"])
  81. env.Append(CCFLAGS=["-fobjc-arc"])
  82. if not "osxcross" in env: # regular native build
  83. if env["macports_clang"] != "no":
  84. mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
  85. mpclangver = env["macports_clang"]
  86. env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
  87. env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
  88. env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
  89. env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
  90. env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
  91. else:
  92. env["CC"] = "clang"
  93. env["CXX"] = "clang++"
  94. detect_darwin_sdk_path("macos", env)
  95. env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  96. env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
  97. else: # osxcross build
  98. root = os.environ.get("OSXCROSS_ROOT", "")
  99. if env["arch"] == "arm64":
  100. basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
  101. else:
  102. basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
  103. ccache_path = os.environ.get("CCACHE")
  104. if ccache_path is None:
  105. env["CC"] = basecmd + "cc"
  106. env["CXX"] = basecmd + "c++"
  107. else:
  108. # there aren't any ccache wrappers available for macOS cross-compile,
  109. # to enable caching we need to prepend the path to the ccache binary
  110. env["CC"] = ccache_path + " " + basecmd + "cc"
  111. env["CXX"] = ccache_path + " " + basecmd + "c++"
  112. env["AR"] = basecmd + "ar"
  113. env["RANLIB"] = basecmd + "ranlib"
  114. env["AS"] = basecmd + "as"
  115. # LTO
  116. if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
  117. env["lto"] = "none"
  118. if env["lto"] != "none":
  119. if env["lto"] == "thin":
  120. env.Append(CCFLAGS=["-flto=thin"])
  121. env.Append(LINKFLAGS=["-flto=thin"])
  122. else:
  123. env.Append(CCFLAGS=["-flto"])
  124. env.Append(LINKFLAGS=["-flto"])
  125. # Sanitizers
  126. if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
  127. env.extra_suffix += ".san"
  128. env.Append(CCFLAGS=["-DSANITIZERS_ENABLED"])
  129. if env["use_ubsan"]:
  130. env.Append(
  131. CCFLAGS=[
  132. "-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"
  133. ]
  134. )
  135. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  136. env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
  137. if env["use_asan"]:
  138. env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
  139. env.Append(LINKFLAGS=["-fsanitize=address"])
  140. if env["use_tsan"]:
  141. env.Append(CCFLAGS=["-fsanitize=thread"])
  142. env.Append(LINKFLAGS=["-fsanitize=thread"])
  143. if env["use_coverage"]:
  144. env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  145. env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
  146. ## Dependencies
  147. if env["builtin_libtheora"] and env["arch"] == "x86_64":
  148. env["x86_libtheora_opt_gcc"] = True
  149. ## Flags
  150. env.Prepend(CPPPATH=["#platform/macos"])
  151. env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED", "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. "-framework",
  175. "QuartzCore",
  176. "-framework",
  177. "Security",
  178. ]
  179. )
  180. env.Append(LIBS=["pthread", "z"])
  181. if env["opengl3"]:
  182. env.Append(CPPDEFINES=["GLES3_ENABLED"])
  183. if env["angle_libs"] != "":
  184. env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
  185. env.Append(LINKFLAGS=["-L" + env["angle_libs"]])
  186. env.Append(LINKFLAGS=["-lANGLE.macos." + env["arch"]])
  187. env.Append(LINKFLAGS=["-lEGL.macos." + env["arch"]])
  188. env.Append(LINKFLAGS=["-lGLES.macos." + env["arch"]])
  189. env.Prepend(CPPPATH=["#thirdparty/angle/include"])
  190. env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
  191. if env["vulkan"]:
  192. env.Append(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
  193. env.Append(LINKFLAGS=["-framework", "Metal", "-framework", "IOSurface"])
  194. if not env["use_volk"]:
  195. env.Append(LINKFLAGS=["-lMoltenVK"])
  196. mvk_path = detect_mvk(env, "macos-arm64_x86_64")
  197. if mvk_path != "":
  198. env.Append(LINKFLAGS=["-L" + os.path.join(mvk_path, "macos-arm64_x86_64")])
  199. else:
  200. print(
  201. "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
  202. )
  203. sys.exit(255)