1 use std::env;
2 use std::process::Command;
3 use std::str::{self, FromStr};
4
5 // The rustc-cfg strings below are *not* public API. Please let us know by
6 // opening a GitHub issue if your build environment requires some way to enable
7 // these cfgs other than by executing our build script.
main()8 fn main() {
9 println!("cargo:rerun-if-changed=build.rs");
10
11 let minor = match rustc_minor_version() {
12 Some(minor) => minor,
13 None => return,
14 };
15
16 // #[track_caller] stabilized in Rust 1.46:
17 // https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html#track_caller
18 if minor < 46 {
19 println!("cargo:rustc-cfg=no_track_caller");
20 }
21 }
22
rustc_minor_version() -> Option<u32>23 fn rustc_minor_version() -> Option<u32> {
24 let rustc = match env::var_os("RUSTC") {
25 Some(rustc) => rustc,
26 None => return None,
27 };
28
29 let output = match Command::new(rustc).arg("--version").output() {
30 Ok(output) => output,
31 Err(_) => return None,
32 };
33
34 let version = match str::from_utf8(&output.stdout) {
35 Ok(version) => version,
36 Err(_) => return None,
37 };
38
39 let mut pieces = version.split('.');
40 if pieces.next() != Some("rustc 1") {
41 return None;
42 }
43
44 let next = match pieces.next() {
45 Some(next) => next,
46 None => return None,
47 };
48
49 u32::from_str(next).ok()
50 }
51