Skip to content

Commit

Permalink
release: Use version string from git if available
Browse files Browse the repository at this point in the history
If the SVSM is built from the git repository, get the version string
via 'git describe' to have a more acurate version identifier. The git
version allows to identify from which commit the code was built.

Signed-off-by: Joerg Roedel <[email protected]>
  • Loading branch information
joergroedel committed Jan 21, 2025
1 parent 4b459da commit 426f409
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ verus*.log
gen_meta
stage1/stage1
stage1/stage1-test
release/src/git_version.rs
print-meta
cbit
.idea/
Expand Down
46 changes: 46 additions & 0 deletions release/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2025 SUSE LLC
//
// Author: Joerg Roedel <[email protected]>

use std::fs::OpenOptions;
use std::io::prelude::*;
use std::process::Command;
use std::string::String;

fn git_version() -> Result<String, ()> {
let output = Command::new("git")
.args(["describe", "--always", "--dirty=+"])
.output()
.map_err(|_| ())?;
if !output.status.success() {
return Err(());
}

let stdout = String::from_utf8(output.stdout).unwrap();
let mut lines = stdout.lines();
let first_line = lines.next().unwrap().trim();

Ok(String::from(first_line))
}

fn write_version(version: String) {
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open("src/git_version.rs")
.unwrap();
writeln!(&mut file, "// SPDX-License-Identifier: MIT\n//").unwrap();
writeln!(
&mut file,
"// This file is automatically generated - Any changes will be overwritten\n//\n"
)
.unwrap();
writeln!(&mut file, "pub const GIT_VERSION: &str = \"{}\";", version).unwrap();
}

fn main() {
write_version(git_version().unwrap_or_default());
}
26 changes: 17 additions & 9 deletions release/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

#![no_std]

mod git_version;

use core::fmt;
use git_version::GIT_VERSION;

#[allow(dead_code)]
#[derive(Debug)]
Expand All @@ -28,15 +31,20 @@ pub struct SvsmVersion {

impl fmt::Display for SvsmVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.release_type {
ReleaseType::Development => {
write!(f, "{}.{:#02}-devel", self.year, self.month)
}
ReleaseType::Candidate(counter) => {
write!(f, "{}.{:#02}-rc{}", self.year, self.month, counter)
}
ReleaseType::Stable(counter) => {
write!(f, "{}.{:#02}.{}", self.year, self.month, counter)
#[allow(clippy::const_is_empty)]
if !GIT_VERSION.is_empty() {
write!(f, "{}", GIT_VERSION)
} else {
match self.release_type {
ReleaseType::Development => {
write!(f, "{}.{:#02}-devel", self.year, self.month)
}
ReleaseType::Candidate(counter) => {
write!(f, "{}.{:#02}-rc{}", self.year, self.month, counter)
}
ReleaseType::Stable(counter) => {
write!(f, "{}.{:#02}.{}", self.year, self.month, counter)
}
}
}
}
Expand Down

0 comments on commit 426f409

Please sign in to comment.