bin_version/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4/// Hidden reexports for the bin_version macro
5pub mod _hidden {
6    pub use const_str::concat;
7    pub use git_version::git_version;
8}
9
10/// Define constants that hold the git revision and package versions.
11///
12/// Defines two global `const`s:
13///   `GIT_REVISION`: The git revision as specified by the `GIT_REVISION` env variable provided at
14///   compile time, or the current git revision as discovered by running `git describe`.
15///
16///   `VERSION`: The value of the `CARGO_PKG_VERSION` environment variable concatenated with the
17///   value of `GIT_REVISION`.
18///
19/// Note: This macro must only be used from a binary, if used inside a library this will fail to
20/// compile.
21#[macro_export]
22macro_rules! bin_version {
23    () => {
24        $crate::git_revision!();
25
26        const VERSION: &str = {
27            if GIT_REVISION.is_empty() {
28                env!("CARGO_PKG_VERSION")
29            } else {
30                $crate::_hidden::concat!(env!("CARGO_PKG_VERSION"), "-", GIT_REVISION)
31            }
32        };
33    };
34}
35
36/// Defines constant that holds the git revision at build time.
37///
38///   `GIT_REVISION`: The git revision as specified by the `GIT_REVISION` env variable provided at
39///   compile time, or the current git revision as discovered by running `git describe`.
40///   `GIT_FULL_SHA_REVISION`: The full git revision as specified by the `GIT_REVISION` env variable
41///   provided at compile time, or the current git revision as discovered by running `git describe`.
42///
43/// Note: This macro must only be used from a binary, if used inside a library this will fail to
44/// compile.
45#[macro_export]
46macro_rules! git_revision {
47    () => {
48        const _ASSERT_IS_BINARY: () = {
49            env!(
50                "CARGO_BIN_NAME",
51                "`bin_version!()` must be used from a binary"
52            );
53        };
54
55        const GIT_REVISION: &str = {
56            if let Some(revision) = option_env!("GIT_REVISION") {
57                revision
58            } else {
59                let version = $crate::_hidden::git_version!(
60                    args = ["--always", "--abbrev=12", "--dirty", "--exclude", "*"],
61                    fallback = ""
62                );
63
64                if version.is_empty() {
65                    panic!("unable to query git revision");
66                }
67                version
68            }
69        };
70
71        const GIT_FULL_SHA_REVISION: &str = {
72            if let Some(revision) = option_env!("GIT_REVISION") {
73                revision
74            } else {
75                let version = $crate::_hidden::git_version!(
76                    args = ["--always", "--abbrev=40", "--dirty", "--exclude", "*"],
77                    fallback = ""
78                );
79
80                if version.is_empty() {
81                    panic!("unable to query git revision");
82                }
83                version
84            }
85        };
86    };
87}