#Rust UniFFI for some reason iOS is trying to find a dylib

15 messages · Page 1 of 1 (latest)

river wharf
#

I'm using UniFFI to write part of my iOS app's backend. My cargo.toml is as follows:

[package]
name = "shared"

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

[[bin]]
name = "uniffi-bindgen"
path = "uniffi-bindgen.rs"

[dependencies]
# ...
uniffi = { version = "0.25.3", features = [ "cli" ] }

[build-dependencies]
uniffi = { version = "0.25.3", features = [ "build" ] }

My build process is as follows

$ $HOME/.cargo/bin/cargo rustc -p shared --lib --crate-type staticlib --target aarch64-apple-ios --release
$ cargo run --bin uniffi-bindgen generate --library target/aarch64-apple-ios/release/libshared.a --language swift --out-dir out

The build goes well. I have added libshared.a to my Swift project, and it looks like things are just golden. When I launch the app, however, I get

dyld[48396]: Library not loaded: /Users/evan/Code/lym/shared/target/aarch64-apple-ios/release/deps/libshared.dylib
  Referenced from: <13D3FB17-599F-37F1-9485-7723FD3232DF> /private/var/containers/Bundle/Application/A0E4BE0B-EE07-4C0D-A77A-7F13ED4BA8CE/lym.app/lym
  Reason: tried: '/Users/evan/Code/lym/shared/target/aarch64-apple-ios/release/deps/libshared.dylib' (no such file), '/private/preboot/Cryptexes/OS/Users/evan/Code/lym/shared/target/aarch64-apple-ios/release/deps/libshared.dylib' (no such file), '/Users/evan/Code/lym/shared/target/aarch64-apple-ios/release/deps/libshared.dylib' (no such file)

Which is odd because it should be a static library.

#

It's worth noting that I'm in over my head by a mile here, and I fear this is too niche of an issue to get much help but it's worth a shot

remote ledge
#

How are you linking the library?

river wharf
#

Hi sorry I got caught up in work. Anyway I got it figured out for anybody who might be hitting this, the correct answer is to make a framework

#
cargo build --target aarch64-apple-ios --release
cargo run --bin uniffi-bindgen generate --library target/aarch64-apple-ios/release/libshared.a --language swift --out-dir out

mkdir -p "${NEW_HEADER_DIR}"
cp "${HEADERPATH}" "${NEW_HEADER_DIR}/"
cp "out/sharedFFI.modulemap" "${NEW_HEADER_DIR}/module.modulemap"

rm -rf "${OUTDIR}/${NAME}_framework.xcframework"

xcodebuild -create-xcframework \
    -library "${TARGETDIR}/aarch64-apple-ios/${RELDIR}/${STATIC_LIB_NAME}" \
    -headers "${NEW_HEADER_DIR}" \
    -output "${OUTDIR}/${NAME}_framework.xcframework"

rm -rf "${NEW_HEADER_DIR}"
elfin quest
#

Omg, I don't know how to thank you. I was about to go crazy! Just renaming to module.modulemap made it work 🎊

My build-ios.sh script:

#!/bin/bash

# Add the ios targets and build
for TARGET in \
        aarch64-apple-darwin \
        aarch64-apple-ios \
        aarch64-apple-ios-sim \
        x86_64-apple-darwin \
        x86_64-apple-ios
do
    rustup target add $TARGET
    # Apple's App Sandbox disallows SysV semaphores; use POSIX semaphores instead
    cargo build --release --target=$TARGET
done

# Create bindings
cargo run --bin uniffi-bindgen generate --library ../../target/debug/libmobile.dylib --language swift --out-dir ./bindings

# Rename *.modulemap to module.modulemap
mv ./bindings/mobileFFI.modulemap ./bindings/module.modulemap

# Move the Swift file to the project
rm ./ios/TodoList/Mobile.swift
mv ./bindings/mobile.swift ./ios/TodoList/Mobile.swift

# # Recreate XCFramework
rm -rf "ios/Mobile.xcframework"
xcodebuild -create-xcframework \
        -library ../../target/aarch64-apple-ios-sim/release/libmobile.a -headers ./bindings \
        -library ../../target/aarch64-apple-ios/release/libmobile.a -headers ./bindings \
        -output "ios/Mobile.xcframework"

# # Cleanup
rm -rf bindings
elfin quest
river wharf
#

using a dylib is interesting. I might try this!

#

I'd be happy to link to yours if you want

elfin quest
#

Nice! I'll check yours, I wish I had found it before 🤦‍♂️

river wharf
#

maybe eventually