1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
include!(concat!(env!("OUT_DIR"), "/build_env.rs"));
#[derive(Debug)]
pub struct BuildMetadata {
pub rust_version: &'static str,
pub core_pkg_version: &'static str,
pub os_family: OsFamily,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum OsFamily {
Windows,
Linux,
Macos,
Android,
Ios,
Other,
}
macro_rules! get_os_family {
($i:ident : $(($s:expr, $t:expr)),+) => (
{ const fn __getcfg() -> OsFamily { $( if cfg!($i=$s) { return $t; } );+ OsFamily::Other } __getcfg() }
)
}
impl OsFamily {
pub const fn from_env() -> Self {
get_os_family!(target_os:
("windows", OsFamily::Windows),
("macos", OsFamily::Macos),
("ios", OsFamily::Ios),
("linux", OsFamily::Linux),
("android", OsFamily::Android)
)
}
}
impl From<&str> for OsFamily {
fn from(s: &str) -> Self {
match s {
"windows" => OsFamily::Windows,
"macos" => OsFamily::Macos,
"ios" => OsFamily::Ios,
"linux" => OsFamily::Linux,
"android" => OsFamily::Android,
_ => OsFamily::Other,
}
}
}
pub const BUILD_METADATA: BuildMetadata = BuildMetadata {
rust_version: RUST_VERSION,
core_pkg_version: env!("CARGO_PKG_VERSION"),
os_family: OsFamily::from_env(),
};
#[cfg(test)]
mod test {
use crate::build_metadata::{OsFamily, BUILD_METADATA};
#[test]
fn valid_build_metadata() {
let meta = &BUILD_METADATA;
assert!(meta.rust_version.starts_with("1."));
assert!(meta.core_pkg_version.starts_with("0."));
if cfg!(target_os = "linux") {
assert_eq!(meta.os_family, OsFamily::Linux);
}
if cfg!(target_os = "macos") {
assert_eq!(meta.os_family, OsFamily::Macos);
}
}
}