|
| 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: &'static [&'static str] = |
| 12 | + &["libclang-3.8", "libclang-3.9", "libclang-4"]; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + println!("cargo:rerun-if-changed=build.rs"); |
| 16 | + |
| 17 | + let mut test_string = String::new(); |
| 18 | + |
| 19 | + for dir in LIBCLANG_VERSION_DIRS { |
| 20 | + let dir = Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap()) |
| 21 | + .join("tests") |
| 22 | + .join(dir); |
| 23 | + |
| 24 | + println!("cargo:rerun-if-changed={}", dir.display()); |
| 25 | + |
| 26 | + for entry in fs::read_dir(dir).unwrap() { |
| 27 | + let entry = entry.unwrap(); |
| 28 | + let path = entry.path(); |
| 29 | + let path = path.canonicalize().unwrap_or_else(|_| path.into()); |
| 30 | + if path.extension().map(|e| e.to_string_lossy()) != Some("rs".into()) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + println!("cargo:rerun-if-changed={}", path.display()); |
| 35 | + |
| 36 | + let module_name: String = path.display() |
| 37 | + .to_string() |
| 38 | + .chars() |
| 39 | + .map(|c| match c { |
| 40 | + 'a'...'z' | 'A'...'Z' | '0'...'9' => c, |
| 41 | + _ => '_', |
| 42 | + }) |
| 43 | + .collect(); |
| 44 | + |
| 45 | + test_string.push_str(&format!( |
| 46 | + r###" |
| 47 | +#[path = "{}"] |
| 48 | +mod {}; |
| 49 | +"###, |
| 50 | + path.display(), |
| 51 | + module_name, |
| 52 | + )); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let out_path = Path::new(&env::var_os("OUT_DIR").unwrap()) |
| 57 | + .join("libclang_version_specific_generated_tests.rs"); |
| 58 | + let mut test_file = fs::File::create(out_path).unwrap(); |
| 59 | + test_file.write_all(test_string.as_bytes()).unwrap(); |
| 60 | +} |
0 commit comments