]> git.lizzy.rs Git - rust.git/blob - src/tools/error_index_generator/build.rs
Rollup merge of #67059 - TommasoBianchi:dropck_fix_pr, r=pnkfelix
[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
12     let error_codes_path = "../../../src/librustc_error_codes/error_codes.rs";
13
14     println!("cargo:rerun-if-changed={}", error_codes_path);
15     let file = fs::read_to_string(error_codes_path).unwrap()
16                   .replace(": include_str!(\"./error_codes/", ": include_str!(\"./");
17     let contents = format!("(|| {{\n{}\n}})()", file);
18     fs::write(&out_dir.join("all_error_codes.rs"), &contents).unwrap();
19
20     // We copy the md files as well to the target directory.
21     for entry in WalkDir::new("../../../src/librustc_error_codes/error_codes") {
22         let entry = entry.unwrap();
23         match entry.path().extension() {
24             Some(s) if s == "md" => {}
25             _ => continue,
26         }
27         println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
28         let md_content = fs::read_to_string(entry.path()).unwrap();
29         fs::write(&out_dir.join(entry.file_name()), &md_content).unwrap();
30     }
31
32     let mut all = String::new();
33     all.push_str(r###"
34 fn register_all() -> Vec<(&'static str, Option<&'static str>)> {
35     let mut long_codes: Vec<(&'static str, Option<&'static str>)> = Vec::new();
36     macro_rules! register_diagnostics {
37         ($($ecode:ident: $message:expr,)*) => (
38             register_diagnostics!{$($ecode:$message,)* ;}
39         );
40
41         ($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => (
42             $(
43                 {long_codes.extend([
44                     (stringify!($ecode), Some($message)),
45                 ].iter());}
46             )*
47             $(
48                 {long_codes.extend([
49                     stringify!($code),
50                 ].iter().cloned().map(|s| (s, None)).collect::<Vec<_>>());}
51             )*
52         )
53     }
54 "###);
55     all.push_str(r#"include!(concat!(env!("OUT_DIR"), "/all_error_codes.rs"));"#);
56     all.push_str("\nlong_codes\n");
57     all.push_str("}\n");
58
59     fs::write(&dest, all).unwrap();
60 }