summaryrefslogtreecommitdiff
path: root/rustlibd/build.rs.in
blob: 9d5741e46130cbb899b2ad627de2872b6fe3730a (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
extern crate bindgen;
// extern crate gcc;

use std::env;
use std::path::PathBuf;

use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
use std::sync::{Arc, RwLock};

use std::collections::HashSet;

//
// This complexity is need to ignore a #define in netdb.h that is shadowing an
// anonymous enum in netinet/in.h
//
#[derive(Debug)]
struct MacroCallback {
    macros: Arc<RwLock<HashSet<String>>>,
}

impl ParseCallbacks for MacroCallback {
    fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
        self.macros.write().unwrap().insert(name.into());

        if name == "IPPORT_RESERVED" {
            return MacroParsingBehavior::Ignore;
        }

        MacroParsingBehavior::Default
    }
}

fn main() {
    // // Tell cargo to look for shared libraries in the specified directory
    // println!("cargo:rustc-link-search=@abs_top_builddir@/lib/.libs/libfrr.so");
    // // Tell cargo to tell rustc to link libfrr shared library.
    // println!("cargo:rustc-link-lib=frr");

    // Tell cargo to invalidate the built crate whenever the wrapper changes
    // println!("cargo:rerun-if-changed=@abs_srcdir@/wrapper.h");
    println!("cargo:rerun-if-changed=wrapper.h");

    let macros = Arc::new(RwLock::new(HashSet::new()));

    // 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(&[
            "-I@abs_top_builddir@", // need to get this dynamically
            "-I@abs_top_srcdir@",
            "-I@abs_top_srcdir@/lib",
            "-DHAVE_CONFIG_H",
            "-D_ATOMIC_WANT_TYPEDEFS",
        ])
        // The input header we would like to generate
        // bindings for.
        // .header("@abs_srcdir@/wrapper.h")
        .header("wrapper.h")
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .parse_callbacks(Box::new(MacroCallback {
            macros: macros.clone(),
        }))
        // Finish the builder and generate the bindings.
        .blocklist_type("IPPORT_.*")
        .blocklist_type("IPPORT_RESERVED")
        // avoid creating bindings for things with u128 which doesn't yet have a
        // stable FFI, for now.

        .blocklist_function("lyd_eval_xpath4")
        .blocklist_function("q[efg]cvt.*")
        .blocklist_function("strto.*")
        .blocklist_function("strfrom.*")

        .generate()
        // Unwrap the Result and panic on failure.
        .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!");

    // cc::Build::new()
    //     .include("@abs_top_builddir@")
    //     .include("@abs_top_srcdir@")
    //     .include("@abs_top_srcdir@/lib")
    //     .define("HAVE_CONFIG_H", None)
    //     .file("@abs_srcdir@/rustlib_main.c")
    //     .compile("c_main_code");
}