extern crate bindgen; use std::fs; use std::env; use std::path::PathBuf; use std::process::Command; fn main() { let src = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap()); let dest = PathBuf::from(&env::var("OUT_DIR").unwrap()); let build = dest.join("build"); let _ = fs::create_dir(&build); let mut cmd = Command::new("cmake"); cmd .arg(&src.join("libftdi")) .arg("-DCFLAGS=-fPIC") .arg("-DSTATICLIBS=ON") .arg("-DBUILD_TESTS=OFF") .arg("-DDOCUMENTATION=OFF") .arg("-DEXAMPLES=OFF") .arg("-DFTDI_EEPROM=OFF") .arg("-DPYTHON_BINDINGS=OFF") .arg("-DLINK_PYTHON_LIBRARY=OFF") .arg(&format!("-DCMAKE_INSTALL_PREFIX={}", dest.display())) .env("CFLAGS", "-fPIC") .current_dir(&build); run(&mut cmd); run(Command::new("cmake") .arg("--build").arg(".") .arg("--target").arg("install") .current_dir(&build)); println!("cargo:root={}", dest.display()); // the static library isn't installed println!("cargo:rustc-flags=-l static=ftdi1"); println!("cargo:rustc-flags=-L {}", dest.join("lib").display()); let header = dest.join("include").join("libftdi1").join("ftdi.h"); // Tell cargo to invalidate the built crate whenever the wrapper changes println!("cargo:rerun-if-changed={}", header.display()); // The bindgen::Builder is the main entry point // to bindgen, and lets you build up options for // the resulting bindings. let bindings = bindgen::Builder::default() // The input header we would like to generate // bindings for. .header(header.to_str().unwrap()) // Tell cargo to invalidate the built crate whenever any of the // included header files changed. .parse_callbacks(Box::new(bindgen::CargoCallbacks)) // Finish the builder and generate the bindings. .derive_default(true) .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: false, }) .bitfield_enum("ftdi_mpsse_mode") .generate() // Unwrap the Result and panic on failure. .expect("Unable to generate bindings"); // Write the bindings to the $OUT_DIR/bindings.rs file. let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings .write_to_file(out_path.join("bindings.rs")) .expect("Couldn't write bindings!"); } fn run(cmd: &mut Command) { let status = match cmd.status() { Ok(status) => status, Err(ref e) => panic!("Failed to execute command: {}", e), }; if !status.success() { panic!("Command did not execute successfully: exit {}", status); } }