| 1 | #!/usr/bin/env bash |
| 2 | # Build a universal macOS binary (arm64 + x86_64) from a crate. Needs Xcode CLT only. |
| 3 | # Wraps it in a .app bundle (icon, Info.plist) and zips that for distribution. |
| 4 | # Also keeps the bare lipo binary for self-update (release asset *-macos-universal). |
| 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 stays unsigned (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 | export PATH="$HOME/.cargo/bin:$PATH" |
| 18 | entitlements="$repoRoot/scripts/macos/RustyTeams.entitlements" |
| 19 | targets=(aarch64-apple-darwin x86_64-apple-darwin) |
| 20 | |
| 21 | rustup target add "${targets[@]}" >/dev/null |
| 22 | |
| 23 | buildArguments=(build --release) |
| 24 | slicePathInTarget="release" |
| 25 | if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then |
| 26 | buildArguments+=(--example "$binaryName") |
| 27 | slicePathInTarget="release/examples" |
| 28 | elif [ -n "$binaryName" ]; then |
| 29 | buildArguments+=(--bin "$binaryName") |
| 30 | fi |
| 31 | |
| 32 | for target in "${targets[@]}"; do |
| 33 | (cd "$crateDir" && cargo "${buildArguments[@]}" --target "$target") |
| 34 | done |
| 35 | |
| 36 | slices=() |
| 37 | for target in "${targets[@]}"; do |
| 38 | sliceDirectory="$CARGO_TARGET_DIR/$target/$slicePathInTarget" |
| 39 | if [ -n "$binaryName" ]; then |
| 40 | slices+=("$sliceDirectory/$binaryName") |
| 41 | else |
| 42 | candidates=("$sliceDirectory"/*) |
| 43 | [ "${#candidates[@]}" -eq 1 ] && [ -f "${candidates[0]}" ] || |
| 44 | { echo "more than one binary in $sliceDirectory, pass the name" >&2; exit 1; } |
| 45 | slices+=("${candidates[0]}") |
| 46 | fi |
| 47 | done |
| 48 | |
| 49 | outputDirectory="$CARGO_TARGET_DIR/universal/release" |
| 50 | mkdir -p "$outputDirectory" |
| 51 | output="$outputDirectory/$(basename "${slices[0]}")" |
| 52 | lipo -create -output "$output" "${slices[@]}" |
| 53 | |
| 54 | # Wrap the binary in a .app so Finder and the Dock treat it as an application: |
| 55 | # double-clickable, with the icon, and with the camera / microphone / screen |
| 56 | # permission prompts (macOS keys those off the bundle, not a bare binary). |
| 57 | app="$outputDirectory/Rusty Teams.app" |
| 58 | binary="$(basename "$output")" |
| 59 | version="$(sed -n 's/^version = "\(.*\)"/\1/p' "$repoRoot/Cargo.toml" | head -1)" |
| 60 | rm -rf "$app" |
| 61 | mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" |
| 62 | cp "$output" "$app/Contents/MacOS/$binary" |
| 63 | cat > "$app/Contents/Info.plist" <<PLIST |
| 64 | <?xml version="1.0" encoding="UTF-8"?> |
| 65 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 66 | <plist version="1.0"> |
| 67 | <dict> |
| 68 | <key>CFBundleName</key> |
| 69 | <string>Rusty Teams</string> |
| 70 | <key>CFBundleDisplayName</key> |
| 71 | <string>Rusty Teams</string> |
| 72 | <key>CFBundleIdentifier</key> |
| 73 | <string>org.rusty-teams.teams</string> |
| 74 | <key>CFBundleExecutable</key> |
| 75 | <string>$binary</string> |
| 76 | <key>CFBundleIconFile</key> |
| 77 | <string>icon</string> |
| 78 | <key>CFBundlePackageType</key> |
| 79 | <string>APPL</string> |
| 80 | <key>CFBundleShortVersionString</key> |
| 81 | <string>$version</string> |
| 82 | <key>CFBundleVersion</key> |
| 83 | <string>$version</string> |
| 84 | <key>NSHighResolutionCapable</key> |
| 85 | <true/> |
| 86 | <key>NSMicrophoneUsageDescription</key> |
| 87 | <string>Rusty Teams uses your microphone for calls.</string> |
| 88 | <key>NSCameraUsageDescription</key> |
| 89 | <string>Rusty Teams uses your camera for video calls.</string> |
| 90 | <key>NSScreenCaptureUsageDescription</key> |
| 91 | <string>Rusty Teams shares your screen in meetings.</string> |
| 92 | </dict> |
| 93 | </plist> |
| 94 | PLIST |
| 95 | |
| 96 | # The same icon the Windows exe embeds (assets/icon/app.rc); iconutil compiles |
| 97 | # whatever sizes the set has, all downscaled from the 256px original. |
| 98 | iconset="$(mktemp -d)/teams.iconset" |
| 99 | mkdir -p "$iconset" |
| 100 | png="$repoRoot/crates/app/assets/icon/teams-fast-256.png" |
| 101 | for size in 16 32 64 128 256; do |
| 102 | sips -z "$size" "$size" "$png" --out "$iconset/icon_${size}x${size}.png" >/dev/null |
| 103 | done |
| 104 | cp "$iconset/icon_32x32.png" "$iconset/icon_16x16@2x.png" |
| 105 | cp "$iconset/icon_64x64.png" "$iconset/icon_32x32@2x.png" |
| 106 | cp "$iconset/icon_256x256.png" "$iconset/icon_128x128@2x.png" |
| 107 | iconutil -c icns "$iconset" -o "$app/Contents/Resources/icon.icns" |
| 108 | |
| 109 | # Developer ID signing when a certificate is configured in the build environment. |
| 110 | if [ -n "${MACOS_CODESIGN_IDENTITY:-}" ]; then |
| 111 | echo "codesign with $MACOS_CODESIGN_IDENTITY" |
| 112 | codesign --force --options runtime --timestamp \ |
| 113 | --entitlements "$entitlements" \ |
| 114 | --sign "$MACOS_CODESIGN_IDENTITY" \ |
| 115 | "$app/Contents/MacOS/$binary" |
| 116 | codesign --force --options runtime --timestamp \ |
| 117 | --entitlements "$entitlements" \ |
| 118 | --sign "$MACOS_CODESIGN_IDENTITY" \ |
| 119 | "$app" |
| 120 | codesign --verify --deep --strict --verbose=2 "$app" |
| 121 | fi |
| 122 | |
| 123 | # ditto keeps the bundle intact (plain zip would not); the result is the .app release asset. |
| 124 | ditto -c -k --keepParent "$app" "$outputDirectory/teams-macos.zip" |
| 125 | |
| 126 | # Bare universal binary for in-app self-update (Mach-O fat; magic cafe babe). |
| 127 | cp "$output" "$outputDirectory/teams-macos-universal" |
| 128 | chmod +x "$outputDirectory/teams-macos-universal" |
| 129 | |
| 130 | if [ -n "${MACOS_CODESIGN_IDENTITY:-}" ] && [ -n "${MACOS_NOTARY_PROFILE:-}" ]; then |
| 131 | echo "notarize with profile $MACOS_NOTARY_PROFILE" |
| 132 | xcrun notarytool submit "$outputDirectory/teams-macos.zip" \ |
| 133 | --keychain-profile "$MACOS_NOTARY_PROFILE" \ |
| 134 | --wait |
| 135 | xcrun stapler staple "$app" |
| 136 | # Re-zip after staple so the published .app zip carries the ticket. |
| 137 | ditto -c -k --keepParent "$app" "$outputDirectory/teams-macos.zip" |
| 138 | fi |
| 139 | |
| 140 | file "$output" |
| 141 | ls -la "$output" "$app/Contents/MacOS/$binary" \ |
| 142 | "$outputDirectory/teams-macos.zip" "$outputDirectory/teams-macos-universal" |