rive-rs icon indicating copy to clipboard operation
rive-rs copied to clipboard

`build.rs` doesn't support `wasm` family architecture

Open nuzzles opened this issue 2 years ago • 15 comments

The build.rs script heavily relies on by C++, which is OK (not ideal) but doesn't seem to be configured correctly for the wasm architecture.

If you attempt to build rive-rs for wasm (at least on MacOS M1), you'll end up with this:

cargo build --target wasm32-unknown-unknown

Output:

  cargo:warning=In file included from ../submodules/rive-cpp/include/rive/animation/animation.hpp:3:

  cargo:warning=../submodules/rive-cpp/include/rive/generated/animation/animation_base.hpp:3:10: fatal error: 'string' file not found

  cargo:warning=#include <string>

  cargo:warning=         ^~~~~~~~

  cargo:warning=1 error generated.

I've tried adjusting the .includes in the build.rs but have been fighting uphill battles.

Wondering if there's a priority to support wasm32 or have a pure-rust parser akin to rive-cpp? I would imagine this (web) is the primary supported for rive, so this should probably be considered a bug.

nuzzles avatar Nov 19 '23 03:11 nuzzles

@simbleau, thank you for reporting. This shouldn't be too hard to fix since we already support wasm.

I'll try to fix this next week. It's likely that we're missing some flags.

dragostis avatar Nov 20 '23 12:11 dragostis

Since we're using C++ stdlib in the C++ runtime, it seems like we'll need to use emscripten to compile the runtime then link it with rive-rs. I might take a crack at this at some point, but I'm currently caught up.

dragostis avatar Nov 27 '23 09:11 dragostis

Hello! I am in the same situation where I am trying to compile a Bevy app to WASM. Any updates to this?

COCPORN avatar Jan 20 '24 09:01 COCPORN

Building wasm32-unknown-emscripten instead of wasm32-unknown-unknown works fine, it seems.

Then, linking with other wasm32-unknown-unknown libs should not be a problem.

dragostis avatar Jan 22 '24 15:01 dragostis

I don't think this should be closed @dragostis.

Most rust developers who work with wasm32-unknown-unknown need documentation or guidance to make this work with their existing codebase. Bevy tests on wasm32-unknown-unknown as well, and is their supported target. It's also pretty common for users to use build tools like Trunk which only compile for wasm32-unknown-unknown.

Perhaps this issue should be closed when it's appropriately documented how to run this on web.

nuzzles avatar Jan 22 '24 15:01 nuzzles

I think plug-and-play compatibility with wasm32-unknown-unknown is at least off the table until we find a solution to compiling the C++ code without Emscripten installed, since rustup won't install it automatically.

~This might be possible with something similar to wasm32-unknown-unknown-openbsd-libc, but this one unfortunately only works for C libraries.~ This cannot work since the C++ code also has a dependency on malloc.

Also, for wasm32-unknown-emscripten we could map wasm32-unknown-unknown -> wasm32-unknown-emscripten automatically in build.rs.

dragostis avatar Jan 22 '24 16:01 dragostis

This might be a stupid question, but does it really need to be compiled with the Rust toolchain? I thought one of the benefits and goals of WASM was to be able to distribute binaries that had a defined ABI and would run ubiquitously. I would be very happy with prebuilt C++ distributed as part of the Rust-packages as an alternative if that is possible.

Also, I second the comment above. This might well work with Bevy, but at the moment I don't understand how to do it to WASM with Bevy and that is the only target I am currently interested in, so to me, Rive is not an option right now. This could be mitigated with documentation.

COCPORN avatar Jan 22 '24 20:01 COCPORN

I was under the impression that simply building rive-rs with wasm32-unknown-emscripten should be easily compatible with wasm32-unknown-unknow, but that's not the case. I tried experimenting with this idea, but for a Bevy project what ends up happening is that the link step will be missing libc to link against in the final step.

Building a Bevy project with wasm32-unknown-emscripten also fails because winit only works with wasm32-unknown-unknown.

Re-opening the issue, but I currently don't have any leads/ideas. tree-sitter-c2rust has a solution to this by providing a mini sysroot to build with, but I'm not aware of any such project for C++.

dragostis avatar Jan 23 '24 10:01 dragostis

What's the potential for a Rust-native runtime? Rust would give you WASM out of the box, and FFI to C++ easily. These problems seem self-inflicted because C++ doesn't go into Rust and then into WASM easily.

I really hate to be "that guy" but this Bevy runtime is pretty much unusable for anyone who wants to get this on web. :(

nuzzles avatar Feb 22 '24 15:02 nuzzles

@simbleau, I just pushed to an experimental branch using emscripten's libcxx implementation. Could you please check to see if it works?

dragostis avatar Feb 26 '24 14:02 dragostis

@simbleau, I just pushed to an experimental branch using emscripten's libcxx implementation. Could you please check to see if it works?

Sure, should I get some bandwidth... not today, but soon, hopefully. :)

nuzzles avatar Feb 26 '24 20:02 nuzzles

Unfortunately, the solution doesn't work because wasm-bindgen's interpreter stumbles when dealing with the C++ part of the generated code. It seems like mixing up non-Rust code is not a way forward for wasm32-unknown-unknown for now.

dragostis avatar Feb 27 '24 13:02 dragostis

There is a rive-wasm api (https://github.com/rive-app/rive-wasm). Any thought to using that for wasm and rive-cpp for native?

cfagot avatar Mar 02 '24 18:03 cfagot

There is a rive-wasm api (https://github.com/rive-app/rive-wasm). Any thought to using that for wasm and rive-cpp for native?

This is an idea worth looking into.

nuzzles avatar Mar 02 '24 20:03 nuzzles

Using rive-wasm you'll end up hitting the same roadblocks unless you completely seal the wasm library and call it only through JS.

dragostis avatar Mar 11 '24 09:03 dragostis