BlueGit

ict rusty-teams

Files

rusty-teams / scripts / build-macos.sh

build-macos.sh

build: add macos build script (#5)
metis Michael Corrigan · pushed by metis · 3 weeks ago
146edfcd History
build-macos.sh 1.6 KiB Download Raw
1#!/usr/bin/env bash
2# Build a universal macOS binary (arm64 + x86_64) from a crate. Needs Xcode CLT only.
3# Usage: scripts/build-macos.sh [crate-dir, default crates/app] [binary-or-example-name, default: the crate's only binary]
4set -euo pipefail
5repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6crateDir="$(realpath "${1:-$repoRoot/crates/app}")"
7binaryName="${2:-}"
8export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$repoRoot/target}"
9export PATH="$HOME/.cargo/bin:$PATH"
10targets=(aarch64-apple-darwin x86_64-apple-darwin)
11
12rustup target add "${targets[@]}" >/dev/null
13
14buildArguments=(build --release)
15slicePathInTarget="release"
16if [ -n "$binaryName" ] && [ -f "$crateDir/examples/$binaryName.rs" ]; then
17 buildArguments+=(--example "$binaryName")
18 slicePathInTarget="release/examples"
19elif [ -n "$binaryName" ]; then
20 buildArguments+=(--bin "$binaryName")
21fi
22
23for target in "${targets[@]}"; do
24 (cd "$crateDir" && cargo "${buildArguments[@]}" --target "$target")
25done
26
27slices=()
28for target in "${targets[@]}"; do
29 sliceDirectory="$CARGO_TARGET_DIR/$target/$slicePathInTarget"
30 if [ -n "$binaryName" ]; then
31 slices+=("$sliceDirectory/$binaryName")
32 else
33 candidates=("$sliceDirectory"/*)
34 [ "${#candidates[@]}" -eq 1 ] && [ -f "${candidates[0]}" ] ||
35 { echo "more than one binary in $sliceDirectory, pass the name" >&2; exit 1; }
36 slices+=("${candidates[0]}")
37 fi
38done
39
40outputDirectory="$CARGO_TARGET_DIR/universal/release"
41mkdir -p "$outputDirectory"
42output="$outputDirectory/$(basename "${slices[0]}")"
43lipo -create -output "$output" "${slices[@]}"
44file "$output"
45ls -la "$output"