fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.58k stars 253 forks source link

how can i generate from rust.dylib library and use it in flutter #1899

Closed cryuhy closed 1 week ago

cryuhy commented 1 week ago

i have a framework RustFramework.xcframework, in ios project,and i want generate some glue code use dart to call rust function in RustFramework,have any method to do this?

welcome[bot] commented 1 week ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 1 week ago

flutter_rust_bridge allows your code to be in whatever folder and compiled by whatever way, thus I guess just modify flutter_rust_bridge.yaml config to point to correct folders.

cryuhy commented 1 week ago

flutter_rust_bridge allows your code to be in whatever folder and compiled by whatever way, thus I guess just modify flutter_rust_bridge.yaml config to point to correct folders.

but dart glue generate by flutter_rust_bridge,must need rust source code in rust/src/api/sample.rs ,and sample.rs also compiled in flutter project,but sample.rs already complied in RustFramework.xcframework,so in dont konw how to ignore rust/src/api/sample.rs to be complied

fzyzcjy commented 1 week ago

sample.rs also compiled in flutter project

It seems to be the flutter_rust_bridge_codegen integrate that generates cargokit boilerplate for you, and cargokit does the compilation. In your scenario, I guess you do not need the cargokit boilerplate (since you compile by your own infra), then just do not add it. flutter_rust_bridge will merely generate some .dart and .rs file for you, and will do nothing else.

cryuhy commented 1 week ago

awesome, it works !,love you,but im facing another question, i see user guide await RustLib.init(externalLibrary: loadMyRustLibraryUsingWhateverMethod()); but how can is assign my framework, full path? ,or name ,my framework is must dynamic? i found my frameworkd is static lib

fzyzcjy commented 1 week ago

Happy to see it works! Just use whatever approach to load your external library. The default loading function https://github.com/fzyzcjy/flutter_rust_bridge/blob/master/frb_dart/lib/src/loader/_io.dart may be a good starting point

cryuhy commented 1 week ago
RedMessageReply  reply = RedMessageReply.fromJson(map);
var mainBundlePath = reply.params?['path'];
ExternalLibrary externalLibrary = ExternalLibrary.open(mainBundlePath, debugInfo: 'im');
await RustLib.init(externalLibrary: externalLibrary);

i got correct rust.dylib path /private/var/containers/Bundle/Application/34396BEE-481A-40AA-8BED-76B59CD8D55E/Runner.app/Frameworks/libsdk.dylib but i got error in this line await RustLib.init(externalLibrary: externalLibrary); error is Invalid argument(s): Invalid argument(s): Failed to lookup symbol 'frb_get_rust_content_hash': dlsym(0xfbb9ad90, frb_get_rust_content_hash): symbol not found how can i fix this error

fzyzcjy commented 1 week ago

It would be great to provide a minimal reproducible sample to debug. Without that, some thoughts: maybe firstly check whether your dylib really has those symbols (by nm/objdump etc)

cryuhy commented 1 week ago

i use nm cmd did not see frb_get_rust_content_hash, frb_get_rust_content_hash symbol look like is contain by flutter_rust_bridge sdk? how can i container this symbol in my dylib, in my rust project i add sdk in my cargo file ![Uploading github.png…]()

cryuhy commented 1 week ago

can you give me email my simple demo to debug has 789mb it is too large.......

cryuhy commented 1 week ago

i call dylib method success, but run flutter_rust_bridge_codgen like this code

#[flutter_rust_bridge::frb(sync)] // Synchronous mode for simplicity of the demo
#[uniffi::export]
pub fn greet(name: String) -> String {
    format!("Hello, {name}!")
}

#[flutter_rust_bridge::frb(init)]
#[uniffi::export]
pub fn init_app() {
    // Default utilities - feel free to customize
    flutter_rust_bridge::setup_default_user_utils();
}

raise error

.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/utils/logs.rs:52] panicked at /Users/chenrui/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/codegen/parser/type_parser/ty.rs:12:61:
called `Result::unwrap()` on an `Err` value: qself "<String>" in "< String as :: uniffi :: Lift < crate :: UniFfiTag > > :: FfiType", and all qself syntax, is unsupported
thread 'main' panicked at /Users/chenrui/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/codegen/parser/type_parser/ty.rs:12:61:
called `Result::unwrap()` on an `Err` value: qself "<String>" in "< String as :: uniffi :: Lift < crate :: UniFfiTag > > :: FfiType", and all qself syntax, is unsupported
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace how can i fixed it
fzyzcjy commented 1 week ago

can you give me email my simple demo to debug has 789mb it is too large.......

Usually the source code itself is enough (no need to upload compiled binaries etc)

i call dylib method success, but run flutter_rust_bridge_codgen like this code

I guess uniffi::export will create something fancy and thus flutter_rust_bridge gets confused. Is there any other ways to use uniffi without macro expansion? Btw, curious why you are using uniffi + flutter_rust_bridge

cryuhy commented 1 week ago

can you give me email my simple demo to debug has 789mb it is too large.......

Usually the source code itself is enough (no need to upload compiled binaries etc)

i call dylib method success, but run flutter_rust_bridge_codgen like this code

I guess uniffi::export will create something fancy and thus flutter_rust_bridge gets confused. Is there any other ways to use uniffi without macro expansion? Btw, curious why you are using uniffi + flutter_rust_bridge

becuase my rust is compiled by cargo swift cmd ,my dylib need both call by flutter and swift directly,if i dont use uniffi::export,dylib can not find symbol such as frb_get_rust_content_hash, but when i use uniffi::export dylib will contain this symbol for example uniffi::export pub fn greet(name: String) -> String { format!("Hello, {name}!") } use outool -tV /my.dylib will see "greet" symbol but without uniffi::export source code like this pub fn greet(name: String) -> String { format!("Hello, {name}!") } use outool -tV /my.dylib did not see "greet" symbol so first source code complied dylib can correct called by flutter second source code complied dylib will crash error is " Failed to lookup symbol "

fzyzcjy commented 1 week ago

my dylib need both call by flutter and swift directly

I see, then this looks pretty reasonable. Then could you please check the expanded uniffi macro to see what is generated? Maybe we can workaround it.

Alternatively, https://mozilla.github.io/uniffi-rs/udl_file_spec.html looks also a possible approach to avoid using uniffi::export

if i dont use uniffi::export,dylib can not find symbol such as frb_get_rust_content_hash

For flutter_rust_bridge, it should find the symbol IMHO, if not it is a bug.

Btw, it would be great to format the code into code blocks for easier reading.

cryuhy commented 1 week ago

my dylib need both call by flutter and swift directly

I see, then this looks pretty reasonable. Then could you please check the expanded uniffi macro to see what is generated? Maybe we can workaround it.

Alternatively, https://mozilla.github.io/uniffi-rs/udl_file_spec.html looks also a possible approach to avoid using uniffi::export

if i dont use uniffi::export,dylib can not find symbol such as frb_get_rust_content_hash

For flutter_rust_bridge, it should find the symbol IMHO, if not it is a bug.

Btw, it would be great to format the code into code blocks for easier reading.

yes use udl may fix this error,but i use anhoter way i write shell to delete uniffi::export when flutter_rust_bridge finished then shell will auto add uniffi::export that works