| 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 | # Usage: scripts/build-macos.sh [crate-dir, default crates/app] [binary-or-example-name, default: the crate's only binary] |
| 5 | set -euo pipefail |
| 6 | repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 7 | crateDir="$(realpath "${1:-$repoRoot/crates/app}")" |
| 8 | binaryName="${2:-}" |
| 9 | export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$repoRoot/target}" |
| 10 | export PATH="$HOME/.cargo/bin:$PATH" |
| 11 | targets=(aarch64-apple-darwin x86_64-apple-darwin) |
| 12 | |
| 13 | rustup target add "${targets[@]}" >/dev/null |
| 14 | |
| 15 | buildArguments=(build --release) |
| 16 | slicePathInTarget="release" |
| 17 | if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then |
| 18 | buildArguments+=(--example "$binaryName") |
| 19 | slicePathInTarget="release/examples" |
| 20 | elif [ -n "$binaryName" ]; then |
| 21 | buildArguments+=(--bin "$binaryName") |
| 22 | fi |
| 23 | |
| 24 | for target in "${targets[@]}"; do |
| 25 | (cd "$crateDir" && cargo "${buildArguments[@]}" --target "$target") |
| 26 | done |
| 27 | |
| 28 | slices=() |
| 29 | for target in "${targets[@]}"; do |
| 30 | sliceDirectory="$CARGO_TARGET_DIR/$target/$slicePathInTarget" |
| 31 | if [ -n "$binaryName" ]; then |
| 32 | slices+=("$sliceDirectory/$binaryName") |
| 33 | else |
| 34 | candidates=("$sliceDirectory"/*) |
| 35 | [ "${#candidates[@]}" -eq 1 ] && [ -f "${candidates[0]}" ] || |
| 36 | { echo "more than one binary in $sliceDirectory, pass the name" >&2; exit 1; } |
| 37 | slices+=("${candidates[0]}") |
| 38 | fi |
| 39 | done |
| 40 | |
| 41 | outputDirectory="$CARGO_TARGET_DIR/universal/release" |
| 42 | mkdir -p "$outputDirectory" |
| 43 | output="$outputDirectory/$(basename "${slices[0]}")" |
| 44 | lipo -create -output "$output" "${slices[@]}" |
| 45 | |
| 46 | # Wrap the binary in a .app so Finder and the Dock treat it as an application: |
| 47 | # double-clickable, with the icon, and with the camera / microphone / screen |
| 48 | # permission prompts (macOS keys those off the bundle, not a bare binary). |
| 49 | app="$outputDirectory/Rusty Teams.app" |
| 50 | binary="$(basename "$output")" |
| 51 | version="$(sed -n 's/^version = "\(.*\)"/\1/p' "$repoRoot/Cargo.toml" | head -1)" |
| 52 | rm -rf "$app" |
| 53 | mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" |
| 54 | cp "$output" "$app/Contents/MacOS/$binary" |
| 55 | cat > "$app/Contents/Info.plist" <<PLIST |
| 56 | <?xml version="1.0" encoding="UTF-8"?> |
| 57 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 58 | <plist version="1.0"> |
| 59 | <dict> |
| 60 | <key>CFBundleName</key> |
| 61 | <string>Rusty Teams</string> |
| 62 | <key>CFBundleDisplayName</key> |
| 63 | <string>Rusty Teams</string> |
| 64 | <key>CFBundleIdentifier</key> |
| 65 | <string>org.rusty-teams.teams</string> |
| 66 | <key>CFBundleExecutable</key> |
| 67 | <string>$binary</string> |
| 68 | <key>CFBundleIconFile</key> |
| 69 | <string>icon</string> |
| 70 | <key>CFBundlePackageType</key> |
| 71 | <string>APPL</string> |
| 72 | <key>CFBundleShortVersionString</key> |
| 73 | <string>$version</string> |
| 74 | <key>CFBundleVersion</key> |
| 75 | <string>$version</string> |
| 76 | <key>NSHighResolutionCapable</key> |
| 77 | <true/> |
| 78 | <key>NSMicrophoneUsageDescription</key> |
| 79 | <string>Rusty Teams uses your microphone for calls.</string> |
| 80 | <key>NSCameraUsageDescription</key> |
| 81 | <string>Rusty Teams uses your camera for video calls.</string> |
| 82 | <key>NSScreenCaptureUsageDescription</key> |
| 83 | <string>Rusty Teams shares your screen in meetings.</string> |
| 84 | </dict> |
| 85 | </plist> |
| 86 | PLIST |
| 87 | |
| 88 | # The same icon the Windows exe embeds (assets/icon/app.rc); iconutil compiles |
| 89 | # whatever sizes the set has, all downscaled from the 256px original. |
| 90 | iconset="$(mktemp -d)/teams.iconset" |
| 91 | mkdir -p "$iconset" |
| 92 | png="$repoRoot/crates/app/assets/icon/teams-fast-256.png" |
| 93 | for size in 16 32 64 128 256; do |
| 94 | sips -z "$size" "$size" "$png" --out "$iconset/icon_${size}x${size}.png" >/dev/null |
| 95 | done |
| 96 | cp "$iconset/icon_32x32.png" "$iconset/icon_16x16@2x.png" |
| 97 | cp "$iconset/icon_64x64.png" "$iconset/icon_32x32@2x.png" |
| 98 | cp "$iconset/icon_256x256.png" "$iconset/icon_128x128@2x.png" |
| 99 | iconutil -c icns "$iconset" -o "$app/Contents/Resources/icon.icns" |
| 100 | |
| 101 | # ditto keeps the bundle intact (plain zip would not); the result is the release asset. |
| 102 | ditto -c -k --keepParent "$app" "$outputDirectory/teams-macos.zip" |
| 103 | |
| 104 | file "$output" |
| 105 | ls -la "$output" "$app/Contents/MacOS/$binary" "$outputDirectory/teams-macos.zip" |