Skip to content
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

feat: add experimental WASM32 support #63

Merged
merged 18 commits into from
Nov 17, 2024
Merged

feat: add experimental WASM32 support #63

merged 18 commits into from
Nov 17, 2024

Conversation

maximmaxim345
Copy link
Owner

@maximmaxim345 maximmaxim345 commented Nov 12, 2024

This PR adds initial support for building and running CADara in WebAssembly (wasm32-unknown-unknown target). The changes include:

  • Split Cadara into library and binary crates for wasm-pack compatibility
  • Add wasm-libc implementation for C++ interop
  • Configure dependencies for WASM (iced webgl, getrandom)
  • Use OpenCASCADE fork with wasm32-unknown-unknown support
  • Add browser initialization and logging infrastructure
  • Implement workarounds for various WASM limitations
  • add various cargo-make commands for building and serving CADara for WebAssembly

⚠️ This is experimental and contains several temporary workarounds marked with TODO(wasm32) comments throughout the codebase. These will be addressed in follow-up PRs.

To build:

  • install the nightly rust toolchain with rustup install nightly
  • build and serve a dev build with cargo make dev (see cargo make help for more wasm related commands)

Known limitations:

  • Some C++ constructors are currently bypassed due to autocxx WASM limitations
  • Incomplete wasm-libc implementation
  • Various unsafe workarounds in modeling workspace
  • Exceptions are broken, calling some OpenCASCADE functions result in a panic

Closes #58

@maximmaxim345 maximmaxim345 changed the title Test/web feat: add support for wasm Nov 13, 2024
@maximmaxim345 maximmaxim345 force-pushed the test/web branch 4 times, most recently from fb65adc to 0c8dd57 Compare November 17, 2024 10:29
@maximmaxim345 maximmaxim345 changed the title feat: add support for wasm feat: add experimental WASM32 support Nov 17, 2024
Building multiple targets now will reuse the same source code download
of opencascade.
…nknown-unknown

While upstream OpenCASCADE has support for wasm32-unknown-emscripten, we
require wasm32-unknown-unknown for wasm-bindgen (and therefore iced).
The `pkg` folder contains the build wasm binary.
Building for WASM requires some extra flags passed through
CXXFLAGS_wasm32_unknown_unknown. `autocxx` currently ignores compiler
flags set through env vars, therefore we manually pass them.
For easier use with wasm-bindgen.
wasm-pack can only work on library crates, so we will split cadara into:
- cadara (binary crate): will just call `cadara_lib::run_cadara()`
- cadara_lib (library crate): library with a single public function
`run_cadara` (previously `main`)
This will be the entry point for all native builds.
We rename the former `main` function to `run_cadara` to keep
compatibility with wasm-bindgen-test if later required (crashes if a
function is called `main`).
This commit both enables the webgl backend of iced by default and
enables wasm compatibilty in the getrandom crate.
tracing is now only used when not running on wasm, for wasm we use
console_log and attach a panic handler.
This new crate implements remaining functions of
`wasm32-unknown-unknown-libcxx` that can not be implemented in pure c.
This is incomplete yet and will be addressed in future PRs.
For Rust/C++ interop to work correctly, we must manually call
`wasm_libc::init`, which calls all global constructors in the C++ code.
Not doing this, makes wasm-bindgen fail.
`run_cadara` (so effectively `main`) will now be automatically started
when the wasm binrary is loaded in the browser.
Autocxx currently does not support constructors with WASM. This is a
workaround for that.
This unsafe workaround will be removed in a future PR.
This adds a couple of `cargo-make` commands that ease building for wasm
by automatically setting up a build environment for building CADara for
WebAssembly.
@maximmaxim345 maximmaxim345 marked this pull request as ready for review November 17, 2024 11:50
@maximmaxim345 maximmaxim345 merged commit 08405b3 into main Nov 17, 2024
10 checks passed
@maximmaxim345 maximmaxim345 deleted the test/web branch November 17, 2024 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate and Implement WebAssembly (WASM) Support
1 participant