1extern crate bindgen;
2extern crate clap;
3#[cfg(feature = "logging")]
4extern crate env_logger;
5#[cfg(feature = "logging")]
6extern crate log;
7
8use std::env;
9
10mod options;
11use crate::options::builder_from_flags;
12
13#[cfg(feature = "logging")]
14fn clang_version_check() {
15    let version = bindgen::clang_version();
16    let expected_version = if cfg!(feature = "testing_only_libclang_9") {
17        Some((9, 0))
18    } else if cfg!(feature = "testing_only_libclang_5") {
19        Some((5, 0))
20    } else {
21        None
22    };
23
24    log::info!(
25        "Clang Version: {}, parsed: {:?}",
26        version.full,
27        version.parsed
28    );
29
30    if expected_version.is_some() {
31        // assert_eq!(version.parsed, version.parsed);
32    }
33}
34
35pub fn main() {
36    #[cfg(feature = "logging")]
37    env_logger::init();
38
39    match builder_from_flags(env::args()) {
40        Ok((builder, output, verbose)) => {
41            #[cfg(feature = "logging")]
42            clang_version_check();
43
44            std::panic::set_hook(Box::new(move |info| {
45                if verbose {
46                    print_verbose_err()
47                }
48                println!("{}", info);
49            }));
50
51            let bindings =
52                builder.generate().expect("Unable to generate bindings");
53
54            let _ = std::panic::take_hook();
55
56            bindings.write(output).expect("Unable to write output");
57        }
58        Err(error) => {
59            println!("{}", error);
60            std::process::exit(1);
61        }
62    };
63}
64
65fn print_verbose_err() {
66    println!("Bindgen unexpectedly panicked");
67    println!(
68        "This may be caused by one of the known-unsupported \
69         things (https://rust-lang.github.io/rust-bindgen/cpp.html), \
70         please modify the bindgen flags to work around it as \
71         described in https://rust-lang.github.io/rust-bindgen/cpp.html"
72    );
73    println!(
74        "Otherwise, please file an issue at \
75         https://github.com/rust-lang/rust-bindgen/issues/new"
76    );
77}
78