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