use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg(target_family = "windows")]
compile_error!("honggfuzz-rs does not currently support Windows but works well under WSL (Windows Subsystem for Linux)");
#[cfg(not(any(
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
)))]
const GNU_MAKE: &str = "make";
#[cfg(any(
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
const GNU_MAKE: &str = "gmake";
fn main() {
let honggfuzz_target = match env::var("CARGO_HONGGFUZZ_TARGET_DIR") {
Ok(path) => path, Err(_) => return,
};
let honggfuzz_build_version =
env::var("CARGO_HONGGFUZZ_BUILD_VERSION").unwrap_or("unknown".to_string());
if VERSION != honggfuzz_build_version {
eprintln!("The version of the honggfuzz library dependency ({0}) and the version of the `cargo-hfuzz` executable ({1}) do not match.\n\
If updating both by running `cargo update` and `cargo install honggfuzz` does not work, you can either:\n\
- change the dependency in `Cargo.toml` to `honggfuzz = \"={1}\"`\n\
- or run `cargo install honggfuzz --version {0}`",
VERSION, honggfuzz_build_version);
process::exit(1);
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); let honggfuzz_target = Path::new(&env::var("CRATE_ROOT").unwrap()) .join(honggfuzz_target);
let status = Command::new(GNU_MAKE)
.args(&["-C", "honggfuzz", "clean"])
.status()
.unwrap_or_else(|_e| panic!("failed to run \"{} -C honggfuzz clean\"", GNU_MAKE));
assert!(status.success());
let status = Command::new(GNU_MAKE)
.args(&["-C", "honggfuzz", "honggfuzz", "libhfuzz/libhfuzz.a", "libhfcommon/libhfcommon.a"])
.status()
.unwrap_or_else(|_e| panic!("failed to run \"{} -C honggfuzz hongfuzz libhfuzz/libhfuzz.a libhfcommon/libhfcommon.a\"", GNU_MAKE));
assert!(status.success());
fs::copy("honggfuzz/libhfuzz/libhfuzz.a", out_dir.join("libhfuzz.a")).unwrap();
fs::copy(
"honggfuzz/libhfcommon/libhfcommon.a",
out_dir.join("libhfcommon.a"),
)
.unwrap();
fs::copy("honggfuzz/honggfuzz", honggfuzz_target.join("honggfuzz")).unwrap();
println!("cargo:rustc-link-lib=static={}", "hfuzz");
println!("cargo:rustc-link-lib=static={}", "hfcommon");
println!("cargo:rustc-link-search=native={}", out_dir.display());
}