1 use std::char;
2 use std::env;
3 use std::ffi::OsStr;
4 use std::fs::{self, File};
5 use std::io::Write;
6 use std::path::{Path, PathBuf};
7
main()8 pub fn main() {
9 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
10 let mut dst = File::create(Path::new(&out_dir).join("tests.rs")).unwrap();
11
12 let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
13 let headers_dir = manifest_dir.join("tests").join("headers");
14
15 let headers = match fs::read_dir(headers_dir) {
16 Ok(dir) => dir,
17 // We may not have headers directory after packaging.
18 Err(..) => return,
19 };
20
21 let entries =
22 headers.map(|result| result.expect("Couldn't read header file"));
23
24 println!("cargo:rerun-if-changed=tests/headers");
25
26 for entry in entries {
27 match entry.path().extension().and_then(OsStr::to_str) {
28 Some("h") | Some("hpp") => {
29 let func = entry
30 .file_name()
31 .to_str()
32 .unwrap()
33 .replace(|c| !char::is_alphanumeric(c), "_")
34 .replace("__", "_")
35 .to_lowercase();
36 writeln!(
37 dst,
38 "test_header!(header_{}, {:?});",
39 func,
40 entry.path(),
41 )
42 .unwrap();
43 }
44 _ => {}
45 }
46 }
47
48 dst.flush().unwrap();
49 }
50