use std::collections::{BTreeSet, HashMap, HashSet};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::sync::OnceLock;
use std::{fmt, iter};
use build_helper::git::GitConfig;
use semver::Version;
use serde::de::{Deserialize, Deserializer, Error as _};
use test::{ColorConfig, OutputFormat};
pub use self::Mode::*;
use crate::util::{PathBufExt, add_dylib_path};
macro_rules! string_enum {
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
$(#[$meta])*
$vis enum $name {
$($variant,)*
}
impl $name {
$vis const VARIANTS: &'static [Self] = &[$(Self::$variant,)*];
$vis const STR_VARIANTS: &'static [&'static str] = &[$(Self::$variant.to_str(),)*];
$vis const fn to_str(&self) -> &'static str {
match self {
$(Self::$variant => $repr,)*
}
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.to_str(), f)
}
}
impl FromStr for $name {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
match s {
$($repr => Ok(Self::$variant),)*
_ => Err(()),
}
}
}
}
}
string_enum! {
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Mode {
Pretty => "pretty",
DebugInfo => "debuginfo",
Codegen => "codegen",
Rustdoc => "rustdoc",
RustdocJson => "rustdoc-json",
CodegenUnits => "codegen-units",
Incremental => "incremental",
RunMake => "run-make",
Ui => "ui",
JsDocTest => "js-doc-test",
MirOpt => "mir-opt",
Assembly => "assembly",
CoverageMap => "coverage-map",
CoverageRun => "coverage-run",
Crashes => "crashes",
}
}
impl Default for Mode {
fn default() -> Self {
Mode::Ui
}
}
impl Mode {
pub fn aux_dir_disambiguator(self) -> &'static str {
match self {
Pretty => ".pretty",
_ => "",
}
}
pub fn output_dir_disambiguator(self) -> &'static str {
match self {
CoverageMap | CoverageRun => self.to_str(),
_ => "",
}
}
}
string_enum! {
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
pub enum PassMode {
Check => "check",
Build => "build",
Run => "run",
}
}
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub enum FailMode {
Check,
Build,
Run,
}
string_enum! {
#[derive(Clone, Debug, PartialEq)]
pub enum CompareMode {
Polonius => "polonius",
NextSolver => "next-solver",
NextSolverCoherence => "next-solver-coherence",
SplitDwarf => "split-dwarf",
SplitDwarfSingle => "split-dwarf-single",
}
}
string_enum! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Debugger {
Cdb => "cdb",
Gdb => "gdb",
Lldb => "lldb",
}
}
#[derive(Clone, Copy, Debug, PartialEq, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PanicStrategy {
#[default]
Unwind,
Abort,
}
impl PanicStrategy {
pub(crate) fn for_miropt_test_tools(&self) -> miropt_test_tools::PanicStrategy {
match self {
PanicStrategy::Unwind => miropt_test_tools::PanicStrategy::Unwind,
PanicStrategy::Abort => miropt_test_tools::PanicStrategy::Abort,
}
}
}
#[derive(Clone, Debug, PartialEq, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Sanitizer {
Address,
Cfi,
Dataflow,
Kcfi,
KernelAddress,
Leak,
Memory,
Memtag,
Safestack,
ShadowCallStack,
Thread,
Hwaddress,
}
#[derive(Debug, Default, Clone)]
pub struct Config {
pub bless: bool,
pub compile_lib_path: PathBuf,
pub run_lib_path: PathBuf,
pub rustc_path: PathBuf,
pub cargo_path: Option<PathBuf>,
pub rustdoc_path: Option<PathBuf>,
pub coverage_dump_path: Option<PathBuf>,
pub python: String,
pub jsondocck_path: Option<String>,
pub jsondoclint_path: Option<String>,
pub llvm_filecheck: Option<PathBuf>,
pub llvm_bin_dir: Option<PathBuf>,
pub run_clang_based_tests_with: Option<String>,
pub src_base: PathBuf,
pub build_base: PathBuf,
pub sysroot_base: PathBuf,
pub stage_id: String,
pub mode: Mode,
pub suite: String,
pub debugger: Option<Debugger>,
pub run_ignored: bool,
pub with_rustc_debug_assertions: bool,
pub with_std_debug_assertions: bool,
pub filters: Vec<String>,
pub skip: Vec<String>,
pub filter_exact: bool,
pub force_pass_mode: Option<PassMode>,
pub run: Option<bool>,
pub logfile: Option<PathBuf>,
pub runner: Option<String>,
pub host_rustcflags: Vec<String>,
pub target_rustcflags: Vec<String>,
pub rust_randomized_layout: bool,
pub optimize_tests: bool,
pub target: String,
pub host: String,
pub cdb: Option<OsString>,
pub cdb_version: Option<[u16; 4]>,
pub gdb: Option<String>,
pub gdb_version: Option<u32>,
pub lldb_version: Option<u32>,
pub llvm_version: Option<Version>,
pub system_llvm: bool,
pub android_cross_path: PathBuf,
pub adb_path: String,
pub adb_test_dir: String,
pub adb_device_status: bool,
pub lldb_python_dir: Option<String>,
pub verbose: bool,
pub format: OutputFormat,
pub color: ColorConfig,
pub remote_test_client: Option<PathBuf>,
pub compare_mode: Option<CompareMode>,
pub rustfix_coverage: bool,
pub has_html_tidy: bool,
pub has_enzyme: bool,
pub channel: String,
pub git_hash: bool,
pub edition: Option<String>,
pub cc: String,
pub cxx: String,
pub cflags: String,
pub cxxflags: String,
pub ar: String,
pub target_linker: Option<String>,
pub host_linker: Option<String>,
pub llvm_components: String,
pub nodejs: Option<String>,
pub npm: Option<String>,
pub force_rerun: bool,
pub only_modified: bool,
pub target_cfgs: OnceLock<TargetCfgs>,
pub builtin_cfg_names: OnceLock<HashSet<String>>,
pub nocapture: bool,
pub git_repository: String,
pub nightly_branch: String,
pub git_merge_commit_email: String,
pub profiler_runtime: bool,
pub diff_command: Option<String>,
pub minicore_path: PathBuf,
}
impl Config {
pub fn run_enabled(&self) -> bool {
self.run.unwrap_or_else(|| {
!self.target.ends_with("-fuchsia")
})
}
pub fn target_cfgs(&self) -> &TargetCfgs {
self.target_cfgs.get_or_init(|| TargetCfgs::new(self))
}
pub fn target_cfg(&self) -> &TargetCfg {
&self.target_cfgs().current
}
pub fn matches_arch(&self, arch: &str) -> bool {
self.target_cfg().arch == arch ||
(arch == "thumb" && self.target.starts_with("thumb"))
}
pub fn matches_os(&self, os: &str) -> bool {
self.target_cfg().os == os
}
pub fn matches_env(&self, env: &str) -> bool {
self.target_cfg().env == env
}
pub fn matches_abi(&self, abi: &str) -> bool {
self.target_cfg().abi == abi
}
pub fn matches_family(&self, family: &str) -> bool {
self.target_cfg().families.iter().any(|f| f == family)
}
pub fn is_big_endian(&self) -> bool {
self.target_cfg().endian == Endian::Big
}
pub fn get_pointer_width(&self) -> u32 {
*&self.target_cfg().pointer_width
}
pub fn can_unwind(&self) -> bool {
self.target_cfg().panic == PanicStrategy::Unwind
}
pub fn builtin_cfg_names(&self) -> &HashSet<String> {
self.builtin_cfg_names.get_or_init(|| builtin_cfg_names(self))
}
pub fn has_threads(&self) -> bool {
if self.target.starts_with("wasm") {
return self.target.contains("threads");
}
true
}
pub fn has_asm_support(&self) -> bool {
static ASM_SUPPORTED_ARCHS: &[&str] = &[
"x86", "x86_64", "arm", "aarch64", "riscv32",
"riscv64",
];
ASM_SUPPORTED_ARCHS.contains(&self.target_cfg().arch.as_str())
}
pub fn git_config(&self) -> GitConfig<'_> {
GitConfig {
git_repository: &self.git_repository,
nightly_branch: &self.nightly_branch,
git_merge_commit_email: &self.git_merge_commit_email,
}
}
}
pub const KNOWN_TARGET_HAS_ATOMIC_WIDTHS: &[&str] = &["8", "16", "32", "64", "128", "ptr"];
#[derive(Debug, Clone)]
pub struct TargetCfgs {
pub current: TargetCfg,
pub all_targets: HashSet<String>,
pub all_archs: HashSet<String>,
pub all_oses: HashSet<String>,
pub all_oses_and_envs: HashSet<String>,
pub all_envs: HashSet<String>,
pub all_abis: HashSet<String>,
pub all_families: HashSet<String>,
pub all_pointer_widths: HashSet<String>,
}
impl TargetCfgs {
fn new(config: &Config) -> TargetCfgs {
let mut targets: HashMap<String, TargetCfg> = serde_json::from_str(&rustc_output(
config,
&["--print=all-target-specs-json", "-Zunstable-options"],
Default::default(),
))
.unwrap();
let mut all_targets = HashSet::new();
let mut all_archs = HashSet::new();
let mut all_oses = HashSet::new();
let mut all_oses_and_envs = HashSet::new();
let mut all_envs = HashSet::new();
let mut all_abis = HashSet::new();
let mut all_families = HashSet::new();
let mut all_pointer_widths = HashSet::new();
if !targets.contains_key(&config.target) {
let mut envs: HashMap<String, String> = HashMap::new();
if let Ok(t) = std::env::var("RUST_TARGET_PATH") {
envs.insert("RUST_TARGET_PATH".into(), t);
}
if config.target.ends_with(".json") || !envs.is_empty() {
targets.insert(
config.target.clone(),
serde_json::from_str(&rustc_output(
config,
&[
"--print=target-spec-json",
"-Zunstable-options",
"--target",
&config.target,
],
envs,
))
.unwrap(),
);
}
}
for (target, cfg) in targets.iter() {
all_archs.insert(cfg.arch.clone());
all_oses.insert(cfg.os.clone());
all_oses_and_envs.insert(cfg.os_and_env());
all_envs.insert(cfg.env.clone());
all_abis.insert(cfg.abi.clone());
for family in &cfg.families {
all_families.insert(family.clone());
}
all_pointer_widths.insert(format!("{}bit", cfg.pointer_width));
all_targets.insert(target.clone());
}
Self {
current: Self::get_current_target_config(config, &targets),
all_targets,
all_archs,
all_oses,
all_oses_and_envs,
all_envs,
all_abis,
all_families,
all_pointer_widths,
}
}
fn get_current_target_config(
config: &Config,
targets: &HashMap<String, TargetCfg>,
) -> TargetCfg {
let mut cfg = targets[&config.target].clone();
for config in
rustc_output(config, &["--print=cfg", "--target", &config.target], Default::default())
.trim()
.lines()
{
let (name, value) = config
.split_once("=\"")
.map(|(name, value)| {
(
name,
Some(
value
.strip_suffix('\"')
.expect("key-value pair should be properly quoted"),
),
)
})
.unwrap_or_else(|| (config, None));
match (name, value) {
("panic", Some("abort")) => cfg.panic = PanicStrategy::Abort,
("panic", Some("unwind")) => cfg.panic = PanicStrategy::Unwind,
("panic", other) => panic!("unexpected value for panic cfg: {other:?}"),
("target_has_atomic", Some(width))
if KNOWN_TARGET_HAS_ATOMIC_WIDTHS.contains(&width) =>
{
cfg.target_has_atomic.insert(width.to_string());
}
("target_has_atomic", Some(other)) => {
panic!("unexpected value for `target_has_atomic` cfg: {other:?}")
}
("target_has_atomic", None) => {}
_ => {}
}
}
cfg
}
}
#[derive(Clone, Debug, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TargetCfg {
pub(crate) arch: String,
#[serde(default = "default_os")]
pub(crate) os: String,
#[serde(default)]
pub(crate) env: String,
#[serde(default)]
pub(crate) abi: String,
#[serde(rename = "target-family", default)]
pub(crate) families: Vec<String>,
#[serde(rename = "target-pointer-width", deserialize_with = "serde_parse_u32")]
pub(crate) pointer_width: u32,
#[serde(rename = "target-endian", default)]
endian: Endian,
#[serde(rename = "panic-strategy", default)]
pub(crate) panic: PanicStrategy,
#[serde(default)]
pub(crate) dynamic_linking: bool,
#[serde(rename = "supported-sanitizers", default)]
pub(crate) sanitizers: Vec<Sanitizer>,
#[serde(rename = "supports-xray", default)]
pub(crate) xray: bool,
#[serde(default = "default_reloc_model")]
pub(crate) relocation_model: String,
#[serde(skip)]
pub(crate) target_has_atomic: BTreeSet<String>,
}
impl TargetCfg {
pub(crate) fn os_and_env(&self) -> String {
format!("{}-{}", self.os, self.env)
}
}
fn default_os() -> String {
"none".into()
}
fn default_reloc_model() -> String {
"pic".into()
}
#[derive(Eq, PartialEq, Clone, Debug, Default, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Endian {
#[default]
Little,
Big,
}
fn builtin_cfg_names(config: &Config) -> HashSet<String> {
rustc_output(
config,
&["--print=check-cfg", "-Zunstable-options", "--check-cfg=cfg()"],
Default::default(),
)
.lines()
.map(|l| if let Some((name, _)) = l.split_once('=') { name.to_string() } else { l.to_string() })
.chain(std::iter::once(String::from("test")))
.collect()
}
fn rustc_output(config: &Config, args: &[&str], envs: HashMap<String, String>) -> String {
let mut command = Command::new(&config.rustc_path);
add_dylib_path(&mut command, iter::once(&config.compile_lib_path));
command.args(&config.target_rustcflags).args(args);
command.env("RUSTC_BOOTSTRAP", "1");
command.envs(envs);
let output = match command.output() {
Ok(output) => output,
Err(e) => panic!("error: failed to run {command:?}: {e}"),
};
if !output.status.success() {
panic!(
"error: failed to run {command:?}\n--- stdout\n{}\n--- stderr\n{}",
String::from_utf8(output.stdout).unwrap(),
String::from_utf8(output.stderr).unwrap(),
);
}
String::from_utf8(output.stdout).unwrap()
}
fn serde_parse_u32<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u32, D::Error> {
let string = String::deserialize(deserializer)?;
string.parse().map_err(D::Error::custom)
}
#[derive(Debug, Clone)]
pub struct TestPaths {
pub file: PathBuf, pub relative_dir: PathBuf, }
pub fn expected_output_path(
testpaths: &TestPaths,
revision: Option<&str>,
compare_mode: &Option<CompareMode>,
kind: &str,
) -> PathBuf {
assert!(UI_EXTENSIONS.contains(&kind));
let mut parts = Vec::new();
if let Some(x) = revision {
parts.push(x);
}
if let Some(ref x) = *compare_mode {
parts.push(x.to_str());
}
parts.push(kind);
let extension = parts.join(".");
testpaths.file.with_extension(extension)
}
pub const UI_EXTENSIONS: &[&str] = &[
UI_STDERR,
UI_SVG,
UI_WINDOWS_SVG,
UI_STDOUT,
UI_FIXED,
UI_RUN_STDERR,
UI_RUN_STDOUT,
UI_STDERR_64,
UI_STDERR_32,
UI_STDERR_16,
UI_COVERAGE,
UI_COVERAGE_MAP,
];
pub const UI_STDERR: &str = "stderr";
pub const UI_SVG: &str = "svg";
pub const UI_WINDOWS_SVG: &str = "windows.svg";
pub const UI_STDOUT: &str = "stdout";
pub const UI_FIXED: &str = "fixed";
pub const UI_RUN_STDERR: &str = "run.stderr";
pub const UI_RUN_STDOUT: &str = "run.stdout";
pub const UI_STDERR_64: &str = "64bit.stderr";
pub const UI_STDERR_32: &str = "32bit.stderr";
pub const UI_STDERR_16: &str = "16bit.stderr";
pub const UI_COVERAGE: &str = "coverage";
pub const UI_COVERAGE_MAP: &str = "cov-map";
pub fn output_relative_path(config: &Config, relative_dir: &Path) -> PathBuf {
config.build_base.join(relative_dir)
}
pub fn output_testname_unique(
config: &Config,
testpaths: &TestPaths,
revision: Option<&str>,
) -> PathBuf {
let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
PathBuf::from(&testpaths.file.file_stem().unwrap())
.with_extra_extension(config.mode.output_dir_disambiguator())
.with_extra_extension(revision.unwrap_or(""))
.with_extra_extension(mode)
.with_extra_extension(debugger)
}
pub fn output_base_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
output_relative_path(config, &testpaths.relative_dir)
.join(output_testname_unique(config, testpaths, revision))
}
pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
}
pub fn incremental_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
output_base_name(config, testpaths, revision).with_extension("inc")
}