napi_build/
lib.rs

1use std::env;
2
3mod android;
4mod wasi;
5mod windows;
6
7pub fn setup() {
8  // compatible with the v2 versions, will remove in the future
9  {
10    println!("cargo:rerun-if-env-changed=DEBUG_GENERATED_CODE");
11    println!("cargo:rerun-if-env-changed=TYPE_DEF_TMP_PATH");
12    println!("cargo:rerun-if-env-changed=CARGO_CFG_NAPI_RS_CLI_VERSION");
13  }
14
15  println!("cargo::rerun-if-env-changed=NAPI_DEBUG_GENERATED_CODE");
16  println!("cargo::rerun-if-env-changed=NAPI_TYPE_DEF_TMP_FOLDER");
17  println!(
18    "cargo::rerun-if-env-changed=NAPI_FORCE_BUILD_{}",
19    env::var("CARGO_PKG_NAME")
20      .expect("CARGO_PKG_NAME is not set")
21      .to_uppercase()
22      .replace("-", "_")
23  );
24
25  let target_env = env::var("CARGO_CFG_TARGET_ENV").expect("CARGO_CFG_TARGET_ENV is not set");
26  let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS is not set");
27
28  match target_os.as_str() {
29    "android" => if android::setup().is_ok() {},
30    "wasi" => {
31      wasi::setup();
32    }
33    "windows" => {
34      if let Ok("gnu") = env::var("CARGO_CFG_TARGET_ENV").as_deref() {
35        windows::setup_gnu();
36      }
37    }
38    _ => {}
39  }
40
41  if (target_env == "gnu" && target_os != "windows") || target_os == "freebsd" {
42    // https://sourceware.org/bugzilla/show_bug.cgi?id=21032
43    // https://sourceware.org/bugzilla/show_bug.cgi?id=21031
44    // https://github.com/rust-lang/rust/issues/134820
45    // pthread_key_create() destructors and segfault after a DSO unloading
46    println!("cargo:rustc-link-arg=-Wl,-z,nodelete");
47  }
48}