build_info_common/
lib.rs

1/*!
2Common types used by the `build-info` and `build-info-build` crates.
3
4User code should not depend on this crate directly, but rather depend on `build-info` (as a `[dependency]`) and `build-info-build` (as a `[build-dependency]`).
5The types provided herein are reexported by `build-info` and should be used that way.
6For example, `build_info_common::BuildInfo` should be used as `build_info::BuildInfo` instead.
7*/
8
9#![forbid(unsafe_code)]
10
11pub use chrono;
12use chrono::{DateTime, NaiveDate, Utc};
13use derive_more::Display;
14pub use semver;
15use semver::Version;
16#[cfg(feature = "serde")]
17use serde::{Deserialize, Serialize};
18
19#[cfg(feature = "serde")]
20mod versioned_string;
21#[cfg(feature = "serde")]
22pub use versioned_string::VersionedString;
23
24mod display;
25
26/// Gets the version of the `build-info-common` crate (this crate)
27pub fn crate_version() -> Version {
28	Version::parse(env!("CARGO_PKG_VERSION")).unwrap()
29}
30
31/// Information about the current build
32#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
33#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
34pub struct BuildInfo {
35	/// Updated whenever `build.rs` is rerun.
36	pub timestamp: DateTime<Utc>,
37
38	/// Cargo currently supports two different build types: `"Release"` and `"Debug"`
39	pub profile: String,
40
41	/// The optimization level can be set in `Cargo.toml` for each profile
42	pub optimization_level: OptimizationLevel,
43
44	/// Information about the current crate
45	pub crate_info: CrateInfo,
46
47	/// Information about the target system
48	pub target: TargetInfo,
49
50	/// Information about the compiler used
51	pub compiler: CompilerInfo,
52
53	/// `Some` if the project is inside a check-out of a supported version control system
54	pub version_control: Option<VersionControl>,
55}
56
57/// The various possible optimization levels
58#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
59#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
60pub enum OptimizationLevel {
61	O0,
62	O1,
63	O2,
64	O3,
65	Os,
66	Oz,
67}
68
69/// Information about the current crate (i.e., the crate for which build information has been generated)
70#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
71#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
72pub struct CrateInfo {
73	/// The name, as defined in `Cargo.toml`.
74	pub name: String,
75
76	/// The version, as defined in `Cargo.toml`.
77	pub version: Version,
78
79	/// The authors, as defined in `Cargo.toml`.
80	pub authors: Vec<String>,
81
82	/// The license string, as defined in `Cargo.toml`.
83	pub license: Option<String>,
84
85	/// The features of this crate that are currently enabled in this configuration.
86	pub enabled_features: Vec<String>,
87
88	/// All features that are available from this crate.
89	pub available_features: Vec<String>,
90
91	/// Dependencies of this crate.
92	/// Will only be filled with data if `collect_dependencies(true)` was called on `build_script()`.
93	pub dependencies: Vec<CrateInfo>,
94}
95
96#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
97#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
98pub struct TargetInfo {
99	/// Identifies the target architecture for which the crate is being compiled
100	pub triple: String,
101	/// A generic description of the target, e.g., `"unix"` or `"wasm"`
102	pub family: String,
103	/// The target OS
104	pub os: String,
105	/// The target CPU
106	pub cpu: CpuInfo,
107}
108
109#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
110#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
111pub struct CpuInfo {
112	/// The CPU target architecture
113	pub arch: String,
114	/// The CPU pointer width
115	pub pointer_width: u64,
116	/// The CPU target endianness
117	pub endianness: Endianness,
118	///  List of CPU target features enabled
119	pub features: Vec<String>,
120}
121
122/// CPU Endianness
123#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
124#[derive(Display, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
125pub enum Endianness {
126	Big,
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/**
177Information about a git repository
178
179If a git repository is detected (and, thereby, this information included), the build script will be rerun whenever the
180currently checked out commit changes.
181*/
182#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
183#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
184pub struct GitInfo {
185	/// Full commit hash for the currently checked out commit
186	pub commit_id: String,
187
188	/// Short commit hash for the currently checked out commit
189	///
190	/// The length of this string depends on the effective value of the git configuration variable `core.abbrev`, and is
191	/// extended to the minimum length required for the id to be unique (at the time it was computed).
192	pub commit_short_id: String,
193
194	/// Timestamp of the currently checked out commit
195	pub commit_timestamp: DateTime<Utc>,
196
197	/// `true` iff the repository had uncommitted changes when building the project.
198	pub dirty: bool,
199
200	/// Names the branch that is currently checked out, if any
201	pub branch: Option<String>,
202
203	/// All tags that point to the current commit (e.g., `["v0.0.10", "sample@v0.0.10"]`)
204	pub tags: Vec<String>,
205}