#![allow(clippy::expect_used, clippy::unwrap_used)]
use std::path::PathBuf;
use std::process::Command;
fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
let host = std::env::var("HOST").unwrap_or_default();
let target = std::env::var("TARGET").unwrap();
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
emit_backend_cfg(&target_os);
if target_os == "windows" {
let src = manifest_dir
.join("tests")
.join("fixtures")
.join("fs_target.rs");
println!("cargo:rerun-if-changed={}", src.display());
if !src.exists() {
return;
}
let output = out_dir.join("hookfs_fs_target.dll");
let status = Command::new(&rustc)
.args(["--edition", "2021", "--crate-type", "cdylib"])
.args(["--crate-name", "hookfs_fs_target"])
.args(["-C", "target-feature=+crt-static"])
.args(["--target", &target])
.arg("-o")
.arg(&output)
.arg(&src)
.status()
.expect("failed to spawn rustc to build the hookfs test fixture");
assert!(status.success(), "failed to build the hookfs test fixture");
println!("cargo:rustc-env=HOOKFS_FIXTURE_DLL={}", output.display());
} else if target_os == "linux" && host == target {
let src = manifest_dir
.join("tests")
.join("fixtures")
.join("fs_target_linux.rs");
println!("cargo:rerun-if-changed={}", src.display());
if !src.exists() {
return;
}
let output = out_dir.join("libhookfs_fs_target.so");
let status = Command::new(&rustc)
.args(["--edition", "2021", "--crate-type", "cdylib"])
.args(["--crate-name", "hookfs_fs_target"])
.args(["--target", &target])
.arg("-o")
.arg(&output)
.arg(&src)
.status()
.expect("failed to spawn rustc to build the Linux hookfs test fixture");
assert!(
status.success(),
"failed to build the Linux hookfs test fixture"
);
println!("cargo:rustc-env=HOOKFS_FIXTURE_SO={}", output.display());
}
}
fn emit_backend_cfg(target_os: &str) {
const BACKEND_TARGET_OS: &[&str] = &["windows", "linux", "macos", "ios"];
println!("cargo::rustc-check-cfg=cfg(hookfs_backend)");
if BACKEND_TARGET_OS.contains(&target_os) {
println!("cargo::rustc-cfg=hookfs_backend");
}
}