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    "macos" => {
34      // Keep the dynamic lookup behavior on macOS to avoid breaking changes.
35      println!("cargo:rustc-cdylib-link-arg=-Wl");
36      println!("cargo:rustc-cdylib-link-arg=-undefined");
37      println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
38    }
39    "windows" => {
40      if let Ok("gnu") = env::var("CARGO_CFG_TARGET_ENV").as_deref() {
41        windows::setup_gnu();
42      }
43    }
44    _ => {}
45  }
46
47  if (target_env == "gnu" && target_os != "windows") || target_os == "freebsd" {
48    // https://sourceware.org/bugzilla/show_bug.cgi?id=21032
49    // https://sourceware.org/bugzilla/show_bug.cgi?id=21031
50    // https://github.com/rust-lang/rust/issues/134820
51    // pthread_key_create() destructors and segfault after a DSO unloading
52    println!("cargo:rustc-link-arg=-Wl,-z,nodelete");
53  }
54}