| 1 | // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause |
| 2 | // SPDX-FileCopyrightText: 2022-2023 Linaro Ltd. |
| 3 | // SPDX-FileCopyrightText: 2022 Viresh Kumar <[email protected]> |
| 4 | // SPDX-FileCopyrightText: 2023 Erik Schilling <[email protected]> |
| 5 | |
| 6 | use std::env; |
| 7 | use std::path::PathBuf; |
| 8 | |
| 9 | fn main() { |
| 10 | // Probe dependency info based on the metadata from Cargo.toml |
| 11 | // (and potentially other sources like environment, pkg-config, ...) |
| 12 | // https://docs.rs/system-deps/latest/system_deps/#overriding-build-flags |
| 13 | let libs = system_deps::Config::new().probe().unwrap(); |
| 14 | |
| 15 | // Tell cargo to invalidate the built crate whenever following files change |
| 16 | println!("cargo:rerun-if-changed=wrapper.h"); |
| 17 | |
| 18 | // The bindgen::Builder is the main entry point |
| 19 | // to bindgen, and lets you build up options for |
| 20 | // the resulting bindings. |
| 21 | let mut builder = bindgen::Builder::default() |
| 22 | // The input header we would like to generate |
| 23 | // bindings for. |
| 24 | .header("wrapper.h") |
| 25 | // Tell cargo to invalidate the built crate whenever any of the |
| 26 | // included header files changed. |
| 27 | .parse_callbacks(Box::new(bindgen::CargoCallbacks)); |
| 28 | |
| 29 | // Inform bindgen about the include paths identified by system_deps. |
| 30 | for (_name, lib) in libs { |
| 31 | for include_path in lib.include_paths { |
| 32 | builder = builder.clang_arg("-I").clang_arg( |
| 33 | include_path |
| 34 | .to_str() |
| 35 | .expect("Failed to convert include_path to &str!"), |
| 36 | ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Finish the builder and generate the bindings. |
| 41 | let bindings = builder |
| 42 | .generate() |
| 43 | // Unwrap the Result and panic on failure. |
| 44 | .expect("Unable to generate bindings"); |
| 45 | |
| 46 | // Write the bindings to the $OUT_DIR/bindings.rs file. |
| 47 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 48 | bindings |
| 49 | .write_to_file(out_path.join("bindings.rs")) |
| 50 | .expect("Couldn't write bindings!"); |
| 51 | } |