1 #![allow(clippy::useless_format, clippy::derive_partial_eq_without_eq, rustc::internal)]
15 // Rustc does not support non-UTF-8 arguments so we make no attempt either.
16 // (We do support non-UTF-8 environment variables though.)
17 let mut args = std::env::args();
21 // Dispatch to `cargo-miri` phase. Here is a rough idea of "who calls who".
23 // Initially, we are invoked as `cargo-miri miri run/test`. We first run the setup phase:
24 // - We use `rustc-build-sysroot`, and set `RUSTC` back to us, together with `MIRI_CALLED_FROM_SETUP`,
25 // so that the sysroot build rustc invocations end up in `phase_rustc` with `RustcPhase::Setup`.
26 // There we then call the Miri driver with `MIRI_BE_RUSTC` to perform the actual build.
28 // Then we call `cargo run/test`, exactly forwarding all user flags, plus some configuration so
29 // that we control every binary invoked by cargo:
30 // - We set RUSTC_WRAPPER to ourselves, so for (almost) all rustc invocations, we end up in
31 // `phase_rustc` with `RustcPhase::Build`. This will in turn either determine that a
32 // dependency needs to be built (for which it invokes the Miri driver with `MIRI_BE_RUSTC`),
33 // or determine that this is a binary Miri should run, in which case we generate a JSON file
34 // with all the information needed to build and run this crate.
35 // (We don't run it yet since cargo thinks this is a build step, not a run step -- running the
36 // binary here would lead to a bad user experience.)
37 // - We set RUSTC to the Miri driver and also set `MIRI_BE_RUSTC`, so that gets called by build
38 // scripts (and cargo uses it for the version query).
39 // - We set `target.*.runner` to `cargo-miri runner`, which ends up calling `phase_runner` for
40 // `RunnerPhase::Cargo`. This parses the JSON file written in `phase_rustc` and then invokes
41 // the actual Miri driver for interpretation.
42 // - We set RUSTDOC to ourselves, which ends up in `phase_rustdoc`. There we call regular
43 // rustdoc with some extra flags, and we set `MIRI_CALLED_FROM_RUSTDOC` to recognize this
44 // phase in our recursive invocations:
45 // - We set the `--test-builder` flag of rustdoc to ourselves, which ends up in `phase_rustc`
46 // with `RustcPhase::Rustdoc`. There we perform a check-build (needed to get the expected
47 // build failures for `compile_fail` doctests) and then store a JSON file with the
48 // information needed to run this test.
49 // - We also set `--runtool` to ourselves, which ends up in `phase_runner` with
50 // `RunnerPhase::Rustdoc`. There we parse the JSON file written in `phase_rustc` and invoke
51 // the Miri driver for interpretation.
53 // Dispatch running as part of sysroot compilation.
54 if env::var_os("MIRI_CALLED_FROM_SETUP").is_some() {
55 phase_rustc(args, RustcPhase::Setup);
59 // The way rustdoc invokes rustc is indistuingishable from the way cargo invokes rustdoc by the
60 // arguments alone. `phase_cargo_rustdoc` sets this environment variable to let us disambiguate.
61 if env::var_os("MIRI_CALLED_FROM_RUSTDOC").is_some() {
62 // ...however, we then also see this variable when rustdoc invokes us as the testrunner!
63 // The runner is invoked as `$runtool ($runtool-arg)* output_file`;
64 // since we don't specify any runtool-args, and rustdoc supplies multiple arguments to
65 // the test-builder unconditionally, we can just check the number of remaining arguments:
67 phase_runner(args, RunnerPhase::Rustdoc);
69 phase_rustc(args, RustcPhase::Rustdoc);
75 let Some(first) = args.next() else {
77 "`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
80 match first.as_str() {
81 "miri" => phase_cargo_miri(args),
82 "runner" => phase_runner(args, RunnerPhase::Cargo),
83 arg if arg == env::var("RUSTC").unwrap() => {
84 // If the first arg is equal to the RUSTC env ariable (which should be set at this
85 // point), then we need to behave as rustc. This is the somewhat counter-intuitive
86 // behavior of having both RUSTC and RUSTC_WRAPPER set
87 // (see https://github.com/rust-lang/cargo/issues/10886).
88 phase_rustc(args, RustcPhase::Build)
91 // Everything else must be rustdoc. But we need to get `first` "back onto the iterator",
92 // it is some part of the rustdoc invocation.
93 phase_rustdoc(iter::once(first).chain(args));