1 extern crate bindgen; 2 extern crate pkg_config; 3 4 use std::path::PathBuf; 5 use std::process::Command; 6 7 fn main() { 8 let out_dir = PathBuf::from(env("OUT_DIR").unwrap()); 9 let statik = env("CARGO_FEATURE_STATIC").is_some(); 10 let system = env("CARGO_FEATURE_SYSTEM").is_some(); 11 12 let ceed_pc = if system { 13 "ceed".to_string() 14 } else { 15 // Install libceed.a or libceed.so to $OUT_DIR/lib 16 let makeflags = env("CARGO_MAKEFLAGS").unwrap(); 17 let mut make = Command::new("make"); 18 make.arg("install") 19 .arg(format!("prefix={}", out_dir.to_string_lossy())) 20 .arg(format!( 21 "OBJDIR={}", 22 out_dir.join("build").to_string_lossy() 23 )) 24 .arg(format!( 25 "LIBDIR={}", 26 out_dir.join("build").join("lib").to_string_lossy() 27 )) 28 .env("MAKEFLAGS", makeflags) 29 .current_dir("c-src"); 30 if statik { 31 make.arg("STATIC=1"); 32 } 33 run(&mut make); 34 out_dir 35 .join("lib") 36 .join("pkgconfig") 37 .join("ceed.pc") 38 .to_string_lossy() 39 .into_owned() 40 }; 41 pkg_config::Config::new() 42 .statik(statik) 43 .atleast_version("0.8") 44 .probe(&ceed_pc) 45 .unwrap(); 46 47 // Tell cargo to invalidate the built crate whenever the wrapper changes 48 println!("cargo:rerun-if-changed=c-src/include/ceed/ceed.h"); 49 println!("cargo:rerun-if-changed=c-src/Makefile"); 50 println!("cargo:rerun-if-changed=c-src/config.mk"); 51 52 // The bindgen::Builder is the main entry point 53 // to bindgen, and lets you build up options for 54 // the resulting bindings. 55 let bindings = bindgen::Builder::default() 56 // The input header we would like to generate 57 // bindings for. 58 .header("c-src/include/ceed/ceed.h") 59 .allowlist_function("Ceed.*") 60 .allowlist_type("Ceed.*") 61 .allowlist_var("Ceed.*") 62 .allowlist_var("CEED_.*") 63 // Don't chase recursive definitions of these system types 64 .opaque_type("FILE") 65 .opaque_type("va_list") 66 // Accessing directly here, but perhaps should use libc crate 67 .allowlist_function("fclose") 68 .allowlist_function("open_memstream") 69 // Tell cargo to not mangle the function names 70 .trust_clang_mangling(false) 71 // Tell cargo to invalidate the built crate whenever any of the 72 // included header files changed. 73 .parse_callbacks(Box::new(bindgen::CargoCallbacks)) 74 // Finish the builder and generate the bindings. 75 .generate() 76 // Unwrap the Result and panic on failure. 77 .expect("Unable to generate bindings"); 78 79 // Write the bindings to the $OUT_DIR/bindings.rs file. 80 bindings 81 .write_to_file(out_dir.join("bindings.rs")) 82 .expect("Couldn't write bindings!"); 83 } 84 85 fn env(k: &str) -> Option<String> { 86 std::env::var(k).ok() 87 } 88 89 fn run(command: &mut Command) { 90 println!("Running: `{:?}`", command); 91 assert!(command.status().unwrap().success()); 92 } 93