bin_version/
lib.rs

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