godot/platform/ios/SCsub

#!/usr/bin/env python
from misc.utility.scons_hints import *

Import("env")

import os
import shutil

from platform_methods import detect_mvk, lipo


def generate_bundle(target, source, env):
    bin_dir = Dir("#bin").abspath

    # Template bundle.
    app_prefix = "godot." + env["platform"]
    rel_prefix = "libgodot." + env["platform"] + "." + "template_release"
    dbg_prefix = "libgodot." + env["platform"] + "." + "template_debug"
    if env.dev_build:
        app_prefix += ".dev"
        rel_prefix += ".dev"
        dbg_prefix += ".dev"
    if env["precision"] == "double":
        app_prefix += ".double"
        rel_prefix += ".double"
        dbg_prefix += ".double"

    # Lipo template libraries.
    rel_target_bin = lipo(bin_dir + "/" + rel_prefix, env.extra_suffix + ".a")
    dbg_target_bin = lipo(bin_dir + "/" + dbg_prefix, env.extra_suffix + ".a")
    rel_target_bin_sim = lipo(bin_dir + "/" + rel_prefix, ".simulator" + env.extra_suffix + ".a")
    dbg_target_bin_sim = lipo(bin_dir + "/" + dbg_prefix, ".simulator" + env.extra_suffix + ".a")

    # Assemble Xcode project bundle.
    app_dir = Dir("#bin/ios_xcode").abspath
    templ = Dir("#misc/dist/ios_xcode").abspath
    if os.path.exists(app_dir):
        shutil.rmtree(app_dir)
    shutil.copytree(templ, app_dir)
    if rel_target_bin != "":
        shutil.copy(rel_target_bin, app_dir + "/libgodot.ios.release.xcframework/ios-arm64/libgodot.a")
    if dbg_target_bin != "":
        shutil.copy(dbg_target_bin, app_dir + "/libgodot.ios.debug.xcframework/ios-arm64/libgodot.a")
    if rel_target_bin_sim != "":
        shutil.copy(
            rel_target_bin_sim, app_dir + "/libgodot.ios.release.xcframework/ios-arm64_x86_64-simulator/libgodot.a"
        )
    if dbg_target_bin_sim != "":
        shutil.copy(
            dbg_target_bin_sim, app_dir + "/libgodot.ios.debug.xcframework/ios-arm64_x86_64-simulator/libgodot.a"
        )
    mvk_path = detect_mvk(env, "ios-arm64")
    if mvk_path != "":
        shutil.copytree(mvk_path, app_dir + "/MoltenVK.xcframework")

    # ZIP Xcode project bundle.
    zip_dir = Dir("#bin/" + (app_prefix + env.extra_suffix).replace(".", "_")).abspath
    shutil.make_archive(zip_dir, "zip", root_dir=app_dir)
    shutil.rmtree(app_dir)


ios_lib = [
    "godot_ios.mm",
    "os_ios.mm",
    "main.m",
    "app_delegate.mm",
    "view_controller.mm",
    "ios.mm",
    "rendering_context_driver_vulkan_ios.mm",
    "display_server_ios.mm",
    "joypad_ios.mm",
    "godot_view.mm",
    "tts_ios.mm",
    "display_layer.mm",
    "godot_app_delegate.m",
    "godot_view_renderer.mm",
    "device_metrics.m",
    "keyboard_input_view.mm",
    "key_mapping_ios.mm",
    "ios_terminal_logger.mm",
]

env_ios = env.Clone()
ios_lib = env_ios.add_library("ios", ios_lib)

# (iOS) Enable module support
env_ios.Append(CCFLAGS=["-fmodules", "-fcxx-modules"])


def combine_libs(target=None, source=None, env=None):
    lib_path = target[0].srcnode().abspath
    if "osxcross" in env:
        libtool = "$IOS_TOOLCHAIN_PATH/usr/bin/${ios_triple}libtool"
    else:
        libtool = "$IOS_TOOLCHAIN_PATH/usr/bin/libtool"
    env.Execute(
        libtool + ' -static -o "' + lib_path + '" ' + " ".join([('"' + lib.srcnode().abspath + '"') for lib in source])
    )


combine_command = env_ios.Command("#bin/libgodot" + env_ios["LIBSUFFIX"], [ios_lib] + env_ios["LIBS"], combine_libs)

if env["generate_bundle"]:
    generate_bundle_command = env.Command("generate_bundle", [], generate_bundle)
    command = env.AlwaysBuild(generate_bundle_command)
    env.Depends(command, [combine_command])