poulpy/spqlios/build.rs
spqlios/build.rsRaw
1use bindgen;
2use std::env;
3use std::fs;
4use std::path::absolute;
5use std::path::PathBuf;
6use std::time::SystemTime;
7
8fn main() {
9 // Path to the C header file
10 let header_paths = [
11 "lib/spqlios/coeffs/coeffs_arithmetic.h",
12 "lib/spqlios/arithmetic/vec_znx_arithmetic.h",
13 ];
14
15 let out_path: PathBuf = PathBuf::from(env::var("OUT_DIR").unwrap());
16 let bindings_file = out_path.join("bindings.rs");
17
18 let regenerate: bool = header_paths.iter().any(|header| {
19 let header_metadata: SystemTime = fs::metadata(header)
20 .and_then(|m| m.modified())
21 .unwrap_or(SystemTime::UNIX_EPOCH);
22 let bindings_metadata: SystemTime = fs::metadata(&bindings_file)
23 .and_then(|m| m.modified())
24 .unwrap_or(SystemTime::UNIX_EPOCH);
25 header_metadata > bindings_metadata
26 });
27
28 if regenerate {
29 // Generate the Rust bindings
30 let mut builder: bindgen::Builder = bindgen::Builder::default();
31 for header in header_paths {
32 builder = builder.header(header);
33 }
34
35 let bindings = builder
36 .generate_comments(false) // Optional: includes comments in bindings
37 .generate_inline_functions(true) // Optional: includes inline functions
38 .generate()
39 .expect("Unable to generate bindings");
40
41 // Write the bindings to the OUT_DIR
42 bindings
43 .write_to_file(&bindings_file)
44 .expect("Couldn't write bindings!");
45 }
46
47 println!(
48 "cargo:rustc-link-search=native={}",
49 absolute("./lib/build/spqlios").unwrap().to_str().unwrap()
50 );
51 println!("cargo:rustc-link-lib=static=spqlios"); //"cargo:rustc-link-lib=dylib=spqlios"
52}