use std::env;
mod android;
mod wasi;
mod windows;
fn wasi_threads_cfg_lines(target: &str) -> Vec<&'static str> {
let mut lines = vec![
"cargo::rustc-check-cfg=cfg(napi_wasi_threads)",
"cargo::rustc-check-cfg=cfg(napi_wasi_no_heap_sync)",
];
if target == "wasm32-wasip1-threads" {
lines.push("cargo::rustc-cfg=napi_wasi_threads");
}
lines
}
pub fn setup() {
for line in wasi_threads_cfg_lines(&env::var("TARGET").unwrap_or_default()) {
println!("{line}");
}
{
println!("cargo:rerun-if-env-changed=DEBUG_GENERATED_CODE");
println!("cargo:rerun-if-env-changed=TYPE_DEF_TMP_PATH");
println!("cargo:rerun-if-env-changed=CARGO_CFG_NAPI_RS_CLI_VERSION");
}
println!("cargo::rerun-if-env-changed=NAPI_DEBUG_GENERATED_CODE");
println!("cargo::rerun-if-env-changed=NAPI_TYPE_DEF_TMP_FOLDER");
println!(
"cargo::rerun-if-env-changed=NAPI_FORCE_BUILD_{}",
env::var("CARGO_PKG_NAME")
.expect("CARGO_PKG_NAME is not set")
.to_uppercase()
.replace("-", "_")
);
let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV is not set");
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS is not set");
match target_os.as_str() {
"android" => if android::setup().is_ok() {},
"wasi" => {
wasi::setup();
}
"macos" => {
println!("cargo:rustc-cdylib-link-arg=-Wl");
println!("cargo:rustc-cdylib-link-arg=-undefined");
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
}
"windows" => {
if let Ok("gnu") = env::var("CARGO_CFG_TARGET_ENV").as_deref() {
windows::setup_gnu();
}
}
_ => {}
}
if (target_env == "gnu" && target_os != "windows")
|| target_os == "freebsd"
|| target_os == "openbsd"
{
println!("cargo:rustc-link-arg=-Wl,-z,nodelete");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn emits_the_cfg_for_the_threaded_wasi_target() {
assert_eq!(
wasi_threads_cfg_lines("wasm32-wasip1-threads"),
vec![
"cargo::rustc-check-cfg=cfg(napi_wasi_threads)",
"cargo::rustc-check-cfg=cfg(napi_wasi_no_heap_sync)",
"cargo::rustc-cfg=napi_wasi_threads",
]
);
}
#[test]
fn keeps_the_cfg_off_the_threadless_wasi_target() {
assert_eq!(
wasi_threads_cfg_lines("wasm32-wasip1"),
vec![
"cargo::rustc-check-cfg=cfg(napi_wasi_threads)",
"cargo::rustc-check-cfg=cfg(napi_wasi_no_heap_sync)",
]
);
}
#[test]
fn still_declares_the_cfg_on_a_native_target() {
assert_eq!(
wasi_threads_cfg_lines("x86_64-unknown-linux-gnu"),
vec![
"cargo::rustc-check-cfg=cfg(napi_wasi_threads)",
"cargo::rustc-check-cfg=cfg(napi_wasi_no_heap_sync)",
]
);
}
}