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

from platform_visionos_builders import generate_bundle
from SCons.Script import Glob

from platform_methods import combine_libs_apple_embedded, setup_swift_builder

Import("env")

swift_files = Glob("*.swift")
swift_file_names = list(map(lambda f: f.name, swift_files))

mm_files = Glob("*.mm")
mm_file_names = list(map(lambda f: f.name, mm_files))

env_visionos = env.Clone()

# Configure Swift builder
additional_swift_file_names = ["../../drivers/apple_embedded/godot_swiftui_view_controller.swift"]
apple_platform = env["APPLE_PLATFORM"]
sdk_path = env["APPLE_SDK_PATH"]
current_path = Dir(".").abspath
bridging_header_filename = "bridging_header_visionos.h"
# The Swift sources are compiled together into a single object that's linked into the library.
swift_module = setup_swift_builder(
    env_visionos,
    apple_platform,
    sdk_path,
    current_path,
    bridging_header_filename,
    swift_file_names + additional_swift_file_names,
)

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

visionos_lib = env_visionos.add_library("visionos", mm_file_names + list(swift_module))
if "LIBS_EXTERNAL" in env_visionos:
    env_visionos.Depends(visionos_lib, env_visionos["LIBS_EXTERNAL"])

# Build the Swift->ObjC interop header before any ObjC++ source that #imports it.
env_visionos.Depends(visionos_lib, env_visionos["SWIFT_OBJC_HEADER_TARGET"])

combine_command = env_visionos.Command(
    "#bin/libgodot" + env_visionos["LIBSUFFIX"], [visionos_lib] + env_visionos["LIBS"], combine_libs_apple_embedded
)

if env["generate_bundle"]:
    env.AlwaysBuild(env.CommandNoCache("generate_bundle", combine_command, env.Run(generate_bundle)))
