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