|
| 1 | +extern crate bindgen; |
| 2 | + |
| 3 | +use std::fmt::format; |
| 4 | +use std::fs; |
| 5 | +use std::env; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +use bindgen::CargoCallbacks; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + let rust_wrapper_path = env::current_dir().unwrap(); |
| 12 | + let libdir_path = rust_wrapper_path |
| 13 | + .join("..\\..\\lib") |
| 14 | + .canonicalize() |
| 15 | + .expect("cannot canonicalize lib path"); |
| 16 | + |
| 17 | + // This is the path to the `c` headers file. |
| 18 | + let headers_path = libdir_path.join("include\\public"); |
| 19 | + let headers_path_str = headers_path.to_str().expect("Path is not a valid string"); |
| 20 | + let c_header_path = libdir_path.join("include\\public\\mat.h"); |
| 21 | + let c_header_path_str = c_header_path.to_str().expect("Path is not a valid string"); |
| 22 | + |
| 23 | + // This is the path to the intermediate object file for our library. |
| 24 | + let obj_path = libdir_path.join("mat.o"); |
| 25 | + // This is the path to the static library file. |
| 26 | + let lib_path = libdir_path.join("mat.a"); |
| 27 | + |
| 28 | + // Tell cargo to look for shared libraries in the specified directory |
| 29 | + // println!("cargo:rustc-link-search={}", libdir_path.to_str().unwrap()); |
| 30 | + |
| 31 | + // Tell cargo to tell rustc to link our `hello` library. Cargo will |
| 32 | + // automatically know it must look for a `libmat.a` file. |
| 33 | + // println!("cargo:rustc-link-lib=mat"); |
| 34 | + |
| 35 | + // Tell cargo to invalidate the built crate whenever the header changes. |
| 36 | + println!("cargo:rerun-if-changed={}", headers_path_str); |
| 37 | + |
| 38 | + // // Run `clang` to compile the `mat.c` file into a `mat.o` object file. |
| 39 | + // // Unwrap if it is not possible to spawn the process. |
| 40 | + // if !std::process::Command::new("clang") |
| 41 | + // .arg("-c") |
| 42 | + // .arg("-o") |
| 43 | + // .arg(&obj_path) |
| 44 | + // .arg(libdir_path.join("hello.c")) |
| 45 | + // .output() |
| 46 | + // .expect("could not spawn `clang`") |
| 47 | + // .status |
| 48 | + // .success() |
| 49 | + // { |
| 50 | + // // Panic if the command was not successful. |
| 51 | + // panic!("could not compile object file"); |
| 52 | + // } |
| 53 | + |
| 54 | + // // Run `ar` to generate the `libhello.a` file from the `hello.o` file. |
| 55 | + // // Unwrap if it is not possible to spawn the process. |
| 56 | + // if !std::process::Command::new("ar") |
| 57 | + // .arg("rcs") |
| 58 | + // .arg(lib_path) |
| 59 | + // .arg(obj_path) |
| 60 | + // .output() |
| 61 | + // .expect("could not spawn `ar`") |
| 62 | + // .status |
| 63 | + // .success() |
| 64 | + // { |
| 65 | + // // Panic if the command was not successful. |
| 66 | + // panic!("could not emit library file"); |
| 67 | + // } |
| 68 | + |
| 69 | + // The bindgen::Builder is the main entry point |
| 70 | + // to bindgen, and lets you build up options for |
| 71 | + // the resulting bindings. |
| 72 | + let bindings = bindgen::Builder::default() |
| 73 | + // The input header we would like to generate |
| 74 | + // bindings for. |
| 75 | + .header(c_header_path_str) |
| 76 | + .clang_arg(format!("-I{}", headers_path_str)) |
| 77 | + // Tell cargo to invalidate the built crate whenever any of the |
| 78 | + // included header files changed. |
| 79 | + .parse_callbacks(Box::new(CargoCallbacks)) |
| 80 | + // Finish the builder and generate the bindings. |
| 81 | + .generate() |
| 82 | + // Unwrap the Result and panic on failure. |
| 83 | + .expect("Unable to generate bindings"); |
| 84 | + |
| 85 | + // Write the bindings to the $OUT_DIR/bindings.rs file. |
| 86 | + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs"); |
| 87 | + bindings |
| 88 | + .write_to_file(out_path) |
| 89 | + .expect("Couldn't write bindings!"); |
| 90 | +} |
0 commit comments