From fa1aab42fc12b072a69a97a057b6217e03b386e5 Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Tue, 23 Jan 2024 16:25:28 +0000 Subject: [PATCH] [opentitantool] Rework stamping Allow opentitantool to be built with stamping disabled, and disable it by default. Update the `version` command to handle the case of missing information. The build time/date is removed since it adds no useful information. Signed-off-by: Amaury Pouly --- sw/host/opentitantool/BUILD | 2 +- sw/host/opentitantool/src/command/gpio.rs | 15 ++++++++--- sw/host/opentitantool/src/command/version.rs | 26 ++++++++++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/sw/host/opentitantool/BUILD b/sw/host/opentitantool/BUILD index 958f698d48b12..0f5089f8187a1 100644 --- a/sw/host/opentitantool/BUILD +++ b/sw/host/opentitantool/BUILD @@ -43,7 +43,7 @@ rust_binary( "stamp-env.txt", ], # stamping is necessary because opentitantool builds version.rs that needs it - stamp = 1, + stamp = -1, deps = [ "//sw/host/opentitanlib", "//sw/host/ot_certs", diff --git a/sw/host/opentitantool/src/command/gpio.rs b/sw/host/opentitantool/src/command/gpio.rs index eaafeaafb15a4..4f0b27e9df8b1 100644 --- a/sw/host/opentitantool/src/command/gpio.rs +++ b/sw/host/opentitantool/src/command/gpio.rs @@ -410,10 +410,17 @@ impl CommandDispatch for GpioMonitoringVcd { let version = super::version::VersionResponse::default(); writeln!( &mut file, - " opentitantool {} {} {}", - version.version, - if version.clean { "clean" } else { "modified" }, - version.timestamp, + " opentitantool {} {}", + version.version.unwrap_or("".into()), + if let Some(clean) = version.clean { + if clean { + "clean" + } else { + "modified" + } + } else { + "" + } )?; writeln!(&mut file, "$end")?; match clock_nature { diff --git a/sw/host/opentitantool/src/command/version.rs b/sw/host/opentitantool/src/command/version.rs index 71a5e2477c5a1..8a88d7ece7cd2 100644 --- a/sw/host/opentitantool/src/command/version.rs +++ b/sw/host/opentitantool/src/command/version.rs @@ -15,19 +15,29 @@ pub struct Version {} #[derive(Debug, serde::Serialize)] pub struct VersionResponse { - pub version: String, - pub revision: String, - pub clean: bool, - pub timestamp: i64, + pub version: Option, + pub revision: Option, + pub clean: Option, +} + +fn parse_variable(content: &str) -> Option { + // If stamping is disabled, the variable substition in stamp-env.txt + // will not happen and the content of the environment variable will be + // `{NAME_OF_VARIABLE}`. If we detect that this is the case, we return + // None, otherwise we return the content. + if content.starts_with('{') { + None + } else { + Some(content.to_string()) + } } impl Default for VersionResponse { fn default() -> Self { VersionResponse { - version: std::env!("BUILD_GIT_VERSION").to_string(), - revision: std::env!("BUILD_SCM_REVISION").to_string(), - clean: std::env!("BUILD_SCM_STATUS") == "clean", - timestamp: std::env!("BUILD_TIMESTAMP").parse::().unwrap(), + version: parse_variable(std::env!("BUILD_GIT_VERSION")), + revision: parse_variable(std::env!("BUILD_SCM_REVISION")), + clean: parse_variable(std::env!("BUILD_SCM_STATUS")).map(|x| x == "clean"), } } }