-
Notifications
You must be signed in to change notification settings - Fork 225
openvmm: add minimal tag-derived source identity #3989
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
Closed
Ben Hillis (benhillis)
wants to merge
17
commits into
microsoft:main
from
benhillis:user/benhill/openvmm-git-build-info
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a2f9630
build_rs_git_info: collect complete source identity
cb47e76
build_rs_git_info: harden source state collection
0dce981
build_rs_git_info: avoid temporary test collisions
8c9b7ff
openvmm: simplify tag-derived source identity
6e91fe9
openvmm: keep release tag inputs coherent
28ec3f2
openvmm: expose source identity from the CLI
8add2eb
openvmm: address version flag review feedback
3d72653
openvmm: make version flags global
c8574e8
guide: clarify development revision length
2ec85c8
openvmm: refresh identity on tracked edits
1a03902
openvmm: honor the end-of-options marker
94aa9b0
openvmm: skip worktree watches in CI
a4aa19e
openvmm: test annotated release tags
1fe510d
openvmm: test strict release tag namespace
dc07901
openvmm: base dirty identity on tracked files
f9b98ae
openvmm: let the entry parser handle version
9aef473
openvmm: watch the Git index for identity
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| [package] | ||
| name = "openvmm_build_info" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
|
|
||
| [build-dependencies] | ||
| serde = { workspace = true, features = ["derive"] } | ||
| serde_json = { workspace = true, features = ["std"] } | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #![expect(missing_docs)] | ||
|
|
||
| use serde::Deserialize; | ||
|
|
||
| mod version; | ||
|
|
||
| const RELEASE_METADATA_SCHEMA: u32 = 1; | ||
|
|
||
| #[derive(Deserialize)] | ||
| #[serde(deny_unknown_fields)] | ||
| struct SerializedReleaseMetadata { | ||
| schema_version: u32, | ||
| version: String, | ||
| tag: String, | ||
| revision: String, | ||
| } | ||
|
|
||
| fn read_release_metadata(path: &std::path::Path) -> Option<version::ReleaseMetadata> { | ||
| let contents = match std::fs::read(path) { | ||
| Ok(contents) => contents, | ||
| Err(error) if error.kind() == std::io::ErrorKind::NotFound => return None, | ||
| Err(error) => panic!("failed to read {}: {error}", path.display()), | ||
| }; | ||
| let metadata: SerializedReleaseMetadata = serde_json::from_slice(&contents) | ||
| .unwrap_or_else(|error| panic!("failed to parse {}: {error}", path.display())); | ||
| assert_eq!( | ||
| metadata.schema_version, RELEASE_METADATA_SCHEMA, | ||
| "unsupported OpenVMM release metadata schema" | ||
| ); | ||
| let metadata = version::ReleaseMetadata { | ||
| version: metadata.version, | ||
| tag: metadata.tag, | ||
| revision: metadata.revision, | ||
| }; | ||
| version::validate_release_metadata(&metadata) | ||
| .unwrap_or_else(|error| panic!("invalid {}: {error}", path.display())); | ||
| Some(metadata) | ||
| } | ||
|
|
||
| fn git_output(repo: &std::path::Path, args: &[&str]) -> Option<String> { | ||
| let output = std::process::Command::new("git") | ||
| .arg("-C") | ||
| .arg(repo) | ||
| .args(args) | ||
| .output() | ||
| .ok()?; | ||
| output | ||
| .status | ||
| .success() | ||
| .then(|| String::from_utf8_lossy(&output.stdout).trim().to_owned()) | ||
| } | ||
|
|
||
| fn watch_tracked_files(repo: &std::path::Path) { | ||
| let output = std::process::Command::new("git") | ||
| .arg("-C") | ||
| .arg(repo) | ||
| .args(["ls-files", "-z"]) | ||
| .output() | ||
| .expect("failed to list tracked OpenVMM files"); | ||
| assert!( | ||
| output.status.success(), | ||
| "git ls-files failed: {}", | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
|
|
||
| for path in output.stdout.split(|byte| *byte == 0) { | ||
| if path.is_empty() { | ||
| continue; | ||
| } | ||
| let path = std::str::from_utf8(path).expect("tracked OpenVMM file path is not valid UTF-8"); | ||
| assert!( | ||
| !path.contains(['\r', '\n']), | ||
| "tracked OpenVMM file path contains a line break" | ||
| ); | ||
| println!("cargo:rerun-if-changed={}", repo.join(path).display()); | ||
| } | ||
| } | ||
|
|
||
| fn watch_git_inputs(repo: &std::path::Path, github_actions: bool) { | ||
| for path in ["HEAD", "index", "refs/tags", "packed-refs"] { | ||
| if let Some(path) = git_output(repo, &["rev-parse", "--git-path", path]) { | ||
| println!("cargo:rerun-if-changed={}", repo.join(path).display()); | ||
| } | ||
| } | ||
| if let Some(head_ref) = git_output(repo, &["symbolic-ref", "HEAD"]) { | ||
| if let Some(path) = git_output(repo, &["rev-parse", "--git-path", &head_ref]) { | ||
| println!("cargo:rerun-if-changed={}", repo.join(path).display()); | ||
| } | ||
| } | ||
| if let Some(tag_refs) = git_output( | ||
| repo, | ||
| &["for-each-ref", "--format=%(refname)", "refs/tags/openvmm-v"], | ||
| ) { | ||
| for tag_ref in tag_refs.lines() { | ||
| if let Some(path) = git_output(repo, &["rev-parse", "--git-path", tag_ref]) { | ||
| println!("cargo:rerun-if-changed={}", repo.join(path).display()); | ||
| } | ||
| } | ||
| } | ||
| if !github_actions { | ||
| watch_tracked_files(repo); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
| println!("cargo:rerun-if-env-changed=GITHUB_ACTIONS"); | ||
|
|
||
| let repo_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); | ||
| let release_metadata_path = repo_root.join(".openvmm-release.json"); | ||
| println!("cargo:rerun-if-changed={}", release_metadata_path.display()); | ||
|
|
||
| let release_metadata = read_release_metadata(&release_metadata_path); | ||
| let github_actions = std::env::var("GITHUB_ACTIONS").as_deref() == Ok("true"); | ||
| let git = version::collect_git_source(&repo_root, github_actions) | ||
| .unwrap_or_else(|error| panic!("failed to collect OpenVMM Git identity: {error}")); | ||
| if git.is_some() { | ||
| watch_git_inputs(&repo_root, github_actions); | ||
| } | ||
| let version = version::resolve_version(git.as_ref(), release_metadata.as_ref()) | ||
| .unwrap_or_else(|error| panic!("failed to resolve OpenVMM version: {error}")); | ||
|
|
||
| if git.is_none() && release_metadata.is_none() { | ||
| println!( | ||
| "cargo:warning=OpenVMM release metadata is unavailable. This build will report \ | ||
| 0.0.0-dev and must not be treated as an official release build. Use a Git checkout \ | ||
| with the release tag available or the official source bundle attached to the GitHub \ | ||
| Release." | ||
| ); | ||
| } | ||
|
|
||
| println!( | ||
| "cargo:rustc-env=OPENVMM_PRODUCT_VERSION={}", | ||
| version.product_version | ||
| ); | ||
| println!("cargo:rustc-env=OPENVMM_VERSION={}", version.version); | ||
| println!("cargo:rustc-env=BUILD_GIT_SHA={}", version.revision); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! OpenVMM product version and source revision. | ||
|
|
||
| #![expect(missing_docs)] | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "../version.rs"] | ||
| mod version; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct BuildInfo { | ||
| product_version: &'static str, | ||
| version: &'static str, | ||
| revision: &'static str, | ||
| } | ||
|
|
||
| impl BuildInfo { | ||
| pub const fn new() -> Self { | ||
| Self { | ||
| product_version: env!("OPENVMM_PRODUCT_VERSION"), | ||
| version: env!("OPENVMM_VERSION"), | ||
| revision: env!("BUILD_GIT_SHA"), | ||
| } | ||
| } | ||
|
|
||
| pub const fn product_version(&self) -> &'static str { | ||
| self.product_version | ||
| } | ||
|
|
||
| pub const fn version(&self) -> &'static str { | ||
| self.version | ||
| } | ||
|
|
||
| pub const fn scm_revision(&self) -> &'static str { | ||
| self.revision | ||
| } | ||
| } | ||
|
|
||
| // Keep the build information easy to discover without a debugger. | ||
| // | ||
| // The static remains reachable through `get`, so `#[used]` is not required. | ||
| // | ||
| // UNSAFETY: `link_section` and `export_name` are unsafe attributes. | ||
| #[expect(unsafe_code)] | ||
| // SAFETY: These are custom metadata sections with no safety requirements. | ||
| #[cfg_attr(target_os = "windows", unsafe(link_section = ".build_i"))] | ||
| #[cfg_attr(target_vendor = "apple", unsafe(link_section = "__DATA,__build_info"))] | ||
| #[cfg_attr( | ||
| not(any(target_os = "windows", target_vendor = "apple")), | ||
| unsafe(link_section = ".build_info") | ||
| )] | ||
| // SAFETY: This symbol is uniquely named for OpenVMM and has no runtime ABI. | ||
| #[unsafe(export_name = "OPENVMM_BUILD_INFO")] | ||
| static OPENVMM_BUILD_INFO: BuildInfo = BuildInfo::new(); | ||
|
|
||
| pub fn get() -> &'static BuildInfo { | ||
| // Prevent fat LTO from optimizing away the metadata static. | ||
| std::hint::black_box(&OPENVMM_BUILD_INFO) | ||
|
benhillis marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.