build_info_common/lib.rs
1//! Common types used by the `build-info` and `build-info-build` crates.
2//!
3//! User code should not depend on this crate directly, but rather depend on `build-info` (as a `[dependency]`) and
4//! `build-info-build` (as a `[build-dependency]`). The types provided herein are reexported by `build-info` and should
5//! be used that way. For example, `build_info_common::BuildInfo` should be used as `build_info::BuildInfo` instead.
6
7#![forbid(unsafe_code)]
8
9pub use chrono;
10use chrono::{DateTime, NaiveDate, Utc};
11use derive_more::Display;
12pub use semver;
13use semver::Version;
14#[cfg(feature = "serde")]
15use serde::{Deserialize, Serialize};
16
17#[cfg(feature = "serde")]
18mod versioned_string;
19#[cfg(feature = "serde")]
20pub use versioned_string::VersionedString;
21
22mod display;
23
24/// Gets the version of the `build-info-common` crate (this crate)
25pub fn crate_version() -> Version {
26 Version::parse(env!("CARGO_PKG_VERSION")).unwrap()
27}
28
29/// Information about the current build
30#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
32pub struct BuildInfo {
33 /// Updated whenever `build.rs` is rerun.
34 pub timestamp: DateTime<Utc>,
35
36 /// Cargo currently supports two different build types: `"Release"` and `"Debug"`
37 pub profile: String,
38
39 /// The optimization level can be set in `Cargo.toml` for each profile
40 pub optimization_level: OptimizationLevel,
41
42 /// Information about the current crate
43 pub crate_info: CrateInfo,
44
45 /// Information about the target system
46 pub target: TargetInfo,
47
48 /// Information about the compiler used
49 pub compiler: CompilerInfo,
50
51 /// `Some` if the project is inside a check-out of a supported version control system
52 pub version_control: Option<VersionControl>,
53}
54
55/// The various possible optimization levels
56#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
57#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
58pub enum OptimizationLevel {
59 O0,
60 O1,
61 O2,
62 O3,
63 Os,
64 Oz,
65}
66
67/// Information about the current crate (i.e., the crate for which build information has been generated)
68#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
69#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
70pub struct CrateInfo {
71 /// The name, as defined in `Cargo.toml`.
72 pub name: String,
73
74 /// The version, as defined in `Cargo.toml`.
75 pub version: Version,
76
77 /// The authors, as defined in `Cargo.toml`.
78 pub authors: Vec<String>,
79
80 /// The license string, as defined in `Cargo.toml`.
81 pub license: Option<String>,
82
83 /// The features of this crate that are currently enabled in this configuration.
84 pub enabled_features: Vec<String>,
85
86 /// All features that are available from this crate.
87 pub available_features: Vec<String>,
88
89 /// Dependencies of this crate.
90 /// Will only be filled with data if `collect_dependencies(true)` was called on `build_script()`.
91 pub dependencies: Vec<CrateInfo>,
92}
93
94#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
95#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
96pub struct TargetInfo {
97 /// Identifies the target architecture for which the crate is being compiled
98 pub triple: String,
99 /// A generic description of the target, e.g., `"unix"` or `"wasm"`
100 pub family: String,
101 /// The target OS
102 pub os: String,
103 /// The target CPU
104 pub cpu: CpuInfo,
105}
106
107#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
108#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
109pub struct CpuInfo {
110 /// The CPU target architecture
111 pub arch: String,
112 /// The CPU pointer width
113 pub pointer_width: u64,
114 /// The CPU target endianness
115 pub endianness: Endianness,
116 /// List of CPU target features enabled
117 pub features: Vec<String>,
118}
119
120/// CPU Endianness
121#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
122#[derive(Display, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
123pub enum Endianness {
124 /// Big endian CPUs store numbers least significant byte first. E.g., `0xAB` is stored as `[0xA, 0xB]`.
125 Big,
126 /// Big endian CPUs store numbers most significant byte first. E.g., `0xAB` is stored as `[0xB, 0xA]`.
127 Little,
128}
129
130/// `rustc` version and configuration
131#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
132#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
133pub struct CompilerInfo {
134 /// Version of the current `rustc`
135 pub version: Version,
136
137 /// Commit hash from which `rustc` was built
138 pub commit_id: Option<String>,
139
140 /// Date on which `rustc` was built
141 pub commit_date: Option<NaiveDate>,
142
143 /// Channel which was configured for this version of `rustc`
144 pub channel: CompilerChannel,
145
146 /// Identifies the host on which `rustc` was running
147 pub host_triple: String,
148}
149
150/// `rustc` distribution channel (some compiler features are only available on specific channels)
151#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
152#[derive(Display, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
153pub enum CompilerChannel {
154 Dev,
155 Nightly,
156 Beta,
157 Stable,
158}
159
160/// Support for different version control systems
161#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
162#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
163pub enum VersionControl {
164 Git(GitInfo),
165}
166
167impl VersionControl {
168 pub fn git(&self) -> Option<&GitInfo> {
169 match self {
170 VersionControl::Git(git) => Some(git),
171 // _ => None, // Pattern currently unreachable
172 }
173 }
174}
175
176/// Information about a git repository
177///
178/// If a git repository is detected (and, thereby, this information included), the build script will be rerun whenever
179/// the currently checked out commit changes.
180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
181#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
182pub struct GitInfo {
183 /// Full commit hash for the currently checked out commit
184 pub commit_id: String,
185
186 /// Short commit hash for the currently checked out commit
187 ///
188 /// The length of this string depends on the effective value of the git configuration variable `core.abbrev`, and is
189 /// extended to the minimum length required for the id to be unique (at the time it was computed).
190 pub commit_short_id: String,
191
192 /// Timestamp of the currently checked out commit
193 pub commit_timestamp: DateTime<Utc>,
194
195 /// `true` iff the repository had uncommitted changes when building the project.
196 pub dirty: bool,
197
198 /// Names the branch that is currently checked out, if any
199 pub branch: Option<String>,
200
201 /// All tags that point to the current commit (e.g., `["v0.0.10", "sample@v0.0.10"]`)
202 pub tags: Vec<String>,
203}