extern crate bindgen;
extern crate cc;
use walkdir::WalkDir;
use std::env;
use std::path::PathBuf;
use std::process::Command;
fn main() {
Command::new("git")
.args(["submodule", "update", "--init", "--recursive"])
.status()
.expect("Failed to initialize/update submodules");
let submodule_path = "vendor/liblcrq";
let dst = PathBuf::from(env::var("OUT_DIR").unwrap());
if pkg_config::Config::new()
.atleast_version("0.2.4")
.probe("lcrq")
.is_ok()
{
} else {
let src = PathBuf::from(submodule_path);
for entry in WalkDir::new(&src).into_iter().filter_map(Result::ok) {
let src_path = entry.path();
let dst_path = dst.join(src_path.strip_prefix(&src).unwrap());
if src_path.is_dir() {
std::fs::create_dir_all(&dst_path).expect("Failed to create directory");
} else {
if src_path.is_symlink() {
continue;
}
std::fs::copy(src_path, &dst_path).expect("Failed to copy a file");
}
}
let include_for_env = format!(
"-I{}",
"-Wall -Wextra -pedantic -O3 -march=native -mpopcnt -ffast-math -funroll-loops -flto -DNDEBUG"
);
unsafe { env::set_var("CFLAGS", include_for_env) };
let status = Command::new("./configure")
.arg("--enable-native")
.current_dir(&dst)
.status()
.expect("Failed to find ./configure");
if !status.success() {
panic!("./configure failed to complete or run in {}", dst.display());
}
let status = Command::new("make")
.current_dir(&dst)
.status()
.expect("Failed to run make");
if !status.success() {
panic!("Failed to run make in {}", dst.display());
}
println!("cargo:rustc-link-search={}/src", dst.display());
println!("cargo:include={}/include", dst.display());
println!("cargo:rustc-link-lib=lcrq");
println!("cargo:rustc-env=LD_LIBRARY_PATH={}/src", dst.display());
}
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}/include", dst.display()))
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(dst.join("bindings.rs"))
.expect("Couldn't write bindings.rs!");
}