WebAssembly / binaryen

Optimizer and compiler/toolchain library for WebAssembly
Apache License 2.0
7.49k stars 741 forks source link

Are there plans for Rust bindings? #6788

Open clacladev opened 3 months ago

clacladev commented 3 months ago

I am not qualified to write rust bindings for the C apis. But I want to write my language AST to WebAssembly and Binaryen seemed perfect for it. There is the binaryen-rs v0.6 project which had bindings, but it's 6 years old. Since then, the author removed them and focused on the optimisation parts of binaryen.

Is there any plan to write them?

kripken commented 3 months ago

If you want the Binaryen C API specifically then rust-bindgen should be able to automatically generate them, I think.

See also https://github.com/brson/wasm-opt-rs , though I think that might be higher-level bindings for the wasm-opt tool, not the IR. But if rust-bindgen is not an option for some reason then you might consider using a Rust library to generate wasm and then use wasm-opt-rs to optimize that wasm. (A downside to doing it that way is you won't get the full Binaryen C API which provides utilities like generating structured control flow from basic blocks, and other things not in wasm itself.)

clacladev commented 3 months ago

Thanks @kripken for the rust-bindgen tip. I am new to WASM and still fresh with Rust and writing programming languages, so I would have liked something done by someone with more experience. But I guess if no-one is going to do it, I am going to give it a go.

romdotdog commented 3 months ago

I have an old private repository with a binaryen.js port to Rust, using a lot of code from binaryen-rs. I'm not intent on publishing it, but I can show you a few useful files:

build.rs ```rs use std::env; fn main() { let target = env::var("TARGET").unwrap(); let mut config = cmake::Config::new("binaryen"); config .define("ARCH", "native") .define("BUILD_STATIC_LIB", "ON") .define("ENABLE_WERROR", "OFF"); if target.contains("windows") { config.build_target("ALL_BUILD") } else { config.build_target("all") }; let dst = config.build(); println!("cargo:rustc-link-search=native={}/build/lib", dst.display()); println!("cargo:rustc-link-lib=static=binaryen"); let lib = if target.contains("msvc") { None } else if target.contains("darwin") { Some("c++".to_string()) } else if target.contains("freebsd") { Some("c++".to_string()) } else if target.contains("musl") { Some("static=stdc++".to_string()) } else { Some("stdc++".to_string()) }; if let Some(lib) = lib { println!("cargo:rustc-link-lib={}", lib); } } ```
bindgen.rs ```rs #[cfg(feature = "bindgen")] fn main() { use std::path::PathBuf; let bindings = bindgen::Builder::default() .header("binaryen/src/binaryen-c.h") .parse_callbacks(Box::new(bindgen::CargoCallbacks)) .generate() .expect("Unable to generate bindings"); let out_path = PathBuf::from(FILE_PATH); bindings .write_to_file(out_path) .expect("Couldn't write bindings!"); } ```

Not sure how well these work now, but they could be useful to you.

clacladev commented 3 months ago

Thanks @romdotdog I will give it a try

clacladev commented 2 months ago

@romdotdog @kripken I am trying to create a crate with the binaryen rust bindings. Here a wip https://github.com/clacladev/binaryen-rust

The problem as soon as I try to use the binded types, as when uncommenting the test in https://github.com/clacladev/binaryen-rust/blob/dev/src/lib.rs it crashes with:

Compiling binaryen-rust v0.1.0 (/Users/claudio/Dev/webby/binaryen-rust)
error: linking with `cc` failed: exit status: 1
  |
  = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET -u XROS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/Users/claudio/.wasmtime/bin:/Users/claudio/.wasmer/bin:/Users/claudio/.bun/bin:/Users/claudio/.rbenv/shims:/Users/claudio/.nvm/versions/node/v20.16.0/bin:/Users/claudio/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/MacGPG2/bin:/Users/claudio/.wasmtime/bin:/Users/claudio/.wasmer/bin:/Users/claudio/.bun/bin:/Users/claudio/.rbenv/shims:/Users/claudio/.nvm/versions/node/v20.16.0/bin:/Users/claudio/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/claudio/Library/pnpm:/Users/claudio/.miniconda3/bin:/Users/claudio/.miniconda3/condabin:/Users/claudio/.starkli/bin:/Users/claudio/.cargo/bin:/Users/claudio/.foundry/bin:/Users/claudio/Library/Android/sdk/emulator:/Users/claudio/Library/Android/sdk/platform-tools:/Users/claudio/.local/bin:/Users/claudio/.foundry/bin:/Users/claudio/Library/Android/sdk/emulator:/Users/claudio/Library/Android/sdk/platform-tools:/Users/claudio/.local/bin" VSLANG="1033" ZERO_AR_DATE="1" "cc" "-arch" "arm64" "/var/folders/k3/2cm7mqwd3nqffkdmf0_36vdh0000gn/T/rustcKMN7Fl/symbols.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.189pok23frc9ep7qqv8r35d8k.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.1fpjbs9x6py06b78pelseik5e.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.1mk3px3y3vrq0hjnjxe6dl04c.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.1pe7waywdnz3aeaf45jqw6sj3.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.3xg6k53az00rd4ywft37o6lce.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.4m6uszeshiemas8fwtfphdqhk.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5248wdh08bf14edqzkafei1cn.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5pteqznd4cw9d1y0myi0dwwhf.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5xoq1v7ptdeacva5lo21rwpm9.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.6djx01iq8a43638bch0jcyijd.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.6jcbski7u9toefgf42c85msbo.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.6wlt211ei5jp3u9e53iv49axf.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.761get07q680x978f1f9sm4qg.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.8rd5mmdnag21rwiwii2vc7e51.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.8v5gokn527pynlc2cdb1ybkzz.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.anjzalk10yfkbb11qsqf2jo6b.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.b3el27uaovlhyyig0ff63bmcs.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.bk2e9gpsh8xwkw8z12b0ll19o.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.cbcn0w4i6shqf5foslxg638ii.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.cfccsu9jjdosinb08qi47qeyd.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.d8cwjb41z73a3wai2oqonzl4j.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.dy194cy38y65ac5sd8qg8jhfp.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.e37wenafym10yvbllvtiu8x25.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.f32n0019ti2eeccb9bt4kupte.rcgu.o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0.5b6w8ognp6zf2r66ubipa0uv5.rcgu.o" "-L" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps" "-L" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libtest-edf45beacd3678bf.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgetopts-71c4eb6d673d56ea.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunicode_width-e28435f78d418cf7.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_std-f3e6f7c04a993e06.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-2cd4f83c0684cf3b.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libpanic_unwind-ae5c5b32e17a4253.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libobject-0a06236df3debd3d.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libmemchr-eb79452a16ef20ff.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libaddr2line-e5400cafb1ff9160.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgimli-cd7b8be743512bf0.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_demangle-13002c379b6a4f94.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd_detect-66b97aed86b38916.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-e526da30a7a44502.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_alloc-8a46d2981213a851.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libminiz_oxide-2f857721294fc67d.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libadler-7bb2d22a2a2c450a.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunwind-96650a40faca9ad5.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcfg_if-10cbd942774a0e4f.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liblibc-542fd8c92681fb67.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liballoc-5487ab1633e6869d.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_core-7be3f18be52763ef.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcore-e0586f91b4987ecb.rlib" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-3f821062253d71e0.rlib" "-lSystem" "-lc" "-lm" "-L" "/Users/claudio/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "-o" "/Users/claudio/Dev/webby/binaryen-rust/target/debug/deps/binaryen_rust-ecd467d9667f97b0" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: Undefined symbols for architecture arm64:
            "_BinaryenAddFunction", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenAddInt32", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenBinary", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenLocalGet", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModuleCreate", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModuleDispose", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModulePrint", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenModulePrintStackIR", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenTypeCreate", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
            "_BinaryenTypeInt32", referenced from:
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
                binaryen_rust::tests::test::h5e95c969f5c774d5 in binaryen_rust-ecd467d9667f97b0.4bpbiqmljpsfg7zf3b7ny48gj.rcgu.o
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: could not compile `binaryen-rust` (lib test) due to 1 previous error

I am not expert in this FFI Rust stuff. Can you help me on this?

romdotdog commented 2 months ago

Your code works for me. Make sure you're using clang.