diff --git a/bolt-sidecar/Cross.toml b/bolt-sidecar/Cross.toml index 674b818ef..5d215602c 100644 --- a/bolt-sidecar/Cross.toml +++ b/bolt-sidecar/Cross.toml @@ -12,5 +12,5 @@ pre-build = [ [build.env] volumes = [ - "GIT_DIR=../.git" + "GIT_DIR=./../.git" ] diff --git a/bolt-sidecar/build.rs b/bolt-sidecar/build.rs index d8f91cb91..674a01419 100644 --- a/bolt-sidecar/build.rs +++ b/bolt-sidecar/build.rs @@ -1,3 +1,15 @@ fn main() { + // Built uses the `MANIFEST_DIR` environment variable to find the location of `Cargo.toml` and + // `.git`. When building from PWD=$BOLT_REPO/bolt-sidecar, the `.git` directory won't be + // found, therefore the git commit info will be missing from the build-time information. + // + // To work around this, we also attempt to read the commit hash from the `.git/FETCH_HEAD` file + // and make it available as an environment variable at runtime. + + // make the commit hash available as an environment variable at runtime + if let Ok(commit_hash) = std::fs::read_to_string("../.git/FETCH_HEAD") { + println!("cargo:rustc-env=GIT_COMMIT_HASH={}", commit_hash.trim()); + } + built::write_built_file().expect("Failed to acquire build-time information"); } diff --git a/bolt-sidecar/src/common/mod.rs b/bolt-sidecar/src/common/mod.rs index ae22bb9bd..6f6faeef1 100644 --- a/bolt-sidecar/src/common/mod.rs +++ b/bolt-sidecar/src/common/mod.rs @@ -1,5 +1,7 @@ use lazy_static::lazy_static; +use crate::built_info; + /// Utilities for retrying a future with backoff. pub mod backoff; @@ -14,6 +16,26 @@ pub mod transactions; lazy_static! { /// The version of the Bolt sidecar binary. - pub static ref BOLT_SIDECAR_VERSION: String = - format!("v{}-{}", env!("CARGO_PKG_VERSION"), crate::built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown")); + /// + /// Example format: "v0.1.0-alpha-abcdefg" + pub static ref BOLT_SIDECAR_VERSION: String = format!( + "v{}{}", + built_info::PKG_VERSION, + // Include the git commit hash, if available + built_info::GIT_COMMIT_HASH_SHORT.map(|s| format!("-{}", s)).unwrap_or_else(|| { + // If built info is not available, try the environment variable + let from_env = std::env::var("GIT_COMMIT_HASH").map(|s| { + // take only the first 7 characters of the full hash + format!("-{}", s.chars().take(7).collect::()) + }); + + // If the environment variable is not set either, return an empty string + if let Ok(s) = from_env { + s + } else { + println!("Warning: Could not determine the git commit hash"); + String::new() + } + }) + ); }