1use std::{env, path::Path};
2
3pub fn setup() {
4 let link_dir = env::var("EMNAPI_LINK_DIR").expect("EMNAPI_LINK_DIR must be set");
5 println!("cargo:rerun-if-env-changed=EMNAPI_LINK_DIR");
6 println!("cargo:rerun-if-env-changed=WASI_REGISTER_TMP_PATH");
7 println!("cargo:rustc-link-search={link_dir}");
8 println!("cargo:rustc-link-lib=static=emnapi-basic-mt");
9 println!("cargo:rustc-link-arg=--export=malloc");
10 println!("cargo:rustc-link-arg=--export=free");
11 println!("cargo:rustc-link-arg=--export=napi_register_wasm_v1");
12 println!("cargo:rustc-link-arg=--export-if-defined=node_api_module_get_api_version_v1");
13 println!("cargo:rustc-link-arg=--export-table");
14 println!("cargo:rustc-link-arg=--export=emnapi_async_worker_create");
15 println!("cargo:rustc-link-arg=--export=emnapi_async_worker_init");
16 println!("cargo:rustc-link-arg=--import-memory");
17 println!("cargo:rustc-link-arg=--import-undefined");
18 println!("cargo:rustc-link-arg=--max-memory=4294967296");
19 // lld only allocates 1MiB for the WebAssembly stack.
20 // 6400000 bytes = 64MiB
21 println!("cargo:rustc-link-arg=-zstack-size=6400000");
22 println!("cargo:rustc-link-arg=--no-check-features");
23 let rustc_path = env::var("RUSTC").expect("RUSTC must be set by Cargo");
24 let target = env::var("TARGET").expect("TARGET must be set by Cargo");
25 let crt_reactor_path = Path::new(&rustc_path)
26 .parent()
27 .and_then(|p| p.parent())
28 .map_or_else(
29 || Path::new("").to_path_buf(),
30 |p| {
31 p.join("lib")
32 .join("rustlib")
33 .join(target)
34 .join("lib")
35 .join("self-contained")
36 .join("crt1-reactor.o")
37 },
38 );
39 if crt_reactor_path.exists() {
40 println!("cargo:rustc-link-arg={}", crt_reactor_path.display());
41 println!("cargo:rustc-link-arg=--export=_initialize");
42 } else {
43 println!(
44 "cargo:warning=crt1-reactor.o not found at {}, the multi-threaded runtime may not be initialized correctly",
45 crt_reactor_path.display()
46 );
47 }
48 if let Ok(setjmp_link_dir) = env::var("SETJMP_LINK_DIR") {
49 println!("cargo:rustc-link-search={setjmp_link_dir}");
50 println!("cargo:rustc-link-lib=static=setjmp-mt");
51 }
52 if let Ok(wasi_sdk_path) = env::var("WASI_SDK_PATH") {
53 println!(
54 "cargo:rustc-link-search={wasi_sdk_path}/share/wasi-sysroot/lib/wasm32-wasip1-threads"
55 );
56 }
57}
58