brson / wasm-opt-rs

Rust bindings for Binaryen's wasm-opt
Apache License 2.0
61 stars 10 forks source link

Getting the `wasm-opt` version programatically #106

Open HCastano opened 1 year ago

HCastano commented 1 year ago

Hey! Thanks for these bindings 😄

I want to be able to get programmatically the version of the underlying version of wasm-opt which was built by the library. So something like wasm_opt::VERSION or wasm_opt::version(). From what I can tell the only way to do that right now is to either parse the crate version number, or use a locally installed wasm-opt binary.

I'd be happy to implement this myself if you point me to the right area of the codebase.

brson commented 1 year ago

@HCastano I am happy to include this feature, though I am interested to know your use case if you can tell me what you will be doing with the version number.

The binaryen version can take two different forms, depending on whether it is being built from git or from a redistributable package. Our build script attempts to reproduce the same logic for extracting the version number as binaryen's own build system.

Here is the build script code for finding the version: https://github.com/brson/wasm-opt-rs/blob/edcdb739fe954d4954dc67e763dbc4d1bbd49231/components/wasm-opt-sys/build.rs#L332-L369

That version is written as a string to the PROJECT_VERSION define in config.h.

Exposing this to the Rust API should entail:

I'd like to call the function binaryen_version to distinguish it from the crate version.

It might be possible to arrange for the function signature to be fn() -> &'static str, but I don't know offhand how to do it. It would probably involve defining a C++ static to hold the value, and carefully defining all the FFI functions. I am fine with either String or &'static str.

brson commented 1 year ago

It would also be possible to expose this as a static value BINARYEN_VERSION, though it would require emitting some Rust code from the wasm-opt-sys build script (probably into OUT_DIR/binaryen_version.rs or something), including it in the wasm-opt-sys crate, possibly with include!, and then reexporting it from wasm-opt.

This method might actually require less code.

I am ok with either way.