Use VERSION_FILE env variable to define opentitantool version The version file path is configurable instead of a hard-coded path in version.rs. There is an existing bazel label to grab the OT version information. Change-Id: I0fc8d456223787cf4e324515edb4c1c3e2178a97
diff --git a/sw/host/opentitantool/BUILD b/sw/host/opentitantool/BUILD index 54d3392..19222cd 100644 --- a/sw/host/opentitantool/BUILD +++ b/sw/host/opentitantool/BUILD
@@ -24,9 +24,15 @@ "src/command/version.rs", "src/main.rs", ], + compile_data = [ + "//util:full_version.txt", + ], proc_macro_deps = [ "//sw/host/opentitanlib/opentitantool_derive", ], + rustc_env = { + "VERSION_FILE": "$(location //util:full_version.txt)", + }, stamp = 1, deps = [ "//sw/host/opentitanlib",
diff --git a/sw/host/opentitantool/src/command/version.rs b/sw/host/opentitantool/src/command/version.rs index 80b007f..375e52e 100644 --- a/sw/host/opentitantool/src/command/version.rs +++ b/sw/host/opentitantool/src/command/version.rs
@@ -19,17 +19,15 @@ /// /// At runtime, this string is parsed into key/value pairs, which are returned. (It would have /// been desirable to perform the parsing at compile time as well.) -/// TODO(ykwang): The include path has to be prefixed with "../../" when setup opentitan as -/// external repo. -///fn get_volatile_status() -> BTreeMap<&'static str, &'static str> { -/// let volatile_status = include_str!("../../../../../bazel-out/volatile-status.txt"); -/// let re = Regex::new(r"([A-Z_]+) ([^\n]+)\n").unwrap(); -/// let mut properties: BTreeMap<&'static str, &'static str> = BTreeMap::new(); -/// for cap in re.captures_iter(volatile_status) { -/// properties.insert(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str()); -/// } -/// properties -///} +fn get_volatile_status() -> BTreeMap<&'static str, &'static str> { + let volatile_status = include_str!(env!("VERSION_FILE")); + let re = Regex::new(r"([A-Z_]+) ([^\n]+)\n").unwrap(); + let mut properties: BTreeMap<&'static str, &'static str> = BTreeMap::new(); + for cap in re.captures_iter(volatile_status) { + properties.insert(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str()); + } + properties +} #[derive(Debug, StructOpt)] pub struct Version {} @@ -40,8 +38,6 @@ clean: bool, timestamp: i64, } -#[derive(Debug, serde::Serialize)] -pub struct DummyResponse {} impl CommandDispatch for Version { fn run( @@ -49,13 +45,12 @@ _context: &dyn Any, _transport: &TransportWrapper, ) -> Result<Option<Box<dyn Serialize>>> { - //let properties = get_volatile_status(); - //Ok(Some(Box::new(VersionResponse { - // version: properties.get("BUILD_GIT_VERSION").unwrap().to_string(), - // revision: properties.get("BUILD_SCM_REVISION").unwrap().to_string(), - // clean: *properties.get("BUILD_SCM_STATUS").unwrap() == "clean", - // timestamp: properties.get("BUILD_TIMESTAMP").unwrap().parse::<i64>()?, - //}))) - Ok(Some(Box::new(DummyResponse {}))) + let properties = get_volatile_status(); + Ok(Some(Box::new(VersionResponse { + version: properties.get("BUILD_GIT_VERSION").unwrap().to_string(), + revision: properties.get("BUILD_SCM_REVISION").unwrap().to_string(), + clean: *properties.get("BUILD_SCM_STATUS").unwrap() == "clean", + timestamp: properties.get("BUILD_TIMESTAMP").unwrap().parse::<i64>()?, + }))) } }
diff --git a/util/BUILD b/util/BUILD index 7d3c2b8..932a1a5 100644 --- a/util/BUILD +++ b/util/BUILD
@@ -19,7 +19,7 @@ genrule( name = "full_version_file", outs = ["full_version.txt"], - cmd = """cp bazel-out/volatile-status.txt $@""", + cmd = """cp -f bazel-out/volatile-status.txt $@""", stamp = 1, # this provides volatile-status.txt )