0.2.0-dev Download ZIP
502 lines ·17.3 KB Raw
| 1 | import os |
| 2 | import platform |
| 3 | import shutil |
| 4 | import subprocess |
| 5 | import sys |
| 6 | |
| 7 | import methods |
| 8 | |
| 9 | # NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle |
| 10 | |
| 11 | |
| 12 | compatibility_platform_aliases = { |
| 13 | "osx": "macos", |
| 14 | "iphone": "ios", |
| 15 | "x11": "linuxbsd", |
| 16 | "javascript": "web", |
| 17 | } |
| 18 | |
| 19 | # CPU architecture options. |
| 20 | architectures = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc64", "wasm32", "loongarch64"] |
| 21 | architecture_aliases = { |
| 22 | "x86": "x86_32", |
| 23 | "x64": "x86_64", |
| 24 | "amd64": "x86_64", |
| 25 | "armv7": "arm32", |
| 26 | "armv8": "arm64", |
| 27 | "arm64v8": "arm64", |
| 28 | "aarch64": "arm64", |
| 29 | "rv": "rv64", |
| 30 | "riscv": "rv64", |
| 31 | "riscv64": "rv64", |
| 32 | "ppc64le": "ppc64", |
| 33 | "loong64": "loongarch64", |
| 34 | } |
| 35 | |
| 36 | |
| 37 | def detect_arch(): |
| 38 | host_machine = platform.machine().lower() |
| 39 | if host_machine in architectures: |
| 40 | return host_machine |
| 41 | elif host_machine in architecture_aliases.keys(): |
| 42 | return architecture_aliases[host_machine] |
| 43 | elif "86" in host_machine: |
| 44 | # Catches x86, i386, i486, i586, i686, etc. |
| 45 | return "x86_32" |
| 46 | else: |
| 47 | methods.print_warning(f'Unsupported CPU architecture: "{host_machine}". Falling back to x86_64.') |
| 48 | return "x86_64" |
| 49 | |
| 50 | |
| 51 | def validate_arch(arch, platform_name, supported_arches): |
| 52 | if arch not in supported_arches: |
| 53 | methods.print_error( |
| 54 | f'Unsupported CPU architecture "{arch}" for {platform_name}. Supported architectures are: {", ".join(supported_arches)}.' |
| 55 | ) |
| 56 | sys.exit(255) |
| 57 | |
| 58 | |
| 59 | def get_build_version(short): |
| 60 | import version |
| 61 | |
| 62 | name = "custom_build" |
| 63 | if os.getenv("BUILD_NAME") is not None: |
| 64 | name = os.getenv("BUILD_NAME") |
| 65 | v = f"{version.major}.{version.minor}" |
| 66 | if version.patch > 0: |
| 67 | v += f".{version.patch}" |
| 68 | status = version.status |
| 69 | if not short: |
| 70 | if os.getenv("GODOT_VERSION_STATUS") is not None: |
| 71 | status = str(os.getenv("GODOT_VERSION_STATUS")) |
| 72 | v += f".{status}.{name}" |
| 73 | return v |
| 74 | |
| 75 | |
| 76 | def lipo(prefix, suffix): |
| 77 | from pathlib import Path |
| 78 | |
| 79 | target_bin = "" |
| 80 | lipo_command = ["lipo", "-create"] |
| 81 | arch_found = 0 |
| 82 | |
| 83 | for arch in architectures: |
| 84 | bin_name = prefix + "." + arch + suffix |
| 85 | if Path(bin_name).is_file(): |
| 86 | target_bin = bin_name |
| 87 | lipo_command += [bin_name] |
| 88 | arch_found += 1 |
| 89 | |
| 90 | if arch_found > 1: |
| 91 | target_bin = prefix + ".fat" + suffix |
| 92 | lipo_command += ["-output", target_bin] |
| 93 | subprocess.run(lipo_command) |
| 94 | |
| 95 | return target_bin |
| 96 | |
| 97 | |
| 98 | def check_accesskit_version(path, req_ver="0.23.1"): |
| 99 | def int_or_zero(i): |
| 100 | try: |
| 101 | return int(i) |
| 102 | except (TypeError, ValueError): |
| 103 | return 0 |
| 104 | |
| 105 | def ver_parse(a): |
| 106 | return [int_or_zero(i) for i in a.split(".")] |
| 107 | |
| 108 | def file_hash(fname): |
| 109 | import hashlib |
| 110 | |
| 111 | sha1 = hashlib.sha1() |
| 112 | with open(fname, "rb") as f: |
| 113 | while True: |
| 114 | data = f.read(4096) |
| 115 | if not data: |
| 116 | break |
| 117 | sha1.update(data) |
| 118 | return sha1.hexdigest() |
| 119 | |
| 120 | deps_folder = os.getenv("LOCALAPPDATA") |
| 121 | if deps_folder: |
| 122 | deps_folder = os.path.join(deps_folder, "Godot", "build_deps") |
| 123 | else: |
| 124 | # Cross-compiling, the deps install script puts things in `bin`. |
| 125 | # Getting an absolute path to it is a bit hacky in Python. |
| 126 | try: |
| 127 | import inspect |
| 128 | |
| 129 | caller_frame = inspect.stack()[1] |
| 130 | caller_script_dir = os.path.dirname(os.path.abspath(caller_frame[1])) |
| 131 | deps_folder = os.path.abspath(os.path.join(caller_script_dir, "..", "..", "bin", "build_deps")) |
| 132 | except Exception: # Give up. |
| 133 | deps_folder = "" |
| 134 | |
| 135 | if os.path.abspath(path) == os.path.join(deps_folder, "accesskit"): # Check auto-downloaded dependency only. |
| 136 | verfile = os.path.join(path, "version") |
| 137 | if os.path.exists(verfile): |
| 138 | with open(verfile) as f: |
| 139 | dep_version = f.read() |
| 140 | else: |
| 141 | # Compatibility, use known hashes to detect version. |
| 142 | hash = file_hash(os.path.join(path, "include", "accesskit.h")) |
| 143 | if hash == "300e20f908da029fc1e3ffa37bc0d2e06adc4334": |
| 144 | dep_version = "0.23.1" |
| 145 | elif hash == "02f692f8282c37156092913a3da136825c14d029": |
| 146 | dep_version = "0.22.3" |
| 147 | else: |
| 148 | dep_version = "0.21.3" |
| 149 | |
| 150 | if ver_parse(dep_version) != ver_parse(req_ver): |
| 151 | methods.print_warning( |
| 152 | f"Incompatible AccessKit version detected. Version required {req_ver}, version found {dep_version}.\n" |
| 153 | f"You can install required version by running `python3 {os.path.join('misc', 'scripts', 'install_accesskit.py')}`.\n" |
| 154 | "See the documentation for more information:\n\t" |
| 155 | "https://docs.godotengine.org/en/latest/engine_details/development/compiling/compiling_for_macos.html#compiling-with-accesskit-support" |
| 156 | "\nAlternatively, disable this driver by compiling with `accesskit=no` explicitly." |
| 157 | ) |
| 158 | return False |
| 159 | |
| 160 | return True |
| 161 | |
| 162 | |
| 163 | def get_mvk_sdk_path(osname): |
| 164 | def int_or_zero(i): |
| 165 | try: |
| 166 | return int(i) |
| 167 | except (TypeError, ValueError): |
| 168 | return 0 |
| 169 | |
| 170 | def ver_parse(a): |
| 171 | return [int_or_zero(i) for i in a.split(".")] |
| 172 | |
| 173 | dirname = os.path.expanduser("~/VulkanSDK") |
| 174 | if not os.path.exists(dirname): |
| 175 | return "" |
| 176 | |
| 177 | ver_min = ver_parse("1.3.231.0") |
| 178 | ver_num = ver_parse("0.0.0.0") |
| 179 | files = os.listdir(dirname) |
| 180 | lib_name_out = dirname |
| 181 | for file in files: |
| 182 | if os.path.isdir(os.path.join(dirname, file)): |
| 183 | ver_comp = ver_parse(file) |
| 184 | if ver_comp > ver_num and ver_comp >= ver_min: |
| 185 | # Try new SDK location. |
| 186 | lib_name = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework/" + osname + "/") |
| 187 | if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")): |
| 188 | ver_num = ver_comp |
| 189 | lib_name_out = os.path.join(os.path.join(dirname, file), "macOS/lib/MoltenVK.xcframework") |
| 190 | else: |
| 191 | # Try old SDK location. |
| 192 | lib_name = os.path.join( |
| 193 | os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework/" + osname + "/" |
| 194 | ) |
| 195 | if os.path.isfile(os.path.join(lib_name, "libMoltenVK.a")): |
| 196 | ver_num = ver_comp |
| 197 | lib_name_out = os.path.join(os.path.join(dirname, file), "MoltenVK/MoltenVK.xcframework") |
| 198 | |
| 199 | return lib_name_out |
| 200 | |
| 201 | |
| 202 | def detect_mvk(env, osname): |
| 203 | mvk_list = [ |
| 204 | get_mvk_sdk_path(osname), |
| 205 | "/opt/homebrew/Frameworks/MoltenVK.xcframework", |
| 206 | "/usr/local/homebrew/Frameworks/MoltenVK.xcframework", |
| 207 | "/opt/local/Frameworks/MoltenVK.xcframework", |
| 208 | ] |
| 209 | if env["vulkan_sdk_path"] != "": |
| 210 | mvk_list.insert(0, os.path.expanduser(env["vulkan_sdk_path"])) |
| 211 | mvk_list.insert( |
| 212 | 0, |
| 213 | os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "macOS/lib/MoltenVK.xcframework"), |
| 214 | ) |
| 215 | mvk_list.insert( |
| 216 | 0, |
| 217 | os.path.join(os.path.expanduser(env["vulkan_sdk_path"]), "MoltenVK/MoltenVK.xcframework"), |
| 218 | ) |
| 219 | |
| 220 | for mvk_path in mvk_list: |
| 221 | if mvk_path and os.path.isfile(os.path.join(mvk_path, f"{osname}/libMoltenVK.a")): |
| 222 | print(f"MoltenVK found at: {mvk_path}") |
| 223 | return mvk_path |
| 224 | |
| 225 | return "" |
| 226 | |
| 227 | |
| 228 | def combine_libs_apple_embedded(target, source, env): |
| 229 | lib_path = target[0].srcnode().abspath |
| 230 | if "osxcross" in env: |
| 231 | libtool = "$APPLE_TOOLCHAIN_PATH/usr/bin/${apple_target_triple}libtool" |
| 232 | else: |
| 233 | libtool = "$APPLE_TOOLCHAIN_PATH/usr/bin/libtool" |
| 234 | env.Execute( |
| 235 | libtool + ' -static -o "' + lib_path + '" ' + " ".join([('"' + lib.srcnode().abspath + '"') for lib in source]) |
| 236 | ) |
| 237 | |
| 238 | |
| 239 | def lipo_and_copy_apple_embedded( |
| 240 | platform, framework_dir, framework_dir_sim, rel_prefix, dbg_prefix, module_prefix, app_dir, env |
| 241 | ): |
| 242 | bin_dir = env.Dir("#bin").abspath |
| 243 | |
| 244 | # Lipo template libraries. |
| 245 | # |
| 246 | # env.extra_suffix contains ".simulator" when building for simulator, |
| 247 | # but it's undesired when calling lipo() |
| 248 | extra_suffix = env.extra_suffix.replace(".simulator", "") |
| 249 | rel_target_bin = lipo(bin_dir + "/libgodot" + module_prefix + "." + rel_prefix, extra_suffix + ".a") |
| 250 | dbg_target_bin = lipo(bin_dir + "/libgodot" + module_prefix + "." + dbg_prefix, extra_suffix + ".a") |
| 251 | rel_target_bin_sim = lipo( |
| 252 | bin_dir + "/libgodot" + module_prefix + "." + rel_prefix, ".simulator" + extra_suffix + ".a" |
| 253 | ) |
| 254 | dbg_target_bin_sim = lipo( |
| 255 | bin_dir + "/libgodot" + module_prefix + "." + dbg_prefix, ".simulator" + extra_suffix + ".a" |
| 256 | ) |
| 257 | # Assemble Xcode project bundle. |
| 258 | if rel_target_bin != "": |
| 259 | print(f' Copying "{platform}" release framework') |
| 260 | shutil.copy( |
| 261 | rel_target_bin, |
| 262 | app_dir |
| 263 | + "/libgodot" |
| 264 | + module_prefix |
| 265 | + "." |
| 266 | + platform |
| 267 | + ".release.xcframework/" |
| 268 | + framework_dir |
| 269 | + "/libgodot" |
| 270 | + module_prefix |
| 271 | + ".a", |
| 272 | ) |
| 273 | if dbg_target_bin != "": |
| 274 | print(f' Copying "{platform}" debug framework') |
| 275 | shutil.copy( |
| 276 | dbg_target_bin, |
| 277 | app_dir |
| 278 | + "/libgodot" |
| 279 | + module_prefix |
| 280 | + "." |
| 281 | + platform |
| 282 | + ".debug.xcframework/" |
| 283 | + framework_dir |
| 284 | + "/libgodot" |
| 285 | + module_prefix |
| 286 | + ".a", |
| 287 | ) |
| 288 | if rel_target_bin_sim != "": |
| 289 | print(f' Copying "{platform}" (simulator) release framework') |
| 290 | shutil.copy( |
| 291 | rel_target_bin_sim, |
| 292 | app_dir |
| 293 | + "/libgodot" |
| 294 | + module_prefix |
| 295 | + "." |
| 296 | + platform |
| 297 | + ".release.xcframework/" |
| 298 | + framework_dir_sim |
| 299 | + "/libgodot" |
| 300 | + module_prefix |
| 301 | + ".a", |
| 302 | ) |
| 303 | if dbg_target_bin_sim != "": |
| 304 | print(f' Copying "{platform}" (simulator) debug framework') |
| 305 | shutil.copy( |
| 306 | dbg_target_bin_sim, |
| 307 | app_dir |
| 308 | + "/libgodot" |
| 309 | + module_prefix |
| 310 | + "." |
| 311 | + platform |
| 312 | + ".debug.xcframework/" |
| 313 | + framework_dir_sim |
| 314 | + "/libgodot" |
| 315 | + module_prefix |
| 316 | + ".a", |
| 317 | ) |
| 318 | |
| 319 | |
| 320 | def generate_bundle_apple_embedded(platform, framework_dir, framework_dir_sim, use_mkv, target, source, env): |
| 321 | # Template bundle. |
| 322 | extra_suffix = env.extra_suffix.replace(".simulator", "") |
| 323 | app_prefix = "godot." + platform |
| 324 | rel_prefix = platform + "." + "template_release" |
| 325 | dbg_prefix = platform + "." + "template_debug" |
| 326 | if env.dev_build: |
| 327 | app_prefix += ".dev" |
| 328 | rel_prefix += ".dev" |
| 329 | dbg_prefix += ".dev" |
| 330 | if env["precision"] == "double": |
| 331 | app_prefix += ".double" |
| 332 | rel_prefix += ".double" |
| 333 | dbg_prefix += ".double" |
| 334 | |
| 335 | app_dir = env.Dir("#bin/" + platform + "_xcode").abspath |
| 336 | templ = env.Dir("#misc/dist/apple_embedded_xcode").abspath |
| 337 | if os.path.exists(app_dir): |
| 338 | shutil.rmtree(app_dir) |
| 339 | shutil.copytree(templ, app_dir) |
| 340 | |
| 341 | lipo_and_copy_apple_embedded(platform, framework_dir, framework_dir_sim, rel_prefix, dbg_prefix, "", app_dir, env) |
| 342 | if "MODULES_EXTERNAL" in env: |
| 343 | for mod in env["MODULES_EXTERNAL"]: |
| 344 | lipo_and_copy_apple_embedded( |
| 345 | platform, framework_dir, framework_dir_sim, rel_prefix, dbg_prefix, mod, app_dir, env |
| 346 | ) |
| 347 | |
| 348 | # Remove other platform xcframeworks |
| 349 | for entry in os.listdir(app_dir): |
| 350 | if (entry.startswith("libgodot.") or entry.startswith("libgodot_")) and entry.endswith(".xcframework"): |
| 351 | parts = entry.split(".") |
| 352 | if len(parts) >= 3 and parts[1] != platform: |
| 353 | full_path = os.path.join(app_dir, entry) |
| 354 | shutil.rmtree(full_path) |
| 355 | |
| 356 | if use_mkv: |
| 357 | mvk_path = detect_mvk(env, "ios-arm64") |
| 358 | if mvk_path != "": |
| 359 | shutil.copytree(mvk_path + "/ios-arm64", app_dir + "/MoltenVK.xcframework/ios-arm64") |
| 360 | shutil.copytree( |
| 361 | mvk_path + "/ios-arm64_x86_64-simulator", app_dir + "/MoltenVK.xcframework/ios-arm64_x86_64-simulator" |
| 362 | ) |
| 363 | shutil.copy(mvk_path + "/Info.plist", app_dir + "/MoltenVK.xcframework/Info.plist") |
| 364 | |
| 365 | if env["accesskit"]: |
| 366 | ak_sdk_path = env.get("accesskit_sdk_path") |
| 367 | if ak_sdk_path: |
| 368 | ak_xcf = os.path.join(ak_sdk_path, "lib", "ios", "AccessKit.xcframework") |
| 369 | if os.path.isdir(ak_xcf): |
| 370 | shutil.copytree(ak_xcf, app_dir + "/AccessKit.xcframework") |
| 371 | |
| 372 | # ZIP Xcode project bundle. |
| 373 | zip_dir = env.Dir("#bin/" + (app_prefix + extra_suffix).replace(".", "_")).abspath |
| 374 | shutil.make_archive(zip_dir, "zip", root_dir=app_dir) |
| 375 | shutil.rmtree(app_dir) |
| 376 | |
| 377 | |
| 378 | def setup_swift_builder( |
| 379 | env, |
| 380 | apple_platform, |
| 381 | sdk_path, |
| 382 | current_path, |
| 383 | bridging_header_filename, |
| 384 | all_swift_files, |
| 385 | ): |
| 386 | # Compile Swift sources and emit a Swift->ObjC interop header. |
| 387 | |
| 388 | from SCons.Script import Action, Builder |
| 389 | |
| 390 | if apple_platform == "macos": |
| 391 | target_suffix = "macosx10.9" |
| 392 | |
| 393 | elif apple_platform == "ios": |
| 394 | target_suffix = "ios15.0" # iOS 15.0 needed for SwiftUI lifecycle |
| 395 | |
| 396 | elif apple_platform == "iossimulator": |
| 397 | target_suffix = "ios15.0-simulator" # iOS 15.0 needed for SwiftUI lifecycle |
| 398 | |
| 399 | elif apple_platform == "visionos": |
| 400 | target_suffix = "xros26.0" |
| 401 | |
| 402 | elif apple_platform == "visionossimulator": |
| 403 | target_suffix = "xros26.0-simulator" |
| 404 | |
| 405 | else: |
| 406 | raise Exception("Invalid platform argument passed to detect_darwin_sdk_path") |
| 407 | |
| 408 | swiftc_target = env["arch"] + "-apple-" + target_suffix |
| 409 | |
| 410 | if "SWIFT_COMPILER" in env and env["SWIFT_COMPILER"] != "": |
| 411 | swiftc_path = env["SWIFT_COMPILER"] |
| 412 | elif "osxcross" not in env: |
| 413 | swiftc_path = "$APPLE_TOOLCHAIN_PATH/usr/bin/swiftc" |
| 414 | else: |
| 415 | swiftc_path = None |
| 416 | |
| 417 | if swiftc_path is None: |
| 418 | raise Exception("Swift compiler path is not set. Please set SWIFT_COMPILER.") |
| 419 | |
| 420 | bridging_header_path = current_path + "/" + bridging_header_filename |
| 421 | swift_module_name = "godot_swift_module" |
| 422 | # Standard `<module>-Swift.h` name plus `.gen.h` so it's covered by `*.gen.*` in `.gitignore`. |
| 423 | swift_objc_header_path = current_path + "/" + swift_module_name + "-Swift.gen.h" |
| 424 | env["SWIFTC"] = swiftc_path # Swift compiler |
| 425 | # Flags for the whole-module Swift compile. |
| 426 | common_swift_flags = [ |
| 427 | "-warnings-as-errors", |
| 428 | "-cxx-interoperability-mode=default", |
| 429 | "-emit-object", |
| 430 | "-emit-objc-header-path", |
| 431 | swift_objc_header_path, |
| 432 | "-target", |
| 433 | swiftc_target, |
| 434 | "-sdk", |
| 435 | sdk_path, |
| 436 | "-import-objc-header", |
| 437 | bridging_header_path, |
| 438 | "-swift-version", |
| 439 | "6", |
| 440 | "-parse-as-library", |
| 441 | "-module-name", |
| 442 | swift_module_name, |
| 443 | "-I./", # Pass the current directory as the header root so bridging headers can include files from any point of the hierarchy |
| 444 | ] |
| 445 | # All sources are compiled together into a single object, which requires whole-module mode. |
| 446 | # Whole-module mode is also required for `-emit-objc-header-path`; per-file mode drops it. |
| 447 | env["SWIFTCFLAGS"] = ["-wmo"] + common_swift_flags |
| 448 | |
| 449 | if "osxcross" in env: |
| 450 | env.Append( |
| 451 | SWIFTCFLAGS=[ |
| 452 | "-resource-dir", |
| 453 | "/root/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift", |
| 454 | "-Xfrontend", |
| 455 | "-enable-cross-import-overlays", |
| 456 | ] |
| 457 | ) |
| 458 | |
| 459 | if env["debug_symbols"]: |
| 460 | env.Append(SWIFTCFLAGS=["-g"]) |
| 461 | |
| 462 | if env["optimize"] in ["speed", "speed_trace"]: |
| 463 | env.Append(SWIFTCFLAGS=["-O"]) |
| 464 | |
| 465 | elif env["optimize"] == "size": |
| 466 | env.Append(SWIFTCFLAGS=["-Osize"]) |
| 467 | |
| 468 | elif env["optimize"] in ["debug", "none"]: |
| 469 | env.Append(SWIFTCFLAGS=["-Onone"]) |
| 470 | |
| 471 | def generate_swift_action(source, target, env, for_signature): |
| 472 | swift_files_string = '"' + '" "'.join([file.abspath for file in source]) + '"' |
| 473 | compile_command = "$SWIFTC " + swift_files_string + " -o $TARGET $SWIFTCFLAGS" |
| 474 | |
| 475 | swift_comdstr = env.get("SWIFTCOMSTR") |
| 476 | if swift_comdstr is not None: |
| 477 | swift_action = Action(compile_command, cmdstr=swift_comdstr) |
| 478 | else: |
| 479 | swift_action = Action(compile_command) |
| 480 | |
| 481 | return swift_action |
| 482 | |
| 483 | def swift_emitter(target, source, env): |
| 484 | # Redirect the object, but keep the interop header next to the Swift sources so |
| 485 | # ObjC++ in the same directory resolves it with a quoted `#import`. |
| 486 | target, source = methods.redirect_emitter(target, source, env) |
| 487 | return target + [env.File(swift_objc_header_path)], source |
| 488 | |
| 489 | # Define Builder that compiles all Swift sources into a single object file plus the |
| 490 | # `@objc` interop header. |
| 491 | swift_builder = Builder( |
| 492 | generator=generate_swift_action, suffix=env["OBJSUFFIX"], src_suffix=".swift", emitter=swift_emitter |
| 493 | ) |
| 494 | |
| 495 | env.Append(BUILDERS={"SwiftModule": swift_builder}) |
| 496 | |
| 497 | swift_sources = [env.File(current_path + "/" + file) for file in all_swift_files] |
| 498 | swift_module, swift_objc_header = env.SwiftModule(current_path + "/" + swift_module_name, swift_sources) |
| 499 | # Lets ObjC++ sources that `#import` the interop header order against its generation. |
| 500 | env["SWIFT_OBJC_HEADER_TARGET"] = swift_objc_header |
| 501 | |
| 502 | return [swift_module] |