| 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] |
| 12 | set -euo pipefail |
| 13 | repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 14 | crateDir="$(realpath "${1:-$repoRoot/crates/app}")" |
| 15 | binaryName="${2:-}" |
| 16 | export 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. |
| 19 | deploymentTarget="${MACOSX_DEPLOYMENT_TARGET:-14.0}" |
| 20 | case "$deploymentTarget" in |
| 21 | *.*.*) deploymentTarget="${deploymentTarget%.0}" ;; |
| 22 | *.*) ;; |
| 23 | *) deploymentTarget="$deploymentTarget.0" ;; |
| 24 | esac |
| 25 | deploymentMajor="${deploymentTarget%%.*}" |
| 26 | if ! [[ "$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 |
| 29 | fi |
| 30 | export MACOSX_DEPLOYMENT_TARGET="$deploymentTarget" |
| 31 | export PATH="$HOME/.cargo/bin:$PATH" |
| 32 | entitlements="$repoRoot/scripts/macos/RustyTeams.entitlements" |
| 33 | target="aarch64-apple-darwin" |
| 34 | |
| 35 | rustup target add "$target" >/dev/null |
| 36 | |
| 37 | buildArguments=(build --release) |
| 38 | slicePathInTarget="release" |
| 39 | if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then |
| 40 | buildArguments+=(--example "$binaryName") |
| 41 | slicePathInTarget="release/examples" |
| 42 | elif [ -n "$binaryName" ]; then |
| 43 | buildArguments+=(--bin "$binaryName") |
| 44 | fi |
| 45 | |
| 46 | (cd "$crateDir" && cargo "${buildArguments[@]}" --target "$target") |
| 47 | |
| 48 | sliceDirectory="$CARGO_TARGET_DIR/$target/$slicePathInTarget" |
| 49 | if [ -n "$binaryName" ]; then |
| 50 | output="$sliceDirectory/$binaryName" |
| 51 | else |
| 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]}" |
| 56 | fi |
| 57 | |
| 58 | minos="$(vtool -arch arm64 -show-build "$output" | awk '$1 == "minos" { print $2 }')" |
| 59 | if [ "$minos" != "$MACOSX_DEPLOYMENT_TARGET" ]; then |
| 60 | echo "error: arm64 binary targets macOS ${minos:-unknown}, expected $MACOSX_DEPLOYMENT_TARGET" >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | outputDirectory="$CARGO_TARGET_DIR/$target/release" |
| 65 | mkdir -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). |
| 70 | app="$outputDirectory/Rusty Teams.app" |
| 71 | binary="$(basename "$output")" |
| 72 | version="$(sed -n 's/^version = "\(.*\)"/\1/p' "$repoRoot/Cargo.toml" | head -1)" |
| 73 | rm -rf "$app" |
| 74 | mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" |
| 75 | cp "$output" "$app/Contents/MacOS/$binary" |
| 76 | cat > "$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> |
| 109 | PLIST |
| 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. |
| 113 | iconset="$(mktemp -d)/teams.iconset" |
| 114 | mkdir -p "$iconset" |
| 115 | png="$repoRoot/crates/app/assets/icon/teams-fast-256.png" |
| 116 | if [ -f "$repoRoot/crates/app/assets/icon/kvt-teams-256.png" ]; then |
| 117 | png="$repoRoot/crates/app/assets/icon/kvt-teams-256.png" |
| 118 | fi |
| 119 | for size in 16 32 64 128 256; do |
| 120 | sips -z "$size" "$size" "$png" --out "$iconset/icon_${size}x${size}.png" >/dev/null |
| 121 | done |
| 122 | cp "$iconset/icon_32x32.png" "$iconset/icon_16x16@2x.png" |
| 123 | cp "$iconset/icon_64x64.png" "$iconset/icon_32x32@2x.png" |
| 124 | cp "$iconset/icon_256x256.png" "$iconset/icon_128x128@2x.png" |
| 125 | iconutil -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). |
| 131 | if [ -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" |
| 142 | else |
| 143 | echo "codesign ad-hoc (-)" |
| 144 | codesign --force --sign - "$app" |
| 145 | codesign --verify --deep --strict --verbose=2 "$app" |
| 146 | fi |
| 147 | |
| 148 | # ditto keeps the bundle intact (plain zip would not); the result is the .app release asset. |
| 149 | ditto -c -k --keepParent "$app" "$outputDirectory/teams-macos.zip" |
| 150 | |
| 151 | if [ -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" |
| 159 | fi |
| 160 | |
| 161 | file "$output" |
| 162 | ls -la "$output" "$app/Contents/MacOS/$binary" \ |
| 163 | "$outputDirectory/teams-macos.zip" |