blob: ca991a5d21a8e0bb24328daa13608899f24adf82 [file] [log] [blame]
Yi Konge94f3922020-08-31 01:24:17 +08001use std::env;
2use std::process::Command;
3use 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.
8fn main() {
9 let minor = match rustc_minor_version() {
10 Some(minor) => minor,
11 None => return,
12 };
13
14 let target = env::var("TARGET").unwrap();
15 let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
16
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
32 // CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
33 // https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
34 // https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
35 if minor >= 20 {
36 println!("cargo:rustc-cfg=de_boxed_c_str");
37 println!("cargo:rustc-cfg=de_boxed_path");
38 }
39
40 // From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21:
41 // https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>>
42 // https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>>
43 if minor >= 21 {
44 println!("cargo:rustc-cfg=de_rc_dst");
45 }
46
47 // Duration available in core since Rust 1.25:
48 // https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations
49 if minor >= 25 {
50 println!("cargo:rustc-cfg=core_duration");
51 }
52
53 // 128-bit integers stabilized in Rust 1.26:
54 // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
55 //
56 // Disabled on Emscripten targets as Emscripten doesn't
57 // currently support integers larger than 64 bits.
58 if minor >= 26 && !emscripten {
59 println!("cargo:rustc-cfg=integer128");
60 }
61
62 // Inclusive ranges methods stabilized in Rust 1.27:
63 // https://github.com/rust-lang/rust/pull/50758
64 if minor >= 27 {
65 println!("cargo:rustc-cfg=range_inclusive");
66 }
67
68 // Non-zero integers stabilized in Rust 1.28:
69 // https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#library-stabilizations
70 if minor >= 28 {
71 println!("cargo:rustc-cfg=num_nonzero");
72 }
73
Haibo Huangb3085842021-02-09 17:58:09 -080074 // Current minimum supported version of serde_derive crate is Rust 1.31.
75 if minor >= 31 {
76 println!("cargo:rustc-cfg=serde_derive");
77 }
78
Yi Konge94f3922020-08-31 01:24:17 +080079 // TryFrom, Atomic types, and non-zero signed integers stabilized in Rust 1.34:
80 // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
81 // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
82 if minor >= 34 {
83 println!("cargo:rustc-cfg=core_try_from");
84 println!("cargo:rustc-cfg=num_nonzero_signed");
85
86 // Whitelist of archs that support std::sync::atomic module. Ideally we
87 // would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
88 // Instead this is based on rustc's src/librustc_target/spec/*.rs.
89 let has_atomic64 = target.starts_with("x86_64")
90 || target.starts_with("i686")
91 || target.starts_with("aarch64")
92 || target.starts_with("powerpc64")
93 || target.starts_with("sparc64")
94 || target.starts_with("mips64el");
95 let has_atomic32 = has_atomic64 || emscripten;
96 if has_atomic64 {
97 println!("cargo:rustc-cfg=std_atomic64");
98 }
99 if has_atomic32 {
100 println!("cargo:rustc-cfg=std_atomic");
101 }
102 }
103}
104
105fn rustc_minor_version() -> Option<u32> {
106 let rustc = match env::var_os("RUSTC") {
107 Some(rustc) => rustc,
108 None => return None,
109 };
110
111 let output = match Command::new(rustc).arg("--version").output() {
112 Ok(output) => output,
113 Err(_) => return None,
114 };
115
116 let version = match str::from_utf8(&output.stdout) {
117 Ok(version) => version,
118 Err(_) => return None,
119 };
120
121 let mut pieces = version.split('.');
122 if pieces.next() != Some("rustc 1") {
123 return None;
124 }
125
126 let next = match pieces.next() {
127 Some(next) => next,
128 None => return None,
129 };
130
131 u32::from_str(next).ok()
132}