• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 extern crate bindgen;
2 
3 use pkg_config::Config;
4 use std::env;
5 use std::path::PathBuf;
6 
main()7 fn main() {
8     // Re-run build if any of these change
9     println!("cargo:rerun-if-changed=bindings/wrapper.hpp");
10     println!("cargo:rerun-if-changed=build.rs");
11 
12     // We need to configure libchrome and libmodp_b64 settings as well
13     let libchrome = Config::new().probe("libchrome").unwrap();
14     let libchrome_paths = libchrome
15         .include_paths
16         .iter()
17         .map(|p| format!("-I{}", p.to_str().unwrap()))
18         .collect::<Vec<String>>();
19 
20     let search_root = env::var("CXX_ROOT_PATH").unwrap();
21     let paths = vec![
22         "/system/",
23         "/system/btcore",
24         "/system/include",
25         "/system/include/hardware",
26         "/system/types",
27     ];
28 
29     let bt_searches =
30         paths.iter().map(|tail| format!("-I{}{}", search_root, tail)).collect::<Vec<String>>();
31 
32     // Also re-run the build if anything in the C++ build changes
33     for path in bt_searches.iter() {
34         println!("cargo:rerun-if-changed={}", path);
35     }
36 
37     // "-x" and "c++" must be separate due to a bug
38     let clang_args: Vec<&str> = vec!["-x", "c++", "-std=c++17"];
39 
40     // The bindgen::Builder is the main entry point
41     // to bindgen, and lets you build up options for
42     // the resulting bindings.
43     let bindings = bindgen::Builder::default()
44         .clang_args(bt_searches)
45         .clang_args(libchrome_paths)
46         .clang_args(clang_args)
47         .enable_cxx_namespaces()
48         .size_t_is_usize(true)
49         .allowlist_type("(bt_|bthh_|btgatt_|btsdp|bluetooth_sdp).*")
50         .allowlist_function("(bt_|bthh_|btgatt_|btsdp).*")
51         .allowlist_function("hal_util_.*")
52         // We must opaque out std:: in order to prevent bindgen from choking
53         .opaque_type("std::.*")
54         // Whitelist std::string though because we use it a lot
55         .allowlist_type("std::string")
56         .rustfmt_bindings(true)
57         .derive_debug(true)
58         .derive_partialeq(true)
59         .derive_eq(true)
60         .derive_default(true)
61         .header("bindings/wrapper.hpp")
62         .generate()
63         .expect("Unable to generate bindings");
64 
65     // Write the bindings to the $OUT_DIR/bindings.rs file.
66     let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
67     bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings!");
68 }
69