common_version/
lib.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(clippy::print_stdout)]
16
17use std::fmt::Display;
18
19mod build {
20    include!(concat!(env!("OUT_DIR"), "/shadow.rs"));
21}
22
23#[derive(Clone, Debug, PartialEq)]
24pub struct BuildInfo {
25    pub branch: &'static str,
26    pub commit: &'static str,
27    pub commit_short: &'static str,
28    pub clean: bool,
29    pub rustc: &'static str,
30    pub target: &'static str,
31    pub version: &'static str,
32}
33
34impl Display for BuildInfo {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(
37            f,
38            "{}",
39            [
40                format!("branch: {}", self.branch),
41                format!("commit: {}", self.commit),
42                format!("commit_short: {}", self.commit_short),
43                format!("clean: {}", self.clean),
44                format!("version: {}", self.version),
45            ]
46            .join("\n")
47        )
48    }
49}
50
51#[derive(Clone, Debug, PartialEq)]
52#[cfg_attr(feature = "codec", derive(serde::Serialize, serde::Deserialize))]
53pub struct OwnedBuildInfo {
54    pub branch: String,
55    pub commit: String,
56    pub commit_short: String,
57    pub clean: bool,
58    pub rustc: String,
59    pub target: String,
60    pub version: String,
61}
62
63impl From<BuildInfo> for OwnedBuildInfo {
64    fn from(info: BuildInfo) -> Self {
65        OwnedBuildInfo {
66            branch: info.branch.to_string(),
67            commit: info.commit.to_string(),
68            commit_short: info.commit_short.to_string(),
69            clean: info.clean,
70            rustc: info.rustc.to_string(),
71            target: info.target.to_string(),
72            version: info.version.to_string(),
73        }
74    }
75}
76
77impl Display for OwnedBuildInfo {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        write!(
80            f,
81            "{}",
82            [
83                format!("branch: {}", self.branch),
84                format!("commit: {}", self.commit),
85                format!("commit_short: {}", self.commit_short),
86                format!("clean: {}", self.clean),
87                format!("version: {}", self.version),
88            ]
89            .join("\n")
90        )
91    }
92}
93
94pub const fn build_info() -> BuildInfo {
95    BuildInfo {
96        branch: build::BRANCH,
97        commit: build::COMMIT_HASH,
98        commit_short: build::SHORT_COMMIT,
99        clean: build::GIT_CLEAN,
100        rustc: build::RUST_VERSION,
101        target: build::BUILD_TARGET,
102        version: env!("GREPTIME_PRODUCT_VERSION"),
103    }
104}
105
106const BUILD_INFO: BuildInfo = build_info();
107
108pub const fn version() -> &'static str {
109    BUILD_INFO.version
110}
111
112pub const fn verbose_version() -> &'static str {
113    const_format::formatcp!(
114        "\nbranch: {}\ncommit: {}\nclean: {}\nversion: {}",
115        BUILD_INFO.branch,
116        BUILD_INFO.commit,
117        BUILD_INFO.clean,
118        BUILD_INFO.version,
119    )
120}
121
122pub const fn short_version() -> &'static str {
123    const BRANCH: &str = BUILD_INFO.branch;
124    const COMMIT_ID: &str = BUILD_INFO.commit_short;
125
126    // When git checkout to a commit, the branch is empty.
127    #[allow(clippy::const_is_empty)]
128    if !BRANCH.is_empty() {
129        const_format::formatcp!("{}-{}", BRANCH, COMMIT_ID)
130    } else {
131        COMMIT_ID
132    }
133}