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