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