Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attempt to include .git in cross-rs build ctx #605

Draft
wants to merge 3 commits into
base: unstable
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions bolt-sidecar/Cross.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ pre-build = [
# more info at: https://github.com/cross-rs/cross/issues/1565#issuecomment-2483968180
"apt-get --assume-yes --no-install-recommends install gcc-10 g++-10 && ln -sf /usr/bin/gcc-10 /usr/bin/gcc && ln -sf /usr/bin/g++-10 /usr/bin/g++"
]

[build.env]
volumes = [
"GIT_DIR=./../.git"
]
12 changes: 12 additions & 0 deletions bolt-sidecar/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
21 changes: 19 additions & 2 deletions bolt-sidecar/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use lazy_static::lazy_static;

use crate::built_info;

/// Utilities for retrying a future with backoff.
pub mod backoff;

Expand All @@ -14,6 +16,21 @@ 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::<String>())
});

// If the environment variable is not set either, return an empty string
from_env.unwrap_or_default()
})
);
}
Loading