Skip to content

Commit e60c90e

Browse files
authored
Add the git hash and commit/date to --version (#896)
Inspired by similar functionality on `wit-bindgen`
1 parent 21478d4 commit e60c90e

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

build.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::{path::Path, process::Command};
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=build.rs");
5+
6+
commit_info();
7+
}
8+
9+
fn commit_info() {
10+
if !Path::new(".git").exists() {
11+
return;
12+
}
13+
let output = match Command::new("git")
14+
.arg("log")
15+
.arg("-1")
16+
.arg("--date=short")
17+
.arg("--format=%H %h %cd")
18+
.arg("--abbrev=9")
19+
.output()
20+
{
21+
Ok(output) if output.status.success() => output,
22+
_ => return,
23+
};
24+
let stdout = String::from_utf8(output.stdout).unwrap();
25+
let mut parts = stdout.split_whitespace();
26+
let mut next = || parts.next().unwrap();
27+
println!("cargo:rustc-env=CARGO_GIT_HASH={}", next());
28+
println!(
29+
"cargo:rustc-env=CARGO_VERSION_INFO={} ({} {})",
30+
env!("CARGO_PKG_VERSION"),
31+
next(),
32+
next()
33+
);
34+
}

src/bin/wasm-tools/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ macro_rules! subcommands {
1414
)*
1515

1616
#[derive(Parser)]
17-
#[clap(version)]
17+
#[clap(version = version())]
1818
#[allow(non_camel_case_types)]
1919
enum WasmTools {
2020
$(
@@ -72,6 +72,11 @@ fn main() -> ExitCode {
7272
ExitCode::FAILURE
7373
}
7474

75+
/// If CARGO_VERSION_INFO is set, use it, otherwise use CARGO_PKG_VERSION.
76+
fn version() -> &'static str {
77+
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
78+
}
79+
7580
#[test]
7681
fn verify_cli() {
7782
use clap::CommandFactory;

0 commit comments

Comments
 (0)