Skip to content

Commit

Permalink
Add extended version information to wasmtime --version
Browse files Browse the repository at this point in the history
This commit adds some more information to `wasmtime --version` which
includes the git commit plus the git commit's date. This matches `rustc
-V` for example which was additionally copied to `wasm-tools` and
mirrored as `wasm-tools -V`.

Personally I've found this useful since it can help point to exact
commits and additionally quickly get a sense of how old a version is
based on its commit date presented.
  • Loading branch information
alexcrichton committed Nov 29, 2023
1 parent 2d7a00e commit ab50122
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
29 changes: 29 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::process::Command;
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=build.rs");

set_commit_info_for_rustc();

let out_dir = PathBuf::from(
env::var_os("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
);
Expand Down Expand Up @@ -281,3 +283,30 @@ fn ignore(testsuite: &str, testname: &str, strategy: &str) -> bool {
_ => false,
}
}

fn set_commit_info_for_rustc() {
if !Path::new(".git").exists() {
return;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.arg("--abbrev=9")
.output()
{
Ok(output) if output.status.success() => output,
_ => return,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace();
let mut next = || parts.next().unwrap();
println!("cargo:rustc-env=WASMTIME_GIT_HASH={}", next());
println!(
"cargo:rustc-env=WASMTIME_VERSION_INFO={} ({} {})",
env!("CARGO_PKG_VERSION"),
next(),
next()
);
}
7 changes: 6 additions & 1 deletion src/bin/wasmtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap::Parser;
/// Wasmtime WebAssembly Runtime
#[derive(Parser, PartialEq)]
#[clap(
version,
version = version(),
after_help = "If a subcommand is not provided, the `run` subcommand will be used.\n\
\n\
Usage examples:\n\
Expand Down Expand Up @@ -40,6 +40,11 @@ struct Wasmtime {
run: wasmtime_cli::commands::RunCommand,
}

/// If WASMTIME_VERSION_INFO is set, use it, otherwise use CARGO_PKG_VERSION.
fn version() -> &'static str {
option_env!("WASMTIME_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
}

#[derive(Parser, PartialEq)]
enum Subcommand {
/// Runs a WebAssembly module
Expand Down

0 comments on commit ab50122

Please sign in to comment.