Add support for Wasm/emscripten target#27
Conversation
There was a problem hiding this comment.
Pull request overview
Adds initial support for building the raylib-ffi crate against the wasm32-unknown-emscripten target by adjusting the CMake configuration and bindgen/clang invocation.
Changes:
- Bump
bindgenbuild-dependency version. - Set raylib CMake
PLATFORM=Webwhen targeting Emscripten. - Add an Emscripten-specific bindgen clang argument (
-fvisibility=default).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| Cargo.toml | Updates bindgen build dependency version. |
| build/bind.rs | Adds Emscripten target detection and adjusts CMake + bindgen configuration accordingly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if cfg!(feature = "dylib") { | ||
| println!("cargo:rustc-link-lib=dylib=raylib"); | ||
| } | ||
| else { | ||
| } else { | ||
| println!("cargo:rustc-link-lib=static=raylib"); | ||
| } |
There was a problem hiding this comment.
link_libs() uses cfg!(windows), cfg!(target_os = "macos"), and cfg!(unix) to decide which system libraries to link. In a build script those cfg! values reflect the host (where the build script runs), not the Cargo target. When building wasm32-unknown-emscripten from a Unix host this will still emit -lX11/etc, which should not be linked for Emscripten and can break the build. Use CARGO_CFG_TARGET_OS (and/or CARGO_CFG_TARGET_ENV) to branch on the target and add an emscripten path that avoids the desktop-only link directives.
| let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); | ||
|
|
||
| if target_os == "emscripten" { | ||
| cmake_config.define("PLATFORM", "Web"); | ||
| } |
There was a problem hiding this comment.
The new Emscripten-specific behavior is introduced here, but the repo CI currently only builds/tests native targets (Linux/Windows/macOS). Without a CI job that at least runs cargo build --target wasm32-unknown-emscripten (with emsdk installed), regressions to the Web path are likely to go unnoticed.
Added support for emscripten to build raylib for wasm target
emsdk needs to be setup correctly i.e.
or simillar.
Then building with
cargo build --target wasm32-unknown-emscriptenworks and generates correct bindings.Also, updated bindgen as I suspected some errors were because of it - didn't hurt - it turned out to be clang issue.