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