#!/usr/bin/env bash # Build Cotton.app, the Mac's menu-bar importer, from a clean checkout. # # pnpm -s mac:cotton:build the whole app, signed # apps/cotton/scripts/build-cotton-mac.sh --adhoc # the same, signed ad-hoc (a dev # loop that must not touch a key) # apps/cotton/scripts/build-cotton-mac.sh --check # Cotton Check (com.decemberfm.cotton.mac.walk): # the same app under its own name and bundle id, # a flavour (AppModel.flavour) walked by hand against # dev beside the installed app, with no updater # apps/cotton/scripts/build-cotton-mac.sh --rust-only # the Rust static library alone # (the Xcode pre-build phase) # # 1. the Rust core `crates/cotton-mac-ffi`, release, arm64, copied to # apps/cotton/Mac/build/rust/libcotton_mac_ffi.a where the project links it; # 2. the WhatsApp helper from apps/cotton-whatsapp-link (Go, tests first: # TestHelperNeverSends keeps it read-only, and no bundle carries a helper # that failed it), and the Chrome broker for WhatsApp's passkey, `cotton-session-host` # (Rust, release, arm64); # 3. xcodegen, then xcodebuild (Release) of the Cotton scheme; # 4. the helpers into Contents/Helpers, then the signature, inside out. # # Output: apps/cotton/Mac/build/Cotton.app, printed on the last line. # # SIGNING: COTTON_SIGNING_IDENTITY when set, otherwise the first Developer ID # Application identity in the keychain, with the hardened runtime and a secure # timestamp; ad-hoc only when the keychain holds none, or with --adhoc. A # headless shell reaches the signature with a locked login keychain and # codesign answers errSecInternalComponent, so KEYCHAIN_PASSWORD_FILE (0600) # unlocks it right before signing. # No notarisation. # # SPARKLE (Mac/Sources/Updates.swift): with COTTON_SPARKLE_PUBLIC_KEY set, the # built bundle's Info.plist gets SUPublicEDKey and SUFeedURL # (COTTON_MAC_FEED_URL, else https://cottondb.com/mac/cotton/appcast.xml — its # own feed); without it neither key is # written and the bundle has no updater. Sparkle.framework's nested code is # signed inside out before the app, in Sparkle's own order. # # The whole run holds the machine's build lock (scripts/with-build-lock.sh, # re-entrant), so a 16 GB Mac runs one heavy build at a time. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" MAC_DIR="$ROOT/apps/cotton/Mac" OUT="$MAC_DIR/build" RUST_TARGET="aarch64-apple-darwin" # The lock is taken before the arguments are read, so the re-run sees them all. if [ "${COTTON_MAC_BUILD_LOCKED:-0}" != 1 ]; then export COTTON_MAC_BUILD_LOCKED=1 export BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-cotton-mac-build}" exec bash "$ROOT/scripts/with-build-lock.sh" bash "${BASH_SOURCE[0]}" "$@" fi RUST_ONLY=0 ADHOC=0 CHECK=0 while [ $# -gt 0 ]; do case "$1" in --rust-only) RUST_ONLY=1; shift ;; --adhoc) ADHOC=1; shift ;; --check) CHECK=1; shift ;; *) echo "usage: build-cotton-mac.sh [--rust-only | --adhoc | --check]" >&2; exit 2 ;; esac done # Xcode's script phases and a non-interactive shell see a short PATH. export PATH="${CARGO_HOME:-$HOME/.cargo}/bin:/opt/homebrew/bin:/usr/local/bin:$PATH" # ── 1. The Rust core ──────────────────────────────────────────────────────── CARGO_PATH_LOG_PREFIX="[cotton-mac]" # shellcheck source=../../../scripts/lib/cargo-path.sh . "$ROOT/scripts/lib/cargo-path.sh" ensure_cargo_on_path || exit 1 export MACOSX_DEPLOYMENT_TARGET="${MACOSX_DEPLOYMENT_TARGET:-26.0}" echo "[cotton-mac] cargo build --release -p cotton-mac-ffi --target $RUST_TARGET" >&2 ( cd "$ROOT/crates" bash "$ROOT/scripts/cargo-target-guard.sh" --run \ cargo build --release -p cotton-mac-ffi --target "$RUST_TARGET" ) target_dir="$(cd "$ROOT/crates" && CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$(bash "$ROOT/scripts/ci/cargo-target-dir.sh" shared)}" cargo metadata --format-version 1 --no-deps | jq -r .target_directory)" archive="$target_dir/$RUST_TARGET/release/libcotton_mac_ffi.a" [ -f "$archive" ] || { echo "[cotton-mac] no archive at $archive" >&2; exit 1; } mkdir -p "$OUT/rust" # Copied only when it changed, so an unchanged core does not relink the app. if ! cmp -s "$archive" "$OUT/rust/libcotton_mac_ffi.a"; then cp "$archive" "$OUT/rust/libcotton_mac_ffi.a" fi echo "[cotton-mac] core → ${OUT#"$ROOT/"}/rust/libcotton_mac_ffi.a" >&2 [ "$RUST_ONLY" -eq 1 ] && exit 0 # ── 2. The WhatsApp helper ────────────────────────────────────────────────── # shellcheck source=lib/go-path.sh . "$ROOT/apps/cotton/scripts/lib/go-path.sh" go_toolchain || { echo "[cotton-mac] no Go toolchain — install it (brew install go); Cotton.app carries cotton-whatsapp-link" >&2; exit 1; } mkdir -p "$OUT/helpers" echo "[cotton-mac] go test + go build cotton-whatsapp-link (darwin/arm64)" >&2 ( cd "$ROOT/apps/cotton-whatsapp-link" CGO_ENABLED=0 "$GO_BIN" test ./... CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 "$GO_BIN" build -trimpath -ldflags "-s -w" \ -o "$OUT/helpers/cotton-whatsapp-link" . ) || { echo "[cotton-mac] cotton-whatsapp-link failed its tests or its build" >&2; exit 1; } # ── 2b. The Chrome broker (WhatsApp's passkey) ────────────────────────────── # `cotton-session-host`, whose Chrome WhatsApp's passkey step asks through # (crates/cotton-mac/src/whatsapp_passkey.rs, HOST_BINARY). It lands in # $OUT/helpers beside the WhatsApp helper; step 4 copies and signs both. echo "[cotton-mac] cargo build --release -p cotton-session-host --target $RUST_TARGET" >&2 ( cd "$ROOT/crates" bash "$ROOT/scripts/cargo-target-guard.sh" --run \ cargo build --release -p cotton-session-host --target "$RUST_TARGET" ) session_host="$target_dir/$RUST_TARGET/release/cotton-session-host" [ -x "$session_host" ] || { echo "[cotton-mac] no cotton-session-host at $session_host" >&2; exit 1; } cp "$session_host" "$OUT/helpers/cotton-session-host" # ── 3. The Xcode project and the app ──────────────────────────────────────── command -v xcodegen >/dev/null || { echo "[cotton-mac] no xcodegen — brew install xcodegen" >&2; exit 1; } echo "[cotton-mac] xcodegen" >&2 xcodegen generate --quiet --spec "$MAC_DIR/project.yml" --project "$MAC_DIR" # The project's one derived-data root (scripts/lib/derived-data.sh, swept like # the cargo targets), in a folder of its own so the Mac project never shares a # build database with the iPhone project's builds from this checkout. # shellcheck source=../../../scripts/lib/derived-data.sh . "$ROOT/scripts/lib/derived-data.sh" DERIVED="$(cotton_derived_data)/mac" APP_NAME="Cotton" FLAVOUR_FLAGS=() if [ "$CHECK" -eq 1 ]; then DERIVED="$(cotton_derived_data)/mac-check" APP_NAME="Cotton Check" FLAVOUR_FLAGS=(PRODUCT_NAME="Cotton Check" PRODUCT_BUNDLE_IDENTIFIER=com.decemberfm.cotton.mac.walk) fi echo "[cotton-mac] xcodebuild Release ($APP_NAME)" >&2 xcodebuild \ -project "$MAC_DIR/CottonMac.xcodeproj" \ -scheme Cotton \ -configuration Release \ -destination 'platform=macOS,arch=arm64' \ -derivedDataPath "$DERIVED" \ COTTON_RUST_PREBUILT=1 \ ${FLAVOUR_FLAGS[@]+"${FLAVOUR_FLAGS[@]}"} \ build -quiet BUNDLE="$OUT/$APP_NAME.app" rm -rf "$BUNDLE" ditto "$DERIVED/Build/Products/Release/$APP_NAME.app" "$BUNDLE" # ── 3b. The update feed ───────────────────────────────────────────────────── if [ "$CHECK" -eq 1 ]; then echo "[cotton-mac] --check: Cotton Check has no updater" >&2 elif [ -n "${COTTON_SPARKLE_PUBLIC_KEY:-}" ]; then plist="$BUNDLE/Contents/Info.plist" feed="${COTTON_MAC_FEED_URL:-https://cottondb.com/mac/cotton/appcast.xml}" /usr/libexec/PlistBuddy -c "Delete :SUFeedURL" -c "Delete :SUPublicEDKey" "$plist" 2>/dev/null || true /usr/libexec/PlistBuddy -c "Add :SUFeedURL string $feed" -c "Add :SUPublicEDKey string $COTTON_SPARKLE_PUBLIC_KEY" "$plist" echo "[cotton-mac] update feed $feed, signed with the given public key" >&2 else echo "[cotton-mac] no COTTON_SPARKLE_PUBLIC_KEY — this bundle has no updater" >&2 fi # ── 4. Helpers, then the signature ────────────────────────────────────────── mkdir -p "$BUNDLE/Contents/Helpers" for helper in "$OUT/helpers/"*; do [ -f "$helper" ] && cp "$helper" "$BUNDLE/Contents/Helpers/" done # The first Developer ID Application identity in the keychain. find_developer_id() { security find-identity -v -p codesigning 2>/dev/null \ | sed -n 's/^ *[0-9]*) *[0-9A-Fa-f]\{40\} *"\(Developer ID Application:.*\)"$/\1/p' \ | head -n 1 } IDENTITY="" if [ "$ADHOC" -eq 0 ]; then IDENTITY="${COTTON_SIGNING_IDENTITY:-}" [ -n "$IDENTITY" ] || IDENTITY="$(find_developer_id || true)" fi if [ -n "$IDENTITY" ] && [ -n "${KEYCHAIN_PASSWORD_FILE:-}" ] && [ -f "$KEYCHAIN_PASSWORD_FILE" ]; then security unlock-keychain -p "$(tr -d '\n' < "$KEYCHAIN_PASSWORD_FILE")" "$HOME/Library/Keychains/login.keychain-db" \ && echo "[cotton-mac] login keychain unlocked for signing (KEYCHAIN_PASSWORD_FILE)" >&2 \ || echo "[cotton-mac] WARNING: the login keychain did not unlock; signing may stop with errSecInternalComponent" >&2 fi signing_failed() { echo "[cotton-mac] codesign failed. errSecInternalComponent means the login keychain is locked (a headless shell):" >&2 echo "[cotton-mac] set KEYCHAIN_PASSWORD_FILE, build from a logged-in Terminal, or pass --adhoc" >&2 exit 1 } # No Developer ID and no --adhoc: stop here, loudly. A bundle signed ad-hoc by surprise is one no other Mac runs # and no installed Cotton takes as an update (publish-cotton-mac.sh refuses it; 23 Nov 2026 a release build in a # shell that saw no identity came out ad-hoc and said nothing). if [ -z "$IDENTITY" ] && [ "$ADHOC" -eq 0 ]; then echo "[cotton-mac] STOP: no \"Developer ID Application\" identity is visible to this shell, so the bundle would be unsigned." >&2 echo "[cotton-mac] security find-identity -v -p codesigning lists what it sees. The identity is in the login keychain:" >&2 echo "[cotton-mac] build from a logged-in session (a shell whose keychain search list holds login.keychain-db)," >&2 echo "[cotton-mac] or name it with COTTON_SIGNING_IDENTITY; pass --adhoc for a bundle only this Mac runs." >&2 exit 1 fi if [ -n "$IDENTITY" ]; then SIGN_FLAGS=(--force --sign "$IDENTITY" --options runtime --timestamp) echo "[cotton-mac] signing as “${IDENTITY}”, hardened runtime" >&2 else SIGN_FLAGS=(--force --sign - --timestamp=none) if [ "$ADHOC" -eq 1 ]; then echo "[cotton-mac] --adhoc: signing ad-hoc, no hardened runtime" >&2 else echo "[cotton-mac] no Developer ID Application identity — signing ad-hoc, no hardened runtime" >&2 fi fi for helper in "$BUNDLE/Contents/Helpers/"*; do if [ -f "$helper" ] && [ -x "$helper" ]; then codesign "${SIGN_FLAGS[@]}" "$helper" || signing_failed fi done SPARKLE="$BUNDLE/Contents/Frameworks/Sparkle.framework" if [ -d "$SPARKLE" ]; then for nested in "$SPARKLE/Versions/B/XPCServices/"*.xpc "$SPARKLE/Versions/B/Autoupdate" "$SPARKLE/Versions/B/Updater.app" "$SPARKLE"; do [ -e "$nested" ] && { codesign "${SIGN_FLAGS[@]}" "$nested" || signing_failed; } done fi codesign "${SIGN_FLAGS[@]}" --entitlements "$MAC_DIR/Cotton.entitlements" "$BUNDLE" || signing_failed codesign --verify --deep --strict "$BUNDLE" echo "[cotton-mac] codesign --verify: ok" >&2 # `-dvv`, not `-dv`: only the second prints the Authority lines. Read into a variable first: `codesign | grep -q` # under pipefail fails whenever grep stops reading early. signature="$(codesign -dvv "$BUNDLE" 2>&1 || true)" if [ "$ADHOC" -eq 0 ] && [[ "$signature" != *"Authority=Developer ID Application"* ]]; then echo "[cotton-mac] STOP: the bundle came out without a Developer ID signature" >&2 exit 1 fi echo "$BUNDLE"