|
| 1 | +//! This module hosts functions required to run rustc as a driver. |
| 2 | +//! |
| 3 | +//! The rustc driver depends on rustc, which interfaces is unstable. This means |
| 4 | +//! that each driver version is bound to a specific version of rustc. The same |
| 5 | +//! goes for Clippy. However, Clippy has the advantage, that it's distributes via |
| 6 | +//! rustup, which handles the version matching for it. We're not so lucky, at |
| 7 | +//! least not yet. Therefore, we're responsible that the driver is compiled and |
| 8 | +//! run with the correct toolchain. |
| 9 | +//! |
| 10 | +//! If no driver is installed, the user will be requested to run the setup command. |
| 11 | +//! That command will first ensure that the required toolchain is installed and then |
| 12 | +//! run `cargo install` for the driver with a specific toolchain. The version and |
| 13 | +//! toolchain are hardcoded in this crate. |
| 14 | +//! |
| 15 | +//! If a driver is already installed. We'll first run the driver to request the |
| 16 | +//! required toolchain and then run the driver using that toolchain. Requesting |
| 17 | +//! the toolchain works, since the argument will be processed before rustc is run. |
| 18 | +//! At least, that's the idea. |
| 19 | +
|
| 20 | +use std::process::Command; |
| 21 | + |
| 22 | +use once_cell::sync::Lazy; |
| 23 | + |
| 24 | +use crate::ExitStatus; |
| 25 | + |
| 26 | +/// This is the driver version and toolchain, that is used by the setup command |
| 27 | +/// to install the driver. |
| 28 | +static DEFAULT_DRIVER_INFO: Lazy<RustcDriverInfo> = Lazy::new(|| RustcDriverInfo { |
| 29 | + toolchain: "nightly-2022-11-03".to_string(), |
| 30 | + version: "0.1.0".to_string(), |
| 31 | +}); |
| 32 | + |
| 33 | +struct RustcDriverInfo { |
| 34 | + toolchain: String, |
| 35 | + version: String, |
| 36 | +} |
| 37 | + |
| 38 | +/// This function checks if the specified toolchain is installed. This requires |
| 39 | +/// rustup. A dependency we have to live with for now. |
| 40 | +fn check_toolchain(toolchain: &str) -> Result<(), ExitStatus> { |
| 41 | + let mut cmd = Command::new("cargo"); |
| 42 | + cmd.args([&format!("+{toolchain}"), "-V"]); |
| 43 | + if cmd.output().is_err() { |
| 44 | + eprintln!("error: the required toolchain `{toolchain}` can't be found"); |
| 45 | + eprintln!(); |
| 46 | + eprintln!("You can install the toolchain by running: rustup toolchain install {toolchain}"); |
| 47 | + Err(ExitStatus::InvalidToolchain) |
| 48 | + } else { |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// This tries to install the rustc driver specified in [`DEFAULT_DRIVER_INFO`]. |
| 54 | +pub fn install_driver(verbose: bool) -> Result<(), ExitStatus> { |
| 55 | + // Prerequisites |
| 56 | + let toolchain = &DEFAULT_DRIVER_INFO.toolchain; |
| 57 | + check_toolchain(&toolchain)?; |
| 58 | + |
| 59 | + // Build driver |
| 60 | + let mut cmd = Command::new("cargo"); |
| 61 | + cmd.arg(&format!("+{toolchain}")); |
| 62 | + |
| 63 | + if verbose { |
| 64 | + cmd.arg("--verbose"); |
| 65 | + } |
| 66 | + |
| 67 | + #[cfg(feature = "dev-build")] |
| 68 | + cmd.args(["build", "--bin", "linter_driver_rustc"]); |
| 69 | + #[cfg(not(feature = "dev-build"))] |
| 70 | + cmd.args([ |
| 71 | + "install", |
| 72 | + "marker_rustc_driver", |
| 73 | + "--version", |
| 74 | + &DEFAULT_DRIVER_INFO.version, |
| 75 | + ]); |
| 76 | + |
| 77 | + let status = cmd |
| 78 | + .spawn() |
| 79 | + .expect("unable to start cargo install for the driver") |
| 80 | + .wait() |
| 81 | + .expect("unable to wait on cargo install for the driver"); |
| 82 | + if status.success() { |
| 83 | + Ok(()) |
| 84 | + } else { |
| 85 | + // The user can see cargo's output, as the command output was passed on |
| 86 | + // to the user via the `.spawn()` call. |
| 87 | + Err(ExitStatus::DriverInstallationFailed) |
| 88 | + } |
| 89 | +} |
0 commit comments