How to create a Rust dynamic Library (dylib) for iOS?

Viewed 1339

The rust toolchain currently does not allow you to create a dynamic library crate type (cdylib or dylib) for the target aarch64-apple-ios.

The reasoning I've seen on the Internet is that Apple doesn't allow dynamic libraries when uploading apps to the App Store.

However, my reading of TN2425 is that dynamic libraries are allowed, as long as they are embedded in the application as a framework. The technical note describes how to create a framework from a dynamic library.

My use case is that I am building a Rust library that makes numerous calls to the Objc system libraries provided by Apple. If I create a static library, Cargo/Rust will link in all the SDK and give me a very large static library that is tied to a specific SDK release.

Is there a way I can manually create a dynamic library from the intermediate files generated from the Rust toolchain? I could then sign it and embed it in my iOS app.

2 Answers

Yes, according to Rust Issue #73516, dynamic linking for iOS is already provided in the nightly toolchain for aarch64-apple-ios.

However, it won't do you much good. The extra size in the static library is from Rust runtimes, which you will need. You can look at what embedded Rust and Rust WASM coders do to reduce the size of the runtime portion.

For iOS you only need to make a static library (Apple does not allow 3rd party dynamic libraries on iOS), so fortunately you don't need to have any of Apple's proprietary frameworks for linking.

If you don't have any dependencies on C libraries it should be easier, because Rust itself knows how to cross-compile.

Related