2024-09-01 05:35:04 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
2024-08-04 13:09:18 -05:00
|
|
|
use std::str;
|
|
|
|
|
2022-11-21 15:55:14 -06:00
|
|
|
/// This macro creates the version string during compilation from the
|
|
|
|
/// current environment
|
2018-09-06 01:19:47 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! get_version_info {
|
|
|
|
() => {{
|
2022-11-21 15:55:14 -06:00
|
|
|
let major = std::env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
|
|
|
|
let minor = std::env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
|
|
|
|
let patch = std::env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();
|
|
|
|
let crate_name = String::from(std::env!("CARGO_PKG_NAME"));
|
2018-09-06 01:19:47 -05:00
|
|
|
|
2022-11-21 15:55:14 -06:00
|
|
|
let host_compiler = std::option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string);
|
|
|
|
let commit_hash = std::option_env!("GIT_HASH").map(str::to_string);
|
|
|
|
let commit_date = std::option_env!("COMMIT_DATE").map(str::to_string);
|
2018-09-06 01:19:47 -05:00
|
|
|
|
2022-11-21 15:55:14 -06:00
|
|
|
$crate::VersionInfo {
|
2018-09-06 01:19:47 -05:00
|
|
|
major,
|
|
|
|
minor,
|
|
|
|
patch,
|
|
|
|
host_compiler,
|
|
|
|
commit_hash,
|
|
|
|
commit_date,
|
2018-09-07 11:03:03 -05:00
|
|
|
crate_name,
|
2018-09-06 01:19:47 -05:00
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-11-21 15:55:14 -06:00
|
|
|
/// This macro can be used in `build.rs` to automatically set the needed
|
|
|
|
/// environment values, namely `GIT_HASH`, `COMMIT_DATE` and
|
|
|
|
/// `RUSTC_RELEASE_CHANNEL`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! setup_version_info {
|
|
|
|
() => {{
|
2024-09-01 05:35:04 -05:00
|
|
|
let _ = $crate::rerun_if_git_changes();
|
2022-11-21 15:55:14 -06:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=GIT_HASH={}",
|
|
|
|
$crate::get_commit_hash().unwrap_or_default()
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=COMMIT_DATE={}",
|
|
|
|
$crate::get_commit_date().unwrap_or_default()
|
|
|
|
);
|
|
|
|
println!("cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", $crate::get_channel());
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-09-06 01:19:47 -05:00
|
|
|
// 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<String>,
|
|
|
|
pub commit_hash: Option<String>,
|
|
|
|
pub commit_date: Option<String>,
|
2018-09-07 11:03:03 -05:00
|
|
|
pub crate_name: String,
|
2018-09-06 01:19:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for VersionInfo {
|
2018-09-13 12:06:04 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2019-01-02 13:12:15 -06:00
|
|
|
let hash = self.commit_hash.clone().unwrap_or_default();
|
|
|
|
let hash_trimmed = hash.trim();
|
|
|
|
|
|
|
|
let date = self.commit_date.clone().unwrap_or_default();
|
|
|
|
let date_trimmed = date.trim();
|
|
|
|
|
|
|
|
if (hash_trimmed.len() + date_trimmed.len()) > 0 {
|
2018-09-13 12:06:04 -05:00
|
|
|
write!(
|
|
|
|
f,
|
2022-09-23 13:25:03 -05:00
|
|
|
"{} {}.{}.{} ({hash_trimmed} {date_trimmed})",
|
|
|
|
self.crate_name, self.major, self.minor, self.patch,
|
2018-09-13 12:06:04 -05:00
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "{} {}.{}.{}", self.crate_name, self.major, self.minor, self.patch)?;
|
|
|
|
}
|
|
|
|
|
2018-09-06 01:19:47 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 16:40:26 -05:00
|
|
|
impl std::fmt::Debug for VersionInfo {
|
2018-09-13 12:06:04 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2018-09-09 16:40:26 -05:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"VersionInfo {{ crate_name: \"{}\", major: {}, minor: {}, patch: {}",
|
|
|
|
self.crate_name, self.major, self.minor, self.patch,
|
|
|
|
)?;
|
2018-09-13 12:06:04 -05:00
|
|
|
if self.commit_hash.is_some() {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
", commit_hash: \"{}\", commit_date: \"{}\" }}",
|
|
|
|
self.commit_hash.clone().unwrap_or_default().trim(),
|
|
|
|
self.commit_date.clone().unwrap_or_default().trim()
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
write!(f, " }}")?;
|
2018-09-09 16:40:26 -05:00
|
|
|
}
|
2018-09-13 12:06:04 -05:00
|
|
|
|
2018-09-09 16:40:26 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2024-09-01 05:35:04 -05:00
|
|
|
fn get_output(cmd: &str, args: &[&str]) -> Option<String> {
|
|
|
|
let output = Command::new(cmd).args(args).output().ok()?;
|
2024-08-05 10:17:18 -05:00
|
|
|
let mut stdout = output.status.success().then_some(output.stdout)?;
|
2024-09-01 05:35:04 -05:00
|
|
|
// Remove trailing newlines.
|
|
|
|
while stdout.last().copied() == Some(b'\n') {
|
|
|
|
stdout.pop();
|
|
|
|
}
|
2024-08-04 13:09:18 -05:00
|
|
|
String::from_utf8(stdout).ok()
|
2018-09-06 01:19:47 -05:00
|
|
|
}
|
|
|
|
|
2024-09-01 05:35:04 -05:00
|
|
|
#[must_use]
|
|
|
|
pub fn rerun_if_git_changes() -> Option<()> {
|
|
|
|
// Make sure we get rerun when the git commit changes.
|
|
|
|
// We want to watch two files: HEAD, which tracks which branch we are on,
|
|
|
|
// and the file for that branch that tracks which commit is is on.
|
|
|
|
|
|
|
|
// First, find the `HEAD` file. This should work even with worktrees.
|
|
|
|
let git_head_file = PathBuf::from(get_output("git", &["rev-parse", "--git-path", "HEAD"])?);
|
|
|
|
if git_head_file.exists() {
|
|
|
|
println!("cargo::rerun-if-changed={}", git_head_file.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the name of the current ref.
|
|
|
|
// This will quit if HEAD is detached.
|
|
|
|
let git_head_ref = get_output("git", &["symbolic-ref", "-q", "HEAD"])?;
|
|
|
|
// Ask git where this ref is stored.
|
|
|
|
let git_head_ref_file = PathBuf::from(get_output("git", &["rev-parse", "--git-path", &git_head_ref])?);
|
|
|
|
// If this ref is packed, the file does not exist. However, the checked-out branch is never (?)
|
|
|
|
// packed, so we should always be able to find this file.
|
|
|
|
if git_head_ref_file.exists() {
|
|
|
|
println!("cargo::rerun-if-changed={}", git_head_ref_file.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn get_commit_hash() -> Option<String> {
|
|
|
|
let mut stdout = get_output("git", &["rev-parse", "HEAD"])?;
|
|
|
|
stdout.truncate(10);
|
|
|
|
Some(stdout)
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2018-09-06 01:19:47 -05:00
|
|
|
pub fn get_commit_date() -> Option<String> {
|
2024-09-01 05:35:04 -05:00
|
|
|
get_output("git", &["log", "-1", "--date=short", "--pretty=format:%cd"])
|
2018-09-06 01:19:47 -05:00
|
|
|
}
|
2018-09-07 12:06:02 -05:00
|
|
|
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2021-05-06 20:22:10 -05:00
|
|
|
pub fn get_channel() -> String {
|
2024-08-04 13:09:18 -05:00
|
|
|
if let Ok(channel) = std::env::var("CFG_RELEASE_CHANNEL") {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if that failed, try to ask rustc -V, do some parsing and find out
|
2024-09-01 05:35:04 -05:00
|
|
|
if let Some(rustc_output) = get_output("rustc", &["-V"]) {
|
|
|
|
if rustc_output.contains("beta") {
|
|
|
|
return String::from("beta");
|
|
|
|
} else if rustc_output.contains("stable") {
|
|
|
|
return String::from("stable");
|
2024-08-04 13:09:18 -05:00
|
|
|
}
|
2019-04-28 13:07:49 -05:00
|
|
|
}
|
2024-08-04 13:09:18 -05:00
|
|
|
|
|
|
|
// default to nightly
|
|
|
|
String::from("nightly")
|
2019-04-28 13:07:49 -05:00
|
|
|
}
|
|
|
|
|
2018-09-07 12:06:02 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct_local() {
|
|
|
|
let vi = get_version_info!();
|
|
|
|
assert_eq!(vi.major, 0);
|
2022-12-15 05:42:08 -06:00
|
|
|
assert_eq!(vi.minor, 3);
|
|
|
|
assert_eq!(vi.patch, 0);
|
2018-09-07 12:06:02 -05:00
|
|
|
assert_eq!(vi.crate_name, "rustc_tools_util");
|
|
|
|
// hard to make positive tests for these since they will always change
|
|
|
|
assert!(vi.commit_hash.is_none());
|
|
|
|
assert!(vi.commit_date.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display_local() {
|
|
|
|
let vi = get_version_info!();
|
2022-12-15 05:42:08 -06:00
|
|
|
assert_eq!(vi.to_string(), "rustc_tools_util 0.3.0");
|
2018-09-09 16:40:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_debug_local() {
|
|
|
|
let vi = get_version_info!();
|
2022-09-23 13:25:03 -05:00
|
|
|
let s = format!("{vi:?}");
|
2018-09-09 16:40:26 -05:00
|
|
|
assert_eq!(
|
|
|
|
s,
|
2022-12-15 05:42:08 -06:00
|
|
|
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 3, patch: 0 }"
|
2018-09-09 16:40:26 -05:00
|
|
|
);
|
2018-09-07 12:06:02 -05:00
|
|
|
}
|
|
|
|
}
|