]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/common.rs
Rollup merge of #105164 - compiler-errors:revert-import-filter, r=estebank
[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::iter;
6 use std::path::{Path, PathBuf};
7 use std::process::Command;
8 use std::str::FromStr;
9
10 use crate::util::{add_dylib_path, PathBufExt};
11 use lazycell::LazyCell;
12 use test::ColorConfig;
13
14 #[derive(Clone, Copy, PartialEq, Debug)]
15 pub enum Mode {
16     RunPassValgrind,
17     Pretty,
18     DebugInfo,
19     Codegen,
20     Rustdoc,
21     RustdocJson,
22     CodegenUnits,
23     Incremental,
24     RunMake,
25     Ui,
26     JsDocTest,
27     MirOpt,
28     Assembly,
29 }
30
31 impl Mode {
32     pub fn disambiguator(self) -> &'static str {
33         // Pretty-printing tests could run concurrently, and if they do,
34         // they need to keep their output segregated.
35         match self {
36             Pretty => ".pretty",
37             _ => "",
38         }
39     }
40 }
41
42 impl FromStr for Mode {
43     type Err = ();
44     fn from_str(s: &str) -> Result<Mode, ()> {
45         match s {
46             "run-pass-valgrind" => Ok(RunPassValgrind),
47             "pretty" => Ok(Pretty),
48             "debuginfo" => Ok(DebugInfo),
49             "codegen" => Ok(Codegen),
50             "rustdoc" => Ok(Rustdoc),
51             "rustdoc-json" => Ok(RustdocJson),
52             "codegen-units" => Ok(CodegenUnits),
53             "incremental" => Ok(Incremental),
54             "run-make" => Ok(RunMake),
55             "ui" => Ok(Ui),
56             "js-doc-test" => Ok(JsDocTest),
57             "mir-opt" => Ok(MirOpt),
58             "assembly" => Ok(Assembly),
59             _ => Err(()),
60         }
61     }
62 }
63
64 impl fmt::Display for Mode {
65     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66         let s = match *self {
67             RunPassValgrind => "run-pass-valgrind",
68             Pretty => "pretty",
69             DebugInfo => "debuginfo",
70             Codegen => "codegen",
71             Rustdoc => "rustdoc",
72             RustdocJson => "rustdoc-json",
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     Polonius,
125     Chalk,
126     SplitDwarf,
127     SplitDwarfSingle,
128 }
129
130 impl CompareMode {
131     pub(crate) fn to_str(&self) -> &'static str {
132         match *self {
133             CompareMode::Polonius => "polonius",
134             CompareMode::Chalk => "chalk",
135             CompareMode::SplitDwarf => "split-dwarf",
136             CompareMode::SplitDwarfSingle => "split-dwarf-single",
137         }
138     }
139
140     pub fn parse(s: String) -> CompareMode {
141         match s.as_str() {
142             "polonius" => CompareMode::Polonius,
143             "chalk" => CompareMode::Chalk,
144             "split-dwarf" => CompareMode::SplitDwarf,
145             "split-dwarf-single" => CompareMode::SplitDwarfSingle,
146             x => panic!("unknown --compare-mode option: {}", x),
147         }
148     }
149 }
150
151 #[derive(Clone, Copy, Debug, PartialEq)]
152 pub enum Debugger {
153     Cdb,
154     Gdb,
155     Lldb,
156 }
157
158 impl Debugger {
159     fn to_str(&self) -> &'static str {
160         match self {
161             Debugger::Cdb => "cdb",
162             Debugger::Gdb => "gdb",
163             Debugger::Lldb => "lldb",
164         }
165     }
166 }
167
168 impl fmt::Display for Debugger {
169     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170         fmt::Display::fmt(self.to_str(), f)
171     }
172 }
173
174 #[derive(Clone, Copy, Debug, PartialEq)]
175 pub enum PanicStrategy {
176     Unwind,
177     Abort,
178 }
179
180 /// Configuration for compiletest
181 #[derive(Debug, Clone)]
182 pub struct Config {
183     /// `true` to overwrite stderr/stdout files instead of complaining about changes in output.
184     pub bless: bool,
185
186     /// The library paths required for running the compiler.
187     pub compile_lib_path: PathBuf,
188
189     /// The library paths required for running compiled programs.
190     pub run_lib_path: PathBuf,
191
192     /// The rustc executable.
193     pub rustc_path: PathBuf,
194
195     /// The rustdoc executable.
196     pub rustdoc_path: Option<PathBuf>,
197
198     /// The rust-demangler executable.
199     pub rust_demangler_path: Option<PathBuf>,
200
201     /// The Python executable to use for LLDB and htmldocck.
202     pub python: String,
203
204     /// The jsondocck executable.
205     pub jsondocck_path: Option<String>,
206
207     /// The jsondoclint executable.
208     pub jsondoclint_path: Option<String>,
209
210     /// The LLVM `FileCheck` binary path.
211     pub llvm_filecheck: Option<PathBuf>,
212
213     /// Path to LLVM's bin directory.
214     pub llvm_bin_dir: Option<PathBuf>,
215
216     /// The valgrind path.
217     pub valgrind_path: Option<String>,
218
219     /// Whether to fail if we can't run run-pass-valgrind tests under valgrind
220     /// (or, alternatively, to silently run them like regular run-pass tests).
221     pub force_valgrind: bool,
222
223     /// The path to the Clang executable to run Clang-based tests with. If
224     /// `None` then these tests will be ignored.
225     pub run_clang_based_tests_with: Option<String>,
226
227     /// The directory containing the tests to run
228     pub src_base: PathBuf,
229
230     /// The directory where programs should be built
231     pub build_base: PathBuf,
232
233     /// The directory containing the compiler sysroot
234     pub sysroot_base: PathBuf,
235
236     /// The name of the stage being built (stage1, etc)
237     pub stage_id: String,
238
239     /// The test mode, e.g. ui or debuginfo.
240     pub mode: Mode,
241
242     /// The test suite (essentially which directory is running, but without the
243     /// directory prefix such as src/test)
244     pub suite: String,
245
246     /// The debugger to use in debuginfo mode. Unset otherwise.
247     pub debugger: Option<Debugger>,
248
249     /// Run ignored tests
250     pub run_ignored: bool,
251
252     /// Only run tests that match these filters
253     pub filters: Vec<String>,
254
255     /// Skip tests tests matching these substrings. Corresponds to
256     /// `test::TestOpts::skip`. `filter_exact` does not apply to these flags.
257     pub skip: Vec<String>,
258
259     /// Exactly match the filter, rather than a substring
260     pub filter_exact: bool,
261
262     /// Force the pass mode of a check/build/run-pass test to this mode.
263     pub force_pass_mode: Option<PassMode>,
264
265     /// Explicitly enable or disable running.
266     pub run: Option<bool>,
267
268     /// Write out a parseable log of tests that were run
269     pub logfile: Option<PathBuf>,
270
271     /// A command line to prefix program execution with,
272     /// for running under valgrind
273     pub runtool: Option<String>,
274
275     /// Flags to pass to the compiler when building for the host
276     pub host_rustcflags: Vec<String>,
277
278     /// Flags to pass to the compiler when building for the target
279     pub target_rustcflags: Vec<String>,
280
281     /// Whether tests should be optimized by default. Individual test-suites and test files may
282     /// override this setting.
283     pub optimize_tests: bool,
284
285     /// Target system to be tested
286     pub target: String,
287
288     /// Host triple for the compiler being invoked
289     pub host: String,
290
291     /// Path to / name of the Microsoft Console Debugger (CDB) executable
292     pub cdb: Option<OsString>,
293
294     /// Version of CDB
295     pub cdb_version: Option<[u16; 4]>,
296
297     /// Path to / name of the GDB executable
298     pub gdb: Option<String>,
299
300     /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
301     pub gdb_version: Option<u32>,
302
303     /// Whether GDB has native rust support
304     pub gdb_native_rust: bool,
305
306     /// Version of LLDB
307     pub lldb_version: Option<u32>,
308
309     /// Whether LLDB has native rust support
310     pub lldb_native_rust: bool,
311
312     /// Version of LLVM
313     pub llvm_version: Option<u32>,
314
315     /// Is LLVM a system LLVM
316     pub system_llvm: bool,
317
318     /// Path to the android tools
319     pub android_cross_path: PathBuf,
320
321     /// Extra parameter to run adb on arm-linux-androideabi
322     pub adb_path: String,
323
324     /// Extra parameter to run test suite on arm-linux-androideabi
325     pub adb_test_dir: String,
326
327     /// status whether android device available or not
328     pub adb_device_status: bool,
329
330     /// the path containing LLDB's Python module
331     pub lldb_python_dir: Option<String>,
332
333     /// Explain what's going on
334     pub verbose: bool,
335
336     /// Print one character per test instead of one line
337     pub quiet: bool,
338
339     /// Whether to use colors in test.
340     pub color: ColorConfig,
341
342     /// where to find the remote test client process, if we're using it
343     pub remote_test_client: Option<PathBuf>,
344
345     /// mode describing what file the actual ui output will be compared to
346     pub compare_mode: Option<CompareMode>,
347
348     /// If true, this will generate a coverage file with UI test files that run `MachineApplicable`
349     /// diagnostics but are missing `run-rustfix` annotations. The generated coverage file is
350     /// created in `/<build_base>/rustfix_missing_coverage.txt`
351     pub rustfix_coverage: bool,
352
353     /// whether to run `tidy` when a rustdoc test fails
354     pub has_tidy: bool,
355
356     /// The current Rust channel
357     pub channel: String,
358
359     /// The default Rust edition
360     pub edition: Option<String>,
361
362     // Configuration for various run-make tests frobbing things like C compilers
363     // or querying about various LLVM component information.
364     pub cc: String,
365     pub cxx: String,
366     pub cflags: String,
367     pub cxxflags: String,
368     pub ar: String,
369     pub linker: Option<String>,
370     pub llvm_components: String,
371
372     /// Path to a NodeJS executable. Used for JS doctests, emscripten and WASM tests
373     pub nodejs: Option<String>,
374     /// Path to a npm executable. Used for rustdoc GUI tests
375     pub npm: Option<String>,
376
377     /// Whether to rerun tests even if the inputs are unchanged.
378     pub force_rerun: bool,
379
380     pub target_cfg: LazyCell<TargetCfg>,
381 }
382
383 impl Config {
384     pub fn run_enabled(&self) -> bool {
385         self.run.unwrap_or_else(|| {
386             // Auto-detect whether to run based on the platform.
387             !self.target.ends_with("-fuchsia")
388         })
389     }
390
391     fn target_cfg(&self) -> &TargetCfg {
392         self.target_cfg.borrow_with(|| TargetCfg::new(self))
393     }
394
395     pub fn matches_arch(&self, arch: &str) -> bool {
396         self.target_cfg().arch == arch ||
397         // Shorthand for convenience. The arch for
398         // asmjs-unknown-emscripten is actually wasm32.
399         (arch == "asmjs" && self.target.starts_with("asmjs")) ||
400         // Matching all the thumb variants as one can be convenient.
401         // (thumbv6m, thumbv7em, thumbv7m, etc.)
402         (arch == "thumb" && self.target.starts_with("thumb"))
403     }
404
405     pub fn matches_os(&self, os: &str) -> bool {
406         self.target_cfg().os == os
407     }
408
409     pub fn matches_env(&self, env: &str) -> bool {
410         self.target_cfg().env == env
411     }
412
413     pub fn matches_abi(&self, abi: &str) -> bool {
414         self.target_cfg().abi == abi
415     }
416
417     pub fn matches_family(&self, family: &str) -> bool {
418         self.target_cfg().families.iter().any(|f| f == family)
419     }
420
421     pub fn is_big_endian(&self) -> bool {
422         self.target_cfg().endian == Endian::Big
423     }
424
425     pub fn get_pointer_width(&self) -> u32 {
426         *&self.target_cfg().pointer_width
427     }
428
429     pub fn can_unwind(&self) -> bool {
430         self.target_cfg().panic == PanicStrategy::Unwind
431     }
432
433     pub fn has_asm_support(&self) -> bool {
434         static ASM_SUPPORTED_ARCHS: &[&str] = &[
435             "x86", "x86_64", "arm", "aarch64", "riscv32",
436             "riscv64",
437             // These targets require an additional asm_experimental_arch feature.
438             // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
439         ];
440         ASM_SUPPORTED_ARCHS.contains(&self.target_cfg().arch.as_str())
441     }
442 }
443
444 #[derive(Clone, Debug)]
445 pub struct TargetCfg {
446     arch: String,
447     os: String,
448     env: String,
449     abi: String,
450     families: Vec<String>,
451     pointer_width: u32,
452     endian: Endian,
453     panic: PanicStrategy,
454 }
455
456 #[derive(Eq, PartialEq, Clone, Debug)]
457 pub enum Endian {
458     Little,
459     Big,
460 }
461
462 impl TargetCfg {
463     fn new(config: &Config) -> TargetCfg {
464         let mut command = Command::new(&config.rustc_path);
465         add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
466         let output = match command
467             .arg("--print=cfg")
468             .arg("--target")
469             .arg(&config.target)
470             .args(&config.target_rustcflags)
471             .output()
472         {
473             Ok(output) => output,
474             Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", config.rustc_path),
475         };
476         if !output.status.success() {
477             panic!(
478                 "error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
479                 config.rustc_path,
480                 String::from_utf8(output.stdout).unwrap(),
481                 String::from_utf8(output.stderr).unwrap(),
482             );
483         }
484         let print_cfg = String::from_utf8(output.stdout).unwrap();
485         let mut arch = None;
486         let mut os = None;
487         let mut env = None;
488         let mut abi = None;
489         let mut families = Vec::new();
490         let mut pointer_width = None;
491         let mut endian = None;
492         let mut panic = None;
493         for line in print_cfg.lines() {
494             if let Some((name, value)) = line.split_once('=') {
495                 let value = value.trim_matches('"');
496                 match name {
497                     "target_arch" => arch = Some(value),
498                     "target_os" => os = Some(value),
499                     "target_env" => env = Some(value),
500                     "target_abi" => abi = Some(value),
501                     "target_family" => families.push(value.to_string()),
502                     "target_pointer_width" => pointer_width = Some(value.parse().unwrap()),
503                     "target_endian" => {
504                         endian = Some(match value {
505                             "little" => Endian::Little,
506                             "big" => Endian::Big,
507                             s => panic!("unexpected {s}"),
508                         })
509                     }
510                     "panic" => {
511                         panic = match value {
512                             "abort" => Some(PanicStrategy::Abort),
513                             "unwind" => Some(PanicStrategy::Unwind),
514                             s => panic!("unexpected {s}"),
515                         }
516                     }
517                     _ => {}
518                 }
519             }
520         }
521         TargetCfg {
522             arch: arch.unwrap().to_string(),
523             os: os.unwrap().to_string(),
524             env: env.unwrap().to_string(),
525             abi: abi.unwrap().to_string(),
526             families,
527             pointer_width: pointer_width.unwrap(),
528             endian: endian.unwrap(),
529             panic: panic.unwrap(),
530         }
531     }
532 }
533
534 #[derive(Debug, Clone)]
535 pub struct TestPaths {
536     pub file: PathBuf,         // e.g., compile-test/foo/bar/baz.rs
537     pub relative_dir: PathBuf, // e.g., foo/bar
538 }
539
540 /// Used by `ui` tests to generate things like `foo.stderr` from `foo.rs`.
541 pub fn expected_output_path(
542     testpaths: &TestPaths,
543     revision: Option<&str>,
544     compare_mode: &Option<CompareMode>,
545     kind: &str,
546 ) -> PathBuf {
547     assert!(UI_EXTENSIONS.contains(&kind));
548     let mut parts = Vec::new();
549
550     if let Some(x) = revision {
551         parts.push(x);
552     }
553     if let Some(ref x) = *compare_mode {
554         parts.push(x.to_str());
555     }
556     parts.push(kind);
557
558     let extension = parts.join(".");
559     testpaths.file.with_extension(extension)
560 }
561
562 pub const UI_EXTENSIONS: &[&str] = &[
563     UI_STDERR,
564     UI_STDOUT,
565     UI_FIXED,
566     UI_RUN_STDERR,
567     UI_RUN_STDOUT,
568     UI_STDERR_64,
569     UI_STDERR_32,
570     UI_STDERR_16,
571 ];
572 pub const UI_STDERR: &str = "stderr";
573 pub const UI_STDOUT: &str = "stdout";
574 pub const UI_FIXED: &str = "fixed";
575 pub const UI_RUN_STDERR: &str = "run.stderr";
576 pub const UI_RUN_STDOUT: &str = "run.stdout";
577 pub const UI_STDERR_64: &str = "64bit.stderr";
578 pub const UI_STDERR_32: &str = "32bit.stderr";
579 pub const UI_STDERR_16: &str = "16bit.stderr";
580
581 /// Absolute path to the directory where all output for all tests in the given
582 /// `relative_dir` group should reside. Example:
583 ///   /path/to/build/host-triple/test/ui/relative/
584 /// This is created early when tests are collected to avoid race conditions.
585 pub fn output_relative_path(config: &Config, relative_dir: &Path) -> PathBuf {
586     config.build_base.join(relative_dir)
587 }
588
589 /// Generates a unique name for the test, such as `testname.revision.mode`.
590 pub fn output_testname_unique(
591     config: &Config,
592     testpaths: &TestPaths,
593     revision: Option<&str>,
594 ) -> PathBuf {
595     let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
596     let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
597     PathBuf::from(&testpaths.file.file_stem().unwrap())
598         .with_extra_extension(revision.unwrap_or(""))
599         .with_extra_extension(mode)
600         .with_extra_extension(debugger)
601 }
602
603 /// Absolute path to the directory where all output for the given
604 /// test/revision should reside. Example:
605 ///   /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
606 pub fn output_base_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
607     output_relative_path(config, &testpaths.relative_dir)
608         .join(output_testname_unique(config, testpaths, revision))
609 }
610
611 /// Absolute path to the base filename used as output for the given
612 /// test/revision. Example:
613 ///   /path/to/build/host-triple/test/ui/relative/testname.revision.mode/testname
614 pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
615     output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
616 }
617
618 /// Absolute path to the directory to use for incremental compilation. Example:
619 ///   /path/to/build/host-triple/test/ui/relative/testname.mode/testname.inc
620 pub fn incremental_dir(config: &Config, testpaths: &TestPaths) -> PathBuf {
621     output_base_name(config, testpaths, None).with_extension("inc")
622 }