xref: /libCEED/rust/libceed-sys/build.rs (revision 007d01f748b27d510688bbd5632200eed7b6bd14)
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             .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.9")
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     if Path::new("c-src/config.mk").is_file() {
51         println!("cargo:rerun-if-changed=c-src/config.mk");
52     }
53 
54     // The bindgen::Builder is the main entry point
55     // to bindgen, and lets you build up options for
56     // the resulting bindings.
57     let bindings = bindgen::Builder::default()
58         // The input header we would like to generate
59         // bindings for.
60         .header("c-src/include/ceed/ceed.h")
61         .allowlist_function("Ceed.*")
62         .allowlist_type("Ceed.*")
63         .allowlist_var("Ceed.*")
64         .allowlist_var("CEED_.*")
65         // Don't chase recursive definitions of these system types
66         .opaque_type("FILE")
67         .opaque_type("va_list")
68         // Accessing directly here, but perhaps should use libc crate
69         .allowlist_function("fclose")
70         .allowlist_function("open_memstream")
71         // Tell cargo to not mangle the function names
72         .trust_clang_mangling(false)
73         // Tell cargo to invalidate the built crate whenever any of the
74         // included header files changed.
75         .parse_callbacks(Box::new(bindgen::CargoCallbacks))
76         // Finish the builder and generate the bindings.
77         .generate()
78         // Unwrap the Result and panic on failure.
79         .expect("Unable to generate bindings");
80 
81     // Write the bindings to the $OUT_DIR/bindings.rs file.
82     bindings
83         .write_to_file(out_dir.join("bindings.rs"))
84         .expect("Couldn't write bindings!");
85 }
86 
87 fn env(k: &str) -> Option<String> {
88     std::env::var(k).ok()
89 }
90 
91 fn run(command: &mut Command) {
92     println!("Running: `{:?}`", command);
93     assert!(command.status().unwrap().success());
94 }
95