-
Notifications
You must be signed in to change notification settings - Fork 717
/
Copy pathbindgen.rs
executable file
·87 lines (76 loc) · 2.48 KB
/
bindgen.rs
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
#![crate_name = "bindgen"]
#![crate_type = "bin"]
extern crate bindgen;
extern crate env_logger;
#[macro_use]
extern crate log;
extern crate clang_sys;
extern crate clap;
extern crate rustc_serialize;
use bindgen::clang_version;
use std::env;
mod options;
use options::builder_from_flags;
pub fn main() {
log::set_logger(|max_log_level| {
use env_logger::Logger;
let env_logger = Logger::new();
max_log_level.set(env_logger.filter());
Box::new(env_logger)
})
.expect("Failed to set logger.");
let mut bind_args: Vec<_> = env::args().collect();
let version = clang_version();
let expected_version = if cfg!(feature = "llvm_stable") {
(3, 8)
} else {
(3, 9)
};
info!("Clang Version: {}", version.full);
match version.parsed {
None => warn!("Couldn't parse libclang version"),
Some(version) if version != expected_version => {
error!("Using clang {:?}, expected {:?}",
version,
expected_version);
}
_ => {}
}
if let Some(clang) = clang_sys::support::Clang::find(None) {
let has_clang_args =
bind_args.iter().rposition(|arg| *arg == "--").is_some();
if !has_clang_args {
bind_args.push("--".to_owned());
}
// If --target is specified, assume caller knows what they're doing and
// don't mess with
// include paths for them
let has_target_arg = bind_args.iter()
.rposition(|arg| arg.starts_with("--target"))
.is_some();
if !has_target_arg {
// TODO: distinguish C and C++ paths? C++'s should be enough, I
// guess.
for path in clang.cpp_search_paths.into_iter() {
if let Ok(path) = path.into_os_string().into_string() {
bind_args.push("-isystem".to_owned());
bind_args.push(path);
}
}
}
}
match builder_from_flags(env::args()) {
Ok((builder, output)) => {
let mut bindings = builder.generate()
.expect("Unable to generate bindings");
bindings.write(output)
.expect("Unable to write output");
bindings.write_dummy_uses()
.expect("Unable to write dummy uses to file.");
}
Err(error) => {
println!("{}", error);
std::process::exit(1);
}
};
}