|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Subcommand: cargo xtask check-features |
| 6 | +
|
| 7 | +use anyhow::{bail, Result}; |
| 8 | +use camino::Utf8PathBuf; |
| 9 | +use clap::Parser; |
| 10 | +use std::{collections::HashSet, process::Command}; |
| 11 | + |
| 12 | +const SUPPORTED_ARCHITECTURES: [&str; 1] = ["x86_64"]; |
| 13 | +const CI_EXCLUDED_FEATURES: [&str; 2] = ["image-trampoline", "image-standard"]; |
| 14 | + |
| 15 | +#[derive(Parser)] |
| 16 | +pub struct Args { |
| 17 | + /// Run in CI mode, with a default set of features excluded. |
| 18 | + #[clap(long, default_value_t = false)] |
| 19 | + ci: bool, |
| 20 | + /// Features to exclude from the check. |
| 21 | + #[clap(long, value_name = "FEATURES")] |
| 22 | + exclude_features: Option<Vec<String>>, |
| 23 | + /// Depth of the feature powerset to check. |
| 24 | + #[clap(long, value_name = "NUM")] |
| 25 | + depth: Option<usize>, |
| 26 | + /// Error format passed to `cargo hack check`. |
| 27 | + #[clap(long, value_name = "FMT")] |
| 28 | + message_format: Option<String>, |
| 29 | + /// Version of `cargo-hack` to install. By default, we download a pre-built |
| 30 | + /// version. |
| 31 | + #[clap(long, value_name = "VERSION")] |
| 32 | + install_version: Option<String>, |
| 33 | +} |
| 34 | + |
| 35 | +/// Run `cargo hack check`. |
| 36 | +pub fn run_cmd(args: Args) -> Result<()> { |
| 37 | + // We cannot specify both `--ci` and `--install-version`, as the former |
| 38 | + // implies we are using a pre-built version. |
| 39 | + if args.ci && args.install_version.is_some() { |
| 40 | + bail!("cannot specify --ci and --install-version together"); |
| 41 | + } |
| 42 | + |
| 43 | + let cargo = |
| 44 | + std::env::var("CARGO").unwrap_or_else(|_| String::from("cargo")); |
| 45 | + |
| 46 | + let mut command = Command::new(&cargo); |
| 47 | + |
| 48 | + // Add the `hack check` subcommand. |
| 49 | + command.args(&["hack", "check"]); |
| 50 | + |
| 51 | + if args.ci { |
| 52 | + install_prebuilt_cargo_hack(&cargo)?; |
| 53 | + |
| 54 | + let ex = if let Some(mut features) = args.exclude_features { |
| 55 | + // Extend the list of features to exclude with the CI defaults. |
| 56 | + features.extend( |
| 57 | + CI_EXCLUDED_FEATURES.into_iter().map(|s| s.to_string()), |
| 58 | + ); |
| 59 | + |
| 60 | + // Remove duplicates. |
| 61 | + let excludes = features.into_iter().collect::<HashSet<_>>(); |
| 62 | + |
| 63 | + excludes.into_iter().collect::<Vec<_>>().join(",") |
| 64 | + } else { |
| 65 | + CI_EXCLUDED_FEATURES.join(",") |
| 66 | + }; |
| 67 | + |
| 68 | + // Add the `--exclude-features` flag if we are running in CI mode. |
| 69 | + command.args(["--exclude-features", &ex]); |
| 70 | + } else { |
| 71 | + install_cargo_hack(&cargo, args.install_version)?; |
| 72 | + // Add "only" the `--exclude-features` flag if it was provided. |
| 73 | + if let Some(features) = args.exclude_features { |
| 74 | + command.args(["--exclude-features", &features.join(",")]); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if let Some(depth) = args.depth { |
| 79 | + command.args(&["--depth", &depth.to_string()]); |
| 80 | + } |
| 81 | + |
| 82 | + // Pass along the `--message-format` flag if it was provided. |
| 83 | + if let Some(fmt) = args.message_format { |
| 84 | + command.args(["--message-format", &fmt]); |
| 85 | + } |
| 86 | + |
| 87 | + command |
| 88 | + // Make sure we check everything. |
| 89 | + .arg("--workspace") |
| 90 | + // We want to check the binaries. |
| 91 | + .arg("--bins") |
| 92 | + // We want to check the feature powerset. |
| 93 | + .arg("--feature-powerset") |
| 94 | + // We will not check the dev-dependencies, which should covered by tests. |
| 95 | + .arg("--no-dev-deps"); |
| 96 | + |
| 97 | + exec(command) |
| 98 | +} |
| 99 | + |
| 100 | +/// The supported operating systems. |
| 101 | +enum Os { |
| 102 | + Illumos, |
| 103 | + Linux, |
| 104 | + Mac, |
| 105 | +} |
| 106 | + |
| 107 | +/// Get the current OS. |
| 108 | +fn os_name() -> Result<Os> { |
| 109 | + let os = match std::env::consts::OS { |
| 110 | + "linux" => Os::Linux, |
| 111 | + "macos" => Os::Mac, |
| 112 | + "solaris" | "illumos" => Os::Illumos, |
| 113 | + other => bail!("OS not supported: {other}"), |
| 114 | + }; |
| 115 | + Ok(os) |
| 116 | +} |
| 117 | + |
| 118 | +/// This is a workaround for the lack of a CARGO_WORKSPACE_DIR environment |
| 119 | +/// variable, as suggested in <https://github.com/rust-lang/cargo/issues/3946#issuecomment-1433384192>. |
| 120 | +/// A better workaround might be to set this in the `[env]` section of |
| 121 | +/// `.cargo/config.toml`. |
| 122 | +fn project_root() -> Utf8PathBuf { |
| 123 | + Utf8PathBuf::from(&concat!(env!("CARGO_MANIFEST_DIR"), "/..")) |
| 124 | +} |
| 125 | + |
| 126 | +/// Get the path to the `out` directory from the project root/workspace |
| 127 | +/// directory. |
| 128 | +fn out_dir() -> Utf8PathBuf { |
| 129 | + project_root().join("out/cargo-hack") |
| 130 | +} |
| 131 | + |
| 132 | +/// Install `cargo-hack` if the `install-version` was specified; otherwise, |
| 133 | +/// download a pre-built version if it's not already in our `out` directory. |
| 134 | +fn install_cargo_hack(cargo: &str, version: Option<String>) -> Result<()> { |
| 135 | + if let Some(version) = version { |
| 136 | + let mut command = Command::new(cargo); |
| 137 | + |
| 138 | + eprintln!( |
| 139 | + "installing cargo-hack at version {} to {}", |
| 140 | + version, |
| 141 | + env!("CARGO_HOME") |
| 142 | + ); |
| 143 | + command.args(&["install", "cargo-hack", "--version", &version]); |
| 144 | + exec(command) |
| 145 | + } else if !out_dir().exists() { |
| 146 | + install_prebuilt_cargo_hack(cargo) |
| 147 | + } else { |
| 148 | + let out_dir = out_dir(); |
| 149 | + eprintln!("cargo-hack found in {}", out_dir); |
| 150 | + Ok(()) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/// Download a pre-built version of `cargo-hack` to the `out` directory via the |
| 155 | +/// download `xtask`. |
| 156 | +fn install_prebuilt_cargo_hack(cargo: &str) -> Result<()> { |
| 157 | + let mut command = Command::new(cargo); |
| 158 | + |
| 159 | + let out_dir = out_dir(); |
| 160 | + eprintln!( |
| 161 | + "cargo-hack not found in {}, downloading a pre-built version", |
| 162 | + out_dir |
| 163 | + ); |
| 164 | + |
| 165 | + let os = os_name()?; |
| 166 | + match os { |
| 167 | + Os::Illumos | Os::Linux | Os::Mac |
| 168 | + if SUPPORTED_ARCHITECTURES.contains(&std::env::consts::ARCH) => |
| 169 | + { |
| 170 | + // Download the pre-built version of `cargo-hack` via our |
| 171 | + // download `xtask`. |
| 172 | + command.args(&["xtask", "download", "cargo-hack"]); |
| 173 | + } |
| 174 | + _ => { |
| 175 | + bail!( |
| 176 | + "cargo-hack is not pre-built for this os {} / arch {}", |
| 177 | + std::env::consts::OS, |
| 178 | + std::env::consts::ARCH |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + exec(command) |
| 184 | +} |
| 185 | + |
| 186 | +/// Execute the command and check the exit status. |
| 187 | +fn exec(mut command: Command) -> Result<()> { |
| 188 | + let cargo = |
| 189 | + std::env::var("CARGO").unwrap_or_else(|_| String::from("cargo")); |
| 190 | + |
| 191 | + eprintln!( |
| 192 | + "running: {:?} {}", |
| 193 | + &cargo, |
| 194 | + command |
| 195 | + .get_args() |
| 196 | + .map(|arg| format!("{:?}", arg.to_str().unwrap())) |
| 197 | + .collect::<Vec<_>>() |
| 198 | + .join(" ") |
| 199 | + ); |
| 200 | + |
| 201 | + let exit_status = command |
| 202 | + .spawn() |
| 203 | + .expect("failed to spawn child process") |
| 204 | + .wait() |
| 205 | + .expect("failed to wait for child process"); |
| 206 | + |
| 207 | + if !exit_status.success() { |
| 208 | + bail!("cargo-hack install failed: {}", exit_status); |
| 209 | + } |
| 210 | + |
| 211 | + Ok(()) |
| 212 | +} |
0 commit comments