|
| 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 | +use std::{ffi::OsString, path::PathBuf, process::Command}; |
| 16 | + |
| 17 | +use once_cell::sync::Lazy; |
| 18 | + |
| 19 | +use crate::ExitStatus; |
| 20 | + |
| 21 | +/// This is the driver version and toolchain, that is used by the setup command |
| 22 | +/// to install the driver. |
| 23 | +static DEFAULT_DRIVER_INFO: Lazy<RustcDriverInfo> = Lazy::new(|| RustcDriverInfo { |
| 24 | + toolchain: "nightly-2022-11-03".to_string(), |
| 25 | + version: "0.1.0".to_string(), |
| 26 | + api_version: "0.1.0".to_string(), |
| 27 | +}); |
| 28 | + |
| 29 | +struct RustcDriverInfo { |
| 30 | + toolchain: String, |
| 31 | + version: String, |
| 32 | + #[allow(unused)] |
| 33 | + api_version: String, |
| 34 | +} |
| 35 | + |
| 36 | +pub fn print_driver_version() { |
| 37 | + println!( |
| 38 | + "rustc driver version: {} (toolchain: {}, api: {})", |
| 39 | + DEFAULT_DRIVER_INFO.version, DEFAULT_DRIVER_INFO.toolchain, DEFAULT_DRIVER_INFO.api_version |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +/// This tries to install the rustc driver specified in [`DEFAULT_DRIVER_INFO`]. |
| 44 | +pub fn install_driver(verbose: bool) -> Result<(), ExitStatus> { |
| 45 | + // The toolchain, driver version and api version should ideally be configurable. |
| 46 | + // However, that will require more prototyping and has a low priority rn. |
| 47 | + // See #60 |
| 48 | + |
| 49 | + // Prerequisites |
| 50 | + let toolchain = &DEFAULT_DRIVER_INFO.toolchain; |
| 51 | + check_toolchain(toolchain)?; |
| 52 | + |
| 53 | + build_driver( |
| 54 | + toolchain, |
| 55 | + &DEFAULT_DRIVER_INFO.version, |
| 56 | + verbose, |
| 57 | + cfg!(feature = "dev-build"), |
| 58 | + )?; |
| 59 | + |
| 60 | + // We don't want to advice the user, to install the driver again. |
| 61 | + check_driver(verbose, false) |
| 62 | +} |
| 63 | + |
| 64 | +/// This function checks if the specified toolchain is installed. This requires |
| 65 | +/// rustup. A dependency we have to live with for now. |
| 66 | +fn check_toolchain(toolchain: &str) -> Result<(), ExitStatus> { |
| 67 | + let mut cmd = Command::new("cargo"); |
| 68 | + cmd.args([&format!("+{toolchain}"), "-V"]); |
| 69 | + if cmd.output().is_err() { |
| 70 | + eprintln!("Error: The required toolchain `{toolchain}` can't be found"); |
| 71 | + eprintln!(); |
| 72 | + eprintln!("You can install the toolchain by running: rustup toolchain install {toolchain}"); |
| 73 | + Err(ExitStatus::InvalidToolchain) |
| 74 | + } else { |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// This tries to compile the driver. If successful the driver binary will |
| 80 | +/// be places next to the executable of `cargo-linter`. |
| 81 | +fn build_driver(toolchain: &str, version: &str, verbose: bool, dev_build: bool) -> Result<(), ExitStatus> { |
| 82 | + if dev_build { |
| 83 | + println!("Compiling rustc driver"); |
| 84 | + } else { |
| 85 | + println!("Compiling rustc driver v{version} with {toolchain}"); |
| 86 | + } |
| 87 | + |
| 88 | + // Build driver |
| 89 | + let mut cmd = Command::new("cargo"); |
| 90 | + |
| 91 | + if !dev_build { |
| 92 | + cmd.arg(&format!("+{toolchain}")); |
| 93 | + } |
| 94 | + |
| 95 | + if verbose { |
| 96 | + cmd.arg("--verbose"); |
| 97 | + } |
| 98 | + |
| 99 | + if dev_build { |
| 100 | + cmd.args(["build", "--bin", "marker_driver_rustc"]); |
| 101 | + } else { |
| 102 | + // TODO Set output path to the local branch thingy |
| 103 | + cmd.args(["install", "marker_rustc_driver", "--version", version]); |
| 104 | + } |
| 105 | + |
| 106 | + let status = cmd |
| 107 | + .spawn() |
| 108 | + .expect("unable to start cargo install for the driver") |
| 109 | + .wait() |
| 110 | + .expect("unable to wait on cargo install for the driver"); |
| 111 | + if status.success() { |
| 112 | + Ok(()) |
| 113 | + } else { |
| 114 | + // The user can see cargo's output, as the command output was passed on |
| 115 | + // to the user via the `.spawn()` call. |
| 116 | + Err(ExitStatus::DriverInstallationFailed) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +fn check_driver(verbose: bool, print_advice: bool) -> Result<(), ExitStatus> { |
| 121 | + let path = get_driver_path(); |
| 122 | + if verbose { |
| 123 | + println!("Searching for driver at: {}", path.display()); |
| 124 | + } |
| 125 | + |
| 126 | + if !path.exists() || !path.is_file() { |
| 127 | + if print_advice { |
| 128 | + eprintln!("Error: The driver binary could not be found."); |
| 129 | + eprintln!(); |
| 130 | + eprintln!("Try installing it via `cargo marker setup`"); |
| 131 | + } |
| 132 | + |
| 133 | + Err(ExitStatus::MissingDriver) |
| 134 | + } else { |
| 135 | + Ok(()) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +pub fn run_driver( |
| 140 | + env: Vec<(OsString, OsString)>, |
| 141 | + cargo_args: impl Iterator<Item = String>, |
| 142 | + verbose: bool, |
| 143 | +) -> Result<(), ExitStatus> { |
| 144 | + check_driver(verbose, true)?; |
| 145 | + println!(); |
| 146 | + println!("Start linting:"); |
| 147 | + |
| 148 | + let mut cmd = Command::new("cargo"); |
| 149 | + cmd.envs(env).arg("check").args(cargo_args); |
| 150 | + if verbose { |
| 151 | + cmd.arg("--verbose"); |
| 152 | + } |
| 153 | + |
| 154 | + let exit_status = cmd |
| 155 | + .spawn() |
| 156 | + .expect("could not run cargo") |
| 157 | + .wait() |
| 158 | + .expect("failed to wait for cargo?"); |
| 159 | + |
| 160 | + if exit_status.success() { |
| 161 | + Ok(()) |
| 162 | + } else { |
| 163 | + Err(ExitStatus::MarkerCheckFailed) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +pub fn get_driver_path() -> PathBuf { |
| 168 | + #[allow(unused_mut)] |
| 169 | + let mut path = std::env::current_exe() |
| 170 | + .expect("unable to retrieve the path of the current executable") |
| 171 | + .with_file_name("marker_driver_rustc"); |
| 172 | + |
| 173 | + #[cfg(target_os = "windows")] |
| 174 | + path.set_extension("exe"); |
| 175 | + |
| 176 | + path |
| 177 | +} |
0 commit comments