From ea4737269fe13a8f643db80d77997fa5fbc6cc6a Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 16 Apr 2024 11:32:48 +0200 Subject: [PATCH 01/10] Fix crypto xcframework apple bindings --- bindings/apple/build_crypto_xcframework.sh | 11 +++++------ bindings/matrix-sdk-crypto-ffi/Cargo.toml | 7 ++++++- bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs | 4 ++++ 3 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs diff --git a/bindings/apple/build_crypto_xcframework.sh b/bindings/apple/build_crypto_xcframework.sh index 10c7818de4e..66928bc08ac 100755 --- a/bindings/apple/build_crypto_xcframework.sh +++ b/bindings/apple/build_crypto_xcframework.sh @@ -54,20 +54,19 @@ lipo -create \ -output "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" # Generate uniffi files -cargo uniffi-bindgen generate \ +cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ --language swift \ - --lib-file "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - --config "${SRC_ROOT}/bindings/${TARGET_CRATE}/uniffi.toml" \ + --library "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ --out-dir ${GENERATED_DIR} \ - "${SRC_ROOT}/bindings/${TARGET_CRATE}/src/olm.udl" # Move headers to the right place HEADERS_DIR=${GENERATED_DIR}/headers mkdir -p ${HEADERS_DIR} mv ${GENERATED_DIR}/*.h ${HEADERS_DIR} -# Rename and move modulemap to the right place -mv ${GENERATED_DIR}/*.modulemap ${HEADERS_DIR}/module.modulemap +# Rename and merge the modulemap files into a single file to the right place +for f in ${GENERATED_DIR}/*.modulemap; do cat $f; echo; done > ${HEADERS_DIR}/module.modulemap +rm ${GENERATED_DIR}/*.modulemap # Move source files to the right place SWIFT_DIR="${GENERATED_DIR}/Sources" diff --git a/bindings/matrix-sdk-crypto-ffi/Cargo.toml b/bindings/matrix-sdk-crypto-ffi/Cargo.toml index 70aefacdeb1..da487027008 100644 --- a/bindings/matrix-sdk-crypto-ffi/Cargo.toml +++ b/bindings/matrix-sdk-crypto-ffi/Cargo.toml @@ -12,6 +12,10 @@ publish = false [lib] crate-type = ["cdylib", "staticlib"] +[[bin]] +name = "matrix_sdk_crypto_ffi" +path = "uniffi-bindgen.rs" + [features] default = ["bundled-sqlite"] bundled-sqlite = ["matrix-sdk-sqlite/bundled"] @@ -31,7 +35,7 @@ sha2 = { workspace = true } thiserror = { workspace = true } tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } # keep in sync with uniffi dependency in matrix-sdk-ffi, and uniffi_bindgen in ffi CI job -uniffi = { workspace = true } +uniffi = { workspace = true , features = ["cli"]} vodozemac = { workspace = true } zeroize = { workspace = true, features = ["zeroize_derive"] } @@ -54,6 +58,7 @@ features = ["rt-multi-thread"] [build-dependencies] vergen = { version = "8.2.5", features = ["build", "git", "gitcl"] } +uniffi = { workspace = true, features = ["build"] } [dev-dependencies] tempfile = "3.8.0" diff --git a/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs b/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs new file mode 100644 index 00000000000..145309ad2c6 --- /dev/null +++ b/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs @@ -0,0 +1,4 @@ + +fn main() { + uniffi::uniffi_bindgen_main() +} \ No newline at end of file From de989b2c5105a135cd2587566c42481a6dd4cd23 Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 16 Apr 2024 11:44:43 +0200 Subject: [PATCH 02/10] fmt --- bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs b/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs index 145309ad2c6..f6cff6cf1d9 100644 --- a/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs +++ b/bindings/matrix-sdk-crypto-ffi/uniffi-bindgen.rs @@ -1,4 +1,3 @@ - fn main() { uniffi::uniffi_bindgen_main() -} \ No newline at end of file +} From 6ad26fff5c33495df41a173c6a07217a02883aca Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 16 Apr 2024 12:04:04 +0200 Subject: [PATCH 03/10] remove unneeded \ at end of script --- bindings/apple/build_crypto_xcframework.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/apple/build_crypto_xcframework.sh b/bindings/apple/build_crypto_xcframework.sh index 66928bc08ac..84b5df8891e 100755 --- a/bindings/apple/build_crypto_xcframework.sh +++ b/bindings/apple/build_crypto_xcframework.sh @@ -57,7 +57,7 @@ lipo -create \ cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ --language swift \ --library "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - --out-dir ${GENERATED_DIR} \ + --out-dir ${GENERATED_DIR} # Move headers to the right place HEADERS_DIR=${GENERATED_DIR}/headers From 604190d3caf847ce1267ac56b31734e44c4596cf Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 13 May 2024 17:35:47 +0200 Subject: [PATCH 04/10] CI: Test CrypoFFI XCFramework generation --- .github/workflows/bindings_ci.yml | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index cd1463d6063..bc04a211e6a 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -116,3 +116,46 @@ jobs: with: use_rust_sdk: "." # use local checkout use_complement_crypto: "MATCHING_BRANCH" + + test-crypto-apple-framework-generation: + name: Generate Crypto FFI Apple XCFramework + runs-on: macos-12 + if: github.event_name == 'push' || !github.event.pull_request.draft + + steps: + - name: Checkout + uses: actions/checkout@v3 + + # install protoc in case we end up rebuilding opentelemetry-proto + - name: Install protoc + uses: taiki-e/install-action@v2 + with: + tool: protoc@3.20.3 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Add rust targets + run: | + rustup target add aarch64-apple-ios + rustup target add aarch64-apple-darwin + rustup target add x86_64-apple-darwin + rustup target add iaarch64-apple-ios-sim + rustup target add x86_64-apple-ios + + # Cargo config can screw with caching and is only used for alias config + # and extra lints, which we don't care about here + - name: Delete cargo config + run: rm .cargo/config.toml + + - name: Load cache + uses: Swatinem/rust-cache@v2 + with: + save-if: ${{ github.ref == 'refs/heads/main' }} + + - name: Run the Build Framework script + run: ./bindings/apple/build_crypto_xcframework.sh + + - name: Is XCFramework generated? + if: ${{ hashFiles('generated/MatrixSDKCryptoFFI.zip') != '' }} + run: echo "XCFramework exists" From 20ada4014dd09849c57896aa3451ff1b95baa793 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 13 May 2024 17:43:06 +0200 Subject: [PATCH 05/10] fix target name --- .github/workflows/bindings_ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index bc04a211e6a..935b7a573b7 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -140,7 +140,7 @@ jobs: rustup target add aarch64-apple-ios rustup target add aarch64-apple-darwin rustup target add x86_64-apple-darwin - rustup target add iaarch64-apple-ios-sim + rustup target add aarch64-apple-ios-sim rustup target add x86_64-apple-ios # Cargo config can screw with caching and is only used for alias config From a1bbe9d81026704687fc2d084e255b3aaaf6123b Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 14 May 2024 13:43:22 +0200 Subject: [PATCH 06/10] reduce build time of CI check --- .github/workflows/bindings_ci.yml | 2 +- bindings/apple/build_crypto_xcframework.sh | 121 ++++++++++++++------- 2 files changed, 81 insertions(+), 42 deletions(-) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index 935b7a573b7..0e98cb6694d 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -154,7 +154,7 @@ jobs: save-if: ${{ github.ref == 'refs/heads/main' }} - name: Run the Build Framework script - run: ./bindings/apple/build_crypto_xcframework.sh + run: ./bindings/apple/build_crypto_xcframework.sh -i - name: Is XCFramework generated? if: ${{ hashFiles('generated/MatrixSDKCryptoFFI.zip') != '' }} diff --git a/bindings/apple/build_crypto_xcframework.sh b/bindings/apple/build_crypto_xcframework.sh index 84b5df8891e..486854847ec 100755 --- a/bindings/apple/build_crypto_xcframework.sh +++ b/bindings/apple/build_crypto_xcframework.sh @@ -1,6 +1,22 @@ #!/usr/bin/env bash set -eEu +helpFunction() { + echo "" + echo "Usage: $0 -only_ios" + echo -e "\t-i Option to build only for iOS. Default will build for all targets." + exit 1 +} + +only_ios='false' + +while getopts ':i' 'opt'; do + case ${opt} in + 'i') only_ios='true' ;; + ?) helpFunction ;; + esac +done + cd "$(dirname "$0")" # Path to the repo root @@ -22,42 +38,59 @@ TARGET_CRATE=matrix-sdk-crypto-ffi # Required by olm-sys crate export IOS_SDK_PATH=`xcrun --show-sdk-path --sdk iphoneos` -# iOS -echo -e "Building for iOS [1/5]" -cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios" - -# MacOS -echo -e "\nBuilding for macOS (Apple Silicon) [2/5]" -cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-darwin" -echo -e "\nBuilding for macOS (Intel) [3/5]" -cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "x86_64-apple-darwin" - -# iOS Simulator -echo -e "\nBuilding for iOS Simulator (Apple Silicon) [4/5]" -cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios-sim" -echo -e "\nBuilding for iOS Simulator (Intel) [5/5]" -cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "x86_64-apple-ios" +if ${only_ios}; then + # iOS + echo -e "Building only for iOS" + cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios" +else + # iOS + echo -e "Building for iOS [1/5]" + cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios" + + # MacOS + echo -e "\nBuilding for macOS (Apple Silicon) [2/5]" + cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-darwin" + echo -e "\nBuilding for macOS (Intel) [3/5]" + cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "x86_64-apple-darwin" + + # iOS Simulator + echo -e "\nBuilding for iOS Simulator (Apple Silicon) [4/5]" + cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "aarch64-apple-ios-sim" + echo -e "\nBuilding for iOS Simulator (Intel) [5/5]" + cargo build -p ${TARGET_CRATE} ${REL_FLAG} --target "x86_64-apple-ios" +fi echo -e "\nCreating XCFramework" # Lipo together the libraries for the same platform -# MacOS -lipo -create \ - "${TARGET_DIR}/x86_64-apple-darwin/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - "${TARGET_DIR}/aarch64-apple-darwin/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - -output "${GENERATED_DIR}/macos/libmatrix_sdk_crypto_ffi.a" - -# iOS Simulator -lipo -create \ - "${TARGET_DIR}/x86_64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - -output "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" - -# Generate uniffi files -cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ - --language swift \ - --library "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - --out-dir ${GENERATED_DIR} +if [ !${only_ios} ]; then + echo "Lipo together the libraries for the same platform" + # MacOS + lipo -create \ + "${TARGET_DIR}/x86_64-apple-darwin/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + "${TARGET_DIR}/aarch64-apple-darwin/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + -output "${GENERATED_DIR}/macos/libmatrix_sdk_crypto_ffi.a" + + # iOS Simulator + lipo -create \ + "${TARGET_DIR}/x86_64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + -output "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" +fi + +if ${only_ios}; then + # Generate uniffi files + cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ + --language swift \ + --library "${TARGET_DIR}/aarch64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + --out-dir ${GENERATED_DIR} +else + # Generate uniffi files + cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ + --language swift \ + --library "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + --out-dir ${GENERATED_DIR} +fi # Move headers to the right place HEADERS_DIR=${GENERATED_DIR}/headers @@ -76,15 +109,21 @@ mv ${GENERATED_DIR}/*.swift ${SWIFT_DIR} # Build the xcframework if [ -d "${GENERATED_DIR}/MatrixSDKCryptoFFI.xcframework" ]; then rm -rf "${GENERATED_DIR}/MatrixSDKCryptoFFI.xcframework"; fi - -xcodebuild -create-xcframework \ - -library "${TARGET_DIR}/aarch64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - -headers ${HEADERS_DIR} \ - -library "${GENERATED_DIR}/macos/libmatrix_sdk_crypto_ffi.a" \ - -headers ${HEADERS_DIR} \ - -library "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" \ - -headers ${HEADERS_DIR} \ - -output "${GENERATED_DIR}/MatrixSDKCryptoFFI.xcframework" +if ${only_ios}; then + xcodebuild -create-xcframework \ + -library "${TARGET_DIR}/aarch64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + -headers ${HEADERS_DIR} \ + -output "${GENERATED_DIR}/MatrixSDKCryptoFFI.xcframework" +else + xcodebuild -create-xcframework \ + -library "${TARGET_DIR}/aarch64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + -headers ${HEADERS_DIR} \ + -library "${GENERATED_DIR}/macos/libmatrix_sdk_crypto_ffi.a" \ + -headers ${HEADERS_DIR} \ + -library "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" \ + -headers ${HEADERS_DIR} \ + -output "${GENERATED_DIR}/MatrixSDKCryptoFFI.xcframework" +fi # Cleanup if [ -d "${GENERATED_DIR}/macos" ]; then rm -rf "${GENERATED_DIR}/macos"; fi From d2ecf461d0d1876fb6ef567c5b4736a4f0b201d0 Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 14 May 2024 14:37:31 +0200 Subject: [PATCH 07/10] fix script --- bindings/apple/build_crypto_xcframework.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/apple/build_crypto_xcframework.sh b/bindings/apple/build_crypto_xcframework.sh index 486854847ec..0d051533762 100755 --- a/bindings/apple/build_crypto_xcframework.sh +++ b/bindings/apple/build_crypto_xcframework.sh @@ -63,7 +63,7 @@ fi echo -e "\nCreating XCFramework" # Lipo together the libraries for the same platform -if [ !${only_ios} ]; then +if ! ${only_ios}; then echo "Lipo together the libraries for the same platform" # MacOS lipo -create \ From f7e06d0c20b99ba25ae5094d22ee9c12c805bc3c Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 21 May 2024 10:18:22 +0200 Subject: [PATCH 08/10] Generate ffi files only using aarch64-apple-ios --- bindings/apple/build_crypto_xcframework.sh | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/bindings/apple/build_crypto_xcframework.sh b/bindings/apple/build_crypto_xcframework.sh index 0d051533762..b10c6d26218 100755 --- a/bindings/apple/build_crypto_xcframework.sh +++ b/bindings/apple/build_crypto_xcframework.sh @@ -78,19 +78,11 @@ if ! ${only_ios}; then -output "${GENERATED_DIR}/simulator/libmatrix_sdk_crypto_ffi.a" fi -if ${only_ios}; then - # Generate uniffi files - cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ - --language swift \ - --library "${TARGET_DIR}/aarch64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - --out-dir ${GENERATED_DIR} -else - # Generate uniffi files - cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ - --language swift \ - --library "${TARGET_DIR}/aarch64-apple-ios-sim/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ - --out-dir ${GENERATED_DIR} -fi +# Generate uniffi files +cd ../matrix-sdk-crypto-ffi && cargo run --bin matrix_sdk_crypto_ffi generate \ + --language swift \ + --library "${TARGET_DIR}/aarch64-apple-ios/${REL_TYPE_DIR}/libmatrix_sdk_crypto_ffi.a" \ + --out-dir ${GENERATED_DIR} # Move headers to the right place HEADERS_DIR=${GENERATED_DIR}/headers From ee040aba6009cafcfd234981bcf96140a87267a4 Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 21 May 2024 13:24:38 +0200 Subject: [PATCH 09/10] split for do in several lines --- bindings/apple/build_crypto_xcframework.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bindings/apple/build_crypto_xcframework.sh b/bindings/apple/build_crypto_xcframework.sh index b10c6d26218..98c7b657e7d 100755 --- a/bindings/apple/build_crypto_xcframework.sh +++ b/bindings/apple/build_crypto_xcframework.sh @@ -90,7 +90,10 @@ mkdir -p ${HEADERS_DIR} mv ${GENERATED_DIR}/*.h ${HEADERS_DIR} # Rename and merge the modulemap files into a single file to the right place -for f in ${GENERATED_DIR}/*.modulemap; do cat $f; echo; done > ${HEADERS_DIR}/module.modulemap +for f in ${GENERATED_DIR}/*.modulemap +do + cat $f; echo; +done > ${HEADERS_DIR}/module.modulemap rm ${GENERATED_DIR}/*.modulemap # Move source files to the right place From b430d95c0aab174a471577f262e91e5a38defa77 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 22 May 2024 11:29:01 +0200 Subject: [PATCH 10/10] Workflow | only add the needed target for script --- .github/workflows/bindings_ci.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index 0e98cb6694d..1578d84d440 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -138,10 +138,6 @@ jobs: - name: Add rust targets run: | rustup target add aarch64-apple-ios - rustup target add aarch64-apple-darwin - rustup target add x86_64-apple-darwin - rustup target add aarch64-apple-ios-sim - rustup target add x86_64-apple-ios # Cargo config can screw with caching and is only used for alias config # and extra lints, which we don't care about here