NordSecurity / uniffi-bindgen-go

Uniffi bindings generator for Golang
Mozilla Public License 2.0
75 stars 21 forks source link

How can I generate bindings from proc_macros? #49

Open pacu opened 4 months ago

pacu commented 4 months ago

I'm trying to generate go bindings for a project I'm working on, but I can't seem to be able to do so from proc_macros

According to help this would be possible

uniffi-bindgen 0.2.1+v0.25.0

USAGE:
    uniffi-bindgen-go [OPTIONS] <SOURCE>

ARGS:
    <SOURCE>    Path to the UDL file, or cdylib if `library-mode` is specified

OPTIONS:
    -c, --config <CONFIG>        Path to optional uniffi config file. This config will be merged
                                 on top of default `uniffi.toml` config in crate root. The merge
                                 recursively upserts TOML keys into the default config
        --crate <CRATE_NAME>     When `--library` is passed, only generate bindings for one crate.
                                 When `--library` is not passed, use this as the crate name instead
                                 of attempting to locate and parse Cargo.toml
    -h, --help                   Print help information
    -l, --lib-file <LIB_FILE>    Extract proc-macro metadata from a native lib (cdylib or staticlib)
                                 for this crate
        --library                Pass in a cdylib path rather than a UDL file
    -n, --no-format              Do not try to format the generated bindings
    -o, --out-dir <OUT_DIR>      Directory in which to write generated files. Default is same folder
                                 as .udl file
    -V, --version                Print version information

but when I try something like

uniffi-bindgen-go -l 'frost-uniffi-sdk/target/universal-ios/release/libfrost_uniffi_sdk.a' frost-uniffi-sdk

fails with Error: UDL file has no grand-parent folder!

how can I achieve it?

Context: this is the project I'm working on https://github.com/pacu/frost-uniffi-sdk

arg0d commented 4 months ago

I'm guessing you want to generate bindings straight from the library, without touching UDL files. In that case, you should use:

uniffi-bindgen-go --library frost-uniffi-sdk/target/universal-ios/release/libfrost_uniffi_sdk.a

The --lib-file API you are trying to use expects you to give a path UDL file, though the error is a bit cryptic :( I'm guessing your project doesn't even have a UDL file.

uniffi-bindgen-go --lib-file path/to/library.a path/to/library.udl
pacu commented 4 months ago

Thank you for your quick response @arg0d.

I'm guessing you want to generate bindings straight from the library, without touching UDL files. In that case, you should use:

Yes I'm actually generating the bindings for swift using proc macros like the docs say so https://mozilla.github.io/uniffi-rs/latest/proc_macro/index.html here using cargo swift see project

I want to achieve something similar for GoLang but I can't seem to be able to figure it out. I'm trying what you suggested

uniffi-bindgen-go --library 'frost-uniffi-sdk/target/universal-ios/
release/libfrost_uniffi_sdk.a' --out-dir .
Error: Failed to extract data from archive member `frost_uniffi_sdk-9ff18b138f383e0a.frost_uniffi_sdk.a7d54b623a26d530-cgu.0.rcgu.o`

Caused by:
    Unknown library format

I guess I could create a UDL file but I'd fancy not to.

pacu commented 4 months ago

Update:

I was able to generate the bindings from a cdylib instead.

I had to add it to my [lib] section of the Cargo.toml

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

then the command uniffi-bindgen-go --library 'frost-uniffi-sdk/target/debug/libfrost_uniffi_sdk.dylib' --out-dir . worked.

arg0d commented 4 months ago

--library doesn't work with a static library?

pacu commented 3 months ago

--library doesn't work with a static library?

Apparently not