From 44e63a3d6c021e587b1d6b29cf40e57dac7df187 Mon Sep 17 00:00:00 2001 From: Steveplays Date: Thu, 9 Jul 2026 17:04:33 +0200 Subject: [PATCH 1/2] Add Git information to cargo-gpu's `--version` CLI subcommand Added the commit hash and date to the `--version` CLI subcommand. --- crates/cargo-gpu/build.rs | 35 ++++++++++++++++++++++++++++------- crates/cargo-gpu/src/lib.rs | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/crates/cargo-gpu/build.rs b/crates/cargo-gpu/build.rs index 31a33575ce8..0ae65519402 100644 --- a/crates/cargo-gpu/build.rs +++ b/crates/cargo-gpu/build.rs @@ -1,12 +1,33 @@ //! cargo-gpu build script. +use std::ffi::OsStr; + fn main() { - let git_hash = std::process::Command::new("git") - .args(["rev-parse", "HEAD"]) - .output() - .map_or_else( - |_| "unknown".to_owned(), - |output| String::from_utf8(output.stdout).unwrap_or_else(|_| "unknown".to_owned()), - ); + let git_directory = invoke_git(["rev-parse", "--git-dir"]); + println!("cargo:rerun-if-changed={git_directory}/HEAD"); + + let git_hash = invoke_git(["rev-parse", "HEAD"]); println!("cargo:rustc-env=GIT_HASH={git_hash}"); + + let git_date = invoke_git([ + "show", + "-s", + "--format=%cd", + "--date=format:%Y-%m-%d", + "HEAD", + ]); + println!("cargo:rustc-env=GIT_DATE={git_date}"); +} + +fn invoke_git(args: I) -> String +where + I: IntoIterator, + S: AsRef, +{ + std::process::Command::new("git") + .args(args) + .output() + .ok() + .and_then(|output| String::from_utf8(output.stdout).ok()) + .unwrap_or_else(|| "unknown".to_owned()) } diff --git a/crates/cargo-gpu/src/lib.rs b/crates/cargo-gpu/src/lib.rs index eadde1c8d82..a8759c04154 100644 --- a/crates/cargo-gpu/src/lib.rs +++ b/crates/cargo-gpu/src/lib.rs @@ -153,7 +153,7 @@ impl Command { /// the Cli struct representing the main cli #[derive(clap::Parser)] -#[clap(author, version, about, subcommand_required = true)] +#[clap(author, version, long_version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_HASH"), "; ", env!("GIT_DATE"), ")"), about, subcommand_required = true)] #[non_exhaustive] pub struct Cli { /// The command to run. From dc947ffd787974858d0587c6c235987885e0e77e Mon Sep 17 00:00:00 2001 From: firestar99 Date: Mon, 13 Jul 2026 15:41:45 +0200 Subject: [PATCH 2/2] version shows when cargo-gpu is installed from crates.io --- crates/cargo-gpu/build.rs | 29 +++++++++++++++++++++-------- crates/cargo-gpu/src/lib.rs | 9 ++++++++- crates/cargo-gpu/src/show.rs | 5 ----- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/crates/cargo-gpu/build.rs b/crates/cargo-gpu/build.rs index 0ae65519402..a5d92604c8a 100644 --- a/crates/cargo-gpu/build.rs +++ b/crates/cargo-gpu/build.rs @@ -1,14 +1,14 @@ //! cargo-gpu build script. use std::ffi::OsStr; +use std::path::PathBuf; fn main() { - let git_directory = invoke_git(["rev-parse", "--git-dir"]); - println!("cargo:rerun-if-changed={git_directory}/HEAD"); - - let git_hash = invoke_git(["rev-parse", "HEAD"]); - println!("cargo:rustc-env=GIT_HASH={git_hash}"); + if let Some(git_directory) = invoke_git(["rev-parse", "--git-dir"]) { + println!("cargo:rerun-if-changed={git_directory}/HEAD"); + } + let git_rev = invoke_git(["rev-parse", "HEAD"]); let git_date = invoke_git([ "show", "-s", @@ -16,10 +16,24 @@ fn main() { "--date=format:%Y-%m-%d", "HEAD", ]); - println!("cargo:rustc-env=GIT_DATE={git_date}"); + + let postfix = if let Some(git_rev) = git_rev + && let Some(git_date) = git_date + { + let git_rev = git_rev.get(..8).unwrap_or(&git_rev); + let git_date = git_date.strip_suffix("\n").unwrap_or(&git_date); + format!(" ({git_rev} {git_date})") + } else { + " (installed from crates.io)".into() + }; + std::fs::write( + PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("version_postfix"), + postfix, + ) + .unwrap(); } -fn invoke_git(args: I) -> String +fn invoke_git(args: I) -> Option where I: IntoIterator, S: AsRef, @@ -29,5 +43,4 @@ where .output() .ok() .and_then(|output| String::from_utf8(output.stdout).ok()) - .unwrap_or_else(|| "unknown".to_owned()) } diff --git a/crates/cargo-gpu/src/lib.rs b/crates/cargo-gpu/src/lib.rs index a8759c04154..c00397ef389 100644 --- a/crates/cargo-gpu/src/lib.rs +++ b/crates/cargo-gpu/src/lib.rs @@ -153,10 +153,17 @@ impl Command { /// the Cli struct representing the main cli #[derive(clap::Parser)] -#[clap(author, version, long_version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("GIT_HASH"), "; ", env!("GIT_DATE"), ")"), about, subcommand_required = true)] +#[clap(author, version = VERSION, about, subcommand_required = true)] #[non_exhaustive] pub struct Cli { /// The command to run. #[clap(subcommand)] pub command: Command, } + +const VERSION: &str = { + concat!( + env!("CARGO_PKG_VERSION"), + include_str!(concat!(env!("OUT_DIR"), "/version_postfix")), + ) +}; diff --git a/crates/cargo-gpu/src/show.rs b/crates/cargo-gpu/src/show.rs index 9e75aa84e81..41188bc0a4f 100644 --- a/crates/cargo-gpu/src/show.rs +++ b/crates/cargo-gpu/src/show.rs @@ -20,8 +20,6 @@ pub enum Info { CacheDirectory, /// The source location of spirv-std SpirvSource(SpirvSourceDep), - /// The git commitsh of this cli tool. - Commitsh, /// All the available SPIR-V capabilities that can be set with `--capabilities` Capabilities, } @@ -53,9 +51,6 @@ impl Show { let rust_gpu_source = SpirvSource::get_rust_gpu_deps_from_shader(&metadata)?; println!("{rust_gpu_source}\n"); } - Info::Commitsh => { - println!("{}", env!("GIT_HASH")); - } Info::Capabilities => { println!("All available options to the `cargo gpu build --capabilities` argument:"); #[expect(