forked from rust-lang/rust-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
82 lines (70 loc) · 2.48 KB
/
build.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
mod target {
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
pub fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut dst =
File::create(Path::new(&out_dir).join("host-target.txt")).unwrap();
dst.write_all(env::var("TARGET").unwrap().as_bytes())
.unwrap();
}
}
mod testgen {
use std::char;
use std::env;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
pub fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut dst =
File::create(Path::new(&out_dir).join("tests.rs")).unwrap();
let manifest_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let headers_dir = manifest_dir.join("tests").join("headers");
let headers = match fs::read_dir(headers_dir) {
Ok(dir) => dir,
// We may not have headers directory after packaging.
Err(..) => return,
};
let entries =
headers.map(|result| result.expect("Couldn't read header file"));
println!("cargo:rerun-if-changed=tests/headers");
for entry in entries {
match entry.path().extension().and_then(OsStr::to_str) {
Some("h") | Some("hpp") => {
let func = entry
.file_name()
.to_str()
.unwrap()
.replace(|c| !char::is_alphanumeric(c), "_")
.replace("__", "_")
.to_lowercase();
writeln!(
dst,
"test_header!(header_{}, {:?});",
func,
entry.path(),
)
.unwrap();
}
_ => {}
}
}
dst.flush().unwrap();
}
}
fn main() {
target::main();
testgen::main();
// On behalf of clang_sys, rebuild ourselves if important configuration
// variables change, to ensure that bindings get rebuilt if the
// underlying libclang changes.
println!("cargo:rerun-if-env-changed=LLVM_CONFIG_PATH");
println!("cargo:rerun-if-env-changed=LIBCLANG_PATH");
println!("cargo:rerun-if-env-changed=LIBCLANG_STATIC_PATH");
println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS");
}