3 use std::ffi::OsString;
6 use std::path::{Path, PathBuf};
7 use std::process::Command;
10 use crate::util::{add_dylib_path, PathBufExt};
11 use lazycell::LazyCell;
12 use test::ColorConfig;
14 #[derive(Clone, Copy, PartialEq, Debug)]
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.
42 impl FromStr for Mode {
44 fn from_str(s: &str) -> Result<Mode, ()> {
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),
56 "js-doc-test" => Ok(JsDocTest),
57 "mir-opt" => Ok(MirOpt),
58 "assembly" => Ok(Assembly),
64 impl fmt::Display for Mode {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 RunPassValgrind => "run-pass-valgrind",
69 DebugInfo => "debuginfo",
72 RustdocJson => "rustdoc-json",
73 CodegenUnits => "codegen-units",
74 Incremental => "incremental",
75 RunMake => "run-make",
77 JsDocTest => "js-doc-test",
79 Assembly => "assembly",
81 fmt::Display::fmt(s, f)
85 #[derive(Clone, Copy, PartialEq, Debug, Hash)]
92 impl FromStr for PassMode {
94 fn from_str(s: &str) -> Result<Self, ()> {
96 "check" => Ok(PassMode::Check),
97 "build" => Ok(PassMode::Build),
98 "run" => Ok(PassMode::Run),
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",
111 fmt::Display::fmt(s, f)
115 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
122 #[derive(Clone, Debug, PartialEq)]
123 pub enum CompareMode {
131 pub(crate) fn to_str(&self) -> &'static str {
133 CompareMode::Polonius => "polonius",
134 CompareMode::Chalk => "chalk",
135 CompareMode::SplitDwarf => "split-dwarf",
136 CompareMode::SplitDwarfSingle => "split-dwarf-single",
140 pub fn parse(s: String) -> CompareMode {
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),
151 #[derive(Clone, Copy, Debug, PartialEq)]
159 fn to_str(&self) -> &'static str {
161 Debugger::Cdb => "cdb",
162 Debugger::Gdb => "gdb",
163 Debugger::Lldb => "lldb",
168 impl fmt::Display for Debugger {
169 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 fmt::Display::fmt(self.to_str(), f)
174 #[derive(Clone, Copy, Debug, PartialEq)]
175 pub enum PanicStrategy {
180 /// Configuration for compiletest
181 #[derive(Debug, Clone)]
183 /// `true` to overwrite stderr/stdout files instead of complaining about changes in output.
186 /// The library paths required for running the compiler.
187 pub compile_lib_path: PathBuf,
189 /// The library paths required for running compiled programs.
190 pub run_lib_path: PathBuf,
192 /// The rustc executable.
193 pub rustc_path: PathBuf,
195 /// The rustdoc executable.
196 pub rustdoc_path: Option<PathBuf>,
198 /// The rust-demangler executable.
199 pub rust_demangler_path: Option<PathBuf>,
201 /// The Python executable to use for LLDB and htmldocck.
204 /// The jsondocck executable.
205 pub jsondocck_path: Option<String>,
207 /// The jsondoclint executable.
208 pub jsondoclint_path: Option<String>,
210 /// The LLVM `FileCheck` binary path.
211 pub llvm_filecheck: Option<PathBuf>,
213 /// Path to LLVM's bin directory.
214 pub llvm_bin_dir: Option<PathBuf>,
216 /// The valgrind path.
217 pub valgrind_path: Option<String>,
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,
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>,
227 /// The directory containing the tests to run
228 pub src_base: PathBuf,
230 /// The directory where programs should be built
231 pub build_base: PathBuf,
233 /// The name of the stage being built (stage1, etc)
234 pub stage_id: String,
236 /// The test mode, e.g. ui or debuginfo.
239 /// The test suite (essentially which directory is running, but without the
240 /// directory prefix such as src/test)
243 /// The debugger to use in debuginfo mode. Unset otherwise.
244 pub debugger: Option<Debugger>,
246 /// Run ignored tests
247 pub run_ignored: bool,
249 /// Only run tests that match these filters
250 pub filters: Vec<String>,
252 /// Skip tests tests matching these substrings. Corresponds to
253 /// `test::TestOpts::skip`. `filter_exact` does not apply to these flags.
254 pub skip: Vec<String>,
256 /// Exactly match the filter, rather than a substring
257 pub filter_exact: bool,
259 /// Force the pass mode of a check/build/run-pass test to this mode.
260 pub force_pass_mode: Option<PassMode>,
262 /// Explicitly enable or disable running.
263 pub run: Option<bool>,
265 /// Write out a parseable log of tests that were run
266 pub logfile: Option<PathBuf>,
268 /// A command line to prefix program execution with,
269 /// for running under valgrind
270 pub runtool: Option<String>,
272 /// Flags to pass to the compiler when building for the host
273 pub host_rustcflags: Vec<String>,
275 /// Flags to pass to the compiler when building for the target
276 pub target_rustcflags: Vec<String>,
278 /// Whether tests should be optimized by default. Individual test-suites and test files may
279 /// override this setting.
280 pub optimize_tests: bool,
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))
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(config: &Config) -> TargetCfg {
461 let mut command = Command::new(&config.rustc_path);
462 add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
463 let output = match command
467 .args(&config.target_rustcflags)
470 Ok(output) => output,
471 Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", config.rustc_path),
473 if !output.status.success() {
475 "error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
477 String::from_utf8(output.stdout).unwrap(),
478 String::from_utf8(output.stderr).unwrap(),
481 let print_cfg = String::from_utf8(output.stdout).unwrap();
486 let mut families = Vec::new();
487 let mut pointer_width = None;
488 let mut endian = None;
489 let mut panic = None;
490 for line in print_cfg.lines() {
491 if let Some((name, value)) = line.split_once('=') {
492 let value = value.trim_matches('"');
494 "target_arch" => arch = Some(value),
495 "target_os" => os = Some(value),
496 "target_env" => env = Some(value),
497 "target_abi" => abi = Some(value),
498 "target_family" => families.push(value.to_string()),
499 "target_pointer_width" => pointer_width = Some(value.parse().unwrap()),
501 endian = Some(match value {
502 "little" => Endian::Little,
503 "big" => Endian::Big,
504 s => panic!("unexpected {s}"),
508 panic = match value {
509 "abort" => Some(PanicStrategy::Abort),
510 "unwind" => Some(PanicStrategy::Unwind),
511 s => panic!("unexpected {s}"),
519 arch: arch.unwrap().to_string(),
520 os: os.unwrap().to_string(),
521 env: env.unwrap().to_string(),
522 abi: abi.unwrap().to_string(),
524 pointer_width: pointer_width.unwrap(),
525 endian: endian.unwrap(),
526 panic: panic.unwrap(),
531 #[derive(Debug, Clone)]
532 pub struct TestPaths {
533 pub file: PathBuf, // e.g., compile-test/foo/bar/baz.rs
534 pub relative_dir: PathBuf, // e.g., foo/bar
537 /// Used by `ui` tests to generate things like `foo.stderr` from `foo.rs`.
538 pub fn expected_output_path(
539 testpaths: &TestPaths,
540 revision: Option<&str>,
541 compare_mode: &Option<CompareMode>,
544 assert!(UI_EXTENSIONS.contains(&kind));
545 let mut parts = Vec::new();
547 if let Some(x) = revision {
550 if let Some(ref x) = *compare_mode {
551 parts.push(x.to_str());
555 let extension = parts.join(".");
556 testpaths.file.with_extension(extension)
559 pub const UI_EXTENSIONS: &[&str] = &[
569 pub const UI_STDERR: &str = "stderr";
570 pub const UI_STDOUT: &str = "stdout";
571 pub const UI_FIXED: &str = "fixed";
572 pub const UI_RUN_STDERR: &str = "run.stderr";
573 pub const UI_RUN_STDOUT: &str = "run.stdout";
574 pub const UI_STDERR_64: &str = "64bit.stderr";
575 pub const UI_STDERR_32: &str = "32bit.stderr";
576 pub const UI_STDERR_16: &str = "16bit.stderr";
578 /// Absolute path to the directory where all output for all tests in the given
579 /// `relative_dir` group should reside. Example:
580 /// /path/to/build/host-triple/test/ui/relative/
581 /// This is created early when tests are collected to avoid race conditions.
582 pub fn output_relative_path(config: &Config, relative_dir: &Path) -> PathBuf {
583 config.build_base.join(relative_dir)
586 /// Generates a unique name for the test, such as `testname.revision.mode`.
587 pub fn output_testname_unique(
589 testpaths: &TestPaths,
590 revision: Option<&str>,
592 let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
593 let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
594 PathBuf::from(&testpaths.file.file_stem().unwrap())
595 .with_extra_extension(revision.unwrap_or(""))
596 .with_extra_extension(mode)
597 .with_extra_extension(debugger)
600 /// Absolute path to the directory where all output for the given
601 /// test/revision should reside. Example:
602 /// /path/to/build/host-triple/test/ui/relative/testname.revision.mode/
603 pub fn output_base_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
604 output_relative_path(config, &testpaths.relative_dir)
605 .join(output_testname_unique(config, testpaths, revision))
608 /// Absolute path to the base filename used as output for the given
609 /// test/revision. Example:
610 /// /path/to/build/host-triple/test/ui/relative/testname.revision.mode/testname
611 pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
612 output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
615 /// Absolute path to the directory to use for incremental compilation. Example:
616 /// /path/to/build/host-triple/test/ui/relative/testname.mode/testname.inc
617 pub fn incremental_dir(config: &Config, testpaths: &TestPaths) -> PathBuf {
618 output_base_name(config, testpaths, None).with_extension("inc")