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

Build fails for wasm on windows 10 #193

Open
largenumberhere opened this issue Jun 9, 2024 · 8 comments
Open

Build fails for wasm on windows 10 #193

largenumberhere opened this issue Jun 9, 2024 · 8 comments

Comments

@largenumberhere
Copy link

Hi,

I am trying to build my project for wasm. I couldn't find any resources that covered this topic. I tried these commands out and had no luck.
cargo build --target=wasm32-unknown-unknown

(This command is similar to the one given in a compile error from raylib-sys, I had to modify it for powershell.)
env EMCC_CFLAGS="-03 -sUSE_GLFW=3 -sGL_ENABLE_GET_PROC_ADDRESS -sWASM=1 -sALLOW_MEMORY_GROWTH=1 -sWASM_MEM_MAX=512MB -sTOTAL_MEM ORY=512MB -sABORTING_MALLOC=0 -sASYNCIFY -sFORCE_FILESYSTEM=1 -sASSERTIONS=1 -sERROR_ON_UNDEFINED_SYMBOLS=0 -sEXPORTED_RUNTIME_METHODS=ccallcwrap" cargo build --target=wasm32-unknown-unknown

I have attached the full backtrace.
wasm_raylibrs_error_log.txt
For completeness, I am working with raylib_rs5.0.1, cmake version 3.28.1, rust cargo 1.78.0 (54d8815d0 2024-03-26).

I hope this information is useful to you.

@Doublestuf
Copy link

I've not tried myself so far, but I believe you're meant to use cross as the compiler and use their command, which would be something like cross build --target wasm32-unknown-emscripten --release (with no equal sign).

@largenumberhere
Copy link
Author

I've not tried myself so far, but I believe you're meant to use cross as the compiler and use their command, which would be something like cross build --target wasm32-unknown-emscripten --release (with no equal sign).

Thanks for the suggestion. I gave these a go with no luck - I ran into a similar error telling me to set the environment variables, perhaps I did it wrong?
cross build --target wasm32-unknown-emscripten --release

env EMCC_CFLAGS="-03 -sUSE_GLFW=3 -sGL_ENABLE_GET_PROC_ADDRESS -sWASM=1 -sALLOW_ME MORY_GROWTH=1 -sWASM_MEM_MAX=512MB -sTOTAL_MEM ORY=512MB -sABORTING_MALLOC=0 -sASYNCIFY -sFORCE_FILESYSTEM=1 -sASSERTIONS=1 -sERROR_ON_UNDEFINED_SYMBOLS=0 -sEXPORTED_RUNTIME_METHODS=ccallcwrap" cross build --target wasm32-unknown-emscripten --release

@Doublestuf
Copy link

I looked into the error logs, seemed like something related to CMake, but can't be sure. But I did find a resource for building for web. Took some thorough reading but the README links to the raysan5 raylib wiki for build instructions, should be up-to-date assuming the build process for these bindings is still the same.

@largenumberhere
Copy link
Author

I looked into the error logs, seemed like something related to CMake, but can't be sure. But I did find a resource for building for web. Took some thorough reading but the README links to the raysan5 raylib wiki for build instructions, should be up-to-date assuming the build process for these bindings is still the same.

It will be related but not the same, I believe I'd have to fork the bindings and play with the build scripts. It's way out of my depth to wrestle with cmake and cargo build scripts so I'm praying this will be a quick fix on the deltaphc's part when they find the time soon

@verdivictor
Copy link

@largenumberhere You manage to compile to WASM?

I'm using these flags, which compile, but produce an error in the browser.
https://github.com/Captainfl4me/rust-raylib-spline/blob/master/build_wasm.

My raylib code fetches images from a local dir during local runs, maybe images must be served through HTTP.

@largenumberhere
Copy link
Author

@largenumberhere You manage to compile to WASM?

I'm using these flags, which compile, but produce an error in the browser. https://github.com/Captainfl4me/rust-raylib-spline/blob/master/build_wasm.

My raylib code fetches images from a local dir during local runs, maybe images must be served through HTTP.

Thanks this gave me some inspiration.
After reading through various scripts and bits of documentation I found a workaround using WSL. I think my issue in windows may be some missing or broken dependency. Anyway I'll leave my workaround here for reference.

  • First I had to clone the latest version of the em-sdk in wsl. I build this latest version of the library as described https://emscripten.org/docs/getting_started/downloads.html.**
  • I have installed other programs in wsl that may be important. build-essential cmake rustup cargo
  • For compiling, I used the nightly branch of rust. I install the wasm32-unknown-emscripten target in the nightly toolchain.
  • in wsl I load the emsdk into the path with its script
  • I create a folder assets for any files the program should use
  • inside wsl I run RUST_BACKTRACE=full EMCC_CFLAGS="-O3 -sUSE_GLFW=3 -sGL_ENABLE_GET_PROC_ADDRESS -sWASM=1 -sALLOW_MEMORY_GROWTH=1 -sWASM_MEM_MAX=1024MB -sTOTAL_MEMORY=512MB -sABORTING_MALLOC=0 -sASYNCIFY -sFORCE_FILESYSTEM=1 -sASSERTIONS=1 -sERROR_ON_UNDEFINED_SYMBOLS=0 -sEXPORTED_RUNTIME_METHODS=ccallcwrap -sASYNCIFY_STACK_SIZE=32768000 -sSTACK_SIZE=200000 --embed-file assets" /home/rose/.cargo/bin/cargo +nightly build --target wasm32-unknown-emscripten --release*
  • When the compilation finally is complete, I copy this template https://github.com/emscripten-core/emscripten/blob/main/src/shell_minimal.html into the target/wasm32-unknown-emscripten/release folder. I replace the {{script}} at the bottom with***
    </script>
    <script src="PROGRAM_NAME.js"></script>
  • Finally, for testing I use a python http server in powershell python -m http.server -d target/wasm32-unknown-emscripten/release.

I have thrown together some scripts to automate most of this process. They will require modifying

build_wasm.sh

#!/bin/bash
cd ./emsdk
./emsdk activate latest
source ./emsdk_env.sh
cd ..
RUST_BACKTRACE=full EMCC_CFLAGS="-O3 -sUSE_GLFW=3 -sGL_ENABLE_GET_PROC_ADDRESS -sWASM=1 -sALLOW_MEMORY_GROWTH=1 -sWASM_MEM_MAX=1024MB -sTOTAL_MEMORY=512MB -sABORTING_MALLOC=0 -sASYNCIFY -sFORCE_FILESYSTEM=1 -sASSERTIONS=1 -sERROR_ON_UNDEFINED_SYMBOLS=0 -sEXPORTED_RUNTIME_METHODS=ccallcwrap -sASYNCIFY_STACK_SIZE=32768000 -sSTACK_SIZE=200000 --embed-file assets"  /home/rose/.cargo/bin/cargo  +nightly build --target wasm32-unknown-emscripten --release
cp shell_minimal.html target/wasm32-unknown-emscripten/release/index.html

run_wasm.sh

wsl  -u YOUR_USERNAME -d Ubuntu --exec bash -c ./build_wasm.sh
python -m http.server -d target/wasm32-unknown-emscripten/release

*I had to modify the build arguments substaintially because the default stack size is easily blown out with raylib programs, This also bundles a folder assets in the process so that sounds, images, etc can be loaded normally.
the allowed arguments are at https://github.com/emscripten-core/emscripten/blob/main/src/settings.js

**It is important to use the latest version of emsdk, because not all flags are supported by outdated builds on package repositories.

***The more proper process here involves node, wasm-pack or something

I haven't ironed out all the faults yet, for one the website seems to hang at some parts of the main loop. I also wouldn't call this a correct solution because it relies on wsl but it'll be good enough for me.
Maybe I should refine this process and make a docker container to make this more accessible?

@IoIxD
Copy link
Contributor

IoIxD commented Oct 29, 2024

For future reference, building for WASM on 5.5 will not require the huge block of environment variables as we will use cmake to compile.

@largenumberhere
Copy link
Author

For future reference, building for WASM on 5.5 will not require the huge block of environment variables as we will use cmake to compile.

I look forward to it

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

No branches or pull requests

4 participants