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 LLVM `FileCheck` binary path.
207 pub llvm_filecheck: Option<PathBuf>,
209 /// Path to LLVM's bin directory.
210 pub llvm_bin_dir: Option<PathBuf>,
212 /// The valgrind path.
213 pub valgrind_path: Option<String>,
215 /// Whether to fail if we can't run run-pass-valgrind tests under valgrind
216 /// (or, alternatively, to silently run them like regular run-pass tests).
217 pub force_valgrind: bool,
219 /// The path to the Clang executable to run Clang-based tests with. If
220 /// `None` then these tests will be ignored.
221 pub run_clang_based_tests_with: Option<String>,
223 /// The directory containing the tests to run
224 pub src_base: PathBuf,
226 /// The directory where programs should be built
227 pub build_base: PathBuf,
229 /// The name of the stage being built (stage1, etc)
230 pub stage_id: String,
232 /// The test mode, e.g. ui or debuginfo.
235 /// The test suite (essentially which directory is running, but without the
236 /// directory prefix such as src/test)
239 /// The debugger to use in debuginfo mode. Unset otherwise.
240 pub debugger: Option<Debugger>,
242 /// Run ignored tests
243 pub run_ignored: bool,
245 /// Only run tests that match these filters
246 pub filters: Vec<String>,
248 /// Skip tests tests matching these substrings. Corresponds to
249 /// `test::TestOpts::skip`. `filter_exact` does not apply to these flags.
250 pub skip: Vec<String>,
252 /// Exactly match the filter, rather than a substring
253 pub filter_exact: bool,
255 /// Force the pass mode of a check/build/run-pass test to this mode.
256 pub force_pass_mode: Option<PassMode>,
258 /// Explicitly enable or disable running.
259 pub run: Option<bool>,
261 /// Write out a parseable log of tests that were run
262 pub logfile: Option<PathBuf>,
264 /// A command line to prefix program execution with,
265 /// for running under valgrind
266 pub runtool: Option<String>,
268 /// Flags to pass to the compiler when building for the host
269 pub host_rustcflags: Option<String>,
271 /// Flags to pass to the compiler when building for the target
272 pub target_rustcflags: Option<String>,
274 /// Whether tests should be optimized by default. Individual test-suites and test files may
275 /// override this setting.
276 pub optimize_tests: bool,
278 /// What panic strategy the target is built with. Unwind supports Abort, but
280 pub target_panic: PanicStrategy,
282 /// Target system to be tested
285 /// Host triple for the compiler being invoked
288 /// Path to / name of the Microsoft Console Debugger (CDB) executable
289 pub cdb: Option<OsString>,
292 pub cdb_version: Option<[u16; 4]>,
294 /// Path to / name of the GDB executable
295 pub gdb: Option<String>,
297 /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
298 pub gdb_version: Option<u32>,
300 /// Whether GDB has native rust support
301 pub gdb_native_rust: bool,
304 pub lldb_version: Option<u32>,
306 /// Whether LLDB has native rust support
307 pub lldb_native_rust: bool,
310 pub llvm_version: Option<u32>,
312 /// Is LLVM a system LLVM
313 pub system_llvm: bool,
315 /// Path to the android tools
316 pub android_cross_path: PathBuf,
318 /// Extra parameter to run adb on arm-linux-androideabi
319 pub adb_path: String,
321 /// Extra parameter to run test suite on arm-linux-androideabi
322 pub adb_test_dir: String,
324 /// status whether android device available or not
325 pub adb_device_status: bool,
327 /// the path containing LLDB's Python module
328 pub lldb_python_dir: Option<String>,
330 /// Explain what's going on
333 /// Print one character per test instead of one line
336 /// Whether to use colors in test.
337 pub color: ColorConfig,
339 /// where to find the remote test client process, if we're using it
340 pub remote_test_client: Option<PathBuf>,
342 /// mode describing what file the actual ui output will be compared to
343 pub compare_mode: Option<CompareMode>,
345 /// If true, this will generate a coverage file with UI test files that run `MachineApplicable`
346 /// diagnostics but are missing `run-rustfix` annotations. The generated coverage file is
347 /// created in `/<build_base>/rustfix_missing_coverage.txt`
348 pub rustfix_coverage: bool,
350 /// whether to run `tidy` when a rustdoc test fails
353 /// The current Rust channel
356 /// The default Rust edition
357 pub edition: Option<String>,
359 // Configuration for various run-make tests frobbing things like C compilers
360 // or querying about various LLVM component information.
364 pub cxxflags: String,
366 pub linker: Option<String>,
367 pub llvm_components: String,
369 /// Path to a NodeJS executable. Used for JS doctests, emscripten and WASM tests
370 pub nodejs: Option<String>,
371 /// Path to a npm executable. Used for rustdoc GUI tests
372 pub npm: Option<String>,
374 /// Whether to rerun tests even if the inputs are unchanged.
375 pub force_rerun: bool,
377 pub target_cfg: LazyCell<TargetCfg>,
381 pub fn run_enabled(&self) -> bool {
382 self.run.unwrap_or_else(|| {
383 // Auto-detect whether to run based on the platform.
384 !self.target.ends_with("-fuchsia")
388 fn target_cfg(&self) -> &TargetCfg {
389 self.target_cfg.borrow_with(|| TargetCfg::new(&self.rustc_path, &self.target))
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 has_asm_support(&self) -> bool {
427 static ASM_SUPPORTED_ARCHS: &[&str] = &[
428 "x86", "x86_64", "arm", "aarch64", "riscv32",
430 // These targets require an additional asm_experimental_arch feature.
431 // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
433 ASM_SUPPORTED_ARCHS.contains(&self.target_cfg().arch.as_str())
437 #[derive(Clone, Debug)]
438 pub struct TargetCfg {
443 families: Vec<String>,
448 #[derive(Eq, PartialEq, Clone, Debug)]
455 fn new(rustc_path: &Path, target: &str) -> TargetCfg {
456 let output = match Command::new(rustc_path)
462 Ok(output) => output,
463 Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", rustc_path),
465 if !output.status.success() {
467 "error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
469 String::from_utf8(output.stdout).unwrap(),
470 String::from_utf8(output.stderr).unwrap(),
473 let print_cfg = String::from_utf8(output.stdout).unwrap();
478 let mut families = Vec::new();
479 let mut pointer_width = None;
480 let mut endian = None;
481 for line in print_cfg.lines() {
482 if let Some((name, value)) = line.split_once('=') {
483 let value = value.trim_matches('"');
485 "target_arch" => arch = Some(value),
486 "target_os" => os = Some(value),
487 "target_env" => env = Some(value),
488 "target_abi" => abi = Some(value),
489 "target_family" => families.push(value.to_string()),
490 "target_pointer_width" => pointer_width = Some(value.parse().unwrap()),
492 endian = Some(match value {
493 "little" => Endian::Little,
494 "big" => Endian::Big,
495 s => panic!("unexpected {s}"),
503 arch: arch.unwrap().to_string(),
504 os: os.unwrap().to_string(),
505 env: env.unwrap().to_string(),
506 abi: abi.unwrap().to_string(),
508 pointer_width: pointer_width.unwrap(),
509 endian: endian.unwrap(),
514 #[derive(Debug, Clone)]
515 pub struct TestPaths {
516 pub file: PathBuf, // e.g., compile-test/foo/bar/baz.rs
517 pub relative_dir: PathBuf, // e.g., foo/bar
520 /// Used by `ui` tests to generate things like `foo.stderr` from `foo.rs`.
521 pub fn expected_output_path(
522 testpaths: &TestPaths,
523 revision: Option<&str>,
524 compare_mode: &Option<CompareMode>,
527 assert!(UI_EXTENSIONS.contains(&kind));
528 let mut parts = Vec::new();
530 if let Some(x) = revision {
533 if let Some(ref x) = *compare_mode {
534 parts.push(x.to_str());
538 let extension = parts.join(".");
539 testpaths.file.with_extension(extension)
542 pub const UI_EXTENSIONS: &[&str] = &[
552 pub const UI_STDERR: &str = "stderr";
553 pub const UI_STDOUT: &str = "stdout";
554 pub const UI_FIXED: &str = "fixed";
555 pub const UI_RUN_STDERR: &str = "run.stderr";
556 pub const UI_RUN_STDOUT: &str = "run.stdout";
557 pub const UI_STDERR_64: &str = "64bit.stderr";
558 pub const UI_STDERR_32: &str = "32bit.stderr";
559 pub const UI_STDERR_16: &str = "16bit.stderr";
561 /// Absolute path to the directory where all output for all tests in the given
562 /// `relative_dir` group should reside. Example:
563 /// /path/to/build/host-triple/test/ui/relative/
564 /// This is created early when tests are collected to avoid race conditions.
565 pub fn output_relative_path(config: &Config, relative_dir: &Path) -> PathBuf {
566 config.build_base.join(relative_dir)
569 /// Generates a unique name for the test, such as `testname.revision.mode`.
570 pub fn output_testname_unique(
572 testpaths: &TestPaths,
573 revision: Option<&str>,
575 let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
576 let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
577 PathBuf::from(&testpaths.file.file_stem().unwrap())
578 .with_extra_extension(revision.unwrap_or(""))
579 .with_extra_extension(mode)
580 .with_extra_extension(debugger)
583 /// Absolute path to the directory where all output for the given
584 /// test/revision should reside. Example:
585 /// /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
586 pub fn output_base_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
587 output_relative_path(config, &testpaths.relative_dir)
588 .join(output_testname_unique(config, testpaths, revision))
591 /// Absolute path to the base filename used as output for the given
592 /// test/revision. Example:
593 /// /path/to/build/host-triple/test/ui/relative/testname.revision.mode/testname
594 pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
595 output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
598 /// Absolute path to the directory to use for incremental compilation. Example:
599 /// /path/to/build/host-triple/test/ui/relative/testname.mode/testname.inc
600 pub fn incremental_dir(config: &Config, testpaths: &TestPaths) -> PathBuf {
601 output_base_name(config, testpaths, None).with_extension("inc")