Skip to content

Initial support for building for wasm32-unknown-unknown #241

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

waywardmonkeys
Copy link
Contributor

On macOS, this needs clang from homebrew as the Apple provided one doesn't include target support for wasm32-unknown-unknown.

An implementation of malloc as well as the other functions declared in the wasm-sysroot headers needs to be supplied. These implementations should come from the copy of musl libc that is used in the WASI libc (top half), as located in https://github.com/WebAssembly/wasi-libc/tree/main/libc-top-half/musl/src

Enabling flecs_rest (and therefore flecs_http) won't work here as there isn't support for that in the wasm32 environment.

Better fixes for the changes in flecs_ecs_sys/src/flecs.[ch] need to be done.

time stuff isn't hooked up yet (ecs_os_has_time() will return false for now still.

build with:

CC=/opt/homebrew/opt/llvm/bin/clang cargo build --target wasm32-unknown-unknown --features flecs_base,flecs_query_rust_traits --no-default-features

This will result in a compiler crash due to what is probably a bug in rustc.

@waywardmonkeys waywardmonkeys marked this pull request as draft March 5, 2025 15:17
@waywardmonkeys
Copy link
Contributor Author

As noted above, on macOS, you must build with the homebrew build of LLVM and not the one from Xcode or the command line tools. This is because the homebrew build is the one that actually supports the wasm32-unknown-unknown target.

@waywardmonkeys waywardmonkeys force-pushed the build-for-wasm32-unknown-unknown branch from 4eeade2 to 72a2a53 Compare March 5, 2025 16:10
On macOS, this needs clang from homebrew as the Apple provided one
doesn't include target support for wasm32-unknown-unknown.

An implementation of malloc as well as the other functions declared
in the wasm-sysroot headers needs to be supplied. These implementations
should come from the copy of musl libc that is used in the WASI
libc (top half), as located in https://github.com/WebAssembly/wasi-libc/tree/main/libc-top-half/musl/src

Enabling flecs_rest (and therefore flecs_http) won't work here as
there isn't support for that in the wasm32 environment.

Better fixes for the changes in flecs_ecs_sys/src/flecs.[ch] need
to be done.

time stuff isn't hooked up yet (ecs_os_has_time() will return false
for now still.

build with:

CC=/opt/homebrew/opt/llvm/bin/clang cargo build --target wasm32-unknown-unknown --features flecs_base,flecs_query_rust_traits --no-default-features

This will result in a compiler crash due to what is probably a
bug in rustc.
@waywardmonkeys waywardmonkeys force-pushed the build-for-wasm32-unknown-unknown branch from 72a2a53 to f67363f Compare March 5, 2025 16:21
@waywardmonkeys
Copy link
Contributor Author

Big things to be done in the wasm-sysroot after I pushed the last couple of updates:

  • printf and friends need their implementation brought over.
  • Same for strtod and strtol
  • Same for malloc, realloc, calloc, free

This uses the same (mostly useless) rand that must has, so don't expect much (same as on wasi targets and Alpine Linux and probably emscripten).

PRs into this branch to add the above things to the wasm-sysroot are welcome.

And I think most of the features from flecs that can run at least parse now ... there be might be some more undefined symbols than those that I just listed.

But the real problem is fixing the Rust compiler crash when building flecs_ecs. The compiler does not crash on flecs_ecs_sys.

@waywardmonkeys
Copy link
Contributor Author

The things that trigger the Rust compiler error can be hacked around by:

diff --git a/flecs_ecs/src/core/component_registration/registration.rs b/flecs_ecs/src/core/component_registration/registration.rs
index ccdc97f..6117e4d 100644
--- a/flecs_ecs/src/core/component_registration/registration.rs
+++ b/flecs_ecs/src/core/component_registration/registration.rs
@@ -206,6 +206,7 @@ pub(crate) fn register_componment_data_explicit<T, const ALLOCATE_TAG: bool>(
 where
     T: ComponentId,
 {
+/*
     let only_type_name = crate::core::get_only_type_name::<T>();
     let only_type_name = compact_str::format_compact!("{}\0", only_type_name);

@@ -246,6 +247,8 @@ where
     );

     entity
+*/
+todo!()
 }

 /// registers the component with the world.
diff --git a/flecs_ecs/src/core/world.rs b/flecs_ecs/src/core/world.rs
index 3631f9c..2aef339 100644
--- a/flecs_ecs/src/core/world.rs
+++ b/flecs_ecs/src/core/world.rs
@@ -71,6 +71,7 @@ impl Default for World {
             components,
             components_array,
         };
+/*
         unsafe {
             sys::ecs_set_binding_ctx(
                 world.raw_world.as_ptr(),
@@ -78,7 +79,7 @@ impl Default for World {
                 None, //we manually destroy it in world drop for ref count check
             );
         }
-
+*/
         world.init_builtin_components();
         world
     }

@waywardmonkeys
Copy link
Contributor Author

waywardmonkeys commented Mar 5, 2025

As noted above, flecs_http and flecs_rest can't work in wasm32 builds, so the way that features are configured will need to change.

Additionally, a small amount of work has to be done to allow flecs_json to build (need snprintf and maybe something else).

Then, you'll run into issues with criterion being a dev dependency and not building for wasm32 with rayon enabled ...

And then you'll run into getrandom if my PR #242 for that hasn't landed yet.

@525c1e21-bd67-4735-ac99-b4b0e5262290

Forcing LTO...

[profile.dev]
lto = true

...avoids the LLVM crash.

@waywardmonkeys
Copy link
Contributor Author

The workaround for the compiler crash in #256.

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.

2 participants