Skip to content

Commit

Permalink
build: set RUST_CXX_NO_EXCEPTIONS automatically for wasm targets
Browse files Browse the repository at this point in the history
Otherwise the cxx crate cannot build for a wasm platform.
  • Loading branch information
ahayzen-kdab committed Nov 18, 2024
1 parent 7a0f3f2 commit 8c43685
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ fn main() {
let manifest_dir_opt = env::var_os("CARGO_MANIFEST_DIR").map(PathBuf::from);
let manifest_dir = manifest_dir_opt.as_deref().unwrap_or(Path::new(""));

cc::Build::new()
let mut builder = cc::Build::new();
builder
.file(manifest_dir.join("src/cxx.cc"))
.cpp(true)
.cpp_link_stdlib(None) // linked via link-cplusplus crate
.std(cxxbridge_flags::STD)
.warnings_into_errors(cfg!(deny_warnings))
.compile("cxxbridge1");
.warnings_into_errors(cfg!(deny_warnings));

// If we are building for WASM then ensure that RUST_CXX_NO_EXCEPTIONS is defined
// otherwise the crate can fail to build for WASM targets
//
// Note that this could also be done via `target_family = "wasm"`
// but instead we are copying how the `cc` crate detects WASM
// https://github.com/rust-lang/cc-rs/blob/a0441c3bca087c6457824d05857e00d8e8b06753/src/lib.rs#L2016
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
builder.define("RUST_CXX_NO_EXCEPTIONS", None);

builder.compile("cxxbridge1");

println!("cargo:rerun-if-changed=src/cxx.cc");
println!("cargo:rerun-if-changed=include/cxx.h");
Expand Down

0 comments on commit 8c43685

Please sign in to comment.