use std::env;
use std::path::{Path, PathBuf};
fn main() {
let out_dir = env::var_os("OUT_DIR")
.map(PathBuf::from)
.expect("fts-sys: Environment variable 'OUT_DIR' was not defined.");
println!("cargo:root={}", out_dir.to_str().unwrap());
if !target_os_is_supported() {
return;
}
generate_bindings(&out_dir)
}
fn target_os_is_supported() -> bool {
match get_target_os().as_deref() {
None => false,
Some("linux") | Some("android") | Some("androideabi") | Some("dragonfly")
| Some("freebsd") | Some("netbsd") | Some("openbsd") => {
true }
_ => false, }
}
fn get_target_os() -> Option<String> {
let target =
env::var("TARGET").expect("selinux-sys: Environment variable 'TARGET' was not defined.");
let components: Vec<_> = target.split('-').collect();
let os_index = match components.len() {
2 => {
if components[1] == "none" {
return None; }
1
}
3 | 4 => {
if components[1] == "none" || components[2] == "none" {
return None; }
2
}
_ => panic!("Unrecognized target triplet '{target}'"),
};
Some(String::from(components[os_index]))
}
fn generate_bindings(out_dir: &Path) {
let bindings = bindgen::Builder::default()
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
.size_t_is_usize(true)
.derive_debug(true)
.derive_copy(true)
.impl_debug(true)
.allowlist_function("fts_(open|read|children|set|close)")
.allowlist_type("FTSENT")
.blocklist_type("__.+")
.blocklist_type("(FTS|stat|timespec|dev_t|ino_t|nlink_t)")
.allowlist_var("FTS_.+")
.header("src/fts-sys.h")
.generate()
.expect("fts-sys: Failed to generate Rust bindings for 'fts.h'.");
bindings
.write_to_file(out_dir.join("fts-sys.rs"))
.expect("fts-sys: Failed to write 'fts-sys.rs'.")
}