1use std::collections::VecDeque;
8use std::num::NonZeroUsize;
9use std::path::PathBuf;
10use std::str::FromStr;
11use std::sync::atomic::{AtomicBool, Ordering};
12use std::thread::{self, ScopedJoinHandle, scope};
13use std::{env, process};
14
15use tidy::*;
16
17fn main() {
18 unsafe {
22 env::set_var("RUSTC_BOOTSTRAP", "1");
23 }
24
25 let root_path: PathBuf = env::args_os().nth(1).expect("need path to root of repo").into();
26 let cargo: PathBuf = env::args_os().nth(2).expect("need path to cargo").into();
27 let output_directory: PathBuf =
28 env::args_os().nth(3).expect("need path to output directory").into();
29 let concurrency: NonZeroUsize =
30 FromStr::from_str(&env::args().nth(4).expect("need concurrency"))
31 .expect("concurrency must be a number");
32
33 let root_manifest = root_path.join("Cargo.toml");
34 let src_path = root_path.join("src");
35 let tests_path = root_path.join("tests");
36 let library_path = root_path.join("library");
37 let compiler_path = root_path.join("compiler");
38 let librustdoc_path = src_path.join("librustdoc");
39 let tools_path = src_path.join("tools");
40 let crashes_path = tests_path.join("crashes");
41
42 let args: Vec<String> = env::args().skip(1).collect();
43 let (cfg_args, pos_args) = match args.iter().position(|arg| arg == "--") {
44 Some(pos) => (&args[..pos], &args[pos + 1..]),
45 None => (&args[..], [].as_slice()),
46 };
47 let verbose = cfg_args.iter().any(|s| *s == "--verbose");
48 let bless = cfg_args.iter().any(|s| *s == "--bless");
49 let extra_checks =
50 cfg_args.iter().find(|s| s.starts_with("--extra-checks=")).map(String::as_str);
51
52 let mut bad = false;
53 let ci_info = CiInfo::new(&mut bad);
54 let bad = std::sync::Arc::new(AtomicBool::new(bad));
55
56 let drain_handles = |handles: &mut VecDeque<ScopedJoinHandle<'_, ()>>| {
57 for i in (0..handles.len()).rev() {
59 if handles[i].is_finished() {
60 handles.swap_remove_back(i).unwrap().join().unwrap();
61 }
62 }
63
64 while handles.len() >= concurrency.get() {
65 handles.pop_front().unwrap().join().unwrap();
66 }
67 };
68
69 scope(|s| {
70 let mut handles: VecDeque<ScopedJoinHandle<'_, ()>> =
71 VecDeque::with_capacity(concurrency.get());
72
73 macro_rules! check {
74 ($p:ident) => {
75 check!(@ $p, name=format!("{}", stringify!($p)));
76 };
77 ($p:ident, $path:expr $(, $args:expr)* ) => {
78 let shortened = $path.strip_prefix(&root_path).unwrap();
79 let name = if shortened == std::path::Path::new("") {
80 format!("{} (.)", stringify!($p))
81 } else {
82 format!("{} ({})", stringify!($p), shortened.display())
83 };
84 check!(@ $p, name=name, $path $(,$args)*);
85 };
86 (@ $p:ident, name=$name:expr $(, $args:expr)* ) => {
87 drain_handles(&mut handles);
88
89 let handle = thread::Builder::new().name($name).spawn_scoped(s, || {
90 let mut flag = false;
91 $p::check($($args, )* &mut flag);
92 if (flag) {
93 bad.store(true, Ordering::Relaxed);
94 }
95 }).unwrap();
96 handles.push_back(handle);
97 }
98 }
99
100 check!(target_specific_tests, &tests_path);
101
102 check!(deps, &root_path, &cargo, bless);
104 check!(extdeps, &root_path);
105
106 check!(tests_placement, &root_path);
108 check!(tests_revision_unpaired_stdout_stderr, &tests_path);
109 check!(debug_artifacts, &tests_path);
110 check!(ui_tests, &root_path, bless);
111 check!(mir_opt_tests, &tests_path, bless);
112 check!(rustdoc_gui_tests, &tests_path);
113 check!(rustdoc_css_themes, &librustdoc_path);
114 check!(rustdoc_templates, &librustdoc_path);
115 check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
116 check!(rustdoc_json, &src_path, &ci_info);
117 check!(known_bug, &crashes_path);
118 check!(unknown_revision, &tests_path);
119
120 check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose, &ci_info);
122 check!(fluent_alphabetical, &compiler_path, bless);
123 check!(fluent_period, &compiler_path);
124 check!(target_policy, &root_path);
125 check!(gcc_submodule, &root_path, &compiler_path);
126
127 check!(pal, &library_path);
129
130 check!(unit_tests, &src_path);
132 check!(unit_tests, &compiler_path);
133 check!(unit_tests, &library_path);
134
135 if bins::check_filesystem_support(&[&root_path], &output_directory) {
136 check!(bins, &root_path);
137 }
138
139 check!(style, &src_path);
140 check!(style, &tests_path);
141 check!(style, &compiler_path);
142 check!(style, &library_path);
143
144 check!(edition, &src_path);
145 check!(edition, &compiler_path);
146 check!(edition, &library_path);
147
148 check!(alphabetical, &root_manifest);
149 check!(alphabetical, &src_path);
150 check!(alphabetical, &tests_path);
151 check!(alphabetical, &compiler_path);
152 check!(alphabetical, &library_path);
153
154 check!(x_version, &root_path, &cargo);
155
156 check!(triagebot, &root_path);
157
158 check!(filenames, &root_path);
159
160 let collected = {
161 drain_handles(&mut handles);
162
163 let mut flag = false;
164 let r = features::check(
165 &src_path,
166 &tests_path,
167 &compiler_path,
168 &library_path,
169 &mut flag,
170 verbose,
171 );
172 if flag {
173 bad.store(true, Ordering::Relaxed);
174 }
175 r
176 };
177 check!(unstable_book, &src_path, collected);
178
179 check!(
180 ext_tool_checks,
181 &root_path,
182 &output_directory,
183 &ci_info,
184 bless,
185 extra_checks,
186 pos_args
187 );
188 });
189
190 if bad.load(Ordering::Relaxed) {
191 eprintln!("some tidy checks failed");
192 process::exit(1);
193 }
194}