compiletest/
common.rs

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