Debian Codegraph @

platform/macos/detect.py

raw symbols SCIP
1 import os
2 import sys
3 from typing import TYPE_CHECKING
4
5 from methods import detect_darwin_sdk_path, get_compiler_version, is_apple_clang, print_error, print_warning
6 from platform_methods import detect_arch, detect_mvk, validate_arch
7
8 if TYPE_CHECKING:
9 from SCons.Script.SConscript import SConsEnvironment
10
11 # To match other platforms
12 STACK_SIZE = 8388608
13 STACK_SIZE_SANITIZERS = 30 * 1024 * 1024
14
15
16 def get_name():
17 return "macOS"
18
19
20 def can_build():
21 if sys.platform == "darwin" or ("OSXCROSS_ROOT" in os.environ):
22 return True
23
24 return False
25
26
27 def get_opts():
28 from SCons.Variables import BoolVariable, EnumVariable
29
30 return [
31 ("osxcross_sdk", "OSXCross SDK version", "darwin16"),
32 ("SWIFT_FRONTEND", "Path to the swift-frontend binary", ""),
33 ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
34 ("vulkan_sdk_path", "Path to the Vulkan SDK", ""),
35 EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ["no", "5.0", "devel"], ignorecase=2),
36 BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
37 BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
38 BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
39 BoolVariable("use_coverage", "Use instrumentation codes in the binary (e.g. for code coverage)", False),
40 ("angle_libs", "Path to the ANGLE static libraries", ""),
41 (
42 "bundle_sign_identity",
43 "The 'Full Name', 'Common Name' or SHA-1 hash of the signing identity used to sign editor .app bundle.",
44 "-",
45 ),
46 BoolVariable("generate_bundle", "Generate an APP bundle after building iOS/macOS binaries", False),
47 ]
48
49
50 def get_doc_classes():
51 return [
52 "EditorExportPlatformMacOS",
53 ]
54
55
56 def get_doc_path():
57 return "doc_classes"
58
59
60 def get_flags():
61 return {
62 "arch": detect_arch(),
63 "use_volk": False,
64 "metal": True,
65 "supported": ["library", "metal", "mono"],
66 }
67
68
69 def configure(env: "SConsEnvironment"):
70 # Validate arch.
71 supported_arches = ["x86_64", "arm64"]
72 validate_arch(env["arch"], get_name(), supported_arches)
73
74 ## Compiler configuration
75
76 # Save this in environment for use by other modules
77 if "OSXCROSS_ROOT" in os.environ:
78 env["osxcross"] = True
79
80 # CPU architecture.
81 if env["arch"] == "arm64":
82 print("Building for macOS 11.0+.")
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 elif env["arch"] == "x86_64":
87 print("Building for macOS 10.13+.")
88 env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
89 env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
90 env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=10.13"])
91
92 env.Append(CCFLAGS=["-ffp-contract=off"])
93 env.Append(CCFLAGS=["-fobjc-arc"])
94
95 cc_version = get_compiler_version(env)
96 cc_version_major = cc_version["apple_major"]
97 cc_version_minor = cc_version["apple_minor"]
98
99 # Workaround for Xcode 15 linker bug.
100 if is_apple_clang(env) and cc_version_major == 1500 and cc_version_minor == 0:
101 env.Prepend(LINKFLAGS=["-ld_classic"])
102
103 if env.dev_build:
104 env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
105
106 ccache_path = os.environ.get("CCACHE", "")
107 if ccache_path != "":
108 ccache_path = ccache_path + " "
109
110 if "osxcross" not in env: # regular native build
111 if env["macports_clang"] != "no":
112 mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
113 mpclangver = env["macports_clang"]
114 env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
115 env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
116 env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
117 env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
118 env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
119 else:
120 env["CC"] = ccache_path + "clang"
121 env["CXX"] = ccache_path + "clang++"
122
123 detect_darwin_sdk_path("macos", env)
124 env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
125 env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
126
127 else: # osxcross build
128 root = os.environ.get("OSXCROSS_ROOT", "")
129 if env["arch"] == "arm64":
130 basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
131 else:
132 basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
133
134 env["CC"] = ccache_path + basecmd + "cc"
135 env["CXX"] = ccache_path + basecmd + "c++"
136 env["AR"] = basecmd + "ar"
137 env["RANLIB"] = basecmd + "ranlib"
138 env["AS"] = basecmd + "as"
139
140 # LTO
141
142 if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
143 env["lto"] = "none"
144
145 if env["lto"] != "none":
146 if env["lto"] == "thin":
147 env.Append(CCFLAGS=["-flto=thin"])
148 env.Append(LINKFLAGS=["-flto=thin"])
149 else:
150 env.Append(CCFLAGS=["-flto"])
151 env.Append(LINKFLAGS=["-flto"])
152
153 # Sanitizers
154
155 if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
156 env.extra_suffix += ".san"
157 env.Append(CPPDEFINES=["SANITIZERS_ENABLED"])
158
159 if env["use_ubsan"]:
160 env.Append(
161 CCFLAGS=[
162 "-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"
163 ]
164 )
165 env.Append(LINKFLAGS=["-fsanitize=undefined"])
166 env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
167
168 if env["use_asan"]:
169 env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
170 env.Append(LINKFLAGS=["-fsanitize=address"])
171
172 if env["use_tsan"]:
173 env.Append(CCFLAGS=["-fsanitize=thread"])
174 env.Append(LINKFLAGS=["-fsanitize=thread"])
175
176 if env["library_type"] == "executable":
177 env.Append(LINKFLAGS=["-Wl,-stack_size," + hex(STACK_SIZE_SANITIZERS)])
178 elif env["library_type"] == "executable":
179 env.Append(LINKFLAGS=["-Wl,-stack_size," + hex(STACK_SIZE)])
180
181 if env["use_coverage"]:
182 env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
183 env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
184
185 ## Dependencies
186
187 if env["accesskit"]:
188 if env["accesskit_sdk_path"] != "":
189 env.Prepend(CPPPATH=[env["accesskit_sdk_path"] + "/include"])
190 if env["arch"] == "arm64" or env["arch"] == "universal":
191 env.Append(LINKFLAGS=["-L" + env["accesskit_sdk_path"] + "/lib/macos/arm64/static/"])
192 if env["arch"] == "x86_64" or env["arch"] == "universal":
193 env.Append(LINKFLAGS=["-L" + env["accesskit_sdk_path"] + "/lib/macos/x86_64/static/"])
194 env.Append(LINKFLAGS=["-laccesskit"])
195 else:
196 env.Append(CPPDEFINES=["ACCESSKIT_DYNAMIC"])
197 env.Append(CPPDEFINES=["ACCESSKIT_ENABLED"])
198
199 if env["builtin_libtheora"] and env["arch"] == "x86_64":
200 env["x86_libtheora_opt_gcc"] = True
201
202 if env["sdl"]:
203 env.Append(CPPDEFINES=["SDL_ENABLED"])
204 env.Append(LINKFLAGS=["-framework", "ForceFeedback"])
205
206 ## Flags
207
208 env.Prepend(CPPPATH=["#platform/macos"])
209 env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
210 env.Append(
211 LINKFLAGS=[
212 "-framework",
213 "Cocoa",
214 "-framework",
215 "Carbon",
216 "-framework",
217 "AudioUnit",
218 "-framework",
219 "CoreAudio",
220 "-framework",
221 "CoreMIDI",
222 "-framework",
223 "IOKit",
224 "-framework",
225 "GameController",
226 "-framework",
227 "CoreHaptics",
228 "-framework",
229 "CoreVideo",
230 "-framework",
231 "AVFoundation",
232 "-framework",
233 "CoreMedia",
234 "-framework",
235 "QuartzCore",
236 "-framework",
237 "Security",
238 "-framework",
239 "UniformTypeIdentifiers",
240 "-framework",
241 "IOSurface",
242 ]
243 )
244 env.Append(LIBS=["pthread", "z"])
245
246 extra_frameworks = set()
247
248 if env["opengl3"]:
249 env.Append(CPPDEFINES=["GLES3_ENABLED"])
250 if env["angle_libs"] != "":
251 env.AppendUnique(CPPDEFINES=["EGL_STATIC"])
252 env.Append(LINKFLAGS=["-L" + env["angle_libs"]])
253 env.Append(LINKFLAGS=["-lANGLE.macos." + env["arch"]])
254 env.Append(LINKFLAGS=["-lEGL.macos." + env["arch"]])
255 env.Append(LINKFLAGS=["-lGLES.macos." + env["arch"]])
256 env.Prepend(CPPPATH=["#thirdparty/angle/include"])
257
258 env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
259
260 if env["metal"] and env["arch"] != "arm64":
261 print_warning("Target architecture '{}' does not support the Metal rendering driver".format(env["arch"]))
262 env["metal"] = False
263
264 if env["metal"]:
265 env.AppendUnique(CPPDEFINES=["METAL_ENABLED", "RD_ENABLED"])
266 extra_frameworks.add("Metal")
267 extra_frameworks.add("MetalKit")
268 extra_frameworks.add("MetalFX")
269 env.Prepend(CPPPATH=["#thirdparty/spirv-cross"])
270
271 if env["vulkan"]:
272 env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED", "RD_ENABLED"])
273 extra_frameworks.add("Metal")
274 if not env["use_volk"]:
275 env.Append(LINKFLAGS=["-lMoltenVK"])
276
277 mvk_path = ""
278 arch_variants = ["macos-arm64_x86_64", "macos-" + env["arch"]]
279 for arch in arch_variants:
280 mvk_path = detect_mvk(env, arch)
281 if mvk_path != "":
282 mvk_path = os.path.join(mvk_path, arch)
283 break
284
285 if mvk_path != "":
286 env.Append(LINKFLAGS=["-L" + mvk_path])
287 else:
288 print_error(
289 "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
290 )
291 sys.exit(255)
292
293 if len(extra_frameworks) > 0:
294 frameworks = [item for key in extra_frameworks for item in ["-framework", key]]
295 env.Append(LINKFLAGS=frameworks)