scx_utils/
build_id.rs

1// Copyright (c) Meta Platforms, Inc. and affiliates.
2//
3// This software may be used and distributed according to the terms of the
4// GNU General Public License version 2.
5
6use std::fmt::Write;
7
8lazy_static::lazy_static! {
9    static ref GIT_VERSION: String = {
10        let mut ver = String::new();
11        match option_env!("VERGEN_GIT_SHA") {
12            Some(v) if v != "VERGEN_IDEMPOTENT_OUTPUT" => {
13                ver += "g";
14                ver += v;
15                if let Some("true") = option_env!("VERGEN_GIT_DIRTY") {
16                    ver += "-dirty";
17                }
18            }
19            _ => {}
20        }
21        ver
22    };
23    static ref BUILD_TAG: String = {
24        let mut tag = env!("VERGEN_CARGO_TARGET_TRIPLE").to_string();
25        if cfg!(debug_assertions) {
26            write!(tag, "/debug").unwrap();
27        }
28        tag
29    };
30}
31
32pub fn full_version(semver: &str) -> String {
33    let mut ver = semver.to_string();
34    if !GIT_VERSION.is_empty() {
35        write!(ver, "-{}", &*GIT_VERSION).unwrap();
36    }
37    if !BUILD_TAG.is_empty() {
38        write!(ver, " {}", &*BUILD_TAG).unwrap();
39    }
40    ver
41}
42
43pub fn ops_version_suffix(semver: &str) -> String {
44    let mut ver = String::from("_");
45    ver.push_str(semver);
46    if !GIT_VERSION.is_empty() {
47        write!(ver, "_{}", &*GIT_VERSION).unwrap();
48    }
49    if !BUILD_TAG.is_empty() {
50        write!(ver, "_{}", &*BUILD_TAG).unwrap();
51    }
52    ver = ver
53        .chars()
54        .map(|c| {
55            if c.is_ascii_alphanumeric() || c == '.' || c == '_' {
56                c
57            } else {
58                '_'
59            }
60        })
61        .collect();
62    ver
63}
64
65lazy_static::lazy_static! {
66    pub static ref SCX_CARGO_VERSION: &'static str = env!("CARGO_PKG_VERSION");
67    pub static ref SCX_FULL_VERSION: String = full_version(*SCX_CARGO_VERSION);
68}
69
70#[cfg(test)]
71mod tests {
72    #[test]
73    fn test_cargo_ver() {
74        //assert_eq!(super::*SCX_CARGO_VERSION, 1);
75        println!("{}", *super::SCX_CARGO_VERSION);
76    }
77
78    #[test]
79    fn test_full_ver() {
80        //assert_eq!(super::*SCX_CARGO_VERSION, 1);
81        println!("{}", *super::SCX_FULL_VERSION);
82    }
83}