| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | MODULE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" |
| 7 | VENDOR_DIR="${MODULE_DIR}/Vendor/libghostty" |
| 8 | |
| 9 | GHOSTTY_SOURCE_DIR="${GHOSTTY_SOURCE_DIR:-${HOME}/ghostty}" |
| 10 | GHOSTTY_ZIG_VERSION="${GHOSTTY_ZIG_VERSION:-0.16.0}" |
| 11 | GHOSTTY_ZIG="${GHOSTTY_ZIG:-}" |
| 12 | |
| 13 | log() { |
| 14 | printf '[libghostty-ios16] %s\n' "$*" |
| 15 | } |
| 16 | |
| 17 | die() { |
| 18 | printf '[libghostty-ios16] error: %s\n' "$*" >&2 |
| 19 | exit 1 |
| 20 | } |
| 21 | |
| 22 | require_cmd() { |
| 23 | command -v "$1" >/dev/null 2>&1 || die "missing required command: $1" |
| 24 | } |
| 25 | |
| 26 | ensure_zig() { |
| 27 | if [[ -n "${GHOSTTY_ZIG}" ]]; then |
| 28 | [[ -x "${GHOSTTY_ZIG}" ]] || die "GHOSTTY_ZIG is not executable: ${GHOSTTY_ZIG}" |
| 29 | return |
| 30 | fi |
| 31 | |
| 32 | if command -v zig >/dev/null 2>&1 && [[ "$(zig version)" == "${GHOSTTY_ZIG_VERSION}" ]]; then |
| 33 | GHOSTTY_ZIG="$(command -v zig)" |
| 34 | return |
| 35 | fi |
| 36 | |
| 37 | local cache_dir="${HOME}/.cache/t3code/zig-${GHOSTTY_ZIG_VERSION}" |
| 38 | local archive_arch |
| 39 | archive_arch="$(uname -m)" |
| 40 | case "${archive_arch}" in |
| 41 | arm64) archive_arch="aarch64" ;; |
| 42 | x86_64) archive_arch="x86_64" ;; |
| 43 | *) die "unsupported macOS architecture for Zig download: ${archive_arch}" ;; |
| 44 | esac |
| 45 | |
| 46 | GHOSTTY_ZIG="${cache_dir}/zig" |
| 47 | if [[ -x "${GHOSTTY_ZIG}" ]]; then |
| 48 | return |
| 49 | fi |
| 50 | |
| 51 | require_cmd curl |
| 52 | require_cmd tar |
| 53 | mkdir -p "${cache_dir}" |
| 54 | log "downloading Zig ${GHOSTTY_ZIG_VERSION}" |
| 55 | curl -fsSL "https://ziglang.org/download/${GHOSTTY_ZIG_VERSION}/zig-${archive_arch}-macos-${GHOSTTY_ZIG_VERSION}.tar.xz" \ |
| 56 | | tar -xJ --strip-components=1 -C "${cache_dir}" |
| 57 | } |
| 58 | |
| 59 | require_cmd git |
| 60 | require_cmd xcodebuild |
| 61 | require_cmd xcrun |
| 62 | require_cmd rsync |
| 63 | ensure_zig |
| 64 | |
| 65 | ghostty_ref="$(git -C "${GHOSTTY_SOURCE_DIR}" rev-parse HEAD)" |
| 66 | log "using Ghostty source: ${GHOSTTY_SOURCE_DIR} @ ${ghostty_ref}" |
| 67 | log "using Zig: ${GHOSTTY_ZIG} ($("${GHOSTTY_ZIG}" version))" |
| 68 | log "building GhosttyKit.xcframework" |
| 69 | |
| 70 | ( |
| 71 | cd "${GHOSTTY_SOURCE_DIR}" |
| 72 | PATH="$(dirname "${GHOSTTY_ZIG}"):${PATH}" "${GHOSTTY_ZIG}" build \ |
| 73 | -Dapp-runtime=none \ |
| 74 | -Demit-xcframework=true \ |
| 75 | -Demit-macos-app=false \ |
| 76 | -Demit-exe=false \ |
| 77 | -Demit-docs=false \ |
| 78 | -Demit-webdata=false \ |
| 79 | -Demit-helpgen=false \ |
| 80 | -Demit-terminfo=false \ |
| 81 | -Demit-termcap=false \ |
| 82 | -Demit-themes=false \ |
| 83 | -Doptimize=ReleaseFast \ |
| 84 | -Dstrip \ |
| 85 | -Dxcframework-target=universal |
| 86 | ) |
| 87 | |
| 88 | xcframework="${GHOSTTY_SOURCE_DIR}/macos/GhosttyKit.xcframework" |
| 89 | ios_archive="${xcframework}/ios-arm64/libghostty-internal.a" |
| 90 | sim_archive="${xcframework}/ios-arm64-simulator/libghostty-internal.a" |
| 91 | [[ -f "${ios_archive}" ]] || die "missing built iOS archive: ${ios_archive}" |
| 92 | [[ -f "${sim_archive}" ]] || die "missing built iOS simulator archive: ${sim_archive}" |
| 93 | |
| 94 | log "stripping iOS archives" |
| 95 | xcrun strip -S -x "${ios_archive}" |
| 96 | xcrun strip -S -x "${sim_archive}" |
| 97 | |
| 98 | log "copying iOS archives into ${VENDOR_DIR}/GhosttyKit.xcframework" |
| 99 | cp "${ios_archive}" "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64/libghostty-fat.a" |
| 100 | cp "${sim_archive}" "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64-simulator/libghostty-fat.a" |
| 101 | rsync -a --delete "${xcframework}/ios-arm64/Headers/" \ |
| 102 | "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64/Headers/" |
| 103 | rsync -a --delete "${xcframework}/ios-arm64-simulator/Headers/" \ |
| 104 | "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64-simulator/Headers/" |
| 105 | sed -i '' -e 's/[[:space:]]*$//' \ |
| 106 | "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64/Headers/ghostty.h" \ |
| 107 | "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64-simulator/Headers/ghostty.h" |
| 108 | |
| 109 | log "done" |