• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Generate a module with a custom `#[path=...]` for each of the files in our
2 //! libclang version-specific test expectations so that they get their layout
3 //! tests run. We need to do this because cargo doesn't automatically detect
4 //! tests subdirectories.
5 
6 use std::env;
7 use std::fs;
8 use std::io::Write;
9 use std::path::Path;
10 
11 const LIBCLANG_VERSION_DIRS: &[&str] = &["libclang-5", "libclang-9"];
12 
main()13 fn main() {
14     println!("cargo:rerun-if-changed=build.rs");
15 
16     let mut test_string = String::new();
17 
18     for dir in LIBCLANG_VERSION_DIRS {
19         let dir = Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap())
20             .join("tests")
21             .join(dir);
22 
23         println!("cargo:rerun-if-changed={}", dir.display());
24 
25         for entry in fs::read_dir(dir).unwrap() {
26             let entry = entry.unwrap();
27             let path = entry.path();
28             let path = path.canonicalize().unwrap_or(path);
29             if path.extension().map(|e| e.to_string_lossy()) !=
30                 Some("rs".into())
31             {
32                 continue;
33             }
34 
35             println!("cargo:rerun-if-changed={}", path.display());
36 
37             let module_name: String = path
38                 .display()
39                 .to_string()
40                 .chars()
41                 .map(|c| match c {
42                     'a'..='z' | 'A'..='Z' | '0'..='9' => c,
43                     _ => '_',
44                 })
45                 .collect();
46 
47             test_string.push_str(&format!(
48                 r###"
49 #[path = "{}"]
50 mod {};
51 "###,
52                 path.display().to_string().replace('\\', "\\\\"),
53                 module_name,
54             ));
55         }
56     }
57 
58     let out_path = Path::new(&env::var_os("OUT_DIR").unwrap())
59         .join("libclang_version_specific_generated_tests.rs");
60     let mut test_file = fs::File::create(out_path).unwrap();
61     test_file.write_all(test_string.as_bytes()).unwrap();
62 }
63