summaryrefslogtreecommitdiff
path: root/system/gd/rust/topshim/build.rs
blob: 56fac779052b96c301cd3fa1bf8f8110dcd0b434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
extern crate bindgen;

use pkg_config::Config;
use std::env;
use std::path::PathBuf;

fn main() {
    // Re-run build if any of these change
    println!("cargo:rerun-if-changed=bindings/wrapper.hpp");
    println!("cargo:rerun-if-changed=build.rs");

    // We need to configure libchrome and libmodp_b64 settings as well
    let libchrome = Config::new().probe("libchrome").unwrap();
    let libchrome_paths = libchrome
        .include_paths
        .iter()
        .map(|p| format!("-I{}", p.to_str().unwrap()))
        .collect::<Vec<String>>();

    let search_root = env::var("CXX_ROOT_PATH").unwrap();
    let paths = vec![
        "/system/",
        "/system/btcore",
        "/system/include",
        "/system/include/hardware",
        "/system/types",
    ];

    let bt_searches =
        paths.iter().map(|tail| format!("-I{}{}", search_root, tail)).collect::<Vec<String>>();

    // Also re-run the build if anything in the C++ build changes
    for path in bt_searches.iter() {
        println!("cargo:rerun-if-changed={}", path);
    }

    // "-x" and "c++" must be separate due to a bug
    let clang_args: Vec<&str> = vec!["-x", "c++", "-std=c++17"];

    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let bindings = bindgen::Builder::default()
        .clang_args(bt_searches)
        .clang_args(libchrome_paths)
        .clang_args(clang_args)
        .enable_cxx_namespaces()
        .size_t_is_usize(true)
        .allowlist_type("(bt_|bthh_|btgatt_|btsdp|bluetooth_sdp).*")
        .allowlist_function("(bt_|bthh_|btgatt_|btsdp).*")
        .allowlist_function("hal_util_.*")
        // We must opaque out std:: in order to prevent bindgen from choking
        .opaque_type("std::.*")
        // Whitelist std::string though because we use it a lot
        .allowlist_type("std::string")
        .rustfmt_bindings(true)
        .derive_debug(true)
        .derive_partialeq(true)
        .derive_eq(true)
        .derive_default(true)
        .header("bindings/wrapper.hpp")
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings!");
}