54 lines
2.1 KiB
Rust

use std::path::PathBuf;
use std::process::Command;
fn main() {
let manifest = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let nim_src = manifest.join("../nim_timer.nim");
let nim_src = nim_src.canonicalize().unwrap_or(manifest.join("../nim_timer.nim"));
// Walk up to find the nim-ffi repo root (directory containing nim_src's library)
// The repo root is where nim c should be run from (contains config.nims).
// We assume nim_src lives somewhere under repo_root.
// Derive repo_root as the ancestor that contains the .nimble file or config.nims.
let mut repo_root = nim_src.clone();
loop {
repo_root = match repo_root.parent() {
Some(p) => p.to_path_buf(),
None => break,
};
if repo_root.join("config.nims").exists() || repo_root.join("ffi.nimble").exists() {
break;
}
}
// Match the per-OS shared-library naming used by the CMakeLists:
// macOS: lib<name>.dylib
// Linux: lib<name>.so
// Windows: <name>.dll (Cargo links the auto-generated <name>.lib import lib)
#[cfg(target_os = "macos")]
let out_lib = repo_root.join("libnimtimer.dylib");
#[cfg(target_os = "linux")]
let out_lib = repo_root.join("libnimtimer.so");
#[cfg(target_os = "windows")]
let out_lib = repo_root.join("nimtimer.dll");
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
compile_error!("nim-ffi build.rs: unsupported target OS (expected macos, linux, or windows)");
let mut cmd = Command::new("nim");
cmd.arg("c")
.arg("--mm:orc")
.arg("-d:chronicles_log_level=WARN")
.arg("--app:lib")
.arg("--noMain")
.arg(format!("--nimMainPrefix:libnimtimer"))
.arg(format!("-o:{}", out_lib.display()));
cmd.arg(&nim_src).current_dir(&repo_root);
let status = cmd.status().expect("failed to run nim compiler");
assert!(status.success(), "Nim compilation failed");
println!("cargo:rustc-link-search={}", repo_root.display());
println!("cargo:rustc-link-lib=nimtimer");
println!("cargo:rerun-if-changed={}", nim_src.display());
}