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 directory containing the compiler sysroot
234 pub sysroot_base: PathBuf,
236 /// The name of the stage being built (stage1, etc)
237 pub stage_id: String,
239 /// The test mode, e.g. ui or debuginfo.
242 /// The test suite (essentially which directory is running, but without the
243 /// directory prefix such as src/test)
246 /// The debugger to use in debuginfo mode. Unset otherwise.
247 pub debugger: Option<Debugger>,
249 /// Run ignored tests
250 pub run_ignored: bool,
252 /// Only run tests that match these filters
253 pub filters: Vec<String>,
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>,
259 /// Exactly match the filter, rather than a substring
260 pub filter_exact: bool,
262 /// Force the pass mode of a check/build/run-pass test to this mode.
263 pub force_pass_mode: Option<PassMode>,
265 /// Explicitly enable or disable running.
266 pub run: Option<bool>,
268 /// Write out a parseable log of tests that were run
269 pub logfile: Option<PathBuf>,
271 /// A command line to prefix program execution with,
272 /// for running under valgrind
273 pub runtool: Option<String>,
275 /// Flags to pass to the compiler when building for the host
276 pub host_rustcflags: Vec<String>,
278 /// Flags to pass to the compiler when building for the target
279 pub target_rustcflags: Vec<String>,
281 /// Whether tests should be optimized by default. Individual test-suites and test files may
282 /// override this setting.
283 pub optimize_tests: bool,
285 /// Target system to be tested
288 /// Host triple for the compiler being invoked
291 /// Path to / name of the Microsoft Console Debugger (CDB) executable
292 pub cdb: Option<OsString>,
295 pub cdb_version: Option<[u16; 4]>,
297 /// Path to / name of the GDB executable
298 pub gdb: Option<String>,
300 /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
301 pub gdb_version: Option<u32>,
303 /// Whether GDB has native rust support
304 pub gdb_native_rust: bool,
307 pub lldb_version: Option<u32>,
309 /// Whether LLDB has native rust support
310 pub lldb_native_rust: bool,
313 pub llvm_version: Option<u32>,
315 /// Is LLVM a system LLVM
316 pub system_llvm: bool,
318 /// Path to the android tools
319 pub android_cross_path: PathBuf,
321 /// Extra parameter to run adb on arm-linux-androideabi
322 pub adb_path: String,
324 /// Extra parameter to run test suite on arm-linux-androideabi
325 pub adb_test_dir: String,
327 /// status whether android device available or not
328 pub adb_device_status: bool,
330 /// the path containing LLDB's Python module
331 pub lldb_python_dir: Option<String>,
333 /// Explain what's going on
336 /// Print one character per test instead of one line
339 /// Whether to use colors in test.
340 pub color: ColorConfig,
342 /// where to find the remote test client process, if we're using it
343 pub remote_test_client: Option<PathBuf>,
345 /// mode describing what file the actual ui output will be compared to
346 pub compare_mode: Option<CompareMode>,
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,
353 /// whether to run `tidy` when a rustdoc test fails
356 /// The current Rust channel
359 /// The default Rust edition
360 pub edition: Option<String>,
362 // Configuration for various run-make tests frobbing things like C compilers
363 // or querying about various LLVM component information.
367 pub cxxflags: String,
369 pub linker: Option<String>,
370 pub llvm_components: String,
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>,
377 /// Whether to rerun tests even if the inputs are unchanged.
378 pub force_rerun: bool,
380 pub target_cfg: LazyCell<TargetCfg>,
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")
391 fn target_cfg(&self) -> &TargetCfg {
392 self.target_cfg.borrow_with(|| TargetCfg::new(self))
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"))
405 pub fn matches_os(&self, os: &str) -> bool {
406 self.target_cfg().os == os
409 pub fn matches_env(&self, env: &str) -> bool {
410 self.target_cfg().env == env
413 pub fn matches_abi(&self, abi: &str) -> bool {
414 self.target_cfg().abi == abi
417 pub fn matches_family(&self, family: &str) -> bool {
418 self.target_cfg().families.iter().any(|f| f == family)
421 pub fn is_big_endian(&self) -> bool {
422 self.target_cfg().endian == Endian::Big
425 pub fn get_pointer_width(&self) -> u32 {
426 *&self.target_cfg().pointer_width
429 pub fn can_unwind(&self) -> bool {
430 self.target_cfg().panic == PanicStrategy::Unwind
433 pub fn has_asm_support(&self) -> bool {
434 static ASM_SUPPORTED_ARCHS: &[&str] = &[
435 "x86", "x86_64", "arm", "aarch64", "riscv32",
437 // These targets require an additional asm_experimental_arch feature.
438 // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
440 ASM_SUPPORTED_ARCHS.contains(&self.target_cfg().arch.as_str())
444 #[derive(Clone, Debug)]
445 pub struct TargetCfg {
450 families: Vec<String>,
453 panic: PanicStrategy,
456 #[derive(Eq, PartialEq, Clone, Debug)]
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
470 .args(&config.target_rustcflags)
473 Ok(output) => output,
474 Err(e) => panic!("error: failed to get cfg info from {:?}: {e}", config.rustc_path),
476 if !output.status.success() {
478 "error: failed to get cfg info from {:?}\n--- stdout\n{}\n--- stderr\n{}",
480 String::from_utf8(output.stdout).unwrap(),
481 String::from_utf8(output.stderr).unwrap(),
484 let print_cfg = String::from_utf8(output.stdout).unwrap();
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('"');
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()),
504 endian = Some(match value {
505 "little" => Endian::Little,
506 "big" => Endian::Big,
507 s => panic!("unexpected {s}"),
511 panic = match value {
512 "abort" => Some(PanicStrategy::Abort),
513 "unwind" => Some(PanicStrategy::Unwind),
514 s => panic!("unexpected {s}"),
522 arch: arch.unwrap().to_string(),
523 os: os.unwrap().to_string(),
524 env: env.unwrap().to_string(),
525 abi: abi.unwrap().to_string(),
527 pointer_width: pointer_width.unwrap(),
528 endian: endian.unwrap(),
529 panic: panic.unwrap(),
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
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>,
547 assert!(UI_EXTENSIONS.contains(&kind));
548 let mut parts = Vec::new();
550 if let Some(x) = revision {
553 if let Some(ref x) = *compare_mode {
554 parts.push(x.to_str());
558 let extension = parts.join(".");
559 testpaths.file.with_extension(extension)
562 pub const UI_EXTENSIONS: &[&str] = &[
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";
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)
589 /// Generates a unique name for the test, such as `testname.revision.mode`.
590 pub fn output_testname_unique(
592 testpaths: &TestPaths,
593 revision: Option<&str>,
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)
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))
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())
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")