Skip to main content

js2rust_bridge/
lib.rs

1// js2rust-bridge: Facade crate for the js2rust_bridge! proc-macro.
2//
3// Users only need to depend on this single crate:
4//
5// ```toml
6// [dependencies]
7// js2rust-bridge = "0.1"
8// [build-dependencies]
9// js2rust-bridge = "0.1"
10// ```
11//
12// The macro transpiles JS to Zig and generates Rust FFI bindings in one step.
13// Call `js2rust_bridge::link()` from your `build.rs` to link the compiled
14// static libraries.
15
16pub use js2rust_bridge_macro::js2rust_bridge;
17
18/// Scan `.js2zig-cache/` and emit `cargo:rustc-link-*` directives for each
19/// compiled static library.
20///
21/// Call this from your `build.rs`:
22///
23/// ```rust,ignore
24/// fn main() {
25///     js2rust_bridge::link();
26/// }
27/// ```
28pub fn link() {
29    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
30    let cache_dir = std::path::Path::new(&manifest_dir).join(".js2zig-cache");
31
32    println!("cargo:rerun-if-changed=.js2zig-cache");
33
34    if !cache_dir.exists() {
35        println!(
36            "cargo:warning=js2zig: .js2zig-cache not found. \
37             Run `cargo build` twice on a clean checkout."
38        );
39        return;
40    }
41
42    if let Ok(entries) = std::fs::read_dir(&cache_dir) {
43        for entry in entries.flatten() {
44            let group_dir = entry.path();
45            let lib_dir = group_dir.join("zig-out").join("lib");
46
47            if lib_dir.exists()
48                && let Some(group_name) = entry.file_name().to_str()
49            {
50                println!("cargo:rustc-link-search=native={}", lib_dir.display());
51                println!("cargo:rustc-link-lib=static={}", group_name);
52            }
53        }
54    }
55}