h3ron icon indicating copy to clipboard operation
h3ron copied to clipboard

h3-sys WASM support

Open olanod opened this issue 2 years ago • 6 comments

It would be neat to add WASM support which I imagine can be done adding some build task in h3-sys that uses emscripten to generate the lib object that is later statically linked.

olanod avatar Sep 15 '21 07:09 olanod

That would indeed be a nice feature to have. H3 uses cmake as it buildsystem, h3-sys is using the cmake-crate to build h3. Looking at the current state in cmake it seems cmake sadly still lacks WASM support. So it seems to cross-compile for WASM, the build needs to be done manually using emscripten.

Another aspect I am not sure how it can be done is the usage of the rayon thread pools in WASM.

As WASM this is not my current usecase, I am not sure how much time I can invest in this issue, but it is definitively a good effort. Maybe something from https://github.com/uber/h3-js can be re-used, at least emscripten is used there to compile to js.

nmandery avatar Sep 15 '21 08:09 nmandery

From h3-js we can get inspiration on how to compile with emscripten, they don't use cmake but rather compile with emcc directly. For rayon I see its already an optional dependency, I guess WASM compilation wouldn't activate the rayon feature?

olanod avatar Sep 15 '21 08:09 olanod

The use-rayon feature is optional for the h3ron crate, yes. For the h3ron-ndarry and h3ron-graph it is required, though. I suppose it would not be to bad when these two crates would not be available for WASM.

Thanks for locating the call to emscripten, I guess using something like that should work. It would even open the opportunity to loose the cmake dependency altogether and compile directly with the systems c-compiler for the non-WASM targets using cc. I only have worked briefly with wasm_bindgen and friends in the past, so I am not so deep into the topic.

nmandery avatar Sep 15 '21 09:09 nmandery

Seems the cc crate automatically compiles with emscripten when the target is emscripten. So we should definitively use cc.

https://github.com/alexcrichton/cc-rs/blob/178fd031e74f643721605e25110ee6abce8dbe05/src/lib.rs#L2140

Edit: Correction, compiling to wasm is not supported: https://github.com/alexcrichton/cc-rs/issues/580

nmandery avatar Sep 15 '21 12:09 nmandery

I did not have much time to look further into this, but I just stumbled over https://github.com/rustwasm/team/issues/291 .

nmandery avatar Sep 30 '21 07:09 nmandery

Compiling to wasm32-wasi works now - see #51. Requires the unreleased version from the main branch.

This won't help when targeting browsers, though. For that the wasm32-unknown-unknown target would be required. That one is still problematic - see https://github.com/rust-lang/cc-rs/issues/715

nmandery avatar Aug 30 '22 18:08 nmandery

Closing as there is now h3o which provides a rust implementation of H3. That should allow targeting WASM without any linking issues.

nmandery avatar Jan 29 '23 10:01 nmandery