diff --git a/z3-sys/README.md b/z3-sys/README.md index f767960a..cfacdea3 100644 --- a/z3-sys/README.md +++ b/z3-sys/README.md @@ -51,6 +51,15 @@ There are 3 ways for this crate to currently find Z3: by that version to be used. In this case, there is no override via the environment variable. +### Finding emscripten + +For wasm32 targets, we may need an existed emscripten installation for a `sysroot`. +One may specify the root of emsdk by environment variable `EMSDK`, +or the root of emscripten by environment variable `EMSCRIPTEN_ROOT`. +Then we'll use `$EMSDK/upstream/emscripten/cache/sysroot` or `$EMSCRIPTEN_ROOT/cache/sysroot`. + +For example, if you're using brew, you can assign `$HOMEBREW_CELLAR/emscripten//libexec` to `EMSCRIPTEN_ROOT`. + ## Support and Maintenance I am developing this library largely on my own so far. I am able diff --git a/z3-sys/build.rs b/z3-sys/build.rs index 21448af3..da67bf22 100644 --- a/z3-sys/build.rs +++ b/z3-sys/build.rs @@ -102,11 +102,22 @@ fn generate_binding(header: &str) { .generate_comments(false) .rustified_enum(format!("Z3_{x}")) .allowlist_type(format!("Z3_{x}")); - if env::var("TARGET").unwrap() == "wasm32-unknown-emscripten" { - enum_bindings = enum_bindings.clang_arg(format!( - "--sysroot={}/upstream/emscripten/cache/sysroot", - env::var("EMSDK").expect("$EMSDK env var missing. Is emscripten installed?") - )); + let target = env::var("TARGET").unwrap(); + let wasm = target.starts_with("wasm"); + let emscripten = target.ends_with("emscripten"); + if wasm { + // It seems that sometimes the cache may be missing. + let sysroot = env::var("EMSDK") + .map(|emsdk| format!("{}/upstream/emscripten/cache/sysroot", emsdk)) + .or_else(|_err| { + env::var("EMSCRIPTEN_ROOT") + .map(|emscripten_root| format!("{}/cache/sysroot", emscripten_root)) + }); + if let Ok(sysroot) = sysroot { + enum_bindings = enum_bindings.clang_arg(format!("--sysroot={}", sysroot)); + } else if emscripten { + panic!("$EMSDK and $EMSCRIPTEN_ROOT env var missing. Is emscripten installed?"); + } } enum_bindings .generate()