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

Compile for Mac Catalyst #1915

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 31 additions & 7 deletions bindings/apple/build_crypto_xcframework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TARGET_DIR="${SRC_ROOT}/target"

GENERATED_DIR="${SRC_ROOT}/generated"
if [ -d "${GENERATED_DIR}" ]; then rm -rf "${GENERATED_DIR}"; fi
mkdir -p ${GENERATED_DIR}/{macos,simulator}
mkdir -p ${GENERATED_DIR}/{macos,simulator,catalyst}

REL_FLAG="--release"
REL_TYPE_DIR="release"
Expand All @@ -19,23 +19,38 @@ TARGET_CRATE=matrix-sdk-crypto-ffi

# Build static libs for all the different architectures

# Required by olm-sys crate
# Required by olm-sys crate. @todo: Is this the right SDK path for Catalyst as well?
export IOS_SDK_PATH=`xcrun --show-sdk-path --sdk iphoneos`

# iOS
echo -e "Building for iOS [1/5]"
echo -e "Building for iOS [1/7]"
cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios"

# Mac Catalyst
# 1) The target has no pre=built rust-std, so we use `-Z build-std`.
# how to build-std: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std
# list of targets with prebuilt rust-std: https://doc.rust-lang.org/nightly/rustc/platform-support.html
# 2) requires a recent nightly wih https://github.com/rust-lang/rust/pull/111384 merged
CATALYST_RUST_NIGHTLY="nightly"
echo -e "\nInstalling nightly and rust-src for Mac Catalyst"
rustup toolchain install ${CATALYST_RUST_NIGHTLY} --no-self-update
rustup component add rust-src --toolchain ${CATALYST_RUST_NIGHTLY}

echo -e "\nBuilding for macOS Catalyst (Apple Silicon) [2/7]"
cargo +${CATALYST_RUST_NIGHTLY} build -p ${TARGET_CRATE} ${REL_FLAG} -Z build-std --target aarch64-apple-ios-macabi
echo -e "\nBuilding for macOS Catalyst (Intel) [3/7]"
cargo +${CATALYST_RUST_NIGHTLY} build -p ${TARGET_CRATE} ${REL_FLAG} -Z build-std --target x86_64-apple-ios-macabi

# MacOS
echo -e "\nBuilding for macOS (Apple Silicon) [2/5]"
echo -e "\nBuilding for macOS (Apple Silicon) [4/7]"
cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-darwin"
echo -e "\nBuilding for macOS (Intel) [3/5]"
echo -e "\nBuilding for macOS (Intel) [5/7]"
cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "x86_64-apple-darwin"

# iOS Simulator
echo -e "\nBuilding for iOS Simulator (Apple Silicon) [4/5]"
echo -e "\nBuilding for iOS Simulator (Apple Silicon) [6/7]"
cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios-sim"
echo -e "\nBuilding for iOS Simulator (Intel) [5/5]"
echo -e "\nBuilding for iOS Simulator (Intel) [7/7]"
cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "x86_64-apple-ios"

echo -e "\nCreating XCFramework"
Expand All @@ -47,6 +62,12 @@ lipo -create \
"${TARGET_DIR}/aarch64-apple-darwin/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \
-output "${GENERATED_DIR}/macos/libmatrix_sdk_crypto_ffi.a"

# Catalyst
lipo -create \
"${TARGET_DIR}/aarch64-apple-ios-macabi/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \
"${TARGET_DIR}/x86_64-apple-ios-macabi/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \
-output "${GENERATED_DIR}/catalyst/libmatrix_sdk_crypto_ffi.a"

# iOS Simulator
lipo -create \
"${TARGET_DIR}/x86_64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \
Expand Down Expand Up @@ -85,10 +106,13 @@ xcodebuild -create-xcframework \
-headers ${HEADERS_DIR} \
-library "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" \
-headers ${HEADERS_DIR} \
-library "${GENERATED_DIR}/catalyst/libmatrix_sdk_crypto_ffi.a" \
-headers ${HEADERS_DIR} \
-output "${GENERATED_DIR}/MatrixSDKCryptoFFI.xcframework"

# Cleanup
if [ -d "${GENERATED_DIR}/macos" ]; then rm -rf "${GENERATED_DIR}/macos"; fi
if [ -d "${GENERATED_DIR}/catalyst" ]; then rm -rf "${GENERATED_DIR}/catalyst"; fi
if [ -d "${GENERATED_DIR}/simulator" ]; then rm -rf "${GENERATED_DIR}/simulator"; fi
if [ -d ${HEADERS_DIR} ]; then rm -rf ${HEADERS_DIR}; fi

Expand Down
40 changes: 31 additions & 9 deletions xtask/src/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ fn generate_uniffi(library_file: &Path, ffi_directory: &Path) -> Result<()> {
}

fn build_for_target(target: &str, profile: &str) -> Result<PathBuf> {
cmd!("cargo build -p matrix-sdk-ffi --target {target} --profile {profile}").run()?;
let is_catalyst = target.ends_with("-macabi");
if is_catalyst {
const CATALYST_RUST_NIGHTLY: &str = "nightly";
cmd!("rustup toolchain install {CATALYST_RUST_NIGHTLY} --no-self-update").run()?;
cmd!("rustup component add rust-src --toolchain {CATALYST_RUST_NIGHTLY}").run()?;
cmd!("cargo +{CATALYST_RUST_NIGHTLY} build -p matrix-sdk-ffi -Z build-std --target {target} --profile {profile}").run()?;
} else {
cmd!("cargo build -p matrix-sdk-ffi --target {target} --profile {profile}").run()?;
}

// The builtin dev profile has its files stored under target/debug, all
// other targets have matching directory names
Expand Down Expand Up @@ -147,20 +155,25 @@ fn build_xcframework(

(vec![build_path.clone()], build_path)
} else {
println!("-- Building for iOS [1/5]");
println!("-- Building for iOS [1/7]");
let ios_path = build_for_target("aarch64-apple-ios", profile)?;

println!("-- Building for macOS (Apple Silicon) [2/5]");
println!("-- Building for macOS (Apple Silicon) [2/7]");
let darwin_arm_path = build_for_target("aarch64-apple-darwin", profile)?;
println!("-- Building for macOS (Intel) [3/5]");
println!("-- Building for macOS (Intel) [3/7]");
let darwin_x86_path = build_for_target("x86_64-apple-darwin", profile)?;

println!("-- Building for iOS Simulator (Apple Silicon) [4/5]");
println!("-- Building for Mac Catalyst (Apple Silicon) [4/7]");
let catalyst_arm_path = build_for_target("aarch64-apple-ios-macabi", profile)?;
println!("-- Building for Mac Catalyst (Intel) [5/7]");
let catalyst_x86_path = build_for_target("x86_64-apple-ios-macabi", profile)?;

println!("-- Building for iOS Simulator (Apple Silicon) [6/7]");
let ios_sim_arm_path = build_for_target("aarch64-apple-ios-sim", profile)?;
println!("-- Building for iOS Simulator (Intel) [5/5]");
println!("-- Building for iOS Simulator (Intel) [7/7]");
let ios_sim_x86_path = build_for_target("x86_64-apple-ios", profile)?;

println!("-- Running Lipo for macOS [1/2]");
println!("-- Running Lipo for macOS [1/3]");
// # macOS
let lipo_target_macos = generated_dir.join("libmatrix_sdk_ffi_macos.a");
cmd!(
Expand All @@ -169,15 +182,24 @@ fn build_xcframework(
)
.run()?;

println!("-- Running Lipo for iOS Simulator [2/2]");
println!("-- Running Lipo for Mac Catalyst [2/3]");
// # Catalyst
let lipo_target_catalyst = generated_dir.join("libmatrix_sdk_ffi_catalyst.a");
cmd!(
"lipo -create {catalyst_x86_path} {catalyst_arm_path}
-output {lipo_target_catalyst}"
)
.run()?;

println!("-- Running Lipo for iOS Simulator [3/3]");
// # iOS Simulator
let lipo_target_sim = generated_dir.join("libmatrix_sdk_ffi_iossimulator.a");
cmd!(
"lipo -create {ios_sim_arm_path} {ios_sim_x86_path}
-output {lipo_target_sim}"
)
.run()?;
(vec![lipo_target_macos, lipo_target_sim, ios_path], darwin_x86_path)
(vec![lipo_target_macos, lipo_target_catalyst, lipo_target_sim, ios_path], darwin_x86_path)
};

println!("-- Generating uniffi files");
Expand Down