]>
Commit | Line | Data |
---|---|---|
8faf50e0 XL |
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. | |
8 | fn main() { | |
9 | let minor = match rustc_minor_version() { | |
10 | Some(minor) => minor, | |
11 | None => return, | |
12 | }; | |
13 | ||
b7449926 XL |
14 | let target = env::var("TARGET").unwrap(); |
15 | let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; | |
16 | ||
dc9dc135 XL |
17 | // std::collections::Bound was stabilized in Rust 1.17 |
18 | // but it was moved to core::ops later in Rust 1.26: | |
19 | // https://doc.rust-lang.org/core/ops/enum.Bound.html | |
20 | if minor >= 26 { | |
21 | println!("cargo:rustc-cfg=ops_bound"); | |
22 | } else if minor >= 17 && cfg!(feature = "std") { | |
23 | println!("cargo:rustc-cfg=collections_bound"); | |
24 | } | |
25 | ||
26 | // core::cmp::Reverse stabilized in Rust 1.19: | |
27 | // https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html | |
28 | if minor >= 19 { | |
29 | println!("cargo:rustc-cfg=core_reverse"); | |
30 | } | |
31 | ||
8faf50e0 XL |
32 | // CString::into_boxed_c_str stabilized in Rust 1.20: |
33 | // https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str | |
34 | if minor >= 20 { | |
35 | println!("cargo:rustc-cfg=de_boxed_c_str"); | |
36 | } | |
37 | ||
38 | // From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21: | |
39 | // https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>> | |
40 | // https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>> | |
41 | if minor >= 21 { | |
42 | println!("cargo:rustc-cfg=de_rc_dst"); | |
43 | } | |
44 | ||
45 | // Duration available in core since Rust 1.25: | |
46 | // https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations | |
47 | if minor >= 25 { | |
48 | println!("cargo:rustc-cfg=core_duration"); | |
49 | } | |
50 | ||
51 | // 128-bit integers stabilized in Rust 1.26: | |
52 | // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html | |
b7449926 XL |
53 | // |
54 | // Disabled on Emscripten targets as Emscripten doesn't | |
55 | // currently support integers larger than 64 bits. | |
56 | if minor >= 26 && !emscripten { | |
8faf50e0 XL |
57 | println!("cargo:rustc-cfg=integer128"); |
58 | } | |
59 | ||
b7449926 XL |
60 | // Inclusive ranges methods stabilized in Rust 1.27: |
61 | // https://github.com/rust-lang/rust/pull/50758 | |
62 | if minor >= 27 { | |
63 | println!("cargo:rustc-cfg=range_inclusive"); | |
64 | } | |
65 | ||
8faf50e0 XL |
66 | // Non-zero integers stabilized in Rust 1.28: |
67 | // https://github.com/rust-lang/rust/pull/50808 | |
68 | if minor >= 28 { | |
69 | println!("cargo:rustc-cfg=num_nonzero"); | |
70 | } | |
71 | } | |
72 | ||
73 | fn rustc_minor_version() -> Option<u32> { | |
74 | let rustc = match env::var_os("RUSTC") { | |
75 | Some(rustc) => rustc, | |
76 | None => return None, | |
77 | }; | |
78 | ||
79 | let output = match Command::new(rustc).arg("--version").output() { | |
80 | Ok(output) => output, | |
81 | Err(_) => return None, | |
82 | }; | |
83 | ||
84 | let version = match str::from_utf8(&output.stdout) { | |
85 | Ok(version) => version, | |
86 | Err(_) => return None, | |
87 | }; | |
88 | ||
8faf50e0 XL |
89 | let mut pieces = version.split('.'); |
90 | if pieces.next() != Some("rustc 1") { | |
91 | return None; | |
92 | } | |
93 | ||
94 | let next = match pieces.next() { | |
95 | Some(next) => next, | |
96 | None => return None, | |
97 | }; | |
98 | ||
99 | u32::from_str(next).ok() | |
100 | } |