Skip to content

Instantly share code, notes, and snippets.

@surpher
Last active July 17, 2025 12:48
Show Gist options
  • Select an option

  • Save surpher/bbf88e191e9d1f01ab2e2bbb85f9b528 to your computer and use it in GitHub Desktop.

Select an option

Save surpher/bbf88e191e9d1f01ab2e2bbb85f9b528 to your computer and use it in GitHub Desktop.

Revisions

  1. surpher revised this gist Apr 25, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,9 @@ Example of an automated script that does most of this: https://github.com/surphe

    ## Install Rust

    curl https://sh.rustup.rs -sSf | sh
    ```shell
    curl https://sh.rustup.rs -sSf | sh
    ```

    ## List available architecture targets

  2. surpher revised this gist Aug 5, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -101,6 +101,10 @@ cargo build --target=aarch64-apple-darwin --release
    cp ../target/arm64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS-arm64/lipact_mock_server.a
    ```

    ### Mac Catalyst

    https://nadim.computer/posts/2022-02-11-maccatalyst.html

    ## List architectures in .a file

    `lipo -info libpact_mock_server.a`
  3. surpher revised this gist Aug 12, 2022. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -127,6 +127,13 @@ For a fat lib, start by having `cargo` build the Rust code separately for each a
    cargo +nightly build -Z build-std --target aarch64-apple-ios-sim
    ```

    ## On Linux

    ```shell
    curll --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    apt-get install protobuf-compiler
    ```

    [https://blog.mozilla.org/data/2021/04/16/this-week-in-glean-rustc-ios-and-an-m1/](https://blog.mozilla.org/data/2021/04/16/this-week-in-glean-rustc-ios-and-an-m1/)

    ## Resources
  4. surpher revised this gist Aug 9, 2021. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -121,7 +121,11 @@ lipo -create foo-arm64.a foo-x86_64.a -output foo.a

    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## M1 stuff
    ## iOS on M1

    ```shell
    cargo +nightly build -Z build-std --target aarch64-apple-ios-sim
    ```

    [https://blog.mozilla.org/data/2021/04/16/this-week-in-glean-rustc-ios-and-an-m1/](https://blog.mozilla.org/data/2021/04/16/this-week-in-glean-rustc-ios-and-an-m1/)

  5. surpher revised this gist Jul 20, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # Dealing with Rust to build PactSwift
    # Dealing with Rust to build PactSwiftMockServer

    Example of an automated script that does most of this: https://github.com/surpher/PactSwiftMockServer/blob/fb8148866bb05f49a0b1dcaae66c67bad1e7eca7/Support/build_rust_dependencies

    ## Install Rust

  6. surpher revised this gist Jul 20, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,9 @@ rustup target list

    ```
    rustup target add aarch64-apple-ios aarch64-apple-darwin x86_64-apple-ios x86_64-apple-darwin
    rustup component add rust-src --toolchain nightly-x86_64-apple-darwin
    # While arm64-apple-ios-sim is still in Tier 3:
    rustup component add rust-src --toolchain nightly-x86_64-apple-darwin
    ```

    ## Install other toolchains
  7. surpher revised this gist Jul 20, 2021. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,7 @@ crate-type = ["staticlib", "cdylib"]

    ## Build binaries

    ### Universal iOS
    ### Universal iOS (arm64 mobile device + x86_64 simulator)

    ```
    cargo lipo --release
    @@ -83,11 +83,18 @@ cargo +nightly build -Z build-std --target aarch64-apple-ios-sim --release
    cp ../target/arm64-apple-ios-sim/release/libpact_mock_server.a /path/to/PactSwift/Resources/arm64iOSsim/libpact_mock_server.a
    ```

    ### macOS
    ### macOS x86_64

    ```
    cargo build --targget=x86_64-apple-darwin --release
    cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS/lipact_mock_server.a
    cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS-x86_64/lipact_mock_server.a
    ```

    ### macOS arm64

    ```
    cargo build --target=aarch64-apple-darwin --release
    cp ../target/arm64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS-arm64/lipact_mock_server.a
    ```

    ## List architectures in .a file
  8. surpher revised this gist Jul 20, 2021. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,7 @@ rustup target list

    ```
    rustup target add aarch64-apple-ios aarch64-apple-darwin x86_64-apple-ios x86_64-apple-darwin
    rustup component add rust-src --toolchain nightly-x86_64-apple-darwin
    ```

    ## Install other toolchains
    @@ -68,13 +69,20 @@ cargo lipo --release
    cp ../target/universal/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a
    ```

    ### iOS Simulator
    ### iOS Simulator x86_64

    ```
    cargo build --target=x86_64-apple-ios --release
    cp ../target/x86_64-apple-ios/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a
    ```

    ### iOS Simulator arm64

    ```
    cargo +nightly build -Z build-std --target aarch64-apple-ios-sim --release
    cp ../target/arm64-apple-ios-sim/release/libpact_mock_server.a /path/to/PactSwift/Resources/arm64iOSsim/libpact_mock_server.a
    ```

    ### macOS

    ```
  9. surpher revised this gist Jul 15, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -102,6 +102,10 @@ lipo -create foo-arm64.a foo-x86_64.a -output foo.a

    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## M1 stuff

    [https://blog.mozilla.org/data/2021/04/16/this-week-in-glean-rustc-ios-and-an-m1/](https://blog.mozilla.org/data/2021/04/16/this-week-in-glean-rustc-ios-and-an-m1/)

    ## Resources
    - https://nachbaur.com/2019/03/22/working-with-multiple-architectures/
    - https://doc.rust-lang.org/edition-guide/rust-2018/rustup-for-managing-rust-versions.html
  10. surpher revised this gist May 17, 2021. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Dealing with Rust to build PactSwift

    ## Install Rust

    curl https://sh.rustup.rs -sSf | sh
    @@ -53,6 +55,10 @@ name = "pact_mock_server"
    crate-type = ["staticlib", "cdylib"]
    ```

    ## Cross compiling

    [https://github.com/japaric/rust-cross](https://github.com/japaric/rust-cross)

    ## Build binaries

    ### Universal iOS
  11. surpher revised this gist Dec 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ rustup target list
    ## Add arch targets

    ```
    rustup target add aarch64-apple-ios x86_64-apple-ios x86_64-apple-darwin
    rustup target add aarch64-apple-ios aarch64-apple-darwin x86_64-apple-ios x86_64-apple-darwin
    ```

    ## Install other toolchains
  12. surpher revised this gist Nov 25, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -97,6 +97,7 @@ lipo -create foo-arm64.a foo-x86_64.a -output foo.a
    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## Resources
    - https://nachbaur.com/2019/03/22/working-with-multiple-architectures/
    - https://doc.rust-lang.org/edition-guide/rust-2018/rustup-for-managing-rust-versions.html
    - https://github.com/guedou/cargo-strip
    - https://github.com/johnthagen/min-sized-rust (Minimize size of rust lib)
  13. surpher revised this gist Oct 17, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,12 @@

    curl https://sh.rustup.rs -sSf | sh

    ## List available architecture targets

    ```
    rustup target list
    ```

    ## Add arch targets

    ```
  14. surpher revised this gist Sep 20, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -91,6 +91,8 @@ lipo -create foo-arm64.a foo-x86_64.a -output foo.a
    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## Resources
    - https://doc.rust-lang.org/edition-guide/rust-2018/rustup-for-managing-rust-versions.html
    - https://github.com/guedou/cargo-strip
    - https://github.com/johnthagen/min-sized-rust (Minimize size of rust lib)
    - https://users.rust-lang.org/t/unable-to-build-a-dynamic-library-dylib-for-ios/24918/4 (building and using dynamic libs for development purposes - would be it enough for PactSwift? It shouldn't be embedded in the release app anyway!)
    - https://visly.app/blog/rust-on-ios
  15. surpher revised this gist Sep 20, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,12 @@
    rustup target add aarch64-apple-ios x86_64-apple-ios x86_64-apple-darwin
    ```

    ## Install other toolchains

    `rustup toolchain install nightly`

    more: https://doc.rust-lang.org/edition-guide/rust-2018/rustup-for-managing-rust-versions.html

    ## Install more tools to help with the build

    `cargo-lipo` - a tool for building universal iOS binaries,
    @@ -85,6 +91,7 @@ lipo -create foo-arm64.a foo-x86_64.a -output foo.a
    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## Resources
    - https://github.com/johnthagen/min-sized-rust (Minimize size of rust lib)
    - https://users.rust-lang.org/t/unable-to-build-a-dynamic-library-dylib-for-ios/24918/4 (building and using dynamic libs for development purposes - would be it enough for PactSwift? It shouldn't be embedded in the release app anyway!)
    - https://visly.app/blog/rust-on-ios
    - https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2
  16. surpher revised this gist Sep 10, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -85,6 +85,7 @@ lipo -create foo-arm64.a foo-x86_64.a -output foo.a
    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## Resources
    - https://users.rust-lang.org/t/unable-to-build-a-dynamic-library-dylib-for-ios/24918/4 (building and using dynamic libs for development purposes - would be it enough for PactSwift? It shouldn't be embedded in the release app anyway!)
    - https://visly.app/blog/rust-on-ios
    - https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2
    - https://www.reddit.com/r/rust/comments/aae6x8/rust_ios_and_linking_against_fat_native_libraries/
  17. surpher revised this gist Sep 2, 2020. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -68,6 +68,23 @@ cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwif

    `lipo -info libpact_mock_server.a`

    ## Extract one of the architectures from a fat library using the lipo command:

    ```
    lipo libfoo.a -thin arm64 -output libfoo-arm64.a
    ```

    Use `-thin` and not `-extract` the latter, still outputs a fat file, one which happens to only contain a single architecture.

    ## Combine multiple architectures into one fat library

    ```
    lipo -create foo-arm64.a foo-x86_64.a -output foo.a
    ```

    For a fat lib, start by having `cargo` build the Rust code separately for each architecture. use `lipo` to combine the outputs into a fat binary.

    ## Resources
    - https://visly.app/blog/rust-on-ios
    - https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2
    - https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2
    - https://www.reddit.com/r/rust/comments/aae6x8/rust_ios_and_linking_against_fat_native_libraries/
  18. surpher revised this gist Sep 2, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -64,6 +64,10 @@ cargo build --targget=x86_64-apple-darwin --release
    cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS/lipact_mock_server.a
    ```

    ## List architectures in .a file

    `lipo -info libpact_mock_server.a`

    ## Resources
    - https://visly.app/blog/rust-on-ios
    - https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2
  19. surpher revised this gist Sep 2, 2020. 1 changed file with 36 additions and 21 deletions.
    57 changes: 36 additions & 21 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -4,50 +4,65 @@

    ## Add arch targets

    rustup target add aarch64-apple-ios x86_64-apple-ios x86_64-apple-darwin
    ```
    rustup target add aarch64-apple-ios x86_64-apple-ios x86_64-apple-darwin
    ```

    ## Install more tools to help with the build

    `cargo-lipo` - a tool for building universal iOS binaries,
    `cbindgen` - a tool for generating C headers from rust

    cargo install cargo-lipo
    cargo install cbindgen
    ```
    cargo install cargo-lipo
    cargo install cbindgen
    ```

    ## Generate the header file

    cd pact-reference/rust/pact_mock_server_ffi
    cbindgen src/lib.rs -l c > pact_mock_server.h
    cp pact_mock_server.h ../PactSwift/Sources/Headers/pact_mock_server.h
    ```
    cd pact-reference/rust/pact_mock_server_ffi
    cbindgen src/lib.rs -l c > pact_mock_server.h
    cp pact_mock_server.h ../PactSwift/Sources/Headers/pact_mock_server.h
    ```

    ## Define the type of binary

    cd pact-reference/rust/pact_mock_server_ffi
    nano Cargo.toml
    ```
    cd pact-reference/rust/pact_mock_server_ffi
    nano Cargo.toml
    ```

    Set the following:

    [lib]
    name = "pact_mock_server"
    crate-type = ["staticlib", "cdylib"]
    ```
    [lib]
    name = "pact_mock_server"
    crate-type = ["staticlib", "cdylib"]
    ```

    ## Build binaries

    ### Universal iOS

    cargo lipo --release
    cp ../target/universal/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a

    ```
    cargo lipo --release
    cp ../target/universal/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a
    ```

    ### iOS Simulator

    cargo build --target=x86_64-apple-ios --release
    cp ../target/x86_64-apple-ios/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a

    ```
    cargo build --target=x86_64-apple-ios --release
    cp ../target/x86_64-apple-ios/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a
    ```

    ### macOS

    cargo build --targget=x86_64-apple-darwin --release
    cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS/lipact_mock_server.a


    ```
    cargo build --targget=x86_64-apple-darwin --release
    cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS/lipact_mock_server.a
    ```

    ## Resources
    - https://visly.app/blog/rust-on-ios
  20. surpher created this gist Sep 2, 2020.
    54 changes: 54 additions & 0 deletions rust_to_swift.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    ## Install Rust

    curl https://sh.rustup.rs -sSf | sh

    ## Add arch targets

    rustup target add aarch64-apple-ios x86_64-apple-ios x86_64-apple-darwin

    ## Install more tools to help with the build

    `cargo-lipo` - a tool for building universal iOS binaries,
    `cbindgen` - a tool for generating C headers from rust

    cargo install cargo-lipo
    cargo install cbindgen

    ## Generate the header file

    cd pact-reference/rust/pact_mock_server_ffi
    cbindgen src/lib.rs -l c > pact_mock_server.h
    cp pact_mock_server.h ../PactSwift/Sources/Headers/pact_mock_server.h

    ## Define the type of binary

    cd pact-reference/rust/pact_mock_server_ffi
    nano Cargo.toml

    Set the following:

    [lib]
    name = "pact_mock_server"
    crate-type = ["staticlib", "cdylib"]

    ## Build binaries

    ### Universal iOS

    cargo lipo --release
    cp ../target/universal/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a

    ### iOS Simulator

    cargo build --target=x86_64-apple-ios --release
    cp ../target/x86_64-apple-ios/release/libpact_mock_server.a /path/to/PactSwift/Resources/iOS/lipact_mock_server.a

    ### macOS

    cargo build --targget=x86_64-apple-darwin --release
    cp ../target/x86_64-apple-darwin/release/libpact_mock_server.a /path/to/PactSwift/Resources/macOS/lipact_mock_server.a


    ## Resources
    - https://visly.app/blog/rust-on-ios
    - https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2