Skip to content
0.2.0-dev Download ZIP
368 lines ·14.4 KB Raw
1import os
2import sys
3from typing import TYPE_CHECKING
4
5from methods import detect_darwin_sdk_path, get_compiler_version, is_apple_clang, print_error, print_warning
6from platform_methods import check_accesskit_version, detect_arch, detect_mvk, validate_arch
7
8if TYPE_CHECKING:
9 from SCons.Script.SConscript import SConsEnvironment
10
11# To match other platforms
12STACK_SIZE = 8388608
13STACK_SIZE_SANITIZERS = 30 * 1024 * 1024
14
15
16def get_name():
17 return "macOS"
18
19
20def can_build():
21 if sys.platform == "darwin" or "OSXCROSS_ROOT" in os.environ or "APPLE_LLVM_CROSS" in os.environ:
22 return True
23
24 return False
25
26
27def get_opts():
28 from SCons.Variables import BoolVariable, EnumVariable
29
30 # Dependencies folder.
31 deps_folder = os.getenv("LOCALAPPDATA")
32 if deps_folder:
33 deps_folder = os.path.join(deps_folder, "Godot", "build_deps")
34 else:
35 # Cross-compiling, the deps install script puts things in `bin`.
36 # Getting an absolute path to it is a bit hacky in Python.
37 try:
38 import inspect
39
40 caller_frame = inspect.stack()[1]
41 caller_script_dir = os.path.dirname(os.path.abspath(caller_frame[1]))
42 deps_folder = os.path.join(caller_script_dir, "bin", "build_deps")
43 except Exception: # Give up.
44 deps_folder = ""
45
46 return [
47 ("osxcross_sdk", "OSXCross SDK version", "darwin16"),
48 (("SWIFT_COMPILER", "SWIFT_FRONTEND"), "Path to the swiftc binary", ""),
49 ("MACOS_SDK_PATH", "Path to the macOS SDK", ""),
50 ("vulkan_sdk_path", "Path to the Vulkan SDK", ""),
51 EnumVariable("macports_clang", "Build using Clang from MacPorts", "no", ["no", "5.0", "devel"], ignorecase=2),
52 BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False),
53 BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN)", False),
54 BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN)", False),
55 BoolVariable("use_coverage", "Use instrumentation codes in the binary (e.g. for code coverage)", False),
56 # Screen reader support.
57 (
58 "accesskit_sdk_path",
59 "Path to the AccessKit C SDK",
60 os.path.join(deps_folder, "accesskit"),
61 ),
62 (
63 "angle_libs",
64 "Path to the ANGLE static libraries",
65 os.path.join(deps_folder, "angle"),
66 ),
67 (
68 "bundle_sign_identity",
69 "The 'Full Name', 'Common Name' or SHA-1 hash of the signing identity used to sign editor .app bundle.",
70 "-",
71 ),
72 BoolVariable("generate_bundle", "Generate an APP bundle after building iOS/macOS binaries", False),
73 ]
74
75
76def get_doc_classes():
77 return [
78 "EditorExportPlatformMacOS",
79 ]
80
81
82def get_doc_path():
83 return "doc_classes"
84
85
86def get_flags():
87 return {
88 "arch": detect_arch(),
89 "use_volk": False,
90 "metal": True,
91 "supported": ["library", "metal", "mono"],
92 }
93
94
95def configure(env: "SConsEnvironment"):
96 # Validate arch.
97 supported_arches = ["x86_64", "arm64"]
98 validate_arch(env["arch"], get_name(), supported_arches)
99
100 ## Compiler configuration
101
102 # Save this in environment for use by other modules
103 if "OSXCROSS_ROOT" in os.environ:
104 env["osxcross"] = True
105
106 # CPU architecture.
107 if env["arch"] == "arm64":
108 print("Building for macOS 13.0+.")
109 env.Append(ASFLAGS=["-arch", "arm64", "-mmacosx-version-min=13.0"])
110 env.Append(CCFLAGS=["-arch", "arm64", "-mmacosx-version-min=13.0"])
111 env.Append(LINKFLAGS=["-arch", "arm64", "-mmacosx-version-min=13.0"])
112 elif env["arch"] == "x86_64":
113 print("Building for macOS 11.0+.")
114 env.Append(ASFLAGS=["-arch", "x86_64", "-mmacosx-version-min=11.0"])
115 env.Append(CCFLAGS=["-arch", "x86_64", "-mmacosx-version-min=11.0"])
116 env.Append(LINKFLAGS=["-arch", "x86_64", "-mmacosx-version-min=11.0"])
117
118 env.Append(CCFLAGS=["-ffp-contract=off"])
119 env.Append(CCFLAGS=["-fobjc-arc", "-fvisibility=hidden"])
120
121 cc_version = get_compiler_version(env)
122 cc_version_major = cc_version["apple_major"]
123 cc_version_minor = cc_version["apple_minor"]
124
125 # Workaround for Xcode 15 linker bug.
126 if is_apple_clang(env) and cc_version_major == 1500 and cc_version_minor == 0:
127 env.Prepend(LINKFLAGS=["-ld_classic"])
128
129 if env.dev_build:
130 env.Prepend(LINKFLAGS=["-Xlinker", "-no_deduplicate"])
131
132 ccache_path = os.environ.get("CCACHE", "")
133 if ccache_path != "":
134 ccache_path = ccache_path + " "
135
136 if "osxcross" in env: # osxcross toolchain (cctools-port wrappers)
137 root = os.environ.get("OSXCROSS_ROOT", "")
138 if env["arch"] == "arm64":
139 basecmd = root + "/target/bin/arm64-apple-" + env["osxcross_sdk"] + "-"
140 else:
141 basecmd = root + "/target/bin/x86_64-apple-" + env["osxcross_sdk"] + "-"
142
143 env["CC"] = ccache_path + basecmd + "cc"
144 env["CXX"] = ccache_path + basecmd + "c++"
145 env["AR"] = basecmd + "ar"
146 env["RANLIB"] = basecmd + "ranlib"
147 env["AS"] = basecmd + "as"
148
149 elif "APPLE_LLVM_CROSS" in os.environ: # cross-compile from Linux with clang
150 # Opt-in path used by the godot-apple build containers (no osxcross).
151 # clang/clang++ resolve to the LLVM toolchain on PATH; the Apple target
152 # triple and SDK sysroot are passed as flags. No Darwin/SDK version is
153 # encoded here: the deployment target comes from -mmacosx-version-min
154 # (set above) and the build SDK from the MACOS_SDK_PATH option.
155 env["CC"] = ccache_path + "clang"
156 env["CXX"] = ccache_path + "clang++"
157 env["AS"] = ccache_path + "clang"
158 env["AR"] = "llvm-ar"
159 env["RANLIB"] = "llvm-ranlib"
160
161 target_triple = env["arch"] + "-apple-darwin"
162 env.Append(ASFLAGS=["-target", target_triple])
163 env.Append(CCFLAGS=["-target", target_triple])
164 env.Append(LINKFLAGS=["-target", target_triple])
165
166 detect_darwin_sdk_path("macos", env)
167 env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
168 env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
169
170 else: # regular native build
171 if env["macports_clang"] != "no":
172 mpprefix = os.environ.get("MACPORTS_PREFIX", "/opt/local")
173 mpclangver = env["macports_clang"]
174 env["CC"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang"
175 env["CXX"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/clang++"
176 env["AR"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ar"
177 env["RANLIB"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-ranlib"
178 env["AS"] = mpprefix + "/libexec/llvm-" + mpclangver + "/bin/llvm-as"
179 else:
180 env["CC"] = ccache_path + "clang"
181 env["CXX"] = ccache_path + "clang++"
182
183 detect_darwin_sdk_path("macos", env)
184 env.Append(CCFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
185 env.Append(LINKFLAGS=["-isysroot", "$MACOS_SDK_PATH"])
186
187 # LTO
188
189 if env["lto"] == "auto": # LTO benefits for macOS (size, performance) haven't been clearly established yet.
190 env["lto"] = "none"
191
192 if env["lto"] != "none":
193 if env["lto"] == "thin":
194 env.Append(CCFLAGS=["-flto=thin"])
195 env.Append(LINKFLAGS=["-flto=thin"])
196 else:
197 env.Append(CCFLAGS=["-flto"])
198 env.Append(LINKFLAGS=["-flto"])
199
200 # Sanitizers
201
202 if env["use_ubsan"] or env["use_asan"] or env["use_tsan"]:
203 env.extra_suffix += ".san"
204
205 if env["use_ubsan"]:
206 env.Append(CPPDEFINES=["UBSAN_ENABLED"])
207 env.Append(
208 CCFLAGS=[
209 "-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"
210 ]
211 )
212 env.Append(LINKFLAGS=["-fsanitize=undefined"])
213 env.Append(CCFLAGS=["-fsanitize=nullability-return,nullability-arg,function,nullability-assign"])
214
215 if env["use_asan"]:
216 env.Append(CPPDEFINES=["ASAN_ENABLED"])
217 env.Append(CCFLAGS=["-fsanitize=address,pointer-subtract,pointer-compare"])
218 env.Append(LINKFLAGS=["-fsanitize=address"])
219
220 if env["use_tsan"]:
221 env.Append(CPPDEFINES=["TSAN_ENABLED"])
222 env.Append(CCFLAGS=["-fsanitize=thread"])
223 env.Append(LINKFLAGS=["-fsanitize=thread"])
224
225 if env["library_type"] == "executable":
226 env.Append(LINKFLAGS=["-Wl,-stack_size," + hex(STACK_SIZE_SANITIZERS)])
227 elif env["library_type"] == "executable":
228 env.Append(LINKFLAGS=["-Wl,-stack_size," + hex(STACK_SIZE)])
229
230 if env["use_coverage"]:
231 env.Append(CCFLAGS=["-ftest-coverage", "-fprofile-arcs"])
232 env.Append(LINKFLAGS=["-ftest-coverage", "-fprofile-arcs"])
233
234 ## Dependencies
235
236 if env["accesskit"]:
237 if os.path.exists(env["accesskit_sdk_path"]):
238 if check_accesskit_version(env["accesskit_sdk_path"]):
239 env.Prepend(CPPPATH=[env["accesskit_sdk_path"] + "/include"])
240 if env["arch"] == "arm64" or env["arch"] == "universal":
241 env.Append(LINKFLAGS=["-L" + env["accesskit_sdk_path"] + "/lib/macos/arm64/static/"])
242 if env["arch"] == "x86_64" or env["arch"] == "universal":
243 env.Append(LINKFLAGS=["-L" + env["accesskit_sdk_path"] + "/lib/macos/x86_64/static/"])
244 env.Append(LINKFLAGS=["-laccesskit"])
245 env.Append(CPPDEFINES=["ACCESSKIT_ENABLED"])
246 else:
247 env["accesskit"] = False
248 else:
249 print_warning(
250 "The screen reader support driver requires dependencies to be installed.\n"
251 f"You can install them by running `python3 {os.path.join('misc', 'scripts', 'install_accesskit.py')}`.\n"
252 "See the documentation for more information:\n\t"
253 "https://docs.godotengine.org/en/latest/engine_details/development/compiling/compiling_for_macos.html#compiling-with-accesskit-support"
254 "\nAlternatively, disable this driver by compiling with `accesskit=no` explicitly."
255 )
256 env["accesskit"] = False
257
258 if env["builtin_libtheora"] and env["arch"] == "x86_64":
259 env["x86_libtheora_opt_gcc"] = True
260
261 if env["sdl"]:
262 env.Append(CPPDEFINES=["SDL_ENABLED"])
263 env.Append(LINKFLAGS=["-framework", "ForceFeedback"])
264
265 ## Flags
266
267 env.Prepend(CPPPATH=["#platform/macos"])
268 env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED", "COREAUDIO_ENABLED", "COREMIDI_ENABLED"])
269 env.Append(
270 LINKFLAGS=[
271 "-framework",
272 "Cocoa",
273 "-framework",
274 "Carbon",
275 "-framework",
276 "AudioUnit",
277 "-framework",
278 "CoreAudio",
279 "-framework",
280 "CoreMIDI",
281 "-framework",
282 "IOKit",
283 "-framework",
284 "GameController",
285 "-framework",
286 "CoreHaptics",
287 "-framework",
288 "CoreVideo",
289 "-framework",
290 "AVFoundation",
291 "-framework",
292 "CoreMedia",
293 "-framework",
294 "QuartzCore",
295 "-framework",
296 "Security",
297 "-framework",
298 "UniformTypeIdentifiers",
299 "-framework",
300 "IOSurface",
301 ]
302 )
303 env.Append(LIBS=["pthread", "z"])
304
305 extra_frameworks = set()
306
307 if env["opengl3"]:
308 env.Append(CPPDEFINES=["GLES3_ENABLED"])
309 if env["angle"]:
310 angle_path = env["angle_libs"] + "-" + env["arch"] + "-macos"
311 if not os.path.exists(angle_path):
312 angle_path = env["angle_libs"]
313 if os.path.exists(angle_path):
314 env.Prepend(CPPPATH=["#thirdparty/angle/include"])
315 env.AppendUnique(CPPDEFINES=["ANGLE_ENABLED", "EGL_STATIC"])
316 env.Append(LINKFLAGS=["-L" + angle_path])
317 env.Append(LINKFLAGS=["-lANGLE.macos." + env["arch"]])
318 env.Append(LINKFLAGS=["-lEGL.macos." + env["arch"]])
319 env.Append(LINKFLAGS=["-lGLES.macos." + env["arch"]])
320 extra_frameworks.add("Metal")
321 else:
322 print_warning(
323 "The ANGLE rendering driver requires dependencies to be installed.\n"
324 f"You can install them by running `python3 {os.path.join('misc', 'scripts', 'install_angle.py')}`.\n"
325 "See the documentation for more information:\n\t"
326 "https://docs.godotengine.org/en/latest/engine_details/development/compiling/compiling_for_macos.html#compiling-with-angle-support"
327 "\nAlternatively, disable this driver by compiling with `angle=no` explicitly."
328 )
329 env["angle"] = False
330
331 env.Append(LINKFLAGS=["-rpath", "@executable_path/../Frameworks", "-rpath", "@executable_path"])
332
333 if env["metal"] and env["arch"] != "arm64":
334 print_warning("Target architecture '{}' does not support the Metal rendering driver".format(env["arch"]))
335 env["metal"] = False
336
337 if env["metal"]:
338 env.AppendUnique(CPPDEFINES=["METAL_ENABLED"])
339 extra_frameworks.add("Metal")
340 extra_frameworks.add("MetalKit")
341 extra_frameworks.add("MetalFX")
342 env.Prepend(CPPPATH=["#thirdparty/spirv-cross"])
343
344 if env["vulkan"]:
345 env.AppendUnique(CPPDEFINES=["VULKAN_ENABLED"])
346 extra_frameworks.add("Metal")
347 if not env["use_volk"]:
348 env.Append(LINKFLAGS=["-lMoltenVK"])
349
350 mvk_path = ""
351 arch_variants = ["macos-arm64_x86_64", "macos-" + env["arch"]]
352 for arch in arch_variants:
353 mvk_path = detect_mvk(env, arch)
354 if mvk_path != "":
355 mvk_path = os.path.join(mvk_path, arch)
356 break
357
358 if mvk_path != "":
359 env.Append(LINKFLAGS=["-L" + mvk_path])
360 else:
361 print_error(
362 "MoltenVK SDK installation directory not found, use 'vulkan_sdk_path' SCons parameter to specify SDK path."
363 )
364 sys.exit(255)
365
366 if len(extra_frameworks) > 0:
367 frameworks = [item for key in sorted(extra_frameworks) for item in ["-framework", key]]
368 env.Append(LINKFLAGS=frameworks)