1 // Copyright (c) 2016, 2018 vergen developers
2 //
3 // Licensed under the Apache License, Version 2.0
4 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. All files in the project carrying such notice may not be copied,
7 // modified, or distributed except according to those terms.
8 
9 //! Flags used to control the build script output.
10 
11 bitflags!(
12     /// Constants Flags
13     ///
14     /// Use these to toggle off the generation of constants you won't use.
15     ///
16     /// ```
17     /// # extern crate vergen;
18     /// #
19     /// # use vergen::ConstantsFlags;
20     /// #
21     /// # fn main() {
22     /// let mut actual_flags = ConstantsFlags::all();
23     /// actual_flags.toggle(ConstantsFlags::SHA_SHORT);
24     /// actual_flags.toggle(ConstantsFlags::BUILD_DATE);
25     /// actual_flags.toggle(ConstantsFlags::SEMVER_LIGHTWEIGHT);
26     /// actual_flags.toggle(ConstantsFlags::SEMVER_FROM_CARGO_PKG);
27     ///
28     /// let expected_flags = ConstantsFlags::BUILD_TIMESTAMP |
29     ///     ConstantsFlags::SHA |
30     ///     ConstantsFlags::COMMIT_DATE |
31     ///     ConstantsFlags::TARGET_TRIPLE |
32     ///     ConstantsFlags::SEMVER |
33     ///     ConstantsFlags::REBUILD_ON_HEAD_CHANGE;
34     ///
35     /// assert_eq!(actual_flags, expected_flags)
36     /// # }
37     /// ```
38     pub struct ConstantsFlags: u64 {
39         /// Generate the build timestamp constant.
40         ///
41         /// `2018-08-09T15:15:57.282334589+00:00`
42         const BUILD_TIMESTAMP        = 0b0000_0000_0001;
43         /// Generate the build date constant.
44         ///
45         /// `2018-08-09`
46         const BUILD_DATE             = 0b0000_0000_0010;
47         /// Generate the SHA constant.
48         ///
49         /// `75b390dc6c05a6a4aa2791cc7b3934591803bc22`
50         const SHA                    = 0b0000_0000_0100;
51         /// Generate the short SHA constant.
52         ///
53         /// `75b390d`
54         const SHA_SHORT              = 0b0000_0000_1000;
55         /// Generate the commit date constant.
56         ///
57         /// `2018-08-08`
58         const COMMIT_DATE            = 0b0000_0001_0000;
59         /// Generate the target triple constant.
60         ///
61         /// `x86_64-unknown-linux-gnu`
62         const TARGET_TRIPLE          = 0b0000_0010_0000;
63         /// Generate the semver constant.
64         ///
65         /// This defaults to the output of `git describe`.  If that output is
66         /// empty, the the `CARGO_PKG_VERSION` environment variable is used.
67         ///
68         /// `v0.1.0`
69         const SEMVER                 = 0b0000_0100_0000;
70         /// Generate the semver constant, including lightweight tags.
71         ///
72         /// This defaults to the output of `git describe --tags`.  If that output
73         /// is empty, the the `CARGO_PKG_VERSION` environment variable is used.
74         ///
75         /// `v0.1.0`
76         const SEMVER_LIGHTWEIGHT     = 0b0000_1000_0000;
77         /// Generate the `cargo:rebuild-if-changed=.git/HEAD` and the
78         /// `cargo:rebuild-if-changed=.git/<ref>` cargo build output.
79         const REBUILD_ON_HEAD_CHANGE = 0b0001_0000_0000;
80         /// Generate the semver constant from `CARGO_PKG_VERSION`.  This is
81         /// mutually exclusive with the `SEMVER` flag.
82         ///
83         /// `0.1.0`
84         const SEMVER_FROM_CARGO_PKG  = 0b0010_0000_0000;
85     }
86 );
87 
88 pub const BUILD_TIMESTAMP_NAME: &str = "VERGEN_BUILD_TIMESTAMP";
89 pub const BUILD_DATE_NAME: &str = "VERGEN_BUILD_DATE";
90 pub const SHA_NAME: &str = "VERGEN_SHA";
91 pub const SHA_SHORT_NAME: &str = "VERGEN_SHA_SHORT";
92 pub const COMMIT_DATE_NAME: &str = "VERGEN_COMMIT_DATE";
93 pub const TARGET_TRIPLE_NAME: &str = "VERGEN_TARGET_TRIPLE";
94 pub const SEMVER_NAME: &str = "VERGEN_SEMVER";
95 pub const SEMVER_TAGS_NAME: &str = "VERGEN_SEMVER_LIGHTWEIGHT";
96 
97 #[cfg(test)]
98 mod test {
99   use super::*;
100 
101   #[test]
bitflags_dont_change()102   fn bitflags_dont_change() {
103     assert_eq!(ConstantsFlags::BUILD_TIMESTAMP.bits(), 0b0000_0001);
104     assert_eq!(ConstantsFlags::BUILD_DATE.bits(), 0b0000_0010);
105     assert_eq!(ConstantsFlags::SHA.bits(), 0b0000_0100);
106     assert_eq!(ConstantsFlags::SHA_SHORT.bits(), 0b0000_1000);
107     assert_eq!(ConstantsFlags::COMMIT_DATE.bits(), 0b0001_0000);
108     assert_eq!(ConstantsFlags::TARGET_TRIPLE.bits(), 0b0010_0000);
109     assert_eq!(ConstantsFlags::SEMVER.bits(), 0b0100_0000);
110     assert_eq!(ConstantsFlags::SEMVER_LIGHTWEIGHT.bits(), 0b1000_0000);
111     assert_eq!(
112       ConstantsFlags::REBUILD_ON_HEAD_CHANGE.bits(),
113       0b0001_0000_0000
114     );
115     assert_eq!(ConstantsFlags::SEMVER_FROM_CARGO_PKG.bits(), 0b0010_0000_0000);
116   }
117 
118   #[test]
constants_dont_change()119   fn constants_dont_change() {
120     assert_eq!(BUILD_TIMESTAMP_NAME, "VERGEN_BUILD_TIMESTAMP");
121     assert_eq!(BUILD_DATE_NAME, "VERGEN_BUILD_DATE");
122     assert_eq!(SHA_NAME, "VERGEN_SHA");
123     assert_eq!(SHA_SHORT_NAME, "VERGEN_SHA_SHORT");
124     assert_eq!(COMMIT_DATE_NAME, "VERGEN_COMMIT_DATE");
125     assert_eq!(TARGET_TRIPLE_NAME, "VERGEN_TARGET_TRIPLE");
126     assert_eq!(SEMVER_NAME, "VERGEN_SEMVER");
127     assert_eq!(SEMVER_TAGS_NAME, "VERGEN_SEMVER_LIGHTWEIGHT");
128   }
129 }
130