BlueGit

ict rusty-teams

Files

rusty-teams / scripts / build-macos.sh

build-macos.sh

build-macos.sh 6.7 KiB Download Raw
1#!/usr/bin/env bash
2# Build an Apple Silicon (arm64) macOS binary from a crate. Needs Xcode CLT only.
3# Wraps it in a .app bundle (icon, Info.plist) and zips that for distribution.
4# The published artifact is the `.app` zip. There is no in-app Mach-O swap.
5#
6# Optional signing / notarization when Apple credentials are present:
7# MACOS_CODESIGN_IDENTITY e.g. "Developer ID Application: …"
8# MACOS_NOTARY_PROFILE notarytool keychain profile (from `xcrun notarytool store-credentials`)
9# Without those env vars the .app is ad-hoc signed (fine for local / internal RC).
10#
11# Usage: scripts/build-macos.sh [crate-dir, default crates/app] [binary-or-example-name, default: the crate's only binary]
12set -euo pipefail
13repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
14crateDir="$(realpath "${1:-$repoRoot/crates/app}")"
15binaryName="${2:-}"
16export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$repoRoot/target}"
17# Keep in sync with .cargo/config.toml. macOS 14 is the minimum: screen and
18# window sharing use SCContentSharingPicker. Info.plist's LSMinimumSystemVersion uses it.
19deploymentTarget="${MACOSX_DEPLOYMENT_TARGET:-14.0}"
20case "$deploymentTarget" in
21 *.*.*) deploymentTarget="${deploymentTarget%.0}" ;;
22 *.*) ;;
23 *) deploymentTarget="$deploymentTarget.0" ;;
24esac
25deploymentMajor="${deploymentTarget%%.*}"
26if ! [[ "$deploymentMajor" =~ ^[0-9]+$ ]] || [ "$deploymentMajor" -lt 14 ]; then
27 echo "error: MACOSX_DEPLOYMENT_TARGET=$deploymentTarget, the app needs macOS 14.0 or later" >&2
28 exit 1
29fi
30export MACOSX_DEPLOYMENT_TARGET="$deploymentTarget"
31export PATH="$HOME/.cargo/bin:$PATH"
32entitlements="$repoRoot/scripts/macos/RustyTeams.entitlements"
33target="aarch64-apple-darwin"
34
35rustup target add "$target" >/dev/null
36
37buildArguments=(build --release)
38slicePathInTarget="release"
39if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then
40 buildArguments+=(--example "$binaryName")
41 slicePathInTarget="release/examples"
42elif [ -n "$binaryName" ]; then
43 buildArguments+=(--bin "$binaryName")
44fi
45
46(cd "$crateDir" && cargo "${buildArguments[@]}" --target "$target")
47
48sliceDirectory="$CARGO_TARGET_DIR/$target/$slicePathInTarget"
49if [ -n "$binaryName" ]; then
50 output="$sliceDirectory/$binaryName"
51else
52 candidates=("$sliceDirectory"/*)
53 [ "${#candidates[@]}" -eq 1 ] && [ -f "${candidates[0]}" ] ||
54 { echo "more than one binary in $sliceDirectory, pass the name" >&2; exit 1; }
55 output="${candidates[0]}"
56fi
57
58minos="$(vtool -arch arm64 -show-build "$output" | awk '$1 == "minos" { print $2 }')"
59if [ "$minos" != "$MACOSX_DEPLOYMENT_TARGET" ]; then
60 echo "error: arm64 binary targets macOS ${minos:-unknown}, expected $MACOSX_DEPLOYMENT_TARGET" >&2
61 exit 1
62fi
63
64outputDirectory="$CARGO_TARGET_DIR/$target/release"
65mkdir -p "$outputDirectory"
66
67# Wrap the binary in a .app so Finder and the Dock treat it as an application:
68# double-clickable, with the icon, and with the camera / microphone / screen
69# permission prompts (macOS keys those off the bundle, not a bare binary).
70app="$outputDirectory/Rusty Teams.app"
71binary="$(basename "$output")"
72version="$(sed -n 's/^version = "\(.*\)"/\1/p' "$repoRoot/Cargo.toml" | head -1)"
73rm -rf "$app"
74mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources"
75cp "$output" "$app/Contents/MacOS/$binary"
76cat > "$app/Contents/Info.plist" <<PLIST
77<?xml version="1.0" encoding="UTF-8"?>
78<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
79<plist version="1.0">
80<dict>
81 <key>CFBundleName</key>
82 <string>Rusty Teams</string>
83 <key>CFBundleDisplayName</key>
84 <string>Rusty Teams</string>
85 <key>CFBundleIdentifier</key>
86 <string>org.rusty-teams.teams</string>
87 <key>CFBundleExecutable</key>
88 <string>$binary</string>
89 <key>CFBundleIconFile</key>
90 <string>icon</string>
91 <key>CFBundlePackageType</key>
92 <string>APPL</string>
93 <key>CFBundleShortVersionString</key>
94 <string>$version</string>
95 <key>CFBundleVersion</key>
96 <string>$version</string>
97 <key>LSMinimumSystemVersion</key>
98 <string>$MACOSX_DEPLOYMENT_TARGET</string>
99 <key>NSHighResolutionCapable</key>
100 <true/>
101 <key>NSMicrophoneUsageDescription</key>
102 <string>Rusty Teams uses your microphone for calls.</string>
103 <key>NSCameraUsageDescription</key>
104 <string>Rusty Teams uses your camera for video calls.</string>
105 <key>NSScreenCaptureUsageDescription</key>
106 <string>Rusty Teams shares your screen in meetings.</string>
107</dict>
108</plist>
109PLIST
110
111# The same icon the Windows exe embeds (assets/icon/app.rc); iconutil compiles
112# whatever sizes the set has, all downscaled from the 256px original.
113iconset="$(mktemp -d)/teams.iconset"
114mkdir -p "$iconset"
115png="$repoRoot/crates/app/assets/icon/teams-fast-256.png"
116if [ -f "$repoRoot/crates/app/assets/icon/kvt-teams-256.png" ]; then
117 png="$repoRoot/crates/app/assets/icon/kvt-teams-256.png"
118fi
119for size in 16 32 64 128 256; do
120 sips -z "$size" "$size" "$png" --out "$iconset/icon_${size}x${size}.png" >/dev/null
121done
122cp "$iconset/icon_32x32.png" "$iconset/icon_16x16@2x.png"
123cp "$iconset/icon_64x64.png" "$iconset/icon_32x32@2x.png"
124cp "$iconset/icon_256x256.png" "$iconset/icon_128x128@2x.png"
125iconutil -c icns "$iconset" -o "$app/Contents/Resources/icon.icns"
126
127# Developer ID signing when a certificate is configured in the build environment;
128# otherwise ad-hoc sign the bundle so the CodeDirectory is bound to Info.plist
129# (CFBundleIdentifier) instead of leaving the arm64 binary linker-signed (which
130# breaks TCC matching on Apple Silicon).
131if [ -n "${MACOS_CODESIGN_IDENTITY:-}" ]; then
132 echo "codesign with $MACOS_CODESIGN_IDENTITY"
133 codesign --force --options runtime --timestamp \
134 --entitlements "$entitlements" \
135 --sign "$MACOS_CODESIGN_IDENTITY" \
136 "$app/Contents/MacOS/$binary"
137 codesign --force --options runtime --timestamp \
138 --entitlements "$entitlements" \
139 --sign "$MACOS_CODESIGN_IDENTITY" \
140 "$app"
141 codesign --verify --deep --strict --verbose=2 "$app"
142else
143 echo "codesign ad-hoc (-)"
144 codesign --force --sign - "$app"
145 codesign --verify --deep --strict --verbose=2 "$app"
146fi
147
148# ditto keeps the bundle intact (plain zip would not); the result is the .app release asset.
149ditto -c -k --keepParent "$app" "$outputDirectory/teams-macos.zip"
150
151if [ -n "${MACOS_CODESIGN_IDENTITY:-}" ] && [ -n "${MACOS_NOTARY_PROFILE:-}" ]; then
152 echo "notarize with profile $MACOS_NOTARY_PROFILE"
153 xcrun notarytool submit "$outputDirectory/teams-macos.zip" \
154 --keychain-profile "$MACOS_NOTARY_PROFILE" \
155 --wait
156 xcrun stapler staple "$app"
157 # Re-zip after staple so the published .app zip carries the ticket.
158 ditto -c -k --keepParent "$app" "$outputDirectory/teams-macos.zip"
159fi
160
161file "$output"
162ls -la "$output" "$app/Contents/MacOS/$binary" \
163 "$outputDirectory/teams-macos.zip"