Skip to main content

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        if let Some(sha) = option_env!("SCX_GIT_SHA") {
12            if !sha.is_empty() {
13                ver += "g";
14                ver += sha;
15                if option_env!("SCX_GIT_DIRTY").is_some() {
16                    ver += "-dirty";
17                }
18            }
19        }
20        ver
21    };
22    static ref BUILD_TAG: String = {
23        let mut tag = env!("SCX_TARGET_TRIPLE").to_string();
24        if cfg!(debug_assertions) {
25            write!(tag, "/debug").unwrap();
26        }
27        tag
28    };
29}
30
31pub fn full_version(semver: &str) -> String {
32    let mut ver = semver.to_string();
33    if !GIT_VERSION.is_empty() {
34        write!(ver, "-{}", &*GIT_VERSION).unwrap();
35    }
36    if !BUILD_TAG.is_empty() {
37        write!(ver, " {}", &*BUILD_TAG).unwrap();
38    }
39    ver
40}
41
42pub fn ops_version_suffix(semver: &str) -> String {
43    let mut ver = String::from("_");
44    ver.push_str(semver);
45    if !GIT_VERSION.is_empty() {
46        write!(ver, "_{}", &*GIT_VERSION).unwrap();
47    }
48    if !BUILD_TAG.is_empty() {
49        write!(ver, "_{}", &*BUILD_TAG).unwrap();
50    }
51    ver = ver
52        .chars()
53        .map(|c| {
54            if c.is_ascii_alphanumeric() || c == '.' || c == '_' {
55                c
56            } else {
57                '_'
58            }
59        })
60        .collect();
61    ver
62}
63
64lazy_static::lazy_static! {
65    pub static ref SCX_CARGO_VERSION: &'static str = env!("CARGO_PKG_VERSION");
66    pub static ref SCX_FULL_VERSION: String = full_version(*SCX_CARGO_VERSION);
67}
68
69#[cfg(test)]
70mod tests {
71    #[test]
72    fn test_cargo_ver() {
73        //assert_eq!(super::*SCX_CARGO_VERSION, 1);
74        println!("{}", *super::SCX_CARGO_VERSION);
75    }
76
77    #[test]
78    fn test_full_ver() {
79        //assert_eq!(super::*SCX_CARGO_VERSION, 1);
80        println!("{}", *super::SCX_FULL_VERSION);
81    }
82}