#!/usr/bin/env bash # Builds the iOS app (arm64) and installs + launches it on a simulator or an attached device. # # ios/build.sh simulator build; boots $AVA2_IOS_SIMULATOR (default: first available # iPhone), installs and launches # ios/build.sh device device build, signed with $AVA2_IOS_SIGN_IDENTITY and # $AVA2_IOS_PROFILE (.mobileprovision); installs + launches when # `xcrun devicectl` sees a device # ios/build.sh deps [device] only (re)build the native dependencies # --no-run (any mode) build only # # Like the APK, the .app holds code only; push the client root with ios/push_assets.py. # Native dependencies (static SDL3 + FreeType) are built once into build/ios-deps (sources are # shared with build/android-deps when already downloaded). # Environment overrides: AVA2_SDK_ROOT, AVA2_SOURCE_DIR (CMake source tree, default this checkout), # AVA2_IOS_BUNDLE_ID (default com.metin2.ava2), AVA2_IOS_SIMULATOR, AVA2_IOS_DEVICE, # AVA2_IOS_BUILD_TYPE (default RelWithDebInfo). set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" ROOT="$(cd "$HERE/.." && pwd)" SOURCE_DIR="${AVA2_SOURCE_DIR:-$ROOT}" SDL_VERSION=3.4.16 FREETYPE_VERSION=2.13.3 DEPLOYMENT_TARGET=15.0 MODE=simulator RUN=1 DEPS_ONLY=0 for arg in "$@"; do case "$arg" in sim|simulator) MODE=simulator ;; device) MODE=device ;; deps) DEPS_ONLY=1 ;; --no-run) RUN=0 ;; *) echo "unknown argument: $arg"; exit 2 ;; esac done if [[ "$MODE" == simulator ]]; then SDK=iphonesimulator; else SDK=iphoneos; fi xcrun --sdk "$SDK" --show-sdk-path >/dev/null 2>&1 || { echo "Xcode $SDK SDK not found (xcode-select)"; exit 1; } DEPS="$ROOT/build/ios-deps" PREFIX="$DEPS/prefix-$SDK" mkdir -p "$DEPS/src" IOS_CMAKE=(-DCMAKE_SYSTEM_NAME=iOS -DCMAKE_OSX_SYSROOT="$SDK" -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_OSX_DEPLOYMENT_TARGET=$DEPLOYMENT_TARGET) fetch() { # url file [[ -f "$DEPS/src/$2" ]] && return if [[ -f "$ROOT/build/android-deps/src/$2" ]]; then cp "$ROOT/build/android-deps/src/$2" "$DEPS/src/$2"; return; fi curl -fL --retry 3 -o "$DEPS/src/$2.part" "$1" || { echo "download failed: $1"; exit 1; } mv -f "$DEPS/src/$2.part" "$DEPS/src/$2" } cmake_dep() { # source-dir build-dir extra-args... local src="$1" bld="$2"; shift 2 cmake -S "$src" -B "$bld" "${IOS_CMAKE[@]}" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PREFIX" "$@" cmake --build "$bld" -j"$(sysctl -n hw.ncpu)" cmake --install "$bld" } if [[ ! -f "$PREFIX/lib/libSDL3.a" ]]; then fetch "https://github.com/libsdl-org/SDL/releases/download/release-$SDL_VERSION/SDL3-$SDL_VERSION.tar.gz" "SDL3-$SDL_VERSION.tar.gz" rm -rf "$DEPS/src/SDL3-$SDL_VERSION-$SDK" && mkdir "$DEPS/src/SDL3-$SDL_VERSION-$SDK" tar -xzf "$DEPS/src/SDL3-$SDL_VERSION.tar.gz" -C "$DEPS/src/SDL3-$SDL_VERSION-$SDK" --strip-components 1 if [[ "$SDK" == iphonesimulator ]]; then # The simulator's Metal device does not report MTLGPUFamilyApple3, so SDL_GPU refuses it # although the host GPU runs the client fine. Skip the check there; device builds are untouched. perl -0pi -e 's/(hasHardwareSupport = \[device supportsFamily:MTLGPUFamilyApple3\])/$1 || TARGET_OS_SIMULATOR/' \ "$DEPS/src/SDL3-$SDL_VERSION-$SDK/src/gpu/metal/SDL_gpu_metal.m" grep -q "MTLGPUFamilyApple3\] || TARGET_OS_SIMULATOR" \ "$DEPS/src/SDL3-$SDL_VERSION-$SDK/src/gpu/metal/SDL_gpu_metal.m" || { echo "SDL simulator patch failed"; exit 1; } fi cmake_dep "$DEPS/src/SDL3-$SDL_VERSION-$SDK" "$DEPS/build-sdl3-$SDK" \ -DSDL_SHARED=OFF -DSDL_STATIC=ON -DSDL_TEST_LIBRARY=OFF -DSDL_TESTS=OFF -DSDL_EXAMPLES=OFF fi if [[ ! -f "$PREFIX/lib/libfreetype.a" ]]; then fetch "https://download.savannah.gnu.org/releases/freetype/freetype-$FREETYPE_VERSION.tar.xz" "freetype-$FREETYPE_VERSION.tar.xz" rm -rf "$DEPS/src/freetype-$FREETYPE_VERSION" && tar -xJf "$DEPS/src/freetype-$FREETYPE_VERSION.tar.xz" -C "$DEPS/src" cmake_dep "$DEPS/src/freetype-$FREETYPE_VERSION" "$DEPS/build-freetype-$SDK" -DBUILD_SHARED_LIBS=OFF \ -DFT_DISABLE_ZLIB=ON -DFT_DISABLE_BZIP2=ON -DFT_DISABLE_PNG=ON -DFT_DISABLE_HARFBUZZ=ON -DFT_DISABLE_BROTLI=ON fi [[ "$DEPS_ONLY" == 1 ]] && exit 0 BUILD="$ROOT/build/ios-$SDK" CONFIG_ARGS=("${IOS_CMAKE[@]}" -DCMAKE_BUILD_TYPE="${AVA2_IOS_BUILD_TYPE:-RelWithDebInfo}" -DAVA2_BUILD_HOST=ON -DAVA2_EMBED_PYTHON=ON -DBUILD_TESTING=OFF -DSDL3_DIR="$PREFIX/lib/cmake/SDL3" -DCMAKE_PREFIX_PATH="$PREFIX" -DAVA2_IOS_BUNDLE_ID="${AVA2_IOS_BUNDLE_ID:-com.metin2.ava2}") [[ -n "${AVA2_SDK_ROOT:-}" ]] && CONFIG_ARGS+=(-DAVA2_SDK_ROOT="$AVA2_SDK_ROOT") cmake -S "$SOURCE_DIR" -B "$BUILD" "${CONFIG_ARGS[@]}" cmake --build "$BUILD" --target ava2_client mtpython_stdlib -j"$(sysctl -n hw.ncpu)" APP="$BUILD/ava2_client.app" BUNDLE_ID="${AVA2_IOS_BUNDLE_ID:-com.metin2.ava2}" if [[ "$MODE" == simulator ]]; then codesign --force --sign - --timestamp=none "$APP" else [[ -n "${AVA2_IOS_SIGN_IDENTITY:-}" && -f "${AVA2_IOS_PROFILE:-}" ]] || { echo "Device builds need AVA2_IOS_SIGN_IDENTITY (security find-identity -v -p codesigning) and" echo "AVA2_IOS_PROFILE (a development .mobileprovision for $BUNDLE_ID)."; exit 1; } cp "$AVA2_IOS_PROFILE" "$APP/embedded.mobileprovision" ENTITLEMENTS="$BUILD/entitlements.plist" security cms -D -i "$AVA2_IOS_PROFILE" > "$BUILD/profile.plist" /usr/libexec/PlistBuddy -x -c "Print :Entitlements" "$BUILD/profile.plist" > "$ENTITLEMENTS" codesign --force --sign "$AVA2_IOS_SIGN_IDENTITY" --entitlements "$ENTITLEMENTS" --timestamp=none "$APP" fi du -sh "$APP" [[ "$RUN" == 1 ]] || exit 0 if [[ "$MODE" == simulator ]]; then SIM="${AVA2_IOS_SIMULATOR:-}" if [[ -z "$SIM" ]]; then SIM="$(xcrun simctl list devices booted | sed -nE 's/.*\(([0-9A-F-]{36})\) \(Booted\).*/\1/p' | head -1)" fi if [[ -z "$SIM" ]]; then SIM="$(xcrun simctl list devices available | grep -E '^ +iPhone' | tail -1 | sed -nE 's/.*\(([0-9A-F-]{36})\).*/\1/p')" fi [[ -n "$SIM" ]] || { echo "No iOS simulator available (Xcode > Settings > Platforms)"; exit 1; } xcrun simctl boot "$SIM" 2>/dev/null || true open -a Simulator --args -CurrentDeviceUDID "$SIM" || true xcrun simctl bootstatus "$SIM" -b >/dev/null xcrun simctl install "$SIM" "$APP" xcrun simctl launch "$SIM" "$BUNDLE_ID" echo "Launched on simulator $SIM. Logs: xcrun simctl spawn $SIM log stream --predicate 'process == \"ava2_client\"'" else DEVICE="${AVA2_IOS_DEVICE:-}" if [[ -z "$DEVICE" ]]; then DEVICE="$(xcrun devicectl list devices 2>/dev/null | awk '/available|connected/ && /iPhone|iPad/ {print $3; exit}')" fi if [[ -z "$DEVICE" ]]; then echo "No device attached; app not installed."; exit 0; fi xcrun devicectl device install app --device "$DEVICE" "$APP" xcrun devicectl device process launch --device "$DEVICE" "$BUNDLE_ID" fi