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