compiletest/
common.rs

1use std::collections::{BTreeSet, HashMap, HashSet};
2use std::iter;
3use std::process::Command;
4use std::sync::OnceLock;
5
6use build_helper::git::GitConfig;
7use camino::{Utf8Path, Utf8PathBuf};
8use semver::Version;
9use serde::de::{Deserialize, Deserializer, Error as _};
10
11use crate::executor::{ColorConfig, OutputFormat};
12use crate::fatal;
13use crate::util::{Utf8PathBufExt, add_dylib_path, string_enum};
14
15string_enum! {
16    #[derive(Clone, Copy, PartialEq, Debug)]
17    pub enum TestMode {
18        Pretty => "pretty",
19        DebugInfo => "debuginfo",
20        Codegen => "codegen",
21        Rustdoc => "rustdoc",
22        RustdocJson => "rustdoc-json",
23        CodegenUnits => "codegen-units",
24        Incremental => "incremental",
25        RunMake => "run-make",
26        Ui => "ui",
27        RustdocJs => "rustdoc-js",
28        MirOpt => "mir-opt",
29        Assembly => "assembly",
30        CoverageMap => "coverage-map",
31        CoverageRun => "coverage-run",
32        Crashes => "crashes",
33    }
34}
35
36impl TestMode {
37    pub fn aux_dir_disambiguator(self) -> &'static str {
38        // Pretty-printing tests could run concurrently, and if they do,
39        // they need to keep their output segregated.
40        match self {
41            TestMode::Pretty => ".pretty",
42            _ => "",
43        }
44    }
45
46    pub fn output_dir_disambiguator(self) -> &'static str {
47        // Coverage tests use the same test files for multiple test modes,
48        // so each mode should have a separate output directory.
49        match self {
50            TestMode::CoverageMap | TestMode::CoverageRun => self.to_str(),
51            _ => "",
52        }
53    }
54}
55
56// Note that coverage tests use the same test files for multiple test modes.
57string_enum! {
58    #[derive(Clone, Copy, PartialEq, Debug)]
59    pub enum TestSuite {
60        AssemblyLlvm => "assembly-llvm",
61        CodegenLlvm => "codegen-llvm",
62        CodegenUnits => "codegen-units",
63        Coverage => "coverage",
64        CoverageRunRustdoc => "coverage-run-rustdoc",
65        Crashes => "crashes",
66        Debuginfo => "debuginfo",
67        Incremental => "incremental",
68        MirOpt => "mir-opt",
69        Pretty => "pretty",
70        RunMake => "run-make",
71        Rustdoc => "rustdoc",
72        RustdocGui => "rustdoc-gui",
73        RustdocJs => "rustdoc-js",
74        RustdocJsStd=> "rustdoc-js-std",
75        RustdocJson => "rustdoc-json",
76        RustdocUi => "rustdoc-ui",
77        Ui => "ui",
78        UiFullDeps => "ui-fulldeps",
79    }
80}
81
82string_enum! {
83    #[derive(Clone, Copy, PartialEq, Debug, Hash)]
84    pub enum PassMode {
85        Check => "check",
86        Build => "build",
87        Run => "run",
88    }
89}
90
91string_enum! {
92    #[derive(Clone, Copy, PartialEq, Debug, Hash)]
93    pub enum RunResult {
94        Pass => "run-pass",
95        Fail => "run-fail",
96        Crash => "run-crash",
97    }
98}
99
100#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
101pub enum RunFailMode {
102    /// Running the program must make it exit with a regular failure exit code
103    /// in the range `1..=127`. If the program is terminated by e.g. a signal
104    /// the test will fail.
105    Fail,
106    /// Running the program must result in a crash, e.g. by `SIGABRT` or
107    /// `SIGSEGV` on Unix or on Windows by having an appropriate NTSTATUS high
108    /// bit in the exit code.
109    Crash,
110    /// Running the program must either fail or crash. Useful for e.g. sanitizer
111    /// tests since some sanitizer implementations exit the process with code 1
112    /// to in the face of memory errors while others abort (crash) the process
113    /// in the face of memory errors.
114    FailOrCrash,
115}
116
117#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
118pub enum FailMode {
119    Check,
120    Build,
121    Run(RunFailMode),
122}
123
124string_enum! {
125    #[derive(Clone, Debug, PartialEq)]
126    pub enum CompareMode {
127        Polonius => "polonius",
128        NextSolver => "next-solver",
129        NextSolverCoherence => "next-solver-coherence",
130        SplitDwarf => "split-dwarf",
131        SplitDwarfSingle => "split-dwarf-single",
132    }
133}
134
135string_enum! {
136    #[derive(Clone, Copy, Debug, PartialEq)]
137    pub enum Debugger {
138        Cdb => "cdb",
139        Gdb => "gdb",
140        Lldb => "lldb",
141    }
142}
143
144#[derive(Clone, Copy, Debug, PartialEq, Default, serde::Deserialize)]
145#[serde(rename_all = "kebab-case")]
146pub enum PanicStrategy {
147    #[default]
148    Unwind,
149    Abort,
150}
151
152impl PanicStrategy {
153    pub(crate) fn for_miropt_test_tools(&self) -> miropt_test_tools::PanicStrategy {
154        match self {
155            PanicStrategy::Unwind => miropt_test_tools::PanicStrategy::Unwind,
156            PanicStrategy::Abort => miropt_test_tools::PanicStrategy::Abort,
157        }
158    }
159}
160
161#[derive(Clone, Debug, PartialEq, serde::Deserialize)]
162#[serde(rename_all = "kebab-case")]
163pub enum Sanitizer {
164    Address,
165    Cfi,
166    Dataflow,
167    Kcfi,
168    KernelAddress,
169    Leak,
170    Memory,
171    Memtag,
172    Safestack,
173    ShadowCallStack,
174    Thread,
175    Hwaddress,
176}
177
178#[derive(Clone, Copy, Debug, PartialEq)]
179pub enum CodegenBackend {
180    Cranelift,
181    Gcc,
182    Llvm,
183}
184
185impl<'a> TryFrom<&'a str> for CodegenBackend {
186    type Error = &'static str;
187
188    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
189        match value.to_lowercase().as_str() {
190            "cranelift" => Ok(Self::Cranelift),
191            "gcc" => Ok(Self::Gcc),
192            "llvm" => Ok(Self::Llvm),
193            _ => Err("unknown backend"),
194        }
195    }
196}
197
198impl CodegenBackend {
199    pub fn as_str(self) -> &'static str {
200        match self {
201            Self::Cranelift => "cranelift",
202            Self::Gcc => "gcc",
203            Self::Llvm => "llvm",
204        }
205    }
206}
207
208/// Configuration for `compiletest` *per invocation*.
209///
210/// In terms of `bootstrap`, this means that `./x test tests/ui tests/run-make` actually correspond
211/// to *two* separate invocations of `compiletest`.
212///
213/// FIXME: this `Config` struct should be broken up into smaller logically contained sub-config
214/// structs, it's too much of a "soup" of everything at the moment.
215///
216/// # Configuration sources
217///
218/// Configuration values for `compiletest` comes from several sources:
219///
220/// - CLI args passed from `bootstrap` while running the `compiletest` binary.
221/// - Env vars.
222/// - Discovery (e.g. trying to identify a suitable debugger based on filesystem discovery).
223/// - Cached output of running the `rustc` under test (e.g. output of `rustc` print requests).
224///
225/// FIXME: make sure we *clearly* account for sources of *all* config options.
226///
227/// FIXME: audit these options to make sure we are not hashing less than necessary for build stamp
228/// (for changed test detection).
229#[derive(Debug, Clone)]
230pub struct Config {
231    /// Some [`TestMode`]s support [snapshot testing], where a *reference snapshot* of outputs (of
232    /// `stdout`, `stderr`, or other form of artifacts) can be compared to the *actual output*.
233    ///
234    /// This option can be set to `true` to update the *reference snapshots* in-place, otherwise
235    /// `compiletest` will only try to compare.
236    ///
237    /// [snapshot testing]: https://jestjs.io/docs/snapshot-testing
238    pub bless: bool,
239
240    /// Attempt to stop as soon as possible after any test fails. We may still run a few more tests
241    /// before stopping when multiple test threads are used.
242    pub fail_fast: bool,
243
244    /// Path to libraries needed to run the *staged* `rustc`-under-test on the **host** platform.
245    ///
246    /// FIXME: maybe rename this to reflect (1) which target platform (host, not target), and (2)
247    /// which `rustc` (the `rustc`-under-test, not the stage 0 `rustc` unless forced).
248    pub compile_lib_path: Utf8PathBuf,
249
250    /// Path to libraries needed to run the compiled executable for the **target** platform. This
251    /// corresponds to the **target** sysroot libraries, including the **target** standard library.
252    ///
253    /// FIXME: maybe rename this to reflect (1) which target platform (target, not host), and (2)
254    /// what "run libraries" are against.
255    ///
256    /// FIXME: this is very under-documented in conjunction with the `remote-test-client` scheme and
257    /// `RUNNER` scheme to actually run the target executable under the target platform environment,
258    /// cf. [`Self::remote_test_client`] and [`Self::runner`].
259    pub run_lib_path: Utf8PathBuf,
260
261    /// Path to the *staged*  `rustc`-under-test. Unless forced, this `rustc` is *staged*, and must
262    /// not be confused with [`Self::stage0_rustc_path`].
263    ///
264    /// FIXME: maybe rename this to reflect that this is the `rustc`-under-test.
265    pub rustc_path: Utf8PathBuf,
266
267    /// Path to a *staged* **host** platform cargo executable (unless stage 0 is forced). This
268    /// staged `cargo` is only used within `run-make` test recipes during recipe run time (and is
269    /// *not* used to compile the test recipes), and so must be staged as there may be differences
270    /// between e.g. beta `cargo` vs in-tree `cargo`.
271    ///
272    /// FIXME: maybe rename this to reflect that this is a *staged* host cargo.
273    ///
274    /// FIXME(#134109): split `run-make` into two test suites, a test suite *with* staged cargo, and
275    /// another test suite *without*.
276    pub cargo_path: Option<Utf8PathBuf>,
277
278    /// Path to the stage 0 `rustc` used to build `run-make` recipes. This must not be confused with
279    /// [`Self::rustc_path`].
280    pub stage0_rustc_path: Option<Utf8PathBuf>,
281
282    /// Path to the stage 1 or higher `rustc` used to obtain target information via
283    /// `--print=all-target-specs-json` and similar queries.
284    ///
285    /// Normally this is unset, because [`Self::rustc_path`] can be used instead.
286    /// But when running "stage 1" ui-fulldeps tests, `rustc_path` is a stage 0
287    /// compiler, whereas target specs must be obtained from a stage 1+ compiler
288    /// (in case the JSON format has changed since the last bootstrap bump).
289    pub query_rustc_path: Option<Utf8PathBuf>,
290
291    /// Path to the `rustdoc`-under-test. Like [`Self::rustc_path`], this `rustdoc` is *staged*.
292    pub rustdoc_path: Option<Utf8PathBuf>,
293
294    /// Path to the `src/tools/coverage-dump/` bootstrap tool executable.
295    pub coverage_dump_path: Option<Utf8PathBuf>,
296
297    /// Path to the Python 3 executable to use for LLDB and htmldocck.
298    ///
299    /// FIXME: the `lldb` setup currently requires I believe Python 3.10 **exactly**, it can't even
300    /// be Python 3.11 or 3.9...
301    pub python: String,
302
303    /// Path to the `src/tools/jsondocck/` bootstrap tool executable.
304    pub jsondocck_path: Option<String>,
305
306    /// Path to the `src/tools/jsondoclint/` bootstrap tool executable.
307    pub jsondoclint_path: Option<String>,
308
309    /// Path to a host LLVM `FileCheck` executable.
310    pub llvm_filecheck: Option<Utf8PathBuf>,
311
312    /// Path to a host LLVM bintools directory.
313    pub llvm_bin_dir: Option<Utf8PathBuf>,
314
315    /// The path to the **target** `clang` executable to run `clang`-based tests with. If `None`,
316    /// then these tests will be ignored.
317    pub run_clang_based_tests_with: Option<String>,
318
319    /// Path to the directory containing the sources. This corresponds to the root folder of a
320    /// `rust-lang/rust` checkout.
321    ///
322    /// FIXME: this name is confusing, because this is actually `$checkout_root`, **not** the
323    /// `$checkout_root/src/` folder.
324    pub src_root: Utf8PathBuf,
325
326    /// Path to the directory containing the test suites sources. This corresponds to the
327    /// `$src_root/tests/` folder.
328    ///
329    /// Must be an immediate subdirectory of [`Self::src_root`].
330    ///
331    /// FIXME: this name is also confusing, maybe just call it `tests_root`.
332    pub src_test_suite_root: Utf8PathBuf,
333
334    /// Path to the build directory (e.g. `build/`).
335    pub build_root: Utf8PathBuf,
336
337    /// Path to the test suite specific build directory (e.g. `build/host/test/ui/`).
338    ///
339    /// Must be a subdirectory of [`Self::build_root`].
340    pub build_test_suite_root: Utf8PathBuf,
341
342    /// Path to the directory containing the sysroot of the `rustc`-under-test.
343    ///
344    /// When stage 0 is forced, this will correspond to the sysroot *of* that specified stage 0
345    /// `rustc`.
346    ///
347    /// FIXME: this name is confusing, because it doesn't specify *which* compiler this sysroot
348    /// corresponds to. It's actually the `rustc`-under-test, and not the bootstrap `rustc`, unless
349    /// stage 0 is forced and no custom stage 0 `rustc` was otherwise specified (so that it
350    /// *happens* to run against the bootstrap `rustc`, but this non-custom bootstrap `rustc` case
351    /// is not really supported).
352    pub sysroot_base: Utf8PathBuf,
353
354    /// The number of the stage under test.
355    pub stage: u32,
356
357    /// The id of the stage under test (stage1-xxx, etc).
358    ///
359    /// FIXME: reconsider this string; this is hashed for test build stamp.
360    pub stage_id: String,
361
362    /// The [`TestMode`]. E.g. [`TestMode::Ui`]. Each test mode can correspond to one or more test
363    /// suites.
364    ///
365    /// FIXME: stop using stringly-typed test suites!
366    pub mode: TestMode,
367
368    /// The test suite.
369    ///
370    /// Example: `tests/ui/` is [`TestSuite::Ui`] test *suite*, which happens to also be of the
371    /// [`TestMode::Ui`] test *mode*.
372    ///
373    /// Note that the same test suite (e.g. `tests/coverage/`) may correspond to multiple test
374    /// modes, e.g. `tests/coverage/` can be run under both [`TestMode::CoverageRun`] and
375    /// [`TestMode::CoverageMap`].
376    pub suite: TestSuite,
377
378    /// When specified, **only** the specified [`Debugger`] will be used to run against the
379    /// `tests/debuginfo` test suite. When unspecified, `compiletest` will attempt to find all three
380    /// of {`lldb`, `cdb`, `gdb`} implicitly, and then try to run the `debuginfo` test suite against
381    /// all three debuggers.
382    ///
383    /// FIXME: this implicit behavior is really nasty, in that it makes it hard for the user to
384    /// control *which* debugger(s) are available and used to run the debuginfo test suite. We
385    /// should have `bootstrap` allow the user to *explicitly* configure the debuggers, and *not*
386    /// try to implicitly discover some random debugger from the user environment. This makes the
387    /// debuginfo test suite particularly hard to work with.
388    pub debugger: Option<Debugger>,
389
390    /// Run ignored tests *unconditionally*, overriding their ignore reason.
391    ///
392    /// FIXME: this is wired up through the test execution logic, but **not** accessible from
393    /// `bootstrap` directly; `compiletest` exposes this as `--ignored`. I.e. you'd have to use `./x
394    /// test $test_suite -- --ignored=true`.
395    pub run_ignored: bool,
396
397    /// Whether *staged* `rustc`-under-test was built with debug assertions.
398    ///
399    /// FIXME: make it clearer that this refers to the staged `rustc`-under-test, not stage 0
400    /// `rustc`.
401    pub with_rustc_debug_assertions: bool,
402
403    /// Whether *staged* `std` was built with debug assertions.
404    ///
405    /// FIXME: make it clearer that this refers to the staged `std`, not stage 0 `std`.
406    pub with_std_debug_assertions: bool,
407
408    /// Only run tests that match these filters (using `libtest` "test name contains" filter logic).
409    ///
410    /// FIXME(#139660): the current hand-rolled test executor intentionally mimics the `libtest`
411    /// "test name contains" filter matching logic to preserve previous `libtest` executor behavior,
412    /// but this is often not intuitive. We should consider changing that behavior with an MCP to do
413    /// test path *prefix* matching which better corresponds to how `compiletest` `tests/` are
414    /// organized, and how users would intuitively expect the filtering logic to work like.
415    pub filters: Vec<String>,
416
417    /// Skip tests matching these substrings. The matching logic exactly corresponds to
418    /// [`Self::filters`] but inverted.
419    ///
420    /// FIXME(#139660): ditto on test matching behavior.
421    pub skip: Vec<String>,
422
423    /// Exactly match the filter, rather than a substring.
424    ///
425    /// FIXME(#139660): ditto on test matching behavior.
426    pub filter_exact: bool,
427
428    /// Force the pass mode of a check/build/run test to instead use this mode instead.
429    ///
430    /// FIXME: make it even more obvious (especially in PR CI where `--pass=check` is used) when a
431    /// pass mode is forced when the test fails, because it can be very non-obvious when e.g. an
432    /// error is emitted only when `//@ build-pass` but not `//@ check-pass`.
433    pub force_pass_mode: Option<PassMode>,
434
435    /// Explicitly enable or disable running of the target test binary.
436    ///
437    /// FIXME: this scheme is a bit confusing, and at times questionable. Re-evaluate this run
438    /// scheme.
439    ///
440    /// FIXME: Currently `--run` is a tri-state, it can be `--run={auto,always,never}`, and when
441    /// `--run=auto` is specified, it's run if the platform doesn't end with `-fuchsia`. See
442    /// [`Config::run_enabled`].
443    pub run: Option<bool>,
444
445    /// A command line to prefix target program execution with, for running under valgrind for
446    /// example, i.e. `$runner target.exe [args..]`. Similar to `CARGO_*_RUNNER` configuration.
447    ///
448    /// Note: this is not to be confused with [`Self::remote_test_client`], which is a different
449    /// scheme.
450    ///
451    /// FIXME: the runner scheme is very under-documented.
452    pub runner: Option<String>,
453
454    /// Compiler flags to pass to the *staged* `rustc`-under-test when building for the **host**
455    /// platform.
456    pub host_rustcflags: Vec<String>,
457
458    /// Compiler flags to pass to the *staged* `rustc`-under-test when building for the **target**
459    /// platform.
460    pub target_rustcflags: Vec<String>,
461
462    /// Whether the *staged* `rustc`-under-test and the associated *staged* `std` has been built
463    /// with randomized struct layouts.
464    pub rust_randomized_layout: bool,
465
466    /// Whether tests should be optimized by default (`-O`). Individual test suites and test files
467    /// may override this setting.
468    ///
469    /// FIXME: this flag / config option is somewhat misleading. For instance, in ui tests, it's
470    /// *only* applied to the [`PassMode::Run`] test crate and not its auxiliaries.
471    pub optimize_tests: bool,
472
473    /// Target platform tuple.
474    pub target: String,
475
476    /// Host platform tuple.
477    pub host: String,
478
479    /// Path to / name of the Microsoft Console Debugger (CDB) executable.
480    ///
481    /// FIXME: this is an *opt-in* "override" option. When this isn't provided, we try to conjure a
482    /// cdb by looking at the user's program files on Windows... See `debuggers::find_cdb`.
483    pub cdb: Option<Utf8PathBuf>,
484
485    /// Version of CDB.
486    ///
487    /// FIXME: `cdb_version` is *derived* from cdb, but it's *not* technically a config!
488    ///
489    /// FIXME: audit cdb version gating.
490    pub cdb_version: Option<[u16; 4]>,
491
492    /// Path to / name of the GDB executable.
493    ///
494    /// FIXME: the fallback path when `gdb` isn't provided tries to find *a* `gdb` or `gdb.exe` from
495    /// `PATH`, which is... arguably questionable.
496    ///
497    /// FIXME: we are propagating a python from `PYTHONPATH`, not from an explicit config for gdb
498    /// debugger script.
499    pub gdb: Option<String>,
500
501    /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
502    ///
503    /// FIXME: this gdb version gating scheme is possibly questionable -- gdb does not use semver,
504    /// only its major version is likely materially meaningful, cf.
505    /// <https://sourceware.org/gdb/wiki/Internals%20Versions>. Even the major version I'm not sure
506    /// is super meaningful. Maybe min gdb `major.minor` version gating is sufficient for the
507    /// purposes of debuginfo tests?
508    ///
509    /// FIXME: `gdb_version` is *derived* from gdb, but it's *not* technically a config!
510    pub gdb_version: Option<u32>,
511
512    /// Version of LLDB.
513    ///
514    /// FIXME: `lldb_version` is *derived* from lldb, but it's *not* technically a config!
515    pub lldb_version: Option<u32>,
516
517    /// Version of LLVM.
518    ///
519    /// FIXME: Audit the fallback derivation of
520    /// [`crate::directives::extract_llvm_version_from_binary`], that seems very questionable?
521    pub llvm_version: Option<Version>,
522
523    /// Is LLVM a system LLVM.
524    pub system_llvm: bool,
525
526    /// Path to the android tools.
527    ///
528    /// Note: this is only used for android gdb debugger script in the debuginfo test suite.
529    ///
530    /// FIXME: take a look at this; this is piggy-backing off of gdb code paths but only for
531    /// `arm-linux-androideabi` target.
532    pub android_cross_path: Utf8PathBuf,
533
534    /// Extra parameter to run adb on `arm-linux-androideabi`.
535    ///
536    /// FIXME: is this *only* `arm-linux-androideabi`, or is it also for other Tier 2/3 android
537    /// targets?
538    ///
539    /// FIXME: take a look at this; this is piggy-backing off of gdb code paths but only for
540    /// `arm-linux-androideabi` target.
541    pub adb_path: String,
542
543    /// Extra parameter to run test suite on `arm-linux-androideabi`.
544    ///
545    /// FIXME: is this *only* `arm-linux-androideabi`, or is it also for other Tier 2/3 android
546    /// targets?
547    ///
548    /// FIXME: take a look at this; this is piggy-backing off of gdb code paths but only for
549    /// `arm-linux-androideabi` target.
550    pub adb_test_dir: String,
551
552    /// Status whether android device available or not. When unavailable, this will cause tests to
553    /// panic when the test binary is attempted to be run.
554    ///
555    /// FIXME: take a look at this; this also influences adb in gdb code paths in a strange way.
556    pub adb_device_status: bool,
557
558    /// Path containing LLDB's Python module.
559    ///
560    /// FIXME: `PYTHONPATH` takes precedence over this flag...? See `runtest::run_lldb`.
561    pub lldb_python_dir: Option<String>,
562
563    /// Verbose dump a lot of info.
564    ///
565    /// FIXME: this is *way* too coarse; the user can't select *which* info to verbosely dump.
566    pub verbose: bool,
567
568    /// (Useless) Adjust libtest output format.
569    ///
570    /// FIXME: the hand-rolled executor does not support non-JSON output, because `compiletest` need
571    /// to package test outcome as `libtest`-esque JSON that `bootstrap` can intercept *anyway*.
572    /// However, now that we don't use the `libtest` executor, this is useless.
573    pub format: OutputFormat,
574
575    /// Whether to use colors in test output.
576    ///
577    /// Note: the exact control mechanism is delegated to [`colored`].
578    pub color: ColorConfig,
579
580    /// Where to find the remote test client process, if we're using it.
581    ///
582    /// Note: this is *only* used for target platform executables created by `run-make` test
583    /// recipes.
584    ///
585    /// Note: this is not to be confused with [`Self::runner`], which is a different scheme.
586    ///
587    /// FIXME: the `remote_test_client` scheme is very under-documented.
588    pub remote_test_client: Option<Utf8PathBuf>,
589
590    /// [`CompareMode`] describing what file the actual ui output will be compared to.
591    ///
592    /// FIXME: currently, [`CompareMode`] is a mishmash of lot of things (different borrow-checker
593    /// model, different trait solver, different debugger, etc.).
594    pub compare_mode: Option<CompareMode>,
595
596    /// If true, this will generate a coverage file with UI test files that run `MachineApplicable`
597    /// diagnostics but are missing `run-rustfix` annotations. The generated coverage file is
598    /// created in `$test_suite_build_root/rustfix_missing_coverage.txt`
599    pub rustfix_coverage: bool,
600
601    /// Whether to run `tidy` (html-tidy) when a rustdoc test fails.
602    pub has_html_tidy: bool,
603
604    /// Whether to run `enzyme` autodiff tests.
605    pub has_enzyme: bool,
606
607    /// The current Rust channel info.
608    ///
609    /// FIXME: treat this more carefully; "stable", "beta" and "nightly" are definitely valid, but
610    /// channel might also be "dev" or such, which should be treated as "nightly".
611    pub channel: String,
612
613    /// Whether adding git commit information such as the commit hash has been enabled for building.
614    ///
615    /// FIXME: `compiletest` cannot trust `bootstrap` for this information, because `bootstrap` can
616    /// have bugs and had bugs on that logic. We need to figure out how to obtain this e.g. directly
617    /// from CI or via git locally.
618    pub git_hash: bool,
619
620    /// The default Rust edition.
621    ///
622    /// FIXME: perform stronger validation for this. There are editions that *definitely* exists,
623    /// but there might also be "future" edition.
624    pub edition: Option<String>,
625
626    // Configuration for various run-make tests frobbing things like C compilers or querying about
627    // various LLVM component information.
628    //
629    // FIXME: this really should be better packaged together.
630    // FIXME: these need better docs, e.g. for *host*, or for *target*?
631    pub cc: String,
632    pub cxx: String,
633    pub cflags: String,
634    pub cxxflags: String,
635    pub ar: String,
636    pub target_linker: Option<String>,
637    pub host_linker: Option<String>,
638    pub llvm_components: String,
639
640    /// Path to a NodeJS executable. Used for JS doctests, emscripten and WASM tests.
641    pub nodejs: Option<String>,
642    /// Path to a npm executable. Used for rustdoc GUI tests.
643    pub npm: Option<String>,
644
645    /// Whether to rerun tests even if the inputs are unchanged.
646    pub force_rerun: bool,
647
648    /// Only rerun the tests that result has been modified according to `git status`.
649    ///
650    /// FIXME: this is undocumented.
651    ///
652    /// FIXME: how does this interact with [`Self::force_rerun`]?
653    pub only_modified: bool,
654
655    // FIXME: these are really not "config"s, but rather are information derived from
656    // `rustc`-under-test. This poses an interesting conundrum: if we're testing the
657    // `rustc`-under-test, can we trust its print request outputs and target cfgs? In theory, this
658    // itself can break or be unreliable -- ideally, we'd be sharing these kind of information not
659    // through `rustc`-under-test's execution output. In practice, however, print requests are very
660    // unlikely to completely break (we also have snapshot ui tests for them). Furthermore, even if
661    // we share them via some kind of static config, that static config can still be wrong! Who
662    // tests the tester? Therefore, we make a pragmatic compromise here, and use information derived
663    // from print requests produced by the `rustc`-under-test.
664    //
665    // FIXME: move them out from `Config`, because they are *not* configs.
666    pub target_cfgs: OnceLock<TargetCfgs>,
667    pub builtin_cfg_names: OnceLock<HashSet<String>>,
668    pub supported_crate_types: OnceLock<HashSet<String>>,
669
670    /// FIXME: this is why we still need to depend on *staged* `std`, it's because we currently rely
671    /// on `#![feature(internal_output_capture)]` for [`std::io::set_output_capture`] to implement
672    /// `libtest`-esque `--no-capture`.
673    ///
674    /// FIXME: rename this to the more canonical `no_capture`, or better, invert this to `capture`
675    /// to avoid `!nocapture` double-negatives.
676    pub nocapture: bool,
677
678    /// Needed both to construct [`build_helper::git::GitConfig`].
679    pub nightly_branch: String,
680    pub git_merge_commit_email: String,
681
682    /// True if the profiler runtime is enabled for this target. Used by the
683    /// `needs-profiler-runtime` directive in test files.
684    pub profiler_runtime: bool,
685
686    /// Command for visual diff display, e.g. `diff-tool --color=always`.
687    pub diff_command: Option<String>,
688
689    /// Path to minicore aux library (`tests/auxiliary/minicore.rs`), used for `no_core` tests that
690    /// need `core` stubs in cross-compilation scenarios that do not otherwise want/need to
691    /// `-Zbuild-std`. Used in e.g. ABI tests.
692    pub minicore_path: Utf8PathBuf,
693
694    /// Current codegen backend used.
695    pub default_codegen_backend: CodegenBackend,
696    /// Name/path of the backend to use instead of `default_codegen_backend`.
697    pub override_codegen_backend: Option<String>,
698}
699
700impl Config {
701    /// Incomplete config intended for `src/tools/rustdoc-gui-test` **only** as
702    /// `src/tools/rustdoc-gui-test` wants to reuse `compiletest`'s directive -> test property
703    /// handling for `//@ {compile,run}-flags`, do not use for any other purpose.
704    ///
705    /// FIXME(#143827): this setup feels very hacky. It so happens that `tests/rustdoc-gui/`
706    /// **only** uses `//@ {compile,run}-flags` for now and not any directives that actually rely on
707    /// info that is assumed available in a fully populated [`Config`].
708    pub fn incomplete_for_rustdoc_gui_test() -> Config {
709        // FIXME(#143827): spelling this out intentionally, because this is questionable.
710        //
711        // For instance, `//@ ignore-stage1` will not work at all.
712        Config {
713            mode: TestMode::Rustdoc,
714            // E.g. this has no sensible default tbh.
715            suite: TestSuite::Ui,
716
717            // Dummy values.
718            edition: Default::default(),
719            bless: Default::default(),
720            fail_fast: Default::default(),
721            compile_lib_path: Utf8PathBuf::default(),
722            run_lib_path: Utf8PathBuf::default(),
723            rustc_path: Utf8PathBuf::default(),
724            cargo_path: Default::default(),
725            stage0_rustc_path: Default::default(),
726            query_rustc_path: Default::default(),
727            rustdoc_path: Default::default(),
728            coverage_dump_path: Default::default(),
729            python: Default::default(),
730            jsondocck_path: Default::default(),
731            jsondoclint_path: Default::default(),
732            llvm_filecheck: Default::default(),
733            llvm_bin_dir: Default::default(),
734            run_clang_based_tests_with: Default::default(),
735            src_root: Utf8PathBuf::default(),
736            src_test_suite_root: Utf8PathBuf::default(),
737            build_root: Utf8PathBuf::default(),
738            build_test_suite_root: Utf8PathBuf::default(),
739            sysroot_base: Utf8PathBuf::default(),
740            stage: Default::default(),
741            stage_id: String::default(),
742            debugger: Default::default(),
743            run_ignored: Default::default(),
744            with_rustc_debug_assertions: Default::default(),
745            with_std_debug_assertions: Default::default(),
746            filters: Default::default(),
747            skip: Default::default(),
748            filter_exact: Default::default(),
749            force_pass_mode: Default::default(),
750            run: Default::default(),
751            runner: Default::default(),
752            host_rustcflags: Default::default(),
753            target_rustcflags: Default::default(),
754            rust_randomized_layout: Default::default(),
755            optimize_tests: Default::default(),
756            target: Default::default(),
757            host: Default::default(),
758            cdb: Default::default(),
759            cdb_version: Default::default(),
760            gdb: Default::default(),
761            gdb_version: Default::default(),
762            lldb_version: Default::default(),
763            llvm_version: Default::default(),
764            system_llvm: Default::default(),
765            android_cross_path: Default::default(),
766            adb_path: Default::default(),
767            adb_test_dir: Default::default(),
768            adb_device_status: Default::default(),
769            lldb_python_dir: Default::default(),
770            verbose: Default::default(),
771            format: Default::default(),
772            color: Default::default(),
773            remote_test_client: Default::default(),
774            compare_mode: Default::default(),
775            rustfix_coverage: Default::default(),
776            has_html_tidy: Default::default(),
777            has_enzyme: Default::default(),
778            channel: Default::default(),
779            git_hash: Default::default(),
780            cc: Default::default(),
781            cxx: Default::default(),
782            cflags: Default::default(),
783            cxxflags: Default::default(),
784            ar: Default::default(),
785            target_linker: Default::default(),
786            host_linker: Default::default(),
787            llvm_components: Default::default(),
788            nodejs: Default::default(),
789            npm: Default::default(),
790            force_rerun: Default::default(),
791            only_modified: Default::default(),
792            target_cfgs: Default::default(),
793            builtin_cfg_names: Default::default(),
794            supported_crate_types: Default::default(),
795            nocapture: Default::default(),
796            nightly_branch: Default::default(),
797            git_merge_commit_email: Default::default(),
798            profiler_runtime: Default::default(),
799            diff_command: Default::default(),
800            minicore_path: Default::default(),
801            default_codegen_backend: CodegenBackend::Llvm,
802            override_codegen_backend: None,
803        }
804    }
805
806    /// FIXME: this run scheme is... confusing.
807    pub fn run_enabled(&self) -> bool {
808        self.run.unwrap_or_else(|| {
809            // Auto-detect whether to run based on the platform.
810            !self.target.ends_with("-fuchsia")
811        })
812    }
813
814    pub fn target_cfgs(&self) -> &TargetCfgs {
815        self.target_cfgs.get_or_init(|| TargetCfgs::new(self))
816    }
817
818    pub fn target_cfg(&self) -> &TargetCfg {
819        &self.target_cfgs().current
820    }
821
822    pub fn matches_arch(&self, arch: &str) -> bool {
823        self.target_cfg().arch == arch ||
824        // Matching all the thumb variants as one can be convenient.
825        // (thumbv6m, thumbv7em, thumbv7m, etc.)
826        (arch == "thumb" && self.target.starts_with("thumb"))
827    }
828
829    pub fn matches_os(&self, os: &str) -> bool {
830        self.target_cfg().os == os
831    }
832
833    pub fn matches_env(&self, env: &str) -> bool {
834        self.target_cfg().env == env
835    }
836
837    pub fn matches_abi(&self, abi: &str) -> bool {
838        self.target_cfg().abi == abi
839    }
840
841    pub fn matches_family(&self, family: &str) -> bool {
842        self.target_cfg().families.iter().any(|f| f == family)
843    }
844
845    pub fn is_big_endian(&self) -> bool {
846        self.target_cfg().endian == Endian::Big
847    }
848
849    pub fn get_pointer_width(&self) -> u32 {
850        *&self.target_cfg().pointer_width
851    }
852
853    pub fn can_unwind(&self) -> bool {
854        self.target_cfg().panic == PanicStrategy::Unwind
855    }
856
857    /// Get the list of builtin, 'well known' cfg names
858    pub fn builtin_cfg_names(&self) -> &HashSet<String> {
859        self.builtin_cfg_names.get_or_init(|| builtin_cfg_names(self))
860    }
861
862    /// Get the list of crate types that the target platform supports.
863    pub fn supported_crate_types(&self) -> &HashSet<String> {
864        self.supported_crate_types.get_or_init(|| supported_crate_types(self))
865    }
866
867    pub fn has_threads(&self) -> bool {
868        // Wasm targets don't have threads unless `-threads` is in the target
869        // name, such as `wasm32-wasip1-threads`.
870        if self.target.starts_with("wasm") {
871            return self.target.contains("threads");
872        }
873        true
874    }
875
876    pub fn has_asm_support(&self) -> bool {
877        // This should match the stable list in `LoweringContext::lower_inline_asm`.
878        static ASM_SUPPORTED_ARCHS: &[&str] = &[
879            "x86",
880            "x86_64",
881            "arm",
882            "aarch64",
883            "arm64ec",
884            "riscv32",
885            "riscv64",
886            "loongarch32",
887            "loongarch64",
888            "s390x",
889            // These targets require an additional asm_experimental_arch feature.
890            // "nvptx64", "hexagon", "mips", "mips64", "spirv", "wasm32",
891        ];
892        ASM_SUPPORTED_ARCHS.contains(&self.target_cfg().arch.as_str())
893    }
894
895    pub fn git_config(&self) -> GitConfig<'_> {
896        GitConfig {
897            nightly_branch: &self.nightly_branch,
898            git_merge_commit_email: &self.git_merge_commit_email,
899        }
900    }
901
902    pub fn has_subprocess_support(&self) -> bool {
903        // FIXME(#135928): compiletest is always a **host** tool. Building and running an
904        // capability detection executable against the **target** is not trivial. The short term
905        // solution here is to hard-code some targets to allow/deny, unfortunately.
906
907        let unsupported_target = self.target_cfg().env == "sgx"
908            || matches!(self.target_cfg().arch.as_str(), "wasm32" | "wasm64")
909            || self.target_cfg().os == "emscripten";
910        !unsupported_target
911    }
912}
913
914/// Known widths of `target_has_atomic`.
915pub const KNOWN_TARGET_HAS_ATOMIC_WIDTHS: &[&str] = &["8", "16", "32", "64", "128", "ptr"];
916
917#[derive(Debug, Clone)]
918pub struct TargetCfgs {
919    pub current: TargetCfg,
920    pub all_targets: HashSet<String>,
921    pub all_archs: HashSet<String>,
922    pub all_oses: HashSet<String>,
923    pub all_oses_and_envs: HashSet<String>,
924    pub all_envs: HashSet<String>,
925    pub all_abis: HashSet<String>,
926    pub all_families: HashSet<String>,
927    pub all_pointer_widths: HashSet<String>,
928    pub all_rustc_abis: HashSet<String>,
929}
930
931impl TargetCfgs {
932    fn new(config: &Config) -> TargetCfgs {
933        let mut targets: HashMap<String, TargetCfg> = serde_json::from_str(&query_rustc_output(
934            config,
935            &["--print=all-target-specs-json", "-Zunstable-options"],
936            Default::default(),
937        ))
938        .unwrap();
939
940        let mut all_targets = HashSet::new();
941        let mut all_archs = HashSet::new();
942        let mut all_oses = HashSet::new();
943        let mut all_oses_and_envs = HashSet::new();
944        let mut all_envs = HashSet::new();
945        let mut all_abis = HashSet::new();
946        let mut all_families = HashSet::new();
947        let mut all_pointer_widths = HashSet::new();
948        // NOTE: for distinction between `abi` and `rustc_abi`, see comment on
949        // `TargetCfg::rustc_abi`.
950        let mut all_rustc_abis = HashSet::new();
951
952        // If current target is not included in the `--print=all-target-specs-json` output,
953        // we check whether it is a custom target from the user or a synthetic target from bootstrap.
954        if !targets.contains_key(&config.target) {
955            let mut envs: HashMap<String, String> = HashMap::new();
956
957            if let Ok(t) = std::env::var("RUST_TARGET_PATH") {
958                envs.insert("RUST_TARGET_PATH".into(), t);
959            }
960
961            // This returns false only when the target is neither a synthetic target
962            // nor a custom target from the user, indicating it is most likely invalid.
963            if config.target.ends_with(".json") || !envs.is_empty() {
964                targets.insert(
965                    config.target.clone(),
966                    serde_json::from_str(&query_rustc_output(
967                        config,
968                        &[
969                            "--print=target-spec-json",
970                            "-Zunstable-options",
971                            "--target",
972                            &config.target,
973                        ],
974                        envs,
975                    ))
976                    .unwrap(),
977                );
978            }
979        }
980
981        for (target, cfg) in targets.iter() {
982            all_archs.insert(cfg.arch.clone());
983            all_oses.insert(cfg.os.clone());
984            all_oses_and_envs.insert(cfg.os_and_env());
985            all_envs.insert(cfg.env.clone());
986            all_abis.insert(cfg.abi.clone());
987            for family in &cfg.families {
988                all_families.insert(family.clone());
989            }
990            all_pointer_widths.insert(format!("{}bit", cfg.pointer_width));
991            if let Some(rustc_abi) = &cfg.rustc_abi {
992                all_rustc_abis.insert(rustc_abi.clone());
993            }
994            all_targets.insert(target.clone());
995        }
996
997        Self {
998            current: Self::get_current_target_config(config, &targets),
999            all_targets,
1000            all_archs,
1001            all_oses,
1002            all_oses_and_envs,
1003            all_envs,
1004            all_abis,
1005            all_families,
1006            all_pointer_widths,
1007            all_rustc_abis,
1008        }
1009    }
1010
1011    fn get_current_target_config(
1012        config: &Config,
1013        targets: &HashMap<String, TargetCfg>,
1014    ) -> TargetCfg {
1015        let mut cfg = targets[&config.target].clone();
1016
1017        // To get the target information for the current target, we take the target spec obtained
1018        // from `--print=all-target-specs-json`, and then we enrich it with the information
1019        // gathered from `--print=cfg --target=$target`.
1020        //
1021        // This is done because some parts of the target spec can be overridden with `-C` flags,
1022        // which are respected for `--print=cfg` but not for `--print=all-target-specs-json`. The
1023        // code below extracts them from `--print=cfg`: make sure to only override fields that can
1024        // actually be changed with `-C` flags.
1025        for config in query_rustc_output(
1026            config,
1027            &["--print=cfg", "--target", &config.target],
1028            Default::default(),
1029        )
1030        .trim()
1031        .lines()
1032        {
1033            let (name, value) = config
1034                .split_once("=\"")
1035                .map(|(name, value)| {
1036                    (
1037                        name,
1038                        Some(
1039                            value
1040                                .strip_suffix('\"')
1041                                .expect("key-value pair should be properly quoted"),
1042                        ),
1043                    )
1044                })
1045                .unwrap_or_else(|| (config, None));
1046
1047            match (name, value) {
1048                // Can be overridden with `-C panic=$strategy`.
1049                ("panic", Some("abort")) => cfg.panic = PanicStrategy::Abort,
1050                ("panic", Some("unwind")) => cfg.panic = PanicStrategy::Unwind,
1051                ("panic", other) => panic!("unexpected value for panic cfg: {other:?}"),
1052
1053                ("target_has_atomic", Some(width))
1054                    if KNOWN_TARGET_HAS_ATOMIC_WIDTHS.contains(&width) =>
1055                {
1056                    cfg.target_has_atomic.insert(width.to_string());
1057                }
1058                ("target_has_atomic", Some(other)) => {
1059                    panic!("unexpected value for `target_has_atomic` cfg: {other:?}")
1060                }
1061                // Nightly-only std-internal impl detail.
1062                ("target_has_atomic", None) => {}
1063                _ => {}
1064            }
1065        }
1066
1067        cfg
1068    }
1069}
1070
1071#[derive(Clone, Debug, serde::Deserialize)]
1072#[serde(rename_all = "kebab-case")]
1073pub struct TargetCfg {
1074    pub(crate) arch: String,
1075    #[serde(default = "default_os")]
1076    pub(crate) os: String,
1077    #[serde(default)]
1078    pub(crate) env: String,
1079    #[serde(default)]
1080    pub(crate) abi: String,
1081    #[serde(rename = "target-family", default)]
1082    pub(crate) families: Vec<String>,
1083    #[serde(rename = "target-pointer-width", deserialize_with = "serde_parse_u32")]
1084    pub(crate) pointer_width: u32,
1085    #[serde(rename = "target-endian", default)]
1086    endian: Endian,
1087    #[serde(rename = "panic-strategy", default)]
1088    pub(crate) panic: PanicStrategy,
1089    #[serde(default)]
1090    pub(crate) dynamic_linking: bool,
1091    #[serde(rename = "supported-sanitizers", default)]
1092    pub(crate) sanitizers: Vec<Sanitizer>,
1093    #[serde(rename = "supports-xray", default)]
1094    pub(crate) xray: bool,
1095    #[serde(default = "default_reloc_model")]
1096    pub(crate) relocation_model: String,
1097    // NOTE: `rustc_abi` should not be confused with `abi`. `rustc_abi` was introduced in #137037 to
1098    // make SSE2 *required* by the ABI (kind of a hack to make a target feature *required* via the
1099    // target spec).
1100    pub(crate) rustc_abi: Option<String>,
1101
1102    // Not present in target cfg json output, additional derived information.
1103    #[serde(skip)]
1104    /// Supported target atomic widths: e.g. `8` to `128` or `ptr`. This is derived from the builtin
1105    /// `target_has_atomic` `cfg`s e.g. `target_has_atomic="8"`.
1106    pub(crate) target_has_atomic: BTreeSet<String>,
1107}
1108
1109impl TargetCfg {
1110    pub(crate) fn os_and_env(&self) -> String {
1111        format!("{}-{}", self.os, self.env)
1112    }
1113}
1114
1115fn default_os() -> String {
1116    "none".into()
1117}
1118
1119fn default_reloc_model() -> String {
1120    "pic".into()
1121}
1122
1123#[derive(Eq, PartialEq, Clone, Debug, Default, serde::Deserialize)]
1124#[serde(rename_all = "kebab-case")]
1125pub enum Endian {
1126    #[default]
1127    Little,
1128    Big,
1129}
1130
1131fn builtin_cfg_names(config: &Config) -> HashSet<String> {
1132    query_rustc_output(
1133        config,
1134        &["--print=check-cfg", "-Zunstable-options", "--check-cfg=cfg()"],
1135        Default::default(),
1136    )
1137    .lines()
1138    .map(|l| if let Some((name, _)) = l.split_once('=') { name.to_string() } else { l.to_string() })
1139    .chain(std::iter::once(String::from("test")))
1140    .collect()
1141}
1142
1143pub const KNOWN_CRATE_TYPES: &[&str] =
1144    &["bin", "cdylib", "dylib", "lib", "proc-macro", "rlib", "staticlib"];
1145
1146fn supported_crate_types(config: &Config) -> HashSet<String> {
1147    let crate_types: HashSet<_> = query_rustc_output(
1148        config,
1149        &["--target", &config.target, "--print=supported-crate-types", "-Zunstable-options"],
1150        Default::default(),
1151    )
1152    .lines()
1153    .map(|l| l.to_string())
1154    .collect();
1155
1156    for crate_type in crate_types.iter() {
1157        assert!(
1158            KNOWN_CRATE_TYPES.contains(&crate_type.as_str()),
1159            "unexpected crate type `{}`: known crate types are {:?}",
1160            crate_type,
1161            KNOWN_CRATE_TYPES
1162        );
1163    }
1164
1165    crate_types
1166}
1167
1168fn query_rustc_output(config: &Config, args: &[&str], envs: HashMap<String, String>) -> String {
1169    let query_rustc_path = config.query_rustc_path.as_deref().unwrap_or(&config.rustc_path);
1170
1171    let mut command = Command::new(query_rustc_path);
1172    add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
1173    command.args(&config.target_rustcflags).args(args);
1174    command.env("RUSTC_BOOTSTRAP", "1");
1175    command.envs(envs);
1176
1177    let output = match command.output() {
1178        Ok(output) => output,
1179        Err(e) => {
1180            fatal!("failed to run {command:?}: {e}");
1181        }
1182    };
1183    if !output.status.success() {
1184        fatal!(
1185            "failed to run {command:?}\n--- stdout\n{}\n--- stderr\n{}",
1186            String::from_utf8(output.stdout).unwrap(),
1187            String::from_utf8(output.stderr).unwrap(),
1188        );
1189    }
1190    String::from_utf8(output.stdout).unwrap()
1191}
1192
1193fn serde_parse_u32<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u32, D::Error> {
1194    let string = String::deserialize(deserializer)?;
1195    string.parse().map_err(D::Error::custom)
1196}
1197
1198#[derive(Debug, Clone)]
1199pub struct TestPaths {
1200    pub file: Utf8PathBuf,         // e.g., compile-test/foo/bar/baz.rs
1201    pub relative_dir: Utf8PathBuf, // e.g., foo/bar
1202}
1203
1204/// Used by `ui` tests to generate things like `foo.stderr` from `foo.rs`.
1205pub fn expected_output_path(
1206    testpaths: &TestPaths,
1207    revision: Option<&str>,
1208    compare_mode: &Option<CompareMode>,
1209    kind: &str,
1210) -> Utf8PathBuf {
1211    assert!(UI_EXTENSIONS.contains(&kind));
1212    let mut parts = Vec::new();
1213
1214    if let Some(x) = revision {
1215        parts.push(x);
1216    }
1217    if let Some(ref x) = *compare_mode {
1218        parts.push(x.to_str());
1219    }
1220    parts.push(kind);
1221
1222    let extension = parts.join(".");
1223    testpaths.file.with_extension(extension)
1224}
1225
1226pub const UI_EXTENSIONS: &[&str] = &[
1227    UI_STDERR,
1228    UI_SVG,
1229    UI_WINDOWS_SVG,
1230    UI_STDOUT,
1231    UI_FIXED,
1232    UI_RUN_STDERR,
1233    UI_RUN_STDOUT,
1234    UI_STDERR_64,
1235    UI_STDERR_32,
1236    UI_STDERR_16,
1237    UI_COVERAGE,
1238    UI_COVERAGE_MAP,
1239];
1240pub const UI_STDERR: &str = "stderr";
1241pub const UI_SVG: &str = "svg";
1242pub const UI_WINDOWS_SVG: &str = "windows.svg";
1243pub const UI_STDOUT: &str = "stdout";
1244pub const UI_FIXED: &str = "fixed";
1245pub const UI_RUN_STDERR: &str = "run.stderr";
1246pub const UI_RUN_STDOUT: &str = "run.stdout";
1247pub const UI_STDERR_64: &str = "64bit.stderr";
1248pub const UI_STDERR_32: &str = "32bit.stderr";
1249pub const UI_STDERR_16: &str = "16bit.stderr";
1250pub const UI_COVERAGE: &str = "coverage";
1251pub const UI_COVERAGE_MAP: &str = "cov-map";
1252
1253/// Absolute path to the directory where all output for all tests in the given `relative_dir` group
1254/// should reside. Example:
1255///
1256/// ```text
1257/// /path/to/build/host-tuple/test/ui/relative/
1258/// ```
1259///
1260/// This is created early when tests are collected to avoid race conditions.
1261pub fn output_relative_path(config: &Config, relative_dir: &Utf8Path) -> Utf8PathBuf {
1262    config.build_test_suite_root.join(relative_dir)
1263}
1264
1265/// Generates a unique name for the test, such as `testname.revision.mode`.
1266pub fn output_testname_unique(
1267    config: &Config,
1268    testpaths: &TestPaths,
1269    revision: Option<&str>,
1270) -> Utf8PathBuf {
1271    let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
1272    let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
1273    Utf8PathBuf::from(&testpaths.file.file_stem().unwrap())
1274        .with_extra_extension(config.mode.output_dir_disambiguator())
1275        .with_extra_extension(revision.unwrap_or(""))
1276        .with_extra_extension(mode)
1277        .with_extra_extension(debugger)
1278}
1279
1280/// Absolute path to the directory where all output for the given
1281/// test/revision should reside. Example:
1282///   /path/to/build/host-tuple/test/ui/relative/testname.revision.mode/
1283pub fn output_base_dir(
1284    config: &Config,
1285    testpaths: &TestPaths,
1286    revision: Option<&str>,
1287) -> Utf8PathBuf {
1288    output_relative_path(config, &testpaths.relative_dir)
1289        .join(output_testname_unique(config, testpaths, revision))
1290}
1291
1292/// Absolute path to the base filename used as output for the given
1293/// test/revision. Example:
1294///   /path/to/build/host-tuple/test/ui/relative/testname.revision.mode/testname
1295pub fn output_base_name(
1296    config: &Config,
1297    testpaths: &TestPaths,
1298    revision: Option<&str>,
1299) -> Utf8PathBuf {
1300    output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
1301}
1302
1303/// Absolute path to the directory to use for incremental compilation. Example:
1304///   /path/to/build/host-tuple/test/ui/relative/testname.mode/testname.inc
1305pub fn incremental_dir(
1306    config: &Config,
1307    testpaths: &TestPaths,
1308    revision: Option<&str>,
1309) -> Utf8PathBuf {
1310    output_base_name(config, testpaths, revision).with_extension("inc")
1311}