Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Simplify Ensu Rust build script

Manav Rathi050a0b75c9af
diff --git a/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh b/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh
index f68b9f84d1..619bfca073 100755
--- a/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh
+++ b/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh
@@ -1,268 +1,135 @@
-#!/bin/sh
-set -euo pipefail
+#!/usr/bin/env bash
-# Build the UniFFI Rust static libraries for the current Xcode build platform.
+# Build Ensu's UniFFI Rust static libs for the current Xcode build.
+# Invoked from an Xcode "Run Script" build phase.
#
-# Outputs universal static libraries at:
-# $TARGET_TEMP_DIR/ensu_rust/libcore.a
-# $TARGET_TEMP_DIR/ensu_rust/libdb.a
-# $TARGET_TEMP_DIR/ensu_rust/libsync.a
-# $TARGET_TEMP_DIR/ensu_rust/libinference.a
-
-if [ -z "${SRCROOT:-}" ] || [ -z "${TARGET_TEMP_DIR:-}" ] || [ -z "${PLATFORM_NAME:-}" ] || [ -z "${ARCHS:-}" ]; then
- echo "Missing required Xcode environment variables (SRCROOT/TARGET_TEMP_DIR/PLATFORM_NAME/ARCHS)" >&2
- exit 1
-fi
-
-REPO_ROOT="$(cd "${SRCROOT}/../../../../.." && pwd)"
-OUT_DIR="${TARGET_TEMP_DIR}/ensu_rust"
-GENERATED_DIR="${SRCROOT}/Ensu/Generated"
-
-CARGO_BIN_DIR="${CARGO_HOME:-${HOME}/.cargo}/bin"
-if [ -d "${CARGO_BIN_DIR}" ]; then
- export PATH="${CARGO_BIN_DIR}:${PATH}"
-fi
-
-find_tool() {
- tool_name="$1"
- shift
-
- tool_path="$(command -v "${tool_name}" 2>/dev/null || true)"
- if [ -n "${tool_path}" ]; then
- printf '%s\n' "${tool_path}"
- return 0
- fi
-
- for candidate in "$@"; do
- if [ -x "${candidate}" ]; then
- printf '%s\n' "${candidate}"
- return 0
- fi
- done
-
- return 1
-}
-
-CARGO_BIN="$(find_tool cargo "${CARGO_BIN_DIR}/cargo" || true)"
-RUSTUP_BIN="$(find_tool rustup "${CARGO_BIN_DIR}/rustup" || true)"
-CMAKE_BIN="$(find_tool cmake /opt/homebrew/bin/cmake /usr/local/bin/cmake /Applications/CMake.app/Contents/bin/cmake || true)"
-
-if [ -z "${CARGO_BIN}" ] || [ -z "${RUSTUP_BIN}" ]; then
- echo "Rust tools not found in PATH for Xcode." >&2
- echo "Expected to find cargo and rustup, usually under ${CARGO_BIN_DIR}." >&2
- exit 1
-fi
-
-if [ -z "${CMAKE_BIN}" ]; then
- echo "cmake not found for Xcode." >&2
- echo "Expected to find cmake on PATH or at /opt/homebrew/bin/cmake, /usr/local/bin/cmake, or /Applications/CMake.app/Contents/bin/cmake." >&2
- exit 1
-fi
-
-export CMAKE="${CMAKE_BIN}"
-export PATH="$(dirname "${CMAKE_BIN}"):${PATH}"
+# Inputs (from Xcode): SRCROOT, TARGET_TEMP_DIR, PLATFORM_NAME, ARCHS,
+# CONFIGURATION, IPHONEOS_DEPLOYMENT_TARGET,
+# ENTE_API_ENDPOINT.
+# Outputs (to $TARGET_TEMP_DIR/ensu_rust):
+# libcore.a libdb.a libsync.a libinference.a
+#
+# UniFFI Swift bindings under $SRCROOT/Ensu/Generated/ are generated
+# out-of-band by `cargo codegen ensu-ios`; this script only checks that
+# they exist.
-mkdir -p "${OUT_DIR}"
-mkdir -p "${GENERATED_DIR}"
+set -euo pipefail
-# Use debug profile for Debug builds to speed iteration.
-CARGO_FLAGS="--locked"
-PROFILE="debug"
-if [ "${CONFIGURATION:-Debug}" = "Release" ]; then
- CARGO_FLAGS="${CARGO_FLAGS} --release"
- PROFILE="release"
+: "${SRCROOT:?}" "${TARGET_TEMP_DIR:?}" "${PLATFORM_NAME:?}" "${ARCHS:?}"
+
+REPO_ROOT=$(cd "$SRCROOT/../../../../.." && pwd)
+GENERATED_DIR="$SRCROOT/Ensu/Generated"
+OUT_DIR="$TARGET_TEMP_DIR/ensu_rust"
+mkdir -p "$GENERATED_DIR" "$OUT_DIR"
+
+# Xcode's build-phase PATH omits Homebrew and rustup's install dir.
+export PATH="${CARGO_HOME:-$HOME/.cargo}/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"
+
+for tool in cargo rustup cmake; do
+ command -v "$tool" >/dev/null || { echo "error: $tool not on PATH" >&2; exit 1; }
+done
+
+for name in core db sync inference; do
+ for ext in .swift FFI.h FFI.modulemap; do
+ [[ -f "$GENERATED_DIR/$name$ext" ]] || {
+ echo "error: missing $GENERATED_DIR/$name$ext" >&2
+ echo " run: (cd $REPO_ROOT/rust && cargo codegen ensu-ios)" >&2
+ exit 1
+ }
+ done
+done
+
+# Bake ENTE_API_ENDPOINT into a generated Swift file.
+endpoint=$(printf '%s' "${ENTE_API_ENDPOINT:-}" | tr -d '\n\r' \
+ | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
+if [[ -n "$endpoint" ]]; then
+ escaped=${endpoint//\\/\\\\}; escaped=${escaped//\"/\\\"}
+ literal="\"$escaped\""
+else
+ literal="nil"
fi
-
-write_endpoint_config() {
- endpoint_value="${ENTE_API_ENDPOINT:-}"
-
- endpoint_value="$(printf "%s" "${endpoint_value}")"
- endpoint_value="${endpoint_value%%$'\n'*}"
- endpoint_value="$(echo "${endpoint_value}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
-
- generated_file="${GENERATED_DIR}/EndpointConfig.swift"
- mkdir -p "$(dirname "${generated_file}")"
-
- if [ -n "${endpoint_value}" ]; then
- escaped_value="${endpoint_value//\\/\\\\}"
- escaped_value="${escaped_value//\"/\\\"}"
- endpoint_literal="\"${escaped_value}\""
- else
- endpoint_literal="nil"
- fi
-
- cat > "${generated_file}" <<EOF
-// This file is generated by scripts/build-rust.sh. Do not edit manually.
-import Foundation
-
+cat >"$GENERATED_DIR/EndpointConfig.swift" <<EOF
+// Generated by scripts/build-rust.sh. Do not edit manually.
enum BuildEndpointConfig {
- static let endpoint: String? = ${endpoint_literal}
+ static let endpoint: String? = $literal
}
EOF
-}
-bindings_missing() {
- for binding in \
- "${GENERATED_DIR}/core.swift" \
- "${GENERATED_DIR}/coreFFI.h" \
- "${GENERATED_DIR}/coreFFI.modulemap" \
- "${GENERATED_DIR}/db.swift" \
- "${GENERATED_DIR}/dbFFI.h" \
- "${GENERATED_DIR}/dbFFI.modulemap" \
- "${GENERATED_DIR}/sync.swift" \
- "${GENERATED_DIR}/syncFFI.h" \
- "${GENERATED_DIR}/syncFFI.modulemap" \
- "${GENERATED_DIR}/inference.swift" \
- "${GENERATED_DIR}/inferenceFFI.h" \
- "${GENERATED_DIR}/inferenceFFI.modulemap"; do
- if [ ! -f "${binding}" ]; then
- return 0
- fi
- done
+# Host build scripts compile against macOS; each target uses its own SDK below.
+SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
+TARGET_SDKROOT=$(xcrun --sdk "$PLATFORM_NAME" --show-sdk-path)
+TARGET_CLANG=$(xcrun --sdk "$PLATFORM_NAME" --find clang)
+export SDKROOT
+[[ -n "${IPHONEOS_DEPLOYMENT_TARGET:-}" ]] && \
+ export CMAKE_OSX_DEPLOYMENT_TARGET="$IPHONEOS_DEPLOYMENT_TARGET"
- return 1
-}
-
-ensure_generated_bindings() {
- if ! bindings_missing; then
- return
- fi
-
- echo "Missing generated UniFFI Swift bindings in ${GENERATED_DIR}." >&2
- echo "Run the repo-local codegen step first:" >&2
- echo " cd \"${REPO_ROOT}/rust\" && cargo codegen ensu-ios" >&2
- exit 1
-}
+case "${CONFIGURATION:-Debug}" in
+ Release) profile=release; cargo_flags=(--locked --release) ;;
+ *) profile=debug; cargo_flags=(--locked) ;;
+esac
-# Map Xcode platform+arch to Rust target triple.
-# Note: For iOS simulator on arm64, Rust uses aarch64-apple-ios-sim.
rust_target_for() {
- platform="$1"
- arch="$2"
-
- case "$platform" in
- iphoneos)
- if [ "$arch" = "arm64" ]; then
- echo "aarch64-apple-ios"
- else
- echo "" # unsupported
- fi
- ;;
- iphonesimulator)
- if [ "$arch" = "arm64" ]; then
- echo "aarch64-apple-ios-sim"
- elif [ "$arch" = "x86_64" ]; then
- echo "x86_64-apple-ios"
- else
- echo "" # unsupported
- fi
- ;;
- *)
- echo "" # unsupported
- ;;
- esac
-}
-
-sdk_for_platform() {
- platform="$1"
- case "$platform" in
- iphoneos)
- echo "iphoneos";
- ;;
- iphonesimulator)
- echo "iphonesimulator";
- ;;
- *)
- echo "";
- ;;
- esac
+ case "$1:$2" in
+ iphoneos:arm64) echo aarch64-apple-ios ;;
+ iphonesimulator:arm64) echo aarch64-apple-ios-sim ;;
+ iphonesimulator:x86_64) echo x86_64-apple-ios ;;
+ *) return 1 ;;
+ esac
}
-TARGET_SDK_NAME="$(sdk_for_platform "${PLATFORM_NAME}")"
-if [ -z "${TARGET_SDK_NAME}" ]; then
- echo "Unsupported platform: ${PLATFORM_NAME}" >&2
- exit 1
-fi
-
-TARGET_SDKROOT="$(xcrun --sdk "${TARGET_SDK_NAME}" --show-sdk-path)"
-TARGET_CLANG="$(xcrun --sdk "${TARGET_SDK_NAME}" --find clang)"
-HOST_SDKROOT="$(xcrun --sdk macosx --show-sdk-path)"
-
-# Ensure host builds (build scripts) use the macOS SDK.
-export SDKROOT="${HOST_SDKROOT}"
-
-ensure_generated_bindings
-write_endpoint_config
-
-build_crate_universal() {
- crate_name="$1" # e.g. core
- crate_dir="$2" # absolute path
- lib_name="$3" # e.g. libcore.a
-
- echo "πŸ”§ Building ${crate_name} β†’ ${lib_name}"
-
- INPUT_LIBS=""
-
- for arch in ${ARCHS}; do
- target="$(rust_target_for "${PLATFORM_NAME}" "$arch")"
- if [ -z "$target" ]; then
- echo "Skipping unsupported arch/platform: ${PLATFORM_NAME} ${arch}" >&2
- continue
- fi
-
- if ! "${RUSTUP_BIN}" target list --installed | grep -q "^${target}$"; then
- echo "Installing Rust target ${target}..."
- "${RUSTUP_BIN}" target add "${target}"
- fi
-
- target_env="$(echo "${target}" | tr '[:lower:]-' '[:upper:]_')"
-
- eval "export CARGO_TARGET_${target_env}_LINKER=\"${TARGET_CLANG}\""
- eval "export CFLAGS_${target_env}=\"-isysroot ${TARGET_SDKROOT}\""
- eval "export CXXFLAGS_${target_env}=\"-isysroot ${TARGET_SDKROOT}\""
-
- if [ -n "${IPHONEOS_DEPLOYMENT_TARGET:-}" ]; then
- export CMAKE_OSX_DEPLOYMENT_TARGET="${IPHONEOS_DEPLOYMENT_TARGET}"
- fi
-
- RUSTFLAGS_TARGET="${RUSTFLAGS:-} -C link-arg=-isysroot -C link-arg=${TARGET_SDKROOT}"
- if [ "${PLATFORM_NAME}" = "iphonesimulator" ] && [ -n "${IPHONEOS_DEPLOYMENT_TARGET:-}" ]; then
- RUSTFLAGS_TARGET="${RUSTFLAGS_TARGET} -C link-arg=-mios-simulator-version-min=${IPHONEOS_DEPLOYMENT_TARGET}"
- elif [ "${PLATFORM_NAME}" = "iphoneos" ] && [ -n "${IPHONEOS_DEPLOYMENT_TARGET:-}" ]; then
- RUSTFLAGS_TARGET="${RUSTFLAGS_TARGET} -C link-arg=-miphoneos-version-min=${IPHONEOS_DEPLOYMENT_TARGET}"
- fi
-
- echo "Building ${crate_name} for ${PLATFORM_NAME} ${arch} (${target}) [${PROFILE}]"
- (cd "${crate_dir}" && RUSTFLAGS="${RUSTFLAGS_TARGET}" "${CARGO_BIN}" build ${CARGO_FLAGS} --target "${target}")
-
- lib_path="${crate_dir}/target/${target}/${PROFILE}/${lib_name}"
- if [ ! -f "${lib_path}" ]; then
- echo "Expected library not found: ${lib_path}" >&2
- exit 1
- fi
-
- arch_lib="${OUT_DIR}/${lib_name%.a}_${arch}.a"
- cp "${lib_path}" "${arch_lib}"
- INPUT_LIBS="${INPUT_LIBS} ${arch_lib}"
- done
-
- universal_lib="${OUT_DIR}/${lib_name}"
- rm -f "${universal_lib}"
-
- set -- ${INPUT_LIBS}
- if [ "$#" -eq 0 ]; then
- echo "No supported architectures found for ${crate_name} (${PLATFORM_NAME} ${ARCHS})" >&2
- exit 1
- elif [ "$#" -eq 1 ]; then
- cp "$1" "${universal_lib}"
- else
- lipo -create ${INPUT_LIBS} -output "${universal_lib}"
- fi
-
- echo "βœ… Built ${universal_lib}"
+build_crate() {
+ local name=$1 crate_dir=$2 lib=$3
+ local arch_libs=()
+
+ echo "==> $name ($profile, $PLATFORM_NAME: $ARCHS)"
+
+ for arch in $ARCHS; do
+ local target
+ target=$(rust_target_for "$PLATFORM_NAME" "$arch") || {
+ echo " skip: unsupported $PLATFORM_NAME/$arch" >&2
+ continue
+ }
+
+ rustup target list --installed | grep -qx "$target" \
+ || rustup target add "$target"
+
+ local suffix
+ suffix=$(echo "$target" | tr '[:lower:]-' '[:upper:]_')
+ export "CARGO_TARGET_${suffix}_LINKER=$TARGET_CLANG"
+ export "CFLAGS_${suffix}=-isysroot $TARGET_SDKROOT"
+ export "CXXFLAGS_${suffix}=-isysroot $TARGET_SDKROOT"
+
+ local rustflags="${RUSTFLAGS:-} -C link-arg=-isysroot -C link-arg=$TARGET_SDKROOT"
+ if [[ -n "${IPHONEOS_DEPLOYMENT_TARGET:-}" ]]; then
+ local min_flag
+ case $PLATFORM_NAME in
+ iphoneos) min_flag=-miphoneos-version-min ;;
+ iphonesimulator) min_flag=-mios-simulator-version-min ;;
+ esac
+ rustflags+=" -C link-arg=$min_flag=$IPHONEOS_DEPLOYMENT_TARGET"
+ fi
+
+ (cd "$crate_dir" && RUSTFLAGS="$rustflags" cargo build "${cargo_flags[@]}" --target "$target")
+
+ local built="$crate_dir/target/$target/$profile/$lib"
+ [[ -f "$built" ]] || { echo "error: expected $built" >&2; exit 1; }
+
+ local arch_lib="$OUT_DIR/${lib%.a}_${arch}.a"
+ cp "$built" "$arch_lib"
+ arch_libs+=("$arch_lib")
+ done
+
+ local out="$OUT_DIR/$lib"
+ rm -f "$out"
+ case ${#arch_libs[@]} in
+ 0) echo "error: no supported archs for $name" >&2; exit 1 ;;
+ 1) cp "${arch_libs[0]}" "$out" ;;
+ *) lipo -create "${arch_libs[@]}" -output "$out" ;;
+ esac
+ echo " -> $out"
}
-build_crate_universal "core" "${REPO_ROOT}/rust/uniffi/core" "libcore.a"
-build_crate_universal "db" "${REPO_ROOT}/rust/uniffi/ensu/db" "libdb.a"
-build_crate_universal "sync" "${REPO_ROOT}/rust/uniffi/ensu/sync" "libsync.a"
-build_crate_universal "inference" "${REPO_ROOT}/rust/uniffi/ensu/inference" "libinference.a"
+build_crate core "$REPO_ROOT/rust/uniffi/core" libcore.a
+build_crate db "$REPO_ROOT/rust/uniffi/ensu/db" libdb.a
+build_crate sync "$REPO_ROOT/rust/uniffi/ensu/sync" libsync.a
+build_crate inference "$REPO_ROOT/rust/uniffi/ensu/inference" libinference.a