use std::{
io::{ Error, Write },
process::{ Command, Stdio },
path::PathBuf,
};
use bindgen::{ Builder, CargoCallbacks, MacroTypeVariation };
fn main() -> Result<(), Error> {
let mut header = String::new();
let mut process = Command::new("cpp")
.arg("-M")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
if let Some(mut child_stdin) = process.stdin.take() {
child_stdin.write_all("#include <sysexits.h>\n".as_bytes())?;
}
let output = process.wait_with_output()?.stdout;
let headers = String::from_utf8(output).map_err(|e| {
Error::other(e.to_string())
})?;
for h in headers.split(' ') {
if h.contains("sysexits.h") {
header = h.trim().to_string();
} else { header = "src/sysexits.h".to_string() }
}
let bindings = Builder::default()
.use_core()
.default_macro_constant_type(MacroTypeVariation::Signed)
.header(header)
.parse_callbacks(Box::new(CargoCallbacks::new()))
.generate()
.map_err(|e| Error::other(e.to_string()))?;
bindings.write_to_file(PathBuf::from("src/").join("lib.rs"))?;
Ok(())
}