Skip to main content

compiletest/
common.rs

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