-
Notifications
You must be signed in to change notification settings - Fork 747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are there plans for Rust bindings? #6788
Comments
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 |
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. |
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.rsuse 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#[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. |
Thanks @romdotdog I will give it a try |
@romdotdog @kripken 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:
I am not expert in this FFI Rust stuff. Can you help me on this? |
Your code works for me. Make sure you're using clang. |
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?
The text was updated successfully, but these errors were encountered: