Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions crates/cargo-gpu/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
//! 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");
println!("cargo:rerun-if-changed={git_directory}/packed-refs");
if let Ok(git_head) = std::fs::read_to_string(format!("{git_directory}/HEAD")) && let Some(git_ref) = git_head.strip_prefix("ref: ") {
println!(
"cargo:rerun-if-changed={git_directory}/{}",
git_ref.trim()
);
}
Comment on lines +7 to +14

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure these rerun-if-changed are actually needed?
If installed from git repository, the revision will have to change, and the whole crate will be recompiled anyway.

I'm not sure what is the situation where these are actually necessary.

@Steveplays28 Steveplays28 Jun 29, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from what I read on StackOverflow it is but I haven't tested without those rerun-if-changed lines 😅

@Steveplays28 Steveplays28 Jun 29, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it, if I make a commit without those rerun-if-changed lines, then re-compile with e.g. cargo run -p cargo-gpu -- --version, COMMIT_HASH doesn't update
so you could get a situation where you think the program is compiled from an old commit when it isn't

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already know what version you have locally, so I don't think it matters in that case. And if it is not local, then it is installed from git repo and will be treated as a separate crate anyway.


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<I, S>(args: I) -> String
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
std::process::Command::new("git")
.args(args)
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.unwrap_or_else(|| "unknown".to_owned())
}
2 changes: 1 addition & 1 deletion crates/cargo-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down