Layers/scripts/macos/build.sh

101 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
cd "$ROOT"
# Renders plugin.json.in -> $2 with @ENTRYPOINT@ replaced by $1.
render_plugin_json() {
local entrypoint="$1"
local out="$2"
local in="$ROOT/plugin.json.in"
if [ ! -f "$in" ]; then
echo "ERROR: $in not found" >&2
exit 1
fi
sed "s|@ENTRYPOINT@|$entrypoint|g" "$in" > "$out"
}
STAGE="$ROOT/build/bin/com.jesshunter.layers"
APP="$STAGE/bin/Layers.app"
CONTENTS="$APP/Contents"
MACOS_DIR="$CONTENTS/MacOS"
RESOURCES_DIR="$CONTENTS/Resources"
rm -rf "$STAGE"
mkdir -p "$STAGE/bin" "$STAGE/resources" "$MACOS_DIR" "$RESOURCES_DIR"
# Icon PNGs for the KiCad toolbar — rendered straight from Layers.svg.
if command -v rsvg-convert >/dev/null 2>&1 && [ -f "$ROOT/resources/Layers.svg" ]; then
for size in 24 48; do
rsvg-convert --width "$size" --height "$size" \
"$ROOT/resources/Layers.svg" -o "$ROOT/resources/icon-${size}.png"
done
fi
# Local path overrides for sibling checkouts (kicad-ipc-rs).
# Injected only while this script runs; Cargo.toml stays clean for commits.
CARGO_TOML="$ROOT/Cargo.toml"
CARGO_BAK=""
PATCH_APPLIED=0
if [ -d "$ROOT/../kicad-ipc-rs" ] || [ -d "$ROOT/../Siphon" ]; then
CARGO_BAK="$(mktemp "${TMPDIR:-/tmp}/Cargo.toml.orig.XXXXXX")"
cp "$CARGO_TOML" "$CARGO_BAK"
{
if [ -d "$ROOT/../kicad-ipc-rs" ]; then
printf '\n[patch."https://git.else-if.org/jess/kicad-ipc-rs.git"]\n'
printf 'kicad-ipc-rs = { path = "../kicad-ipc-rs" }\n'
echo "using local ../kicad-ipc-rs (patch injected transiently)" >&2
fi
if [ -d "$ROOT/../Siphon" ]; then
printf '\n[patch."https://git.else-if.org/jess/Siphon.git"]\n'
printf 'siphon = { path = "../Siphon" }\n'
echo "using local ../Siphon (patch injected transiently)" >&2
fi
} >> "$CARGO_TOML"
PATCH_APPLIED=1
trap 'if [ $PATCH_APPLIED -eq 1 ] && [ -f "$CARGO_BAK" ]; then cp "$CARGO_BAK" "$CARGO_TOML"; rm -f "$CARGO_BAK"; fi' EXIT
fi
# Rust staticlib.
cargo build --release
RUST_LIB="$ROOT/target/release"
if [ ! -f "$RUST_LIB/liblayers.a" ]; then
echo "ERROR: liblayers.a not found at $RUST_LIB" >&2
exit 1
fi
SDK="$(xcrun --show-sdk-path)"
RUST_FLAGS=(-import-objc-header "$ROOT/include/layers.h" -L "$RUST_LIB" -llayers)
# Swift shell → Layers.app/Contents/MacOS/Layers
swiftc \
-target arm64-apple-macosx14.0 \
-sdk "$SDK" \
"${RUST_FLAGS[@]}" \
-framework Cocoa \
-framework Metal \
-framework MetalKit \
-framework QuartzCore \
-framework CoreGraphics \
-framework CoreFoundation \
-framework CoreVideo \
-O \
-o "$MACOS_DIR/Layers" \
"$ROOT"/src/*.swift
cp "$ROOT/Info.plist" "$CONTENTS/Info.plist"
# Adhoc sign so Gatekeeper lets it launch.
codesign --force --sign - "$APP"
# KiCad plugin payload alongside the .app bundle.
render_plugin_json "bin/Layers.app/Contents/MacOS/Layers" "$STAGE/plugin.json"
cp -R "$ROOT/resources/." "$STAGE/resources/"
if [ -f "$ROOT/LICENCE" ]; then
cp "$ROOT/LICENCE" "$STAGE/LICENCE"
fi
echo "staged: $STAGE"
echo "app: $APP"