From f540bf72c7fe6152f0ac13e1ed8832c8d489b31f Mon Sep 17 00:00:00 2001 From: Lucas Franceschino Date: Wed, 16 Oct 2024 13:20:34 +0200 Subject: [PATCH] feat(cli): better version information Versions were broken: `cargo hax -V` was displaying the latest tag attached to the commit. So newer commits on main were tagged `v0.1.0-alpha`. Also, if hax is built without git, the version was empty. Now we use `CARGO_PKG_VERSION` as a fallback. --- hax-types/build.rs | 57 +++++++++++++++++++++----------- hax-types/src/cli_options/mod.rs | 9 ++++- hax-types/src/lib.rs | 3 ++ 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/hax-types/build.rs b/hax-types/build.rs index b4ab527a0..bda3c1c49 100644 --- a/hax-types/build.rs +++ b/hax-types/build.rs @@ -1,24 +1,43 @@ -macro_rules! set_empty_env_var_with_git { - ($var:literal, $args: expr) => { - if let None = option_env!($var) { - println!( - "cargo:rustc-env={}={}", - $var, - std::process::Command::new("git") - .args($args) - .output() - .map(|output| String::from_utf8(output.stdout).unwrap()) - .unwrap_or("unknown".into()) - ); - } +macro_rules! set_empty_env_var_with { + ($var:literal, $f: expr) => {{ println!("cargo:rurun-if-env-changed={}", $var); - }; + match option_env!($var) { + Some(value) => value.to_string(), + None => { + let value = $f; + println!("cargo:rustc-env={}={}", $var, value); + value + } + } + }}; +} + +const UNKNOWN: &str = "unknown"; + +fn git_command(args: &[&str]) -> String { + std::process::Command::new("git") + .args(args) + .output() + .map(|output| String::from_utf8(output.stdout).unwrap().trim().to_string()) + .ok() + .filter(|s| !s.is_empty()) + .unwrap_or(UNKNOWN.to_string()) } fn main() { - set_empty_env_var_with_git!( - "HAX_GIT_DESCRIBE", - ["describe", "--tags", "--always", "--abbrev=0"] - ); - set_empty_env_var_with_git!("HAX_GIT_COMMIT_HASH", ["rev-parse", "HEAD"]); + let commit_hash = + set_empty_env_var_with!("HAX_GIT_COMMIT_HASH", git_command(&["rev-parse", "HEAD"])); + + set_empty_env_var_with!("HAX_VERSION", { + if commit_hash == UNKNOWN { + env!("CARGO_PKG_VERSION").into() + } else { + git_command(&["tag", "--contains", &commit_hash]) + .lines() + .next() + .and_then(|tag| tag.split_once("hax-v")) + .map(|(_, version)| version.trim().to_string()) + .unwrap_or_else(|| format!("untagged-git-rev-{}", &commit_hash[0..10])) + } + }); } diff --git a/hax-types/src/cli_options/mod.rs b/hax-types/src/cli_options/mod.rs index adecc485f..75b771966 100644 --- a/hax-types/src/cli_options/mod.rs +++ b/hax-types/src/cli_options/mod.rs @@ -405,7 +405,14 @@ pub enum ExportBodyKind { #[derive_group(Serializers)] #[derive(JsonSchema, Parser, Debug, Clone)] -#[command(author, version = concat!("commit=", env!("HAX_GIT_COMMIT_HASH"), " ", "describe=", env!("HAX_GIT_DESCRIBE")), name = "hax", about, long_about = None)] +#[command( + author, + version = crate::HAX_VERSION, + long_version = concat!("\nversion=", env!("HAX_VERSION"), "\n", "commit=", env!("HAX_GIT_COMMIT_HASH")), + name = "hax", + about, + long_about = None +)] pub struct ExtensibleOptions { /// Replace the expansion of each macro matching PATTERN by their /// invocation. PATTERN denotes a rust path (i.e. `A::B::c`) in diff --git a/hax-types/src/lib.rs b/hax-types/src/lib.rs index ab974d41b..b59b7b0d0 100644 --- a/hax-types/src/lib.rs +++ b/hax-types/src/lib.rs @@ -25,3 +25,6 @@ pub mod driver_api; /// The types used to communicate between `cargo-hax` and /// `hax-engine`. pub mod engine_api; + +/// Compile-time version of hax +pub const HAX_VERSION: &str = env!("HAX_VERSION");