2018-11-25 09:08:24 -06:00
|
|
|
#![feature(inner_deref)]
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
use std::fs::{self, File};
|
2019-12-23 05:56:23 -06:00
|
|
|
use std::io::{self, BufRead, Write};
|
2019-08-04 03:14:51 -05:00
|
|
|
use std::ops::Not;
|
2019-12-23 05:56:23 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::process::Command;
|
2017-01-24 06:28:36 -06:00
|
|
|
|
2019-11-08 09:36:57 -06:00
|
|
|
const XARGO_MIN_VERSION: (u32, u32, u32) = (0, 3, 17);
|
|
|
|
|
2019-02-07 06:00:42 -06:00
|
|
|
const CARGO_MIRI_HELP: &str = r#"Interprets bin crates and tests in Miri
|
2017-01-24 06:28:36 -06:00
|
|
|
|
|
|
|
Usage:
|
2019-02-09 05:42:16 -06:00
|
|
|
cargo miri [subcommand] [options] [--] [<miri opts>...] [--] [<program opts>...]
|
2018-11-25 09:30:11 -06:00
|
|
|
|
|
|
|
Subcommands:
|
|
|
|
run Run binaries (default)
|
|
|
|
test Run tests
|
|
|
|
setup Only perform automatic setup, but without asking questions (for getting a proper libstd)
|
2017-01-24 06:28:36 -06:00
|
|
|
|
|
|
|
Common options:
|
|
|
|
-h, --help Print this message
|
|
|
|
--features Features to compile for the package
|
|
|
|
-V, --version Print version info and exit
|
|
|
|
|
2019-02-09 05:42:16 -06:00
|
|
|
Other [options] are the same as `cargo rustc`. Everything after the first "--" is
|
|
|
|
passed verbatim to Miri, which will pass everything after the second "--" verbatim
|
|
|
|
to the interpreted program.
|
2017-01-24 06:28:36 -06:00
|
|
|
"#;
|
|
|
|
|
2018-11-25 09:30:11 -06:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2018-11-25 09:08:24 -06:00
|
|
|
enum MiriCommand {
|
|
|
|
Run,
|
|
|
|
Test,
|
|
|
|
Setup,
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:28:36 -06:00
|
|
|
fn show_help() {
|
|
|
|
println!("{}", CARGO_MIRI_HELP);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_version() {
|
2019-12-23 05:56:23 -06:00
|
|
|
println!(
|
|
|
|
"miri {} ({} {})",
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
env!("VERGEN_SHA_SHORT"),
|
|
|
|
env!("VERGEN_COMMIT_DATE")
|
|
|
|
);
|
2017-01-24 06:28:36 -06:00
|
|
|
}
|
|
|
|
|
2018-11-25 09:30:11 -06:00
|
|
|
fn show_error(msg: String) -> ! {
|
|
|
|
eprintln!("fatal error: {}", msg);
|
|
|
|
std::process::exit(1)
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Determines whether a `--flag` is present.
|
2019-02-07 06:00:27 -06:00
|
|
|
fn has_arg_flag(name: &str) -> bool {
|
|
|
|
let mut args = std::env::args().take_while(|val| val != "--");
|
|
|
|
args.any(|val| val == name)
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Gets the value of a `--flag`.
|
2018-12-10 02:23:27 -06:00
|
|
|
fn get_arg_flag_value(name: &str) -> Option<String> {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Stop searching at `--`.
|
2019-02-07 06:00:27 -06:00
|
|
|
let mut args = std::env::args().take_while(|val| val != "--");
|
|
|
|
loop {
|
|
|
|
let arg = match args.next() {
|
|
|
|
Some(arg) => arg,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
if !arg.starts_with(name) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-15 19:29:38 -06:00
|
|
|
// Strip leading `name`.
|
|
|
|
let suffix = &arg[name.len()..];
|
2019-02-07 06:00:27 -06:00
|
|
|
if suffix.is_empty() {
|
2019-02-15 19:29:38 -06:00
|
|
|
// This argument is exactly `name`; the next one is the value.
|
2019-02-07 06:00:27 -06:00
|
|
|
return args.next();
|
|
|
|
} else if suffix.starts_with('=') {
|
2019-02-15 19:29:38 -06:00
|
|
|
// This argument is `name=value`; get the value.
|
|
|
|
// Strip leading `=`.
|
|
|
|
return Some(suffix[1..].to_owned());
|
2019-02-07 06:00:27 -06:00
|
|
|
}
|
2018-12-10 02:23:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
fn list_targets() -> impl Iterator<Item = cargo_metadata::Target> {
|
2018-11-25 09:08:24 -06:00
|
|
|
// We need to get the manifest, and then the metadata, to enumerate targets.
|
2019-12-23 05:56:23 -06:00
|
|
|
let manifest_path =
|
|
|
|
get_arg_flag_value("--manifest-path").map(|m| Path::new(&m).canonicalize().unwrap());
|
2018-11-25 09:08:24 -06:00
|
|
|
|
2019-04-27 16:31:48 -05:00
|
|
|
let mut cmd = cargo_metadata::MetadataCommand::new();
|
|
|
|
if let Some(ref manifest_path) = manifest_path {
|
|
|
|
cmd.manifest_path(manifest_path);
|
|
|
|
}
|
|
|
|
let mut metadata = if let Ok(metadata) = cmd.exec() {
|
2018-11-25 09:08:24 -06:00
|
|
|
metadata
|
|
|
|
} else {
|
2019-10-16 14:45:17 -05:00
|
|
|
show_error(format!("Could not obtain Cargo metadata; likely an ill-formed manifest"));
|
2018-11-25 09:08:24 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let current_dir = std::env::current_dir();
|
|
|
|
|
|
|
|
let package_index = metadata
|
|
|
|
.packages
|
|
|
|
.iter()
|
|
|
|
.position(|package| {
|
|
|
|
let package_manifest_path = Path::new(&package.manifest_path);
|
|
|
|
if let Some(ref manifest_path) = manifest_path {
|
|
|
|
package_manifest_path == manifest_path
|
|
|
|
} else {
|
2019-12-23 05:56:23 -06:00
|
|
|
let current_dir = current_dir.as_ref().expect("could not read current directory");
|
|
|
|
let package_manifest_directory = package_manifest_path
|
|
|
|
.parent()
|
|
|
|
.expect("could not find parent directory of package manifest");
|
2018-11-25 09:08:24 -06:00
|
|
|
package_manifest_directory == current_dir
|
|
|
|
}
|
|
|
|
})
|
2019-12-23 05:56:23 -06:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
show_error(format!(
|
|
|
|
"This seems to be a workspace, which is not supported by cargo-miri"
|
|
|
|
))
|
|
|
|
});
|
2018-11-25 09:08:24 -06:00
|
|
|
let package = metadata.packages.remove(package_index);
|
|
|
|
|
|
|
|
// Finally we got the list of targets to build
|
|
|
|
package.targets.into_iter()
|
|
|
|
}
|
|
|
|
|
2019-06-09 12:47:09 -05:00
|
|
|
/// Returns the path to the `miri` binary
|
|
|
|
fn find_miri() -> PathBuf {
|
|
|
|
let mut path = std::env::current_exe().expect("current executable path invalid");
|
|
|
|
path.set_file_name("miri");
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2019-06-09 06:48:18 -05:00
|
|
|
/// Make sure that the `miri` and `rustc` binary are from the same sysroot.
|
|
|
|
/// This can be violated e.g. when miri is locally built and installed with a different
|
|
|
|
/// toolchain than what is used when `cargo miri` is run.
|
|
|
|
fn test_sysroot_consistency() {
|
|
|
|
fn get_sysroot(mut cmd: Command) -> PathBuf {
|
2019-12-23 05:56:23 -06:00
|
|
|
let out = cmd
|
|
|
|
.arg("--print")
|
|
|
|
.arg("sysroot")
|
|
|
|
.output()
|
|
|
|
.expect("Failed to run rustc to get sysroot info");
|
2019-06-14 04:15:09 -05:00
|
|
|
let stdout = String::from_utf8(out.stdout).expect("stdout is not valid UTF-8");
|
|
|
|
let stderr = String::from_utf8(out.stderr).expect("stderr is not valid UTF-8");
|
2019-10-12 11:11:44 -05:00
|
|
|
assert!(
|
|
|
|
out.status.success(),
|
|
|
|
"Bad status code {} when getting sysroot info via {:?}.\nstdout:\n{}\nstderr:\n{}",
|
2019-12-23 05:56:23 -06:00
|
|
|
out.status,
|
|
|
|
cmd,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
2019-10-12 11:11:44 -05:00
|
|
|
);
|
2019-06-14 04:15:09 -05:00
|
|
|
let stdout = stdout.trim();
|
2019-12-23 05:56:23 -06:00
|
|
|
PathBuf::from(stdout)
|
|
|
|
.canonicalize()
|
2019-06-14 04:15:09 -05:00
|
|
|
.unwrap_or_else(|_| panic!("Failed to canonicalize sysroot: {}", stdout))
|
2019-06-09 06:48:18 -05:00
|
|
|
}
|
2019-07-31 08:15:14 -05:00
|
|
|
|
2019-12-22 11:13:13 -06:00
|
|
|
// Do not check sysroots if we got built as part of a Rust distribution.
|
|
|
|
// During `bootstrap`, the sysroot does not match anyway, and then some distros
|
|
|
|
// play symlink tricks so the sysroots may be different even for the final stage
|
|
|
|
// (see <https://github.com/mozilla/nixpkgs-mozilla/issues/198>).
|
|
|
|
if option_env!("RUSTC_STAGE").is_some() {
|
2019-07-31 08:15:14 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-06-09 06:48:18 -05:00
|
|
|
|
|
|
|
let rustc_sysroot = get_sysroot(Command::new("rustc"));
|
2019-06-09 12:47:09 -05:00
|
|
|
let miri_sysroot = get_sysroot(Command::new(find_miri()));
|
2019-06-09 06:48:18 -05:00
|
|
|
|
|
|
|
if rustc_sysroot != miri_sysroot {
|
|
|
|
show_error(format!(
|
|
|
|
"miri was built for a different sysroot than the rustc in your current toolchain.\n\
|
|
|
|
Make sure you use the same toolchain to run miri that you used to build it!\n\
|
|
|
|
rustc sysroot: `{}`\n\
|
|
|
|
miri sysroot: `{}`",
|
2019-12-23 05:56:23 -06:00
|
|
|
rustc_sysroot.display(),
|
|
|
|
miri_sysroot.display()
|
2019-06-09 06:48:18 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 06:53:02 -05:00
|
|
|
fn cargo() -> Command {
|
|
|
|
if let Ok(val) = std::env::var("CARGO") {
|
|
|
|
// Bootstrap tells us where to find cargo
|
|
|
|
Command::new(val)
|
|
|
|
} else {
|
|
|
|
Command::new("cargo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn xargo() -> Command {
|
|
|
|
if let Ok(val) = std::env::var("XARGO") {
|
|
|
|
// Bootstrap tells us where to find xargo
|
|
|
|
Command::new(val)
|
|
|
|
} else {
|
|
|
|
Command::new("xargo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 08:08:03 -06:00
|
|
|
fn xargo_version() -> Option<(u32, u32, u32)> {
|
2019-08-03 06:53:02 -05:00
|
|
|
let out = xargo().arg("--version").output().ok()?;
|
2018-12-15 08:08:03 -06:00
|
|
|
if !out.status.success() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// Parse output. The first line looks like "xargo 0.3.12 (b004f1c 2018-12-13)".
|
2019-12-23 05:56:23 -06:00
|
|
|
let line = out
|
|
|
|
.stderr
|
|
|
|
.lines()
|
|
|
|
.nth(0)
|
2018-12-15 08:08:03 -06:00
|
|
|
.expect("malformed `xargo --version` output: not at least one line")
|
|
|
|
.expect("malformed `xargo --version` output: error reading first line");
|
2018-12-19 01:41:31 -06:00
|
|
|
let (name, version) = {
|
|
|
|
let mut split = line.split(' ');
|
2019-12-23 05:56:23 -06:00
|
|
|
(
|
|
|
|
split.next().expect("malformed `xargo --version` output: empty"),
|
|
|
|
split.next().expect("malformed `xargo --version` output: not at least two words"),
|
|
|
|
)
|
2018-12-19 01:41:31 -06:00
|
|
|
};
|
2019-05-10 01:05:34 -05:00
|
|
|
if name != "xargo" {
|
|
|
|
// This is some fork of xargo
|
2019-05-01 13:37:08 -05:00
|
|
|
return None;
|
|
|
|
}
|
2018-12-15 08:08:03 -06:00
|
|
|
let mut version_pieces = version.split('.');
|
2019-12-23 05:56:23 -06:00
|
|
|
let major = version_pieces
|
|
|
|
.next()
|
2018-12-15 08:08:03 -06:00
|
|
|
.expect("malformed `xargo --version` output: not a major version piece")
|
|
|
|
.parse()
|
|
|
|
.expect("malformed `xargo --version` output: major version is not an integer");
|
2019-12-23 05:56:23 -06:00
|
|
|
let minor = version_pieces
|
|
|
|
.next()
|
2018-12-15 08:08:03 -06:00
|
|
|
.expect("malformed `xargo --version` output: not a minor version piece")
|
|
|
|
.parse()
|
|
|
|
.expect("malformed `xargo --version` output: minor version is not an integer");
|
2019-12-23 05:56:23 -06:00
|
|
|
let patch = version_pieces
|
|
|
|
.next()
|
2018-12-15 08:08:03 -06:00
|
|
|
.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() {
|
|
|
|
panic!("malformed `xargo --version` output: more than three pieces in version");
|
|
|
|
}
|
|
|
|
Some((major, minor, patch))
|
|
|
|
}
|
|
|
|
|
2019-09-13 03:39:36 -05:00
|
|
|
fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
|
|
|
|
if ask {
|
|
|
|
let mut buf = String::new();
|
|
|
|
print!("I will run `{:?}` to {}. Proceed? [Y/n] ", cmd, text);
|
|
|
|
io::stdout().flush().unwrap();
|
|
|
|
io::stdin().read_line(&mut buf).unwrap();
|
|
|
|
match buf.trim().to_lowercase().as_ref() {
|
|
|
|
// Proceed.
|
2019-12-23 05:56:23 -06:00
|
|
|
"" | "y" | "yes" => {}
|
2019-09-13 03:39:36 -05:00
|
|
|
"n" | "no" => show_error(format!("Aborting as per your request")),
|
2019-12-23 05:56:23 -06:00
|
|
|
a => show_error(format!("I do not understand `{}`", a)),
|
2019-09-13 03:39:36 -05:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
println!("Running `{:?}` to {}.", cmd, text);
|
|
|
|
}
|
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
if cmd.status().expect(&format!("failed to execute {:?}", cmd)).success().not() {
|
2019-09-13 03:39:36 -05:00
|
|
|
show_error(format!("Failed to {}", text));
|
|
|
|
}
|
2018-11-25 09:30:11 -06:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// 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.
|
2018-11-25 09:30:11 -06:00
|
|
|
fn setup(ask_user: bool) {
|
|
|
|
if std::env::var("MIRI_SYSROOT").is_ok() {
|
2019-04-19 12:27:19 -05:00
|
|
|
if !ask_user {
|
|
|
|
println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
|
|
|
|
}
|
2018-11-25 09:30:11 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// First, we need xargo.
|
2019-11-08 09:36:57 -06:00
|
|
|
if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
|
2019-10-21 03:25:47 -05:00
|
|
|
if std::env::var("XARGO").is_ok() {
|
|
|
|
// The user manually gave us a xargo binary; don't do anything automatically.
|
|
|
|
show_error(format!("Your xargo is too old; please upgrade to the latest version"))
|
|
|
|
}
|
2019-09-13 03:39:36 -05:00
|
|
|
let mut cmd = cargo();
|
|
|
|
cmd.args(&["install", "xargo", "-f"]);
|
|
|
|
ask_to_run(cmd, ask_user, "install a recent enough xargo");
|
2018-11-25 09:30:11 -06:00
|
|
|
}
|
2018-11-25 10:09:49 -06:00
|
|
|
|
2019-11-23 03:33:49 -06:00
|
|
|
// Determine where the rust sources are located. `XARGO_RUST_SRC` env var trumps everything.
|
|
|
|
let rust_src = match std::env::var("XARGO_RUST_SRC") {
|
|
|
|
Ok(val) => PathBuf::from(val),
|
|
|
|
Err(_) => {
|
|
|
|
// Check for `rust-src` rustup component.
|
2019-12-23 05:56:23 -06:00
|
|
|
let sysroot = Command::new("rustc")
|
|
|
|
.args(&["--print", "sysroot"])
|
|
|
|
.output()
|
2019-11-23 03:33:49 -06:00
|
|
|
.expect("failed to get rustc sysroot")
|
|
|
|
.stdout;
|
|
|
|
let sysroot = std::str::from_utf8(&sysroot).unwrap();
|
|
|
|
let sysroot = Path::new(sysroot.trim_end_matches('\n'));
|
|
|
|
// First try: `$SYSROOT/lib/rustlib/src/rust`; test if that contains `Cargo.lock`.
|
|
|
|
let rustup_src = sysroot.join("lib").join("rustlib").join("src").join("rust");
|
|
|
|
let base_dir = if rustup_src.join("Cargo.lock").exists() {
|
|
|
|
// Just use this.
|
|
|
|
rustup_src
|
|
|
|
} else {
|
|
|
|
// Maybe this is a local toolchain built with `x.py` and linked into `rustup`?
|
|
|
|
// Second try: `$SYSROOT/../../..`; test if that contains `x.py`.
|
|
|
|
let local_src = sysroot.parent().and_then(Path::parent).and_then(Path::parent);
|
|
|
|
match local_src {
|
|
|
|
Some(local_src) if local_src.join("x.py").exists() => {
|
|
|
|
// Use this.
|
|
|
|
PathBuf::from(local_src)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Fallback: Ask the user to install the `rust-src` component, and use that.
|
|
|
|
let mut cmd = Command::new("rustup");
|
|
|
|
cmd.args(&["component", "add", "rust-src"]);
|
2019-12-23 05:56:23 -06:00
|
|
|
ask_to_run(
|
|
|
|
cmd,
|
|
|
|
ask_user,
|
|
|
|
"install the rustc-src component for the selected toolchain",
|
|
|
|
);
|
2019-11-23 03:33:49 -06:00
|
|
|
rustup_src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
base_dir.join("src") // Xargo wants the src-subdir
|
2018-11-25 10:09:49 -06:00
|
|
|
}
|
2019-11-23 03:33:49 -06:00
|
|
|
};
|
|
|
|
if !rust_src.exists() {
|
|
|
|
show_error(format!("Given Rust source directory `{}` does not exist.", rust_src.display()));
|
2018-11-25 10:09:49 -06:00
|
|
|
}
|
|
|
|
|
2018-11-27 04:26:53 -06:00
|
|
|
// Next, we need our own libstd. We will do this work in whatever is a good cache dir for this platform.
|
2019-06-20 12:45:39 -05:00
|
|
|
let dirs = directories::ProjectDirs::from("org", "rust-lang", "miri").unwrap();
|
2018-11-27 04:26:53 -06:00
|
|
|
let dir = dirs.cache_dir();
|
2018-11-25 10:09:49 -06:00
|
|
|
if !dir.exists() {
|
2018-11-27 04:43:02 -06:00
|
|
|
fs::create_dir_all(&dir).unwrap();
|
2018-11-25 10:09:49 -06:00
|
|
|
}
|
|
|
|
// The interesting bit: Xargo.toml
|
2019-12-23 05:56:23 -06:00
|
|
|
File::create(dir.join("Xargo.toml"))
|
|
|
|
.unwrap()
|
|
|
|
.write_all(
|
|
|
|
br#"
|
2018-11-25 10:09:49 -06:00
|
|
|
[dependencies.std]
|
2018-12-02 07:03:29 -06:00
|
|
|
default_features = false
|
|
|
|
# We need the `panic_unwind` feature because we use the `unwind` panic strategy.
|
|
|
|
# Using `abort` works for libstd, but then libtest will not compile.
|
2019-09-16 15:22:54 -05:00
|
|
|
features = ["panic_unwind"]
|
2018-11-25 10:09:49 -06:00
|
|
|
|
|
|
|
[dependencies.test]
|
2019-12-23 05:56:23 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-02-15 19:29:38 -06:00
|
|
|
// The boring bits: a dummy project for xargo.
|
2019-12-23 05:56:23 -06:00
|
|
|
File::create(dir.join("Cargo.toml"))
|
|
|
|
.unwrap()
|
|
|
|
.write_all(
|
|
|
|
br#"
|
2018-11-25 10:09:49 -06:00
|
|
|
[package]
|
|
|
|
name = "miri-xargo"
|
|
|
|
description = "A dummy project for building libstd with xargo."
|
|
|
|
version = "0.0.0"
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
path = "lib.rs"
|
2019-12-23 05:56:23 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-25 10:09:49 -06:00
|
|
|
File::create(dir.join("lib.rs")).unwrap();
|
2019-08-03 10:20:16 -05:00
|
|
|
// Prepare xargo invocation.
|
2018-12-10 02:32:54 -06:00
|
|
|
let target = get_arg_flag_value("--target");
|
2019-10-19 09:36:45 -05:00
|
|
|
let print_sysroot = !ask_user && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
|
2019-08-03 06:53:02 -05:00
|
|
|
let mut command = xargo();
|
2019-08-03 10:20:16 -05:00
|
|
|
command.arg("build").arg("-q");
|
|
|
|
command.current_dir(&dir);
|
|
|
|
command.env("RUSTFLAGS", miri::miri_default_args().join(" "));
|
2019-11-23 03:33:49 -06:00
|
|
|
command.env("XARGO_HOME", &dir);
|
|
|
|
command.env("XARGO_RUST_SRC", &rust_src);
|
2019-08-03 10:20:16 -05:00
|
|
|
// In bootstrap, make sure we don't get debug assertons into our libstd.
|
|
|
|
command.env("RUSTC_DEBUG_ASSERTIONS", "false");
|
|
|
|
// Handle target flag.
|
2018-12-10 02:32:54 -06:00
|
|
|
if let Some(ref target) = target {
|
|
|
|
command.arg("--target").arg(&target);
|
|
|
|
}
|
2019-08-03 10:20:16 -05:00
|
|
|
// Finally run it!
|
2019-12-23 05:56:23 -06:00
|
|
|
if command.status().expect("failed to run xargo").success().not() {
|
2018-11-25 10:09:49 -06:00
|
|
|
show_error(format!("Failed to run xargo"));
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// That should be it! But we need to figure out where xargo built stuff.
|
2018-12-10 02:32:54 -06:00
|
|
|
// Unfortunately, it puts things into a different directory when the
|
|
|
|
// architecture matches the host.
|
|
|
|
let is_host = match target {
|
|
|
|
None => true,
|
|
|
|
Some(target) => target == rustc_version::version_meta().unwrap().host,
|
|
|
|
};
|
|
|
|
let sysroot = if is_host { dir.join("HOST") } else { PathBuf::from(dir) };
|
2019-06-09 10:10:04 -05:00
|
|
|
std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
|
2019-10-19 09:36:45 -05:00
|
|
|
if print_sysroot {
|
|
|
|
// Print just the sysroot and nothing else; this way we do not need any escaping.
|
|
|
|
println!("{}", sysroot.display());
|
2019-05-27 07:40:27 -05:00
|
|
|
} else if !ask_user {
|
2019-06-09 06:48:18 -05:00
|
|
|
println!("A libstd for Miri is now available in `{}`.", sysroot.display());
|
2018-11-30 02:23:44 -06:00
|
|
|
}
|
2018-11-25 09:30:11 -06:00
|
|
|
}
|
|
|
|
|
2017-01-24 06:28:36 -06:00
|
|
|
fn main() {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Check for version and help flags even when invoked as `cargo-miri`.
|
2017-01-24 06:28:36 -06:00
|
|
|
if std::env::args().any(|a| a == "--help" || a == "-h") {
|
|
|
|
show_help();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if std::env::args().any(|a| a == "--version" || a == "-V") {
|
|
|
|
show_version();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some("miri") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
|
2019-02-15 19:29:38 -06:00
|
|
|
// This arm is for when `cargo miri` is called. We call `cargo rustc` for each applicable target,
|
|
|
|
// but with the `RUSTC` env var set to the `cargo-miri` binary so that we come back in the other branch,
|
|
|
|
// and dispatch the invocations to `rustc` and `miri`, respectively.
|
2019-02-07 06:00:27 -06:00
|
|
|
in_cargo_miri();
|
2018-12-12 10:03:40 -06:00
|
|
|
} else if let Some("rustc") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
|
2019-02-15 19:29:38 -06:00
|
|
|
// This arm is executed when `cargo-miri` runs `cargo rustc` with the `RUSTC_WRAPPER` env var set to itself:
|
|
|
|
// dependencies get dispatched to `rustc`, the final test/binary to `miri`.
|
2019-02-07 06:00:27 -06:00
|
|
|
inside_cargo_rustc();
|
|
|
|
} else {
|
2019-02-15 19:29:38 -06:00
|
|
|
show_error(format!("must be called with either `miri` or `rustc` as first argument."))
|
2019-02-07 06:00:27 -06:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 06:28:36 -06:00
|
|
|
|
2019-02-07 06:00:27 -06:00
|
|
|
fn in_cargo_miri() {
|
2019-07-26 15:50:01 -05:00
|
|
|
let (subcommand, skip) = match std::env::args().nth(2).as_deref() {
|
2019-02-07 06:00:27 -06:00
|
|
|
Some("test") => (MiriCommand::Test, 3),
|
|
|
|
Some("run") => (MiriCommand::Run, 3),
|
|
|
|
Some("setup") => (MiriCommand::Setup, 3),
|
2019-02-15 19:29:38 -06:00
|
|
|
// Default command, if there is an option or nothing.
|
2019-02-07 06:00:27 -06:00
|
|
|
Some(s) if s.starts_with("-") => (MiriCommand::Run, 2),
|
|
|
|
None => (MiriCommand::Run, 2),
|
2019-02-15 19:29:38 -06:00
|
|
|
// Invalid command.
|
2019-12-23 05:56:23 -06:00
|
|
|
Some(s) => show_error(format!("Unknown command `{}`", s)),
|
2019-02-07 06:00:27 -06:00
|
|
|
};
|
|
|
|
let verbose = has_arg_flag("-v");
|
2017-01-24 06:28:36 -06:00
|
|
|
|
2019-06-09 06:48:18 -05:00
|
|
|
// Some basic sanity checks
|
|
|
|
test_sysroot_consistency();
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// We always setup.
|
2019-02-07 06:00:27 -06:00
|
|
|
let ask = subcommand != MiriCommand::Setup;
|
|
|
|
setup(ask);
|
|
|
|
if subcommand == MiriCommand::Setup {
|
|
|
|
// Stop here.
|
|
|
|
return;
|
|
|
|
}
|
2017-01-24 06:28:36 -06:00
|
|
|
|
2019-02-07 06:00:27 -06:00
|
|
|
// Now run the command.
|
|
|
|
for target in list_targets() {
|
|
|
|
let mut args = std::env::args().skip(skip);
|
2019-12-23 05:56:23 -06:00
|
|
|
let kind = target
|
|
|
|
.kind
|
|
|
|
.get(0)
|
|
|
|
.expect("badly formatted cargo metadata: target::kind is an empty array");
|
2019-02-07 06:00:27 -06:00
|
|
|
// Now we run `cargo rustc $FLAGS $ARGS`, giving the user the
|
2019-03-10 11:53:30 -05:00
|
|
|
// change to add additional arguments. `FLAGS` is set to identify
|
2019-02-07 06:00:27 -06:00
|
|
|
// this target. The user gets to control what gets actually passed to Miri.
|
2019-08-03 06:53:02 -05:00
|
|
|
let mut cmd = cargo();
|
2019-02-07 06:00:27 -06:00
|
|
|
cmd.arg("rustc");
|
2019-02-27 04:38:43 -06:00
|
|
|
match (subcommand, kind.as_str()) {
|
2019-02-07 06:00:27 -06:00
|
|
|
(MiriCommand::Run, "bin") => {
|
2019-02-15 19:29:38 -06:00
|
|
|
// FIXME: we just run all the binaries here.
|
2019-02-07 06:00:27 -06:00
|
|
|
// We should instead support `cargo miri --bin foo`.
|
|
|
|
cmd.arg("--bin").arg(target.name);
|
|
|
|
}
|
|
|
|
(MiriCommand::Test, "test") => {
|
|
|
|
cmd.arg("--test").arg(target.name);
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2019-02-07 09:33:46 -06:00
|
|
|
(MiriCommand::Test, "lib") => {
|
2019-02-15 19:29:38 -06:00
|
|
|
// There can be only one lib.
|
2019-02-07 09:33:46 -06:00
|
|
|
cmd.arg("--lib").arg("--profile").arg("test");
|
|
|
|
}
|
2019-02-07 06:00:27 -06:00
|
|
|
(MiriCommand::Test, "bin") => {
|
2019-02-07 09:33:46 -06:00
|
|
|
cmd.arg("--bin").arg(target.name).arg("--profile").arg("test");
|
2019-02-07 06:00:27 -06:00
|
|
|
}
|
2019-02-15 19:29:38 -06:00
|
|
|
// The remaining targets we do not even want to build.
|
2019-02-07 06:00:27 -06:00
|
|
|
_ => continue,
|
|
|
|
}
|
2019-02-15 19:29:38 -06:00
|
|
|
// Add user-defined args until first `--`.
|
2019-02-07 06:00:27 -06:00
|
|
|
while let Some(arg) = args.next() {
|
|
|
|
if arg == "--" {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd.arg(arg);
|
|
|
|
}
|
2019-02-15 19:29:38 -06:00
|
|
|
// Add `--` (to end the `cargo` flags), and then the user flags. We add markers around the
|
2019-03-10 11:53:30 -05:00
|
|
|
// user flags to be able to identify them later. "cargo rustc" adds more stuff after this,
|
|
|
|
// so we have to mark both the beginning and the end.
|
2019-12-23 05:56:23 -06:00
|
|
|
cmd.arg("--").arg("cargo-miri-marker-begin").args(args).arg("cargo-miri-marker-end");
|
2019-02-07 06:00:27 -06:00
|
|
|
let path = std::env::current_exe().expect("current executable path invalid");
|
|
|
|
cmd.env("RUSTC_WRAPPER", path);
|
|
|
|
if verbose {
|
|
|
|
eprintln!("+ {:?}", cmd);
|
|
|
|
}
|
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
let exit_status =
|
|
|
|
cmd.spawn().expect("could not run cargo").wait().expect("failed to wait for cargo?");
|
2019-02-07 06:00:27 -06:00
|
|
|
|
|
|
|
if !exit_status.success() {
|
|
|
|
std::process::exit(exit_status.code().unwrap_or(-1))
|
2017-01-24 06:28:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 06:00:27 -06:00
|
|
|
fn inside_cargo_rustc() {
|
2019-06-09 07:10:42 -05:00
|
|
|
let sysroot = std::env::var("MIRI_SYSROOT").expect("The wrapper should have set MIRI_SYSROOT");
|
2017-01-24 06:28:36 -06:00
|
|
|
|
2019-06-09 07:10:42 -05:00
|
|
|
let rustc_args = std::env::args().skip(2); // skip `cargo rustc`
|
2019-12-23 05:56:23 -06:00
|
|
|
let mut args: Vec<String> =
|
|
|
|
rustc_args.chain(Some("--sysroot".to_owned())).chain(Some(sysroot)).collect();
|
2019-02-07 06:00:27 -06:00
|
|
|
args.splice(0..0, miri::miri_default_args().iter().map(ToString::to_string));
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// See if we can find the `cargo-miri` markers. Those only get added to the binary we want to
|
|
|
|
// run. They also serve to mark the user-defined arguments, which we have to move all the way
|
|
|
|
// to the end (they get added somewhere in the middle).
|
2019-12-23 05:56:23 -06:00
|
|
|
let needs_miri =
|
|
|
|
if let Some(begin) = args.iter().position(|arg| arg == "cargo-miri-marker-begin") {
|
|
|
|
let end = args
|
|
|
|
.iter()
|
|
|
|
.position(|arg| arg == "cargo-miri-marker-end")
|
|
|
|
.expect("cannot find end marker");
|
|
|
|
// These mark the user arguments. We remove the first and last as they are the markers.
|
|
|
|
let mut user_args = args.drain(begin..=end);
|
|
|
|
assert_eq!(user_args.next().unwrap(), "cargo-miri-marker-begin");
|
|
|
|
assert_eq!(user_args.next_back().unwrap(), "cargo-miri-marker-end");
|
|
|
|
// Collect the rest and add it back at the end.
|
|
|
|
let mut user_args = user_args.collect::<Vec<String>>();
|
|
|
|
args.append(&mut user_args);
|
|
|
|
// Run this in Miri.
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2019-02-07 06:00:27 -06:00
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
let mut command = if needs_miri { Command::new(find_miri()) } else { Command::new("rustc") };
|
2019-02-07 06:00:27 -06:00
|
|
|
command.args(&args);
|
|
|
|
if has_arg_flag("-v") {
|
|
|
|
eprintln!("+ {:?}", command);
|
|
|
|
}
|
|
|
|
|
|
|
|
match command.status() {
|
2019-12-23 05:56:23 -06:00
|
|
|
Ok(exit) =>
|
2019-02-07 06:00:27 -06:00
|
|
|
if !exit.success() {
|
|
|
|
std::process::exit(exit.code().unwrap_or(42));
|
2019-12-23 05:56:23 -06:00
|
|
|
},
|
2019-02-07 06:00:27 -06:00
|
|
|
Err(ref e) if needs_miri => panic!("error during miri run: {:?}", e),
|
|
|
|
Err(ref e) => panic!("error during rustc call: {:?}", e),
|
2017-01-24 06:28:36 -06:00
|
|
|
}
|
|
|
|
}
|