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