]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/common.rs
Merge branch 'master' into feature/incorporate-tracing
[rust.git] / src / tools / compiletest / src / common.rs
1 pub use self::Mode::*;
2
3 use std::ffi::OsString;
4 use std::fmt;
5 use std::path::{Path, PathBuf};
6 use std::str::FromStr;
7
8 use crate::util::PathBufExt;
9 use test::ColorConfig;
10
11 #[derive(Clone, Copy, PartialEq, Debug)]
12 pub enum Mode {
13     CompileFail,
14     RunFail,
15     RunPassValgrind,
16     Pretty,
17     DebugInfo,
18     Codegen,
19     Rustdoc,
20     CodegenUnits,
21     Incremental,
22     RunMake,
23     Ui,
24     JsDocTest,
25     MirOpt,
26     Assembly,
27 }
28
29 impl Mode {
30     pub fn disambiguator(self) -> &'static str {
31         // Pretty-printing tests could run concurrently, and if they do,
32         // they need to keep their output segregated.
33         match self {
34             Pretty => ".pretty",
35             _ => "",
36         }
37     }
38 }
39
40 impl FromStr for Mode {
41     type Err = ();
42     fn from_str(s: &str) -> Result<Mode, ()> {
43         match s {
44             "compile-fail" => Ok(CompileFail),
45             "run-fail" => Ok(RunFail),
46             "run-pass-valgrind" => Ok(RunPassValgrind),
47             "pretty" => Ok(Pretty),
48             "debuginfo" => Ok(DebugInfo),
49             "codegen" => Ok(Codegen),
50             "rustdoc" => Ok(Rustdoc),
51             "codegen-units" => Ok(CodegenUnits),
52             "incremental" => Ok(Incremental),
53             "run-make" => Ok(RunMake),
54             "ui" => Ok(Ui),
55             "js-doc-test" => Ok(JsDocTest),
56             "mir-opt" => Ok(MirOpt),
57             "assembly" => Ok(Assembly),
58             _ => Err(()),
59         }
60     }
61 }
62
63 impl fmt::Display for Mode {
64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         let s = match *self {
66             CompileFail => "compile-fail",
67             RunFail => "run-fail",
68             RunPassValgrind => "run-pass-valgrind",
69             Pretty => "pretty",
70             DebugInfo => "debuginfo",
71             Codegen => "codegen",
72             Rustdoc => "rustdoc",
73             CodegenUnits => "codegen-units",
74             Incremental => "incremental",
75             RunMake => "run-make",
76             Ui => "ui",
77             JsDocTest => "js-doc-test",
78             MirOpt => "mir-opt",
79             Assembly => "assembly",
80         };
81         fmt::Display::fmt(s, f)
82     }
83 }
84
85 #[derive(Clone, Copy, PartialEq, Debug, Hash)]
86 pub enum PassMode {
87     Check,
88     Build,
89     Run,
90 }
91
92 impl FromStr for PassMode {
93     type Err = ();
94     fn from_str(s: &str) -> Result<Self, ()> {
95         match s {
96             "check" => Ok(PassMode::Check),
97             "build" => Ok(PassMode::Build),
98             "run" => Ok(PassMode::Run),
99             _ => Err(()),
100         }
101     }
102 }
103
104 impl fmt::Display for PassMode {
105     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106         let s = match *self {
107             PassMode::Check => "check",
108             PassMode::Build => "build",
109             PassMode::Run => "run",
110         };
111         fmt::Display::fmt(s, f)
112     }
113 }
114
115 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
116 pub enum FailMode {
117     Check,
118     Build,
119     Run,
120 }
121
122 #[derive(Clone, Debug, PartialEq)]
123 pub enum CompareMode {
124     Nll,
125     Polonius,
126     Chalk,
127 }
128
129 impl CompareMode {
130     pub(crate) fn to_str(&self) -> &'static str {
131         match *self {
132             CompareMode::Nll => "nll",
133             CompareMode::Polonius => "polonius",
134             CompareMode::Chalk => "chalk",
135         }
136     }
137
138     pub fn parse(s: String) -> CompareMode {
139         match s.as_str() {
140             "nll" => CompareMode::Nll,
141             "polonius" => CompareMode::Polonius,
142             "chalk" => CompareMode::Chalk,
143             x => panic!("unknown --compare-mode option: {}", x),
144         }
145     }
146 }
147
148 #[derive(Clone, Copy, Debug, PartialEq)]
149 pub enum Debugger {
150     Cdb,
151     Gdb,
152     Lldb,
153 }
154
155 impl Debugger {
156     fn to_str(&self) -> &'static str {
157         match self {
158             Debugger::Cdb => "cdb",
159             Debugger::Gdb => "gdb",
160             Debugger::Lldb => "lldb",
161         }
162     }
163 }
164
165 impl fmt::Display for Debugger {
166     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167         fmt::Display::fmt(self.to_str(), f)
168     }
169 }
170
171 /// Configuration for compiletest
172 #[derive(Clone)]
173 pub struct Config {
174     /// `true` to to overwrite stderr/stdout files instead of complaining about changes in output.
175     pub bless: bool,
176
177     /// The library paths required for running the compiler.
178     pub compile_lib_path: PathBuf,
179
180     /// The library paths required for running compiled programs.
181     pub run_lib_path: PathBuf,
182
183     /// The rustc executable.
184     pub rustc_path: PathBuf,
185
186     /// The rustdoc executable.
187     pub rustdoc_path: Option<PathBuf>,
188
189     /// The rust-demangler executable.
190     pub rust_demangler_path: Option<PathBuf>,
191
192     /// The Python executable to use for LLDB.
193     pub lldb_python: String,
194
195     /// The Python executable to use for htmldocck.
196     pub docck_python: String,
197
198     /// The LLVM `FileCheck` binary path.
199     pub llvm_filecheck: Option<PathBuf>,
200
201     /// Path to LLVM's bin directory.
202     pub llvm_bin_dir: Option<PathBuf>,
203
204     /// The valgrind path.
205     pub valgrind_path: Option<String>,
206
207     /// Whether to fail if we can't run run-pass-valgrind tests under valgrind
208     /// (or, alternatively, to silently run them like regular run-pass tests).
209     pub force_valgrind: bool,
210
211     /// The path to the Clang executable to run Clang-based tests with. If
212     /// `None` then these tests will be ignored.
213     pub run_clang_based_tests_with: Option<String>,
214
215     /// The directory containing the tests to run
216     pub src_base: PathBuf,
217
218     /// The directory where programs should be built
219     pub build_base: PathBuf,
220
221     /// The name of the stage being built (stage1, etc)
222     pub stage_id: String,
223
224     /// The test mode, compile-fail, run-fail, ui
225     pub mode: Mode,
226
227     /// The debugger to use in debuginfo mode. Unset otherwise.
228     pub debugger: Option<Debugger>,
229
230     /// Run ignored tests
231     pub run_ignored: bool,
232
233     /// Only run tests that match this filter
234     pub filter: Option<String>,
235
236     /// Exactly match the filter, rather than a substring
237     pub filter_exact: bool,
238
239     /// Force the pass mode of a check/build/run-pass test to this mode.
240     pub force_pass_mode: Option<PassMode>,
241
242     /// Write out a parseable log of tests that were run
243     pub logfile: Option<PathBuf>,
244
245     /// A command line to prefix program execution with,
246     /// for running under valgrind
247     pub runtool: Option<String>,
248
249     /// Flags to pass to the compiler when building for the host
250     pub host_rustcflags: Option<String>,
251
252     /// Flags to pass to the compiler when building for the target
253     pub target_rustcflags: Option<String>,
254
255     /// Target system to be tested
256     pub target: String,
257
258     /// Host triple for the compiler being invoked
259     pub host: String,
260
261     /// Path to / name of the Microsoft Console Debugger (CDB) executable
262     pub cdb: Option<OsString>,
263
264     /// Path to / name of the GDB executable
265     pub gdb: Option<String>,
266
267     /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
268     pub gdb_version: Option<u32>,
269
270     /// Whether GDB has native rust support
271     pub gdb_native_rust: bool,
272
273     /// Version of LLDB
274     pub lldb_version: Option<u32>,
275
276     /// Whether LLDB has native rust support
277     pub lldb_native_rust: bool,
278
279     /// Version of LLVM
280     pub llvm_version: Option<u32>,
281
282     /// Is LLVM a system LLVM
283     pub system_llvm: bool,
284
285     /// Path to the android tools
286     pub android_cross_path: PathBuf,
287
288     /// Extra parameter to run adb on arm-linux-androideabi
289     pub adb_path: String,
290
291     /// Extra parameter to run test suite on arm-linux-androideabi
292     pub adb_test_dir: String,
293
294     /// status whether android device available or not
295     pub adb_device_status: bool,
296
297     /// the path containing LLDB's Python module
298     pub lldb_python_dir: Option<String>,
299
300     /// Explain what's going on
301     pub verbose: bool,
302
303     /// Print one character per test instead of one line
304     pub quiet: bool,
305
306     /// Whether to use colors in test.
307     pub color: ColorConfig,
308
309     /// where to find the remote test client process, if we're using it
310     pub remote_test_client: Option<PathBuf>,
311
312     /// mode describing what file the actual ui output will be compared to
313     pub compare_mode: Option<CompareMode>,
314
315     /// If true, this will generate a coverage file with UI test files that run `MachineApplicable`
316     /// diagnostics but are missing `run-rustfix` annotations. The generated coverage file is
317     /// created in `/<build_base>/rustfix_missing_coverage.txt`
318     pub rustfix_coverage: bool,
319
320     // Configuration for various run-make tests frobbing things like C compilers
321     // or querying about various LLVM component information.
322     pub cc: String,
323     pub cxx: String,
324     pub cflags: String,
325     pub ar: String,
326     pub linker: Option<String>,
327     pub llvm_components: String,
328
329     /// Path to a NodeJS executable. Used for JS doctests, emscripten and WASM tests
330     pub nodejs: Option<String>,
331 }
332
333 #[derive(Debug, Clone)]
334 pub struct TestPaths {
335     pub file: PathBuf,         // e.g., compile-test/foo/bar/baz.rs
336     pub relative_dir: PathBuf, // e.g., foo/bar
337 }
338
339 /// Used by `ui` tests to generate things like `foo.stderr` from `foo.rs`.
340 pub fn expected_output_path(
341     testpaths: &TestPaths,
342     revision: Option<&str>,
343     compare_mode: &Option<CompareMode>,
344     kind: &str,
345 ) -> PathBuf {
346     assert!(UI_EXTENSIONS.contains(&kind));
347     let mut parts = Vec::new();
348
349     if let Some(x) = revision {
350         parts.push(x);
351     }
352     if let Some(ref x) = *compare_mode {
353         parts.push(x.to_str());
354     }
355     parts.push(kind);
356
357     let extension = parts.join(".");
358     testpaths.file.with_extension(extension)
359 }
360
361 pub const UI_EXTENSIONS: &[&str] = &[UI_STDERR, UI_STDOUT, UI_FIXED, UI_RUN_STDERR, UI_RUN_STDOUT];
362 pub const UI_STDERR: &str = "stderr";
363 pub const UI_STDOUT: &str = "stdout";
364 pub const UI_FIXED: &str = "fixed";
365 pub const UI_RUN_STDERR: &str = "run.stderr";
366 pub const UI_RUN_STDOUT: &str = "run.stdout";
367
368 /// Absolute path to the directory where all output for all tests in the given
369 /// `relative_dir` group should reside. Example:
370 ///   /path/to/build/host-triple/test/ui/relative/
371 /// This is created early when tests are collected to avoid race conditions.
372 pub fn output_relative_path(config: &Config, relative_dir: &Path) -> PathBuf {
373     config.build_base.join(relative_dir)
374 }
375
376 /// Generates a unique name for the test, such as `testname.revision.mode`.
377 pub fn output_testname_unique(
378     config: &Config,
379     testpaths: &TestPaths,
380     revision: Option<&str>,
381 ) -> PathBuf {
382     let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
383     let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
384     PathBuf::from(&testpaths.file.file_stem().unwrap())
385         .with_extra_extension(revision.unwrap_or(""))
386         .with_extra_extension(mode)
387         .with_extra_extension(debugger)
388 }
389
390 /// Absolute path to the directory where all output for the given
391 /// test/revision should reside. Example:
392 ///   /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
393 pub fn output_base_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
394     output_relative_path(config, &testpaths.relative_dir)
395         .join(output_testname_unique(config, testpaths, revision))
396 }
397
398 /// Absolute path to the base filename used as output for the given
399 /// test/revision. Example:
400 ///   /path/to/build/host-triple/test/ui/relative/testname.revision.mode/testname
401 pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
402     output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
403 }