Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an additional XCode + Rust project option #247

Open
bes opened this issue Nov 22, 2023 · 1 comment
Open

Add an additional XCode + Rust project option #247

bes opened this issue Nov 22, 2023 · 1 comment

Comments

@bes
Copy link
Contributor

bes commented Nov 22, 2023

I've been successfully using the "Swift Package" approach to build my swift-bridge library, but every time it's compiled XCode spends a long time refreshing the swift package(s), which can be tens of minutes!

There is another way though, and that is just adapting the XCode + Cargo variant to not build every time XCode builds, but rather do the rust build separately, with some additional scripts to copy things to the right place.

It's a very slight variation, but it's working for me.

@bes
Copy link
Contributor Author

bes commented Nov 22, 2023

Here is the build script I'm using on the rust-project side:

#!/bin/bash

set -e

# Make the script run in its own directory instead of the caller's directory.
cd "$(cd -P -- "$(dirname -- "$0")" && pwd -P)" || exit 1

XCODE_IOS_DIR="../ios"
RUST_LIB_DIR="$XCODE_IOS_DIR/rust-lib"
# The library name of the Cargo project
RUST_CRATE_NAME="my-rust-lib"
RUST_LIB_NAME="libmy_rust_lib_ios.a"

if [ ! -d "$XCODE_IOS_DIR" ]; then
  echo "$XCODE_IOS_DIR does not exist."
fi
mkdir -p "./target/simulator-ios/release"
mkdir -p "./target/simulator-ios/debug"
# The directory for the source files
mkdir -p "$RUST_LIB_DIR/sources/$RUST_CRATE_NAME"
# The directory for the aarch64/arm64 lib
mkdir -p "$RUST_LIB_DIR/libs/arm64-ios"
# The directory for the simulator lib
mkdir -p "$RUST_LIB_DIR/libs/simulator-ios"
# The directory actually used by the compiler (XCode will use this directory)
mkdir -p "$RUST_LIB_DIR/libs/working-lib"

case "$1" in
  "ci")
    cargo build
    ;;
  "debug")
    cargo build --target aarch64-apple-ios
    cargo build --target x86_64-apple-ios
    cargo build --target aarch64-apple-ios-sim
    lipo \
        "./target/aarch64-apple-ios-sim/debug/$RUST_LIB_NAME" \
        "./target/x86_64-apple-ios/debug/$RUST_LIB_NAME" -create -output \
        "./target/simulator-ios/debug/$RUST_LIB_NAME"
    cp "./target/aarch64-apple-ios/debug/$RUST_LIB_NAME" "$RUST_LIB_DIR/libs/arm64-ios/"
    cp "./target/simulator-ios/debug/$RUST_LIB_NAME" "$RUST_LIB_DIR/libs/simulator-ios/"
    cp -R "./generated/" "$RUST_LIB_DIR/sources/"
    ;;
  "debug-arm64-only")
    cargo build --target aarch64-apple-ios
    cp "./target/aarch64-apple-ios/debug/$RUST_LIB_NAME" "$RUST_LIB_DIR/libs/arm64-ios/"
    cp -R "./generated/" "$RUST_LIB_DIR/sources/"
    ;;
  "release")
    cargo build --release --target aarch64-apple-ios
    cargo build --release --target x86_64-apple-ios
    cargo build --release --target aarch64-apple-ios-sim
    lipo \
        "./target/aarch64-apple-ios-sim/release/$RUST_LIB_NAME" \
        "./target/x86_64-apple-ios/release/$RUST_LIB_NAME" -create -output \
        "./target/simulator-ios/release/$RUST_LIB_NAME"
    cp "./target/aarch64-apple-ios/release/$RUST_LIB_NAME" "$RUST_LIB_DIR/libs/arm64-ios/"
    cp "./target/simulator-ios/release/$RUST_LIB_NAME" "$RUST_LIB_DIR/libs/simulator-ios/"
    cp -R "./generated/" "$RUST_LIB_DIR/sources/"
    ;;
  "release-arm64-only")
    cargo build --release --target aarch64-apple-ios
    cp "./target/aarch64-apple-ios/release/$RUST_LIB_NAME" "$RUST_LIB_DIR/libs/arm64-ios/"
    cp -R "./generated/" "$RUST_LIB_DIR/sources/"
    ;;
  *)
    echo "Must give option argument (debug | release)"
    exit 1
    ;;
esac

On the XCode side I have this build phase script

echo "Copying the correct version of the iOS library for platform $PLATFORM_NAME and architectures $ARCHS"
mkdir -p "$PROJECT_DIR/rust-lib/libs/working-lib"
if [[ "$PLATFORM_NAME" = "iphonesimulator" ]]; then
    cp "$PROJECT_DIR/rust-lib/libs/simulator-ios/libmy_rust_lib_ios.a" "$PROJECT_DIR/rust-lib/libs/working-lib/libmy_rust_lib_ios.a"
else
    cp "$PROJECT_DIR/rust-lib/libs/arm64-ios/libmy_rust_lib_ios.a" "$PROJECT_DIR/rust-lib/libs/working-lib/libmy_rust_lib_ios.a"
fi

Using those two scripts, most of the other XCode + Cargo steps still make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant