3 use std::ffi::OsString;
5 use std::path::{Path, PathBuf};
6 use std::process::Command;
9 use crate::util::PathBufExt;
10 use lazycell::LazyCell;
11 use test::ColorConfig;
13 #[derive(Clone, Copy, PartialEq, Debug)]
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.
41 impl FromStr for Mode {
43 fn from_str(s: &str) -> Result<Mode, ()> {
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),
55 "js-doc-test" => Ok(JsDocTest),
56 "mir-opt" => Ok(MirOpt),
57 "assembly" => Ok(Assembly),
63 impl fmt::Display for Mode {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 RunPassValgrind => "run-pass-valgrind",
68 DebugInfo => "debuginfo",
71 RustdocJson => "rustdoc-json",
72 CodegenUnits => "codegen-units",
73 Incremental => "incremental",
74 RunMake => "run-make",
76 JsDocTest => "js-doc-test",
78 Assembly => "assembly",
80 fmt::Display::fmt(s, f)
84 #[derive(Clone, Copy, PartialEq, Debug, Hash)]
91 impl FromStr for PassMode {
93 fn from_str(s: &str) -> Result<Self, ()> {
95 "check" => Ok(PassMode::Check),
96 "build" => Ok(PassMode::Build),
97 "run" => Ok(PassMode::Run),
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",
110 fmt::Display::fmt(s, f)
114 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
121 #[derive(Clone, Debug, PartialEq)]
122 pub enum CompareMode {
130 pub(crate) fn to_str(&self) -> &'static str {
132 CompareMode::Polonius => "polonius",
133 CompareMode::Chalk => "chalk",
134 CompareMode::SplitDwarf => "split-dwarf",
135 CompareMode::SplitDwarfSingle => "split-dwarf-single",
139 pub fn parse(s: String) -> CompareMode {
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),
150 #[derive(Clone, Copy, Debug, PartialEq)]
158 fn to_str(&self) -> &'static str {
160 Debugger::Cdb => "cdb",
161 Debugger::Gdb => "gdb",
162 Debugger::Lldb => "lldb",
167 impl fmt::Display for Debugger {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 fmt::Display::fmt(self.to_str(), f)
173 #[derive(Clone, Copy, Debug, PartialEq)]
174 pub enum PanicStrategy {
179 /// Configuration for compiletest
180 #[derive(Debug, Clone)]
182 /// `true` to overwrite stderr/stdout files instead of complaining about changes in output.
185 /// The library paths required for running the compiler.
186 pub compile_lib_path: PathBuf,
188 /// The library paths required for running compiled programs.
189 pub run_lib_path: PathBuf,
191 /// The rustc executable.
192 pub rustc_path: PathBuf,
194 /// The rustdoc executable.
195 pub rustdoc_path: Option<PathBuf>,
197 /// The rust-demangler executable.
198 pub rust_demangler_path: Option<PathBuf>,
200 /// The Python executable to use for LLDB and htmldocck.
203 /// The jsondocck executable.
204 pub jsondocck_path: Option<String>,
206 /// The jsondoclint executable.
207 pub jsondoclint_path: Option<String>,
209 /// The LLVM `FileCheck` binary path.
210 pub llvm_filecheck: Option<PathBuf>,
212 /// Path to LLVM's bin directory.
213 pub llvm_bin_dir: Option<PathBuf>,
215 /// The valgrind path.
216 pub valgrind_path: Option<String>,
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,
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>,
226 /// The directory containing the tests to run
227 pub src_base: PathBuf,
229 /// The directory where programs should be built
230 pub build_base: PathBuf,
232 /// The name of the stage being built (stage1, etc)
233 pub stage_id: String,
235 /// The test mode, e.g. ui or debuginfo.
238 /// The test suite (essentially which directory is running, but without the
239 /// directory prefix such as src/test)
242 /// The debugger to use in debuginfo mode. Unset otherwise.
243 pub debugger: Option<Debugger>,
245 /// Run ignored tests
246 pub run_ignored: bool,
248 /// Only run tests that match these filters
249 pub filters: Vec<String>,
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>,
255 /// Exactly match the filter, rather than a substring
256 pub filter_exact: bool,
258 /// Force the pass mode of a check/build/run-pass test to this mode.
259 pub force_pass_mode: Option<PassMode>,
261 /// Explicitly enable or disable running.
262 pub run: Option<bool>,
264 /// Write out a parseable log of tests that were run
265 pub logfile: Option<PathBuf>,
267 /// A command line to prefix program execution with,
268 /// for running under valgrind
269 pub runtool: Option<String>,
271 /// Flags to pass to the compiler when building for the host
272 pub host_rustcflags: Vec<String>,
274 /// Flags to pass to the compiler when building for the target
275 pub target_rustcflags: Vec<String>,
277 /// Whether tests should be optimized by default. Individual test-suites and test files may
278 /// override this setting.
279 pub optimize_tests: bool,
281 /// Target system to be tested
284 /// Host triple for the compiler being invoked
287 /// Path to / name of the Microsoft Console Debugger (CDB) executable
288 pub cdb: Option<OsString>,
291 pub cdb_version: Option<[u16; 4]>,
293 /// Path to / name of the GDB executable
294 pub gdb: Option<String>,
296 /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
297 pub gdb_version: Option<u32>,
299 /// Whether GDB has native rust support
300 pub gdb_native_rust: bool,
303 pub lldb_version: Option<u32>,
305 /// Whether LLDB has native rust support
306 pub lldb_native_rust: bool,
309 pub llvm_version: Option<u32>,
311 /// Is LLVM a system LLVM
312 pub system_llvm: bool,
314 /// Path to the android tools
315 pub android_cross_path: PathBuf,
317 /// Extra parameter to run adb on arm-linux-androideabi
318 pub adb_path: String,
320 /// Extra parameter to run test suite on arm-linux-androideabi
321 pub adb_test_dir: String,
323 /// status whether android device available or not
324 pub adb_device_status: bool,
326 /// the path containing LLDB's Python module
327 pub lldb_python_dir: Option<String>,
329 /// Explain what's going on
332 /// Print one character per test instead of one line
335 /// Whether to use colors in test.
336 pub color: ColorConfig,
338 /// where to find the remote test client process, if we're using it
339 pub remote_test_client: Option<PathBuf>,
341 /// mode describing what file the actual ui output will be compared to
342 pub compare_mode: Option<CompareMode>,
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,
349 /// whether to run `tidy` when a rustdoc test fails
352 /// The current Rust channel
355 /// The default Rust edition
356 pub edition: Option<String>,
358 // Configuration for various run-make tests frobbing things like C compilers
359 // or querying about various LLVM component information.
363 pub cxxflags: String,
365 pub linker: Option<String>,
366 pub llvm_components: String,
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>,
373 /// Whether to rerun tests even if the inputs are unchanged.
374 pub force_rerun: bool,
376 pub target_cfg: LazyCell<TargetCfg>,
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")
387 fn target_cfg(&self) -> &TargetCfg {
389 .borrow_with(|| TargetCfg::new(&self.rustc_path, &self.target, &self.target_rustcflags))
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"))
402 pub fn matches_os(&self, os: &str) -> bool {
403 self.target_cfg().os == os
406 pub fn matches_env(&self, env: &str) -> bool {
407 self.target_cfg().env == env
410 pub fn matches_abi(&self, abi: &str) -> bool {
411 self.target_cfg().abi == abi
414 pub fn matches_family(&self, family: &str) -> bool {
415 self.target_cfg().families.iter().any(|f| f == family)
418 pub fn is_big_endian(&self) -> bool {
419 self.target_cfg().endian == Endian::Big
422 pub fn get_pointer_width(&self) -> u32 {
423 *&self.target_cfg().pointer_width
426 pub fn can_unwind(&self) -> bool {
427 self.target_cfg().panic == PanicStrategy::Unwind
430 pub fn has_asm_support(&self) -> bool {
431 static ASM_SUPPORTED_ARCHS: &[&str] = &[
432 "x86", "x86_64", "arm", "aarch64", "riscv32",
434 // These targets require an additional asm_experimental_arch feature.
435 // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
437 ASM_SUPPORTED_ARCHS.contains(&self.target_cfg().arch.as_str())
441 #[derive(Clone, Debug)]
442 pub struct TargetCfg {
447 families: Vec<String>,
450 panic: PanicStrategy,
453 #[derive(Eq, PartialEq, Clone, Debug)]
460 fn new(rustc_path: &Path, target: &str, target_rustcflags: &Vec<String>) -> TargetCfg {
461 let output = match Command::new(rustc_path)
465 .args(target_rustcflags)
468 Ok(output) => output,
469 Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", rustc_path),
471 if !output.status.success() {
473 "error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
475 String::from_utf8(output.stdout).unwrap(),
476 String::from_utf8(output.stderr).unwrap(),
479 let print_cfg = String::from_utf8(output.stdout).unwrap();
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('"');
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()),
499 endian = Some(match value {
500 "little" => Endian::Little,
501 "big" => Endian::Big,
502 s => panic!("unexpected {s}"),
506 panic = match value {
507 "abort" => Some(PanicStrategy::Abort),
508 "unwind" => Some(PanicStrategy::Unwind),
509 s => panic!("unexpected {s}"),
517 arch: arch.unwrap().to_string(),
518 os: os.unwrap().to_string(),
519 env: env.unwrap().to_string(),
520 abi: abi.unwrap().to_string(),
522 pointer_width: pointer_width.unwrap(),
523 endian: endian.unwrap(),
524 panic: panic.unwrap(),
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
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>,
542 assert!(UI_EXTENSIONS.contains(&kind));
543 let mut parts = Vec::new();
545 if let Some(x) = revision {
548 if let Some(ref x) = *compare_mode {
549 parts.push(x.to_str());
553 let extension = parts.join(".");
554 testpaths.file.with_extension(extension)
557 pub const UI_EXTENSIONS: &[&str] = &[
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";
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)
584 /// Generates a unique name for the test, such as `testname.revision.mode`.
585 pub fn output_testname_unique(
587 testpaths: &TestPaths,
588 revision: Option<&str>,
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)
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))
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())
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")