]> git.lizzy.rs Git - rust.git/blob - src/tools/error_index_generator/build.rs
Auto merge of #65202 - pietroalbini:scriptify-ci-config, r=alexcrichton
[rust.git] / src / tools / error_index_generator / build.rs
1 use walkdir::WalkDir;
2 use std::path::PathBuf;
3 use std::{env, fs};
4
5 fn main() {
6     // The src directory (we are in src/tools/error_index_generator)
7     // Note that we could skip one of the .. but this ensures we at least loosely find the right
8     // directory.
9     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
10     let dest = out_dir.join("error_codes.rs");
11     let mut idx = 0;
12     for entry in WalkDir::new("../../../src") {
13         let entry = entry.unwrap();
14         if entry.file_name() == "error_codes.rs" {
15             println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
16             let file = fs::read_to_string(entry.path()).unwrap()
17                 .replace("syntax::register_diagnostics!", "register_diagnostics!");
18             let contents = format!("(|| {{\n{}\n}})()", file);
19
20             fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();
21
22             idx += 1;
23         }
24     }
25
26     let mut all = String::new();
27     all.push_str(r###"
28 fn register_all() -> Vec<(&'static str, Option<&'static str>)> {
29     let mut long_codes: Vec<(&'static str, Option<&'static str>)> = Vec::new();
30     macro_rules! register_diagnostics {
31         ($($ecode:ident: $message:expr,)*) => (
32             register_diagnostics!{$($ecode:$message,)* ;}
33         );
34
35         ($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => (
36             $(
37                 {long_codes.extend([
38                     (stringify!($ecode), Some($message)),
39                 ].iter());}
40             )*
41             $(
42                 {long_codes.extend([
43                     stringify!($code),
44                 ].iter().cloned().map(|s| (s, None)).collect::<Vec<_>>());}
45             )*
46         )
47     }
48 "###);
49     for idx in 0..idx {
50         all.push_str(&format!(r#"include!(concat!(env!("OUT_DIR"), "/error_{}.rs"));"#, idx));
51         all.push_str("\n");
52     }
53     all.push_str("\nlong_codes\n");
54     all.push_str("}\n");
55
56     fs::write(&dest, all).unwrap();
57 }