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