diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..2246ee5 --- /dev/null +++ b/build.rs @@ -0,0 +1,34 @@ +use std::process::Command; + +fn main() { + let git_hash = Command::new("git") + .args(["describe", "--always", "--dirty"]) + .output() + .map(|output| { + if output.status.success() { + String::from_utf8_lossy(&output.stdout).trim().to_string() + } else { + "unknown".to_string() + } + }) + .unwrap_or_else(|_| "unknown".to_string()); + + let git_date = Command::new("git") + .args(["log", "-1", "--format=%cs"]) + .output() + .map(|output| { + if output.status.success() { + String::from_utf8_lossy(&output.stdout).trim().to_string() + } else { + "unknown".to_string() + } + }) + .unwrap_or_else(|_| "unknown".to_string()); + + // Make the git info available to the program + println!("cargo:rustc-env=GIT_HASH={}", git_hash); + println!("cargo:rustc-env=GIT_DATE={}", git_date); + + // Re-run build script if git HEAD changes + println!("cargo:rerun-if-changed=.git/HEAD"); +} diff --git a/src/main.rs b/src/main.rs index 735ddb0..c84d690 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,16 @@ use std::env; use cargo_hyperlight::cargo; +const VERSION: &str = env!("CARGO_PKG_VERSION"); +const GIT_HASH: &str = env!("GIT_HASH"); +const GIT_DATE: &str = env!("GIT_DATE"); + fn main() { + if env::args().any(|arg| arg == "--version" || arg == "-V") { + println!("cargo-hyperlight {} ({} {})", VERSION, GIT_HASH, GIT_DATE); + return; + } + let args = env::args_os().enumerate().filter_map(|(i, arg)| { // skip the binary name and the "hyperlight" subcommand if present if i == 0 || (i == 1 && arg == "hyperlight") {