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
48 changes: 41 additions & 7 deletions crates/cargo-gpu/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
//! cargo-gpu build script.

use std::ffi::OsStr;
use std::path::PathBuf;

fn main() {
let git_hash = std::process::Command::new("git")
.args(["rev-parse", "HEAD"])
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",
"--format=%cd",
"--date=format:%Y-%m-%d",
"HEAD",
]);

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<I, S>(args: I) -> Option<String>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
std::process::Command::new("git")
.args(args)
.output()
.map_or_else(
|_| "unknown".to_owned(),
|output| String::from_utf8(output.stdout).unwrap_or_else(|_| "unknown".to_owned()),
);
println!("cargo:rustc-env=GIT_HASH={git_hash}");
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
}
9 changes: 8 additions & 1 deletion crates/cargo-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,17 @@ impl Command {

/// the Cli struct representing the main cli
#[derive(clap::Parser)]
#[clap(author, version, 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")),
)
};
5 changes: 0 additions & 5 deletions crates/cargo-gpu/src/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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(
Expand Down