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