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
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SetupOptions {
wasi_stack_size: Option<u32>,
}
impl SetupOptions {
pub fn new() -> Self {
Self::default()
}
pub fn wasi_stack_size(mut self, bytes: u32) -> Self {
self.wasi_stack_size = Some(bytes);
self
}
}
pub fn setup() {
setup_with(SetupOptions::default());
}
pub fn setup_with(options: SetupOptions) {
if let Some(bytes) = options.wasi_stack_size {
wasi::validate_stack_size(bytes, "SetupOptions::wasi_stack_size")
.unwrap_or_else(|error| panic!("{error}"));
}
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(options.wasi_stack_size);
}
"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)",
]
);
}
#[test]
fn setup_options_defaults_and_builder() {
let default_options = SetupOptions::default();
assert_eq!(SetupOptions::new(), default_options);
assert_eq!(default_options.wasi_stack_size, None);
let configured = SetupOptions::new().wasi_stack_size(8_388_608);
assert_eq!(configured.wasi_stack_size, Some(8_388_608));
assert_eq!(configured.clone(), configured);
}
#[test]
#[should_panic(expected = "SetupOptions::wasi_stack_size must be greater than 0 bytes")]
fn setup_with_rejects_zero_wasi_stack_size_before_target_dispatch() {
setup_with(SetupOptions::new().wasi_stack_size(0));
}
#[test]
#[should_panic(expected = "SetupOptions::wasi_stack_size must be a 16-byte multiple")]
fn setup_with_rejects_unaligned_wasi_stack_size_before_target_dispatch() {
setup_with(SetupOptions::new().wasi_stack_size(17));
}
}