diff --git a/Cargo.lock b/Cargo.lock index cb57afded0..dfd3278645 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -401,8 +401,15 @@ dependencies = [ name = "build_info" version = "0.0.0" dependencies = [ + "build_rs_git_info", "inspect", - "vergen", +] + +[[package]] +name = "build_rs_git_info" +version = "0.0.0" +dependencies = [ + "anyhow", ] [[package]] @@ -7575,7 +7582,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c" dependencies = [ "deranged", - "itoa", "libc", "num-conv", "num_threads", @@ -8352,6 +8358,7 @@ name = "underhill_crash" version = "0.0.0" dependencies = [ "anyhow", + "build_rs_git_info", "fs-err", "futures", "get_protocol", @@ -8362,7 +8369,6 @@ dependencies = [ "tracing", "tracing-subscriber", "underhill_confidentiality", - "vergen", "vmbus_async", "vmbus_user_channel", "zerocopy", @@ -8400,13 +8406,13 @@ name = "underhill_init" version = "0.0.0" dependencies = [ "anyhow", + "build_rs_git_info", "fs-err", "kmsg_defs", "libc", "log", "nix 0.30.1", "underhill_confidentiality", - "vergen", "walkdir", ] @@ -8590,18 +8596,6 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" -[[package]] -name = "vergen" -version = "8.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2990d9ea5967266ea0ccf413a4aa5c42a93dbcfda9cb49a97de6931726b12566" -dependencies = [ - "anyhow", - "cfg-if", - "rustversion", - "time", -] - [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index 735c7b45fa..91059cabad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,7 @@ pipette_protocol = { path = "petri/pipette_protocol" } # support crates address_filter = { path = "support/address_filter" } +build_rs_git_info = { path = "support/build_rs_git_info" } arc_cyclic_builder = { path = "support/arc_cyclic_builder" } atomic_ringbuf = { path = "support/atomic_ringbuf" } cache_topology = { path = "support/cache_topology" } @@ -553,7 +554,6 @@ typed-path = "0.11" uefi = "0.35.0" unicycle = "0.10.2" urlencoding = "2.1.3" -vergen = "8.2" vfio-bindings = "0.4.0" walkdir = "2.3" wchar = "0.11" diff --git a/openhcl/build_info/Cargo.toml b/openhcl/build_info/Cargo.toml index 395fdeb7cf..7f302ce17f 100644 --- a/openhcl/build_info/Cargo.toml +++ b/openhcl/build_info/Cargo.toml @@ -10,7 +10,7 @@ rust-version.workspace = true inspect.workspace = true [build-dependencies] -vergen = { workspace = true, features = ["git", "gitcl"] } +build_rs_git_info.workspace = true [lints] workspace = true diff --git a/openhcl/build_info/build.rs b/openhcl/build_info/build.rs index 5a6d24149e..bf72c2b98e 100644 --- a/openhcl/build_info/build.rs +++ b/openhcl/build_info/build.rs @@ -4,5 +4,5 @@ #![expect(missing_docs)] fn main() { - vergen::EmitBuilder::builder().all_git().emit().unwrap(); + build_rs_git_info::emit_git_info().unwrap(); } diff --git a/openhcl/build_info/src/lib.rs b/openhcl/build_info/src/lib.rs index 7c0868d411..66596b7094 100644 --- a/openhcl/build_info/src/lib.rs +++ b/openhcl/build_info/src/lib.rs @@ -31,12 +31,12 @@ impl BuildInfo { // structure to be closer to PODs. Self { crate_name: env!("CARGO_PKG_NAME"), - revision: if let Some(r) = option_env!("VERGEN_GIT_SHA") { + revision: if let Some(r) = option_env!("BUILD_GIT_SHA") { r } else { "" }, - branch: if let Some(b) = option_env!("VERGEN_GIT_BRANCH") { + branch: if let Some(b) = option_env!("BUILD_GIT_BRANCH") { b } else { "" diff --git a/openhcl/underhill_crash/Cargo.toml b/openhcl/underhill_crash/Cargo.toml index 12f436e189..18f456effa 100644 --- a/openhcl/underhill_crash/Cargo.toml +++ b/openhcl/underhill_crash/Cargo.toml @@ -24,7 +24,7 @@ tracing-subscriber.workspace = true zerocopy.workspace = true [build-dependencies] -vergen = { workspace = true, features = ["git", "gitcl"] } +build_rs_git_info.workspace = true [lints] workspace = true diff --git a/openhcl/underhill_crash/build.rs b/openhcl/underhill_crash/build.rs index 5a6d24149e..bf72c2b98e 100644 --- a/openhcl/underhill_crash/build.rs +++ b/openhcl/underhill_crash/build.rs @@ -4,5 +4,5 @@ #![expect(missing_docs)] fn main() { - vergen::EmitBuilder::builder().all_git().emit().unwrap(); + build_rs_git_info::emit_git_info().unwrap(); } diff --git a/openhcl/underhill_crash/src/lib.rs b/openhcl/underhill_crash/src/lib.rs index 8a0e46b42a..402eb32f55 100644 --- a/openhcl/underhill_crash/src/lib.rs +++ b/openhcl/underhill_crash/src/lib.rs @@ -283,7 +283,7 @@ pub fn main() -> ! { let os_version = OsVersionInfo::new(); - let crate_revision = option_env!("VERGEN_GIT_SHA").unwrap_or("UNKNOWN_REVISION"); + let crate_revision = option_env!("BUILD_GIT_SHA").unwrap_or("UNKNOWN_REVISION"); let openhcl_version = option_env!("OPENHCL_VERSION").unwrap_or("UNKNOWN_VERSION"); let os_version_major = os_version.major(); diff --git a/openhcl/underhill_init/Cargo.toml b/openhcl/underhill_init/Cargo.toml index 8162fb32ab..d24da48313 100644 --- a/openhcl/underhill_init/Cargo.toml +++ b/openhcl/underhill_init/Cargo.toml @@ -19,7 +19,7 @@ nix = { workspace = true, features = ["ioctl"] } walkdir.workspace = true [build-dependencies] -vergen = { workspace = true, features = ["git", "gitcl"] } +build_rs_git_info.workspace = true [lints] workspace = true diff --git a/openhcl/underhill_init/build.rs b/openhcl/underhill_init/build.rs index 5a6d24149e..bf72c2b98e 100644 --- a/openhcl/underhill_init/build.rs +++ b/openhcl/underhill_init/build.rs @@ -4,5 +4,5 @@ #![expect(missing_docs)] fn main() { - vergen::EmitBuilder::builder().all_git().emit().unwrap(); + build_rs_git_info::emit_git_info().unwrap(); } diff --git a/openhcl/underhill_init/src/lib.rs b/openhcl/underhill_init/src/lib.rs index 0530183f90..0c29aa7eca 100644 --- a/openhcl/underhill_init/src/lib.rs +++ b/openhcl/underhill_init/src/lib.rs @@ -436,8 +436,8 @@ fn do_main() -> anyhow::Result<()> { log::info!( "Initial process: crate_name={}, crate_revision={}, crate_branch={}", env!("CARGO_PKG_NAME"), - option_env!("VERGEN_GIT_SHA").unwrap_or("UNKNOWN_REVISION"), - option_env!("VERGEN_GIT_BRANCH").unwrap_or("UNKNOWN_BRANCH"), + option_env!("BUILD_GIT_SHA").unwrap_or("UNKNOWN_REVISION"), + option_env!("BUILD_GIT_BRANCH").unwrap_or("UNKNOWN_BRANCH"), ); let stat_files = [ diff --git a/support/build_rs_git_info/Cargo.toml b/support/build_rs_git_info/Cargo.toml new file mode 100644 index 0000000000..b95d1a0d5e --- /dev/null +++ b/support/build_rs_git_info/Cargo.toml @@ -0,0 +1,13 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +[package] +name = "build_rs_git_info" +edition.workspace = true +rust-version.workspace = true + +[dependencies] +anyhow.workspace = true + +[lints] +workspace = true diff --git a/support/build_rs_git_info/src/lib.rs b/support/build_rs_git_info/src/lib.rs new file mode 100644 index 0000000000..8a856f158f --- /dev/null +++ b/support/build_rs_git_info/src/lib.rs @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +//! Build-script helper that emits `BUILD_GIT_SHA` and `BUILD_GIT_BRANCH` +//! cargo environment variables by invoking the `git` CLI. + +use std::process::Command; + +fn git_output(args: &[&str]) -> anyhow::Result { + let output = Command::new("git").args(args).output()?; + + if !output.status.success() { + anyhow::bail!( + "git {:?} failed with code {:?}: {}", + args, + output.status.code(), + String::from_utf8_lossy(&output.stderr) + ); + } + + let output = String::from_utf8(output.stdout).unwrap().trim().to_owned(); + Ok(output) +} + +fn git_path(args: &[&str]) -> anyhow::Result { + let output = git_output(args)?; + Ok(std::path::absolute(&output)?) +} + +/// Emit git information as `cargo:rustc-env` variables so they are available via +/// `env!()` / `option_env!()` in the consuming crate. +pub fn emit_git_info() -> anyhow::Result<()> { + // Always rerun when HEAD changes (e.g. branch switch). + let head_path = git_path(&["rev-parse", "--git-path", "HEAD"])?; + println!("cargo:rerun-if-changed={}", head_path.display()); + + // If HEAD is a symbolic ref (i.e. points at a branch), also watch the + // branch ref file so we rebuild when new commits land on that branch. + if let Ok(head_ref) = git_output(&["symbolic-ref", "HEAD"]) { + // e.g. refs/heads/main → .git/refs/heads/main (or the worktree equivalent) + let ref_path = git_path(&["rev-parse", "--git-path", &head_ref])?; + println!("cargo:rerun-if-changed={}", ref_path.display()); + } + + let sha = git_output(&["rev-parse", "HEAD"])?; + let branch = git_output(&["rev-parse", "--abbrev-ref", "HEAD"])?; + + println!("cargo:rustc-env=BUILD_GIT_SHA={sha}"); + println!("cargo:rustc-env=BUILD_GIT_BRANCH={branch}"); + + Ok(()) +}