pub use js2rust_bridge_macro::js2rust_bridge;
pub use js2zig_core::{HostConfig, HostFunction, HostType};
use std::path::PathBuf;
pub struct BuildConfig {
pub name: String,
pub js_file: String,
pub additional_js_files: Vec<String>,
pub host_functions: Option<HostConfig>,
pub force_rebuild: bool,
}
pub fn build(config: BuildConfig) {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let cache_dir = PathBuf::from(&manifest_dir).join(".js2zig-cache");
let js_file_path = PathBuf::from(&manifest_dir).join(&config.js_file);
let additional_js_paths: Vec<PathBuf> = config
.additional_js_files
.iter()
.map(|p| PathBuf::from(&manifest_dir).join(p))
.collect();
let project_config = js2zig_core::ProjectConfig {
name: config.name,
js_file: js_file_path,
additional_js_files: additional_js_paths,
out_dir: cache_dir.clone(),
host_config: config.host_functions,
force_rebuild: config.force_rebuild,
run_zig_build: true, };
match js2zig_core::transpile_project(&project_config) {
Ok(_result) => {
link_from_cache(&cache_dir);
}
Err(e) => {
eprintln!("js2rust_bridge::build: transpilation error: {}", e);
link_from_cache(&cache_dir);
}
}
}
pub fn link() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let cache_dir = PathBuf::from(&manifest_dir).join(".js2zig-cache");
link_from_cache(&cache_dir);
}
fn link_from_cache(cache_dir: &std::path::Path) {
if !cache_dir.exists() {
println!(
"cargo:warning=js2zig: .js2zig-cache not found. \
Run `cargo build` twice on a clean checkout."
);
return;
}
let mut found = false;
if let Ok(entries) = std::fs::read_dir(cache_dir) {
for entry in entries.flatten() {
let group_dir = entry.path();
let lib_dir = group_dir.join("zig-out").join("lib");
if lib_dir.exists()
&& let Some(group_name) = entry.file_name().to_str() {
if group_name == "host.zig" || group_name == "groups.json" {
continue;
}
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-lib=static={}", group_name);
found = true;
}
}
}
if !found {
println!(
"cargo:warning=js2zig: no compiled libraries found in {}. \
Run `cargo build` twice on a clean checkout.",
cache_dir.display()
);
}
}