+#![feature(let_else)]
+#![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq)]
+
+mod version;
+
use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
-use serde::{Deserialize, Serialize};
-
use rustc_version::VersionMeta;
+use serde::{Deserialize, Serialize};
-const XARGO_MIN_VERSION: (u32, u32, u32) = (0, 3, 23);
+use version::*;
const CARGO_MIRI_HELP: &str = r#"Runs binary crates and tests in Miri
cargo miri test -- test-suite-filter
"#;
-#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+#[derive(Clone, Debug)]
enum MiriCommand {
- Run,
- Test,
+ /// Our own special 'setup' command.
Setup,
+ /// A command to be forwarded to cargo.
+ Forward(String),
}
/// The information to run a crate with the given environment.
// Only use `option_env` on vergen variables to ensure the build succeeds
// when vergen failed to find the git info.
if let Some(sha) = option_env!("VERGEN_GIT_SHA_SHORT") {
+ // This `unwrap` can never fail because if VERGEN_GIT_SHA_SHORT exists, then so does
+ // VERGEN_GIT_COMMIT_DATE.
+ #[allow(clippy::option_env_unwrap)]
write!(&mut version, " ({} {})", sha, option_env!("VERGEN_GIT_COMMIT_DATE").unwrap())
.unwrap();
}
fn next(&mut self) -> Option<Self::Item> {
let arg = self.args.next()?;
- if arg.starts_with(self.name) {
+ if let Some(suffix) = arg.strip_prefix(self.name) {
// Strip leading `name`.
- let suffix = &arg[self.name.len()..];
if suffix.is_empty() {
// This argument is exactly `name`; the next one is the value.
return self.args.next().map(Ok);
- } else if suffix.starts_with('=') {
+ } else if let Some(suffix) = suffix.strip_prefix('=') {
// This argument is `name=value`; get the value.
- // Strip leading `=`.
- return Some(Ok(suffix[1..].to_owned()));
+ return Some(Ok(suffix.to_owned()));
}
}
Some(Err(arg))
let line = out
.stderr
.lines()
- .nth(0)
+ .next()
.expect("malformed `xargo --version` output: not at least one line")
.expect("malformed `xargo --version` output: error reading first line");
let (name, version) = {
.expect("malformed `xargo --version` output: not a patch version piece")
.parse()
.expect("malformed `xargo --version` output: patch version is not an integer");
- if !version_pieces.next().is_none() {
+ if version_pieces.next().is_some() {
panic!("malformed `xargo --version` output: more than three pieces in version");
}
Some((major, minor, patch))
println!("Running `{:?}` to {}.", cmd, text);
}
- if cmd.status().expect(&format!("failed to execute {:?}", cmd)).success().not() {
+ if cmd.status().unwrap_or_else(|_| panic!("failed to execute {:?}", cmd)).success().not() {
show_error(format!("failed to {}", text));
}
}
+/// Writes the given content to the given file *cross-process atomically*, in the sense that another
+/// process concurrently reading that file will see either the old content or the new content, but
+/// not some intermediate (e.g., empty) state.
+///
+/// We assume no other parts of this same process are trying to read or write that file.
+fn write_to_file(filename: &Path, content: &str) {
+ // Create a temporary file with the desired contents.
+ let mut temp_filename = filename.as_os_str().to_os_string();
+ temp_filename.push(&format!(".{}", std::process::id()));
+ let mut temp_file = File::create(&temp_filename).unwrap();
+ temp_file.write_all(content.as_bytes()).unwrap();
+ drop(temp_file);
+
+ // Move file to the desired location.
+ fs::rename(temp_filename, filename).unwrap();
+}
+
/// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
/// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
/// done all this already.
-fn setup(subcommand: MiriCommand) {
+fn setup(subcommand: &MiriCommand) {
+ let only_setup = matches!(subcommand, MiriCommand::Setup);
if std::env::var_os("MIRI_SYSROOT").is_some() {
- if subcommand == MiriCommand::Setup {
+ if only_setup {
println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
}
return;
// Subcommands other than `setup` will do a setup if necessary, but
// interactively confirm first.
- let ask_user = subcommand != MiriCommand::Setup;
+ let ask_user = !only_setup;
// First, we need xargo.
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
}
None => {
// Check for `rust-src` rustup component.
- let sysroot = miri()
- .args(&["--print", "sysroot"])
- .output()
- .expect("failed to determine sysroot")
- .stdout;
- let sysroot = std::str::from_utf8(&sysroot).unwrap();
+ let output =
+ miri().args(&["--print", "sysroot"]).output().expect("failed to determine sysroot");
+ if !output.status.success() {
+ show_error(format!(
+ "Failed to determine sysroot; Miri said:\n{}",
+ String::from_utf8_lossy(&output.stderr).trim_end()
+ ));
+ }
+ let sysroot = std::str::from_utf8(&output.stdout).unwrap();
let sysroot = Path::new(sysroot.trim_end_matches('\n'));
// Check for `$SYSROOT/lib/rustlib/src/rust/library`; test if that contains `std/Cargo.toml`.
let rustup_src =
if !dir.exists() {
fs::create_dir_all(&dir).unwrap();
}
- // The interesting bit: Xargo.toml
- File::create(dir.join("Xargo.toml"))
- .unwrap()
- .write_all(
- br#"
+ // The interesting bit: Xargo.toml (only needs content if we actually need std)
+ let xargo_toml = if std::env::var_os("MIRI_NO_STD").is_some() {
+ ""
+ } else {
+ r#"
[dependencies.std]
default_features = false
# We support unwinding, so enable that panic runtime.
features = ["panic_unwind", "backtrace"]
[dependencies.test]
-"#,
- )
- .unwrap();
+"#
+ };
+ write_to_file(&dir.join("Xargo.toml"), xargo_toml);
// The boring bits: a dummy project for xargo.
// FIXME: With xargo-check, can we avoid doing this?
- File::create(dir.join("Cargo.toml"))
- .unwrap()
- .write_all(
- br#"
+ write_to_file(
+ &dir.join("Cargo.toml"),
+ r#"
[package]
name = "miri-xargo"
description = "A dummy project for building libstd with xargo."
[lib]
path = "lib.rs"
"#,
- )
- .unwrap();
- File::create(dir.join("lib.rs")).unwrap();
+ );
+ write_to_file(&dir.join("lib.rs"), "#![no_std]");
// Determine architectures.
// We always need to set a target so rustc bootstrap can tell apart host from target crates.
let sysroot = if target == &host { dir.join("HOST") } else { PathBuf::from(dir) };
std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
// Figure out what to print.
- let print_sysroot = subcommand == MiriCommand::Setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
+ let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
if print_sysroot {
// Print just the sysroot and nothing else; this way we do not need any escaping.
println!("{}", sysroot.display());
- } else if subcommand == MiriCommand::Setup {
+ } else if only_setup {
println!("A libstd for Miri is now available in `{}`.", sysroot.display());
}
}
for arg in ArgSplitFlagValue::new(
env::args().skip(3), // skip the program name, "miri" and "run" / "test"
config_flag,
- ) {
- if let Ok(config) = arg {
- cmd.arg(config_flag).arg(config);
- }
+ )
+ // Only look at `Ok`
+ .flatten()
+ {
+ cmd.arg(config_flag).arg(arg);
}
let mut child = cmd
.stdin(process::Stdio::null())
/// Additionally, somewhere between cargo metadata and TyCtxt, '-' gets replaced with '_' so we
/// make that same transformation here.
fn local_crates(metadata: &Metadata) -> String {
- assert!(metadata.workspace_members.len() > 0);
+ assert!(!metadata.workspace_members.is_empty());
let mut local_crates = String::new();
for member in &metadata.workspace_members {
- let name = member.split(" ").nth(0).unwrap();
- let name = name.replace("-", "_");
+ let name = member.split(' ').next().unwrap();
+ let name = name.replace('-', "_");
local_crates.push_str(&name);
local_crates.push(',');
}
// Require a subcommand before any flags.
// We cannot know which of those flags take arguments and which do not,
// so we cannot detect subcommands later.
- let subcommand = match args.next().as_deref() {
- Some("test" | "t") => MiriCommand::Test,
- Some("run" | "r") => MiriCommand::Run,
- Some("setup") => MiriCommand::Setup,
+ let Some(subcommand) = args.next() else {
+ show_error(format!("`cargo miri` needs to be called with a subcommand (`run`, `test`)"));
+ };
+ let subcommand = match &*subcommand {
+ "setup" => MiriCommand::Setup,
+ "test" | "t" | "run" | "r" => MiriCommand::Forward(subcommand),
// Invalid command.
_ =>
show_error(format!(
let verbose = has_arg_flag("-v");
// We always setup.
- setup(subcommand);
+ setup(&subcommand);
// Invoke actual cargo for the job, but with different flags.
// We re-use `cargo test` and `cargo run`, which makes target and binary handling very easy but
// harder.
let cargo_miri_path = std::env::current_exe().expect("current executable path invalid");
let cargo_cmd = match subcommand {
- MiriCommand::Test => "test",
- MiriCommand::Run => "run",
+ MiriCommand::Forward(s) => s,
MiriCommand::Setup => return, // `cargo miri setup` stops here.
};
let mut cmd = cargo();
);
}
cmd.env("RUSTC_WRAPPER", &cargo_miri_path);
+ // Having both `RUSTC_WRAPPER` and `RUSTC` set does some odd things, so let's avoid that.
+ // See <https://github.com/rust-lang/miri/issues/2238>.
+ if env::var_os("RUSTC").is_some() && env::var_os("MIRI").is_none() {
+ println!(
+ "WARNING: Ignoring `RUSTC` environment variable; set `MIRI` if you want to control the binary used as the driver."
+ );
+ }
+ cmd.env_remove("RUSTC");
let runner_env_name =
|triple: &str| format!("CARGO_TARGET_{}_RUNNER", triple.to_uppercase().replace('-', "_"));
get_arg_flag_value("--crate-name").unwrap(),
// This is technically a `-C` flag but the prefix seems unique enough...
// (and cargo passes this before the filename so it should be unique)
- get_arg_flag_value("extra-filename").unwrap_or(String::new()),
+ get_arg_flag_value("extra-filename").unwrap_or_default(),
suffix,
));
path
// Forward arguments, but remove "link" from "--emit" to make this a check-only build.
let emit_flag = "--emit";
while let Some(arg) = args.next() {
- if arg.starts_with(emit_flag) {
+ if let Some(val) = arg.strip_prefix(emit_flag) {
// Patch this argument. First, extract its value.
- let val = &arg[emit_flag.len()..];
- assert!(val.starts_with("="), "`cargo` should pass `--emit=X` as one argument");
- let val = &val[1..];
+ let val =
+ val.strip_prefix('=').expect("`cargo` should pass `--emit=X` as one argument");
let mut val: Vec<_> = val.split(',').collect();
// Now make sure "link" is not in there, but "metadata" is.
if let Some(i) = val.iter().position(|&s| s == "link") {
while let Some(arg) = args.next() {
if arg == "--extern" {
forward_patched_extern_arg(&mut args, &mut cmd);
- } else if arg.starts_with(error_format_flag) {
- let suffix = &arg[error_format_flag.len()..];
+ } else if let Some(suffix) = arg.strip_prefix(error_format_flag) {
assert!(suffix.starts_with('='));
// Drop this argument.
- } else if arg.starts_with(json_flag) {
- let suffix = &arg[json_flag.len()..];
+ } else if let Some(suffix) = arg.strip_prefix(json_flag) {
assert!(suffix.starts_with('='));
// Drop this argument.
} else {
// Skip binary name.
args.next().unwrap();
+ // Dispatch to `cargo-miri` phase. There are four phases:
+ // - When we are called via `cargo miri`, we run as the frontend and invoke the underlying
+ // cargo. We set RUSTDOC, RUSTC_WRAPPER and CARGO_TARGET_RUNNER to ourselves.
+ // - When we are executed due to RUSTDOC, we run rustdoc and set both `--test-builder` and
+ // `--runtool` to ourselves.
+ // - When we are executed due to RUSTC_WRAPPER (or as the rustdoc test builder), we build crates
+ // or store the flags of binary crates for later interpretation.
+ // - When we are executed due to CARGO_TARGET_RUNNER (or as the rustdoc runtool), we start
+ // interpretation based on the flags that were stored earlier.
+ //
+ // Additionally, we also set ourselves as RUSTC when calling xargo to build the sysroot, which
+ // has to be treated slightly differently than when we build regular crates.
+
// Dispatch running as part of sysroot compilation.
if env::var_os("MIRI_CALLED_FROM_XARGO").is_some() {
phase_rustc(args, RustcPhase::Setup);
return;
}
- // Dispatch to `cargo-miri` phase. There are three phases:
- // - When we are called via `cargo miri`, we run as the frontend and invoke the underlying
- // cargo. We set RUSTC_WRAPPER and CARGO_TARGET_RUNNER to ourselves.
- // - When we are executed due to RUSTC_WRAPPER, we build crates or store the flags of
- // binary crates for later interpretation.
- // - When we are executed due to CARGO_TARGET_RUNNER, we start interpretation based on the
- // flags that were stored earlier.
- // On top of that, we are also called as RUSTDOC, but that is just a stub currently.
match args.next().as_deref() {
Some("miri") => phase_cargo_miri(args),
Some("rustc") => phase_rustc(args, RustcPhase::Build),