Skip to content
Merged
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
4 changes: 4 additions & 0 deletions crates/cargo-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ serde_json.workspace = true
semver.workspace = true
dunce.workspace = true

[build-dependencies]
serde.workspace = true
serde_json.workspace = true

[dev-dependencies]
cargo-gpu-install = { workspace = true, features = ["test"] }
test-log.workspace = true
Expand Down
72 changes: 65 additions & 7 deletions crates/cargo-gpu/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
//! cargo-gpu build script.

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

fn main() {
let git_hash = std::process::Command::new("git")
.args(["rev-parse", "HEAD"])
compute_version_extra();
}

fn compute_version_extra() {
let extra = version_crates_io()
.or_else(version_git)
.unwrap_or(String::from(" installed from unknown source"));

std::fs::write(
PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("version_extra"),
extra,
)
.unwrap();
}

fn version_git() -> Option<String> {
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",
]);

if let Some(rev) = git_rev
&& let Some(git_date) = git_date
{
let rev = rev.get(..8).unwrap_or(&rev);
let git_date = git_date.strip_suffix("\n").unwrap_or(&git_date);
Some(format!(" installed from repo at rev {rev} ({git_date})"))
} else {
None
}
}

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())
}

/// `cargo package` adds the `.cargo_vcs_info.json` file on it's own, let's just use that to resolve the rev
fn version_crates_io() -> Option<String> {
let json = std::fs::read_to_string(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(".cargo_vcs_info.json"),
)
.ok()?;
let value = serde_json::from_str::<Value>(&json).ok()?;
let rev = value.get("git")?.get("sha1")?.as_str()?;
let rev = rev.get(..8).unwrap_or(rev);
Some(format!(" installed from crates.io (rev {rev})"))
}
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_extra")),
)
};
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
Loading