forked from getsentry/sentry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
67 lines (60 loc) · 1.93 KB
/
build.rs
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
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use rustc_version::{version, version_meta, Channel};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = env::var("OUT_DIR")?;
let dest_path = Path::new(&out_dir).join("constants.gen.rs");
let mut f = File::create(&dest_path)?;
let target = env::var("TARGET")?;
let mut target_bits = target.split('-');
// `NoneError` does not implement `std::error::Error`
// See https://github.com/rust-lang/rust/issues/46871
let arch = target_bits.next().unwrap();
target_bits.next();
let platform = target_bits.next().unwrap();
writeln!(
f,
"/// The rustc version that was used to compile this crate."
)?;
writeln!(
f,
"pub const RUSTC_VERSION: Option<&'static str> = {};",
if let Ok(version) = version() {
format!("Some(\"{}\")", version)
} else {
"None".into()
}
)?;
writeln!(
f,
"/// The rustc release channel that was used to compile this crate."
)?;
writeln!(
f,
"pub const RUSTC_CHANNEL: Option<&'static str> = {};",
if let Ok(version_meta) = version_meta() {
let chan = match version_meta.channel {
Channel::Dev => "dev",
Channel::Nightly => "nightly",
Channel::Beta => "beta",
Channel::Stable => "stable",
};
format!("Some(\"{}\")", chan)
} else {
"None".into()
}
)?;
writeln!(f, "/// The platform identifier.")?;
writeln!(
f,
"#[allow(unused)] pub const PLATFORM: &str = \"{}\";",
platform
)?;
writeln!(f, "/// The CPU architecture identifier.")?;
writeln!(f, "pub const ARCH: &str = \"{}\";", arch)?;
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.toml");
Ok(())
}