diff --git a/.gitignore b/.gitignore index 5fd7f2fc1a0..166cab60a58 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ Cargo.lock /clippy_lints/target /clippy_workspace_tests/target /clippy_dev/target +/rustc_tools_util/target # Generated by dogfood /target_recur/ diff --git a/Cargo.toml b/Cargo.toml index fb43e0100bd..b293f06713d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ clippy_lints = { version = "0.0.212", path = "clippy_lints" } # end automatic update regex = "1" semver = "0.9" +rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"} [dev-dependencies] clippy_dev = { version = "0.0.1", path = "clippy_dev" } @@ -60,5 +61,8 @@ derive-new = "0.5" # for more information. rustc-workspace-hack = "1.0.0" +[build-dependencies] +rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"} + [features] debugging = [] diff --git a/build.rs b/build.rs index 1c930c1b2c9..146a8dae745 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,15 @@ -use std::env; - fn main() { // Forward the profile to the main compilation - println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap()); + println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap()); // Don't rebuild even if nothing changed println!("cargo:rerun-if-changed=build.rs"); + // forward git repo hashes we build at + println!( + "cargo:rustc-env=GIT_HASH={}", + rustc_tools_util::get_commit_hash().unwrap_or_default() + ); + println!( + "cargo:rustc-env=COMMIT_DATE={}", + rustc_tools_util::get_commit_date().unwrap_or_default() + ); } diff --git a/ci/base-tests.sh b/ci/base-tests.sh index b85ed4fab66..94a810e4ef4 100755 --- a/ci/base-tests.sh +++ b/ci/base-tests.sh @@ -20,6 +20,8 @@ cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../.. cd clippy_workspace_tests/subcrate/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../../.. cd clippy_dev && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd .. +cd rustc_tools_util/ && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd .. + # test --manifest-path PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=clippy_workspace_tests/Cargo.toml -- -D clippy::all cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=../Cargo.toml -- -D clippy::all && cd ../.. diff --git a/rustc_tools_util/Cargo.toml b/rustc_tools_util/Cargo.toml new file mode 100644 index 00000000000..01dca0a65b0 --- /dev/null +++ b/rustc_tools_util/Cargo.toml @@ -0,0 +1,8 @@ +cargo-features = ["edition"] + +[package] +name = "rustc_tools_util" +version = "0.1.0" +authors = ["Matthias Krüger "] +edition = "2018" +[dependencies] diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs new file mode 100644 index 00000000000..20b598346f1 --- /dev/null +++ b/rustc_tools_util/src/lib.rs @@ -0,0 +1,82 @@ +#![feature(tool_lints)] + +use std::env; + +#[macro_export] +macro_rules! get_version_info { + () => {{ + let major = env!("CARGO_PKG_VERSION_MAJOR").parse::().unwrap(); + let minor = env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap(); + let patch = env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(); + + let host_compiler = $crate::get_channel(); + let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string()); + let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string()); + + VersionInfo { + major, + minor, + patch, + host_compiler, + commit_hash, + commit_date, + } + }}; +} + +// some code taken and adapted from RLS and cargo +pub struct VersionInfo { + pub major: u8, + pub minor: u8, + pub patch: u16, + pub host_compiler: Option, + pub commit_hash: Option, + pub commit_date: Option, +} + +impl std::fmt::Display for VersionInfo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self.commit_hash { + Some(_) => { + write!( + f, + "clippy {}.{}.{} ({} {})", + self.major, + self.minor, + self.patch, + self.commit_hash.clone().unwrap_or_default().trim(), + self.commit_date.clone().unwrap_or_default().trim(), + )?; + }, + None => { + write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?; + }, + }; + Ok(()) + } +} + +pub fn get_channel() -> Option { + if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") { + Some(channel) + } else { + // we could ask ${RUSTC} -Vv and do some parsing and find out + Some(String::from("nightly")) + } +} + +pub fn get_commit_hash() -> Option { + std::process::Command::new("git") + .args(&["rev-parse", "--short", "HEAD"]) + .output() + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) +} + +pub fn get_commit_date() -> Option { + std::process::Command::new("git") + .args(&["log", "-1", "--date=short", "--pretty=format:%cd"]) + .output() + .ok() + .and_then(|r| String::from_utf8(r.stdout).ok()) +} diff --git a/src/main.rs b/src/main.rs index 12c07f60a11..cf26549774c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ #![feature(tool_lints)] #![allow(unknown_lints, clippy::missing_docs_in_private_items)] +use rustc_tools_util::*; + const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code. Usage: @@ -36,7 +38,8 @@ fn show_help() { #[allow(clippy::print_stdout)] fn show_version() { - println!(env!("CARGO_PKG_VERSION")); + let version_info = rustc_tools_util::get_version_info!(); + println!("{}", version_info); } pub fn main() { @@ -45,6 +48,7 @@ pub fn main() { show_help(); return; } + if std::env::args().any(|a| a == "--version" || a == "-V") { show_version(); return; @@ -94,8 +98,7 @@ where .into_os_string() }, ) - }) - .map(|p| ("CARGO_TARGET_DIR", p)); + }).map(|p| ("CARGO_TARGET_DIR", p)); let exit_status = std::process::Command::new("cargo") .args(&args)