1#!/usr/bin/env bash
2
3set -euo pipefail
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6MODULE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
7VENDOR_DIR="${MODULE_DIR}/Vendor/libghostty"
8
9GHOSTTY_SOURCE_DIR="${GHOSTTY_SOURCE_DIR:-${HOME}/ghostty}"
10GHOSTTY_ZIG_VERSION="${GHOSTTY_ZIG_VERSION:-0.16.0}"
11GHOSTTY_ZIG="${GHOSTTY_ZIG:-}"
12
13log() {
14 printf '[libghostty-ios16] %s\n' "$*"
15}
16
17die() {
18 printf '[libghostty-ios16] error: %s\n' "$*" >&2
19 exit 1
20}
21
22require_cmd() {
23 command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
24}
25
26ensure_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
59require_cmd git
60require_cmd xcodebuild
61require_cmd xcrun
62require_cmd rsync
63ensure_zig
64
65ghostty_ref="$(git -C "${GHOSTTY_SOURCE_DIR}" rev-parse HEAD)"
66log "using Ghostty source: ${GHOSTTY_SOURCE_DIR} @ ${ghostty_ref}"
67log "using Zig: ${GHOSTTY_ZIG} ($("${GHOSTTY_ZIG}" version))"
68log "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
88xcframework="${GHOSTTY_SOURCE_DIR}/macos/GhosttyKit.xcframework"
89ios_archive="${xcframework}/ios-arm64/libghostty-internal.a"
90sim_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
94log "stripping iOS archives"
95xcrun strip -S -x "${ios_archive}"
96xcrun strip -S -x "${sim_archive}"
97
98log "copying iOS archives into ${VENDOR_DIR}/GhosttyKit.xcframework"
99cp "${ios_archive}" "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64/libghostty-fat.a"
100cp "${sim_archive}" "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64-simulator/libghostty-fat.a"
101rsync -a --delete "${xcframework}/ios-arm64/Headers/" \
102 "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64/Headers/"
103rsync -a --delete "${xcframework}/ios-arm64-simulator/Headers/" \
104 "${VENDOR_DIR}/GhosttyKit.xcframework/ios-arm64-simulator/Headers/"
105sed -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
109log "done"