Skip to content

Building library for iOS

Ivan Schütz edited this page May 14, 2020 · 21 revisions

Generate library (.a file):

  • Install rustup
  • Add targets: device: rustup target add aarch64-apple-ios, simulator: rustup target add x86_64-apple-ios,

The next 2 sections are here only for context. Skip to "Universal (that works)" if you just want to build quickly.

Update: Now you can execute the create_ios_universal.sh script in ./scripts/ios/ to generate the universal library (that works).

Universal

This generates a library with all supported architectures.

  • Run in the root directory of this repo: cargo lipo --release
  • You will find the generated library in ./target/universal/release/

If you try to run it on a device, you will likely get an error:

“libtcn_client.a does not contain bitcode”

Which leads to the next section.

Device

Building for a real device is a bit more complicated, because Xcode uses often a different LLVM version than Rust. The rust-bitcode library helps us with this.

After you've installed this library, in this repo's root folder, execute:

RUSTFLAGS="-Z embed-bitcode" cargo +ios-arm64 build --target aarch64-apple-ios --release --lib

To generate the library for the device. It will be in ./target/aarch64-apple-ios/release/

But now we've 2 separate libraries for the simulator and the device. We need only one.

Universal (that works)

From the previous sections, it follows that we need to generate the libraries for the simulator and the device separately and merge them in one single library.

So:

  • Generate lib for device, as described in "Device"
  • Generate lib only for the simulator, with: cargo build --target=x86_64-apple-ios

Now, from the root directory of this repo, we will call libtool to assemble these 2 libraries into one:

libtool -static -o libtcn_client.a ./target/aarch64-apple-ios/release/libtcn_client.a ./target/x86_64-apple-ios/release/libtcn_client.a

The generated libtcn_client.a will be where you indicate, in this case, in the same directory where libtool was executed.

To use library in the iOS app:

  • Copy libtcn_client.a file into the iOS app's root dir.

To change the interface:

  • Aside from updating the library (.a file), update the headers in mobileapp-ios.h (which is in the iOS app).
Clone this wiki locally