Skip to content

Commit 8c43685

Browse files
committed
build: set RUST_CXX_NO_EXCEPTIONS automatically for wasm targets
Otherwise the cxx crate cannot build for a wasm platform.
1 parent 7a0f3f2 commit 8c43685

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

build.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ fn main() {
99
let manifest_dir_opt = env::var_os("CARGO_MANIFEST_DIR").map(PathBuf::from);
1010
let manifest_dir = manifest_dir_opt.as_deref().unwrap_or(Path::new(""));
1111

12-
cc::Build::new()
12+
let mut builder = cc::Build::new();
13+
builder
1314
.file(manifest_dir.join("src/cxx.cc"))
1415
.cpp(true)
1516
.cpp_link_stdlib(None) // linked via link-cplusplus crate
1617
.std(cxxbridge_flags::STD)
17-
.warnings_into_errors(cfg!(deny_warnings))
18-
.compile("cxxbridge1");
18+
.warnings_into_errors(cfg!(deny_warnings));
19+
20+
// If we are building for WASM then ensure that RUST_CXX_NO_EXCEPTIONS is defined
21+
// otherwise the crate can fail to build for WASM targets
22+
//
23+
// Note that this could also be done via `target_family = "wasm"`
24+
// but instead we are copying how the `cc` crate detects WASM
25+
// https://github.com/rust-lang/cc-rs/blob/a0441c3bca087c6457824d05857e00d8e8b06753/src/lib.rs#L2016
26+
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
27+
builder.define("RUST_CXX_NO_EXCEPTIONS", None);
28+
29+
builder.compile("cxxbridge1");
1930

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

0 commit comments

Comments
 (0)