blob: a1103b520a56e3f47ef7f6b58cb344c986836e58 [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
David LeGared1bc1012022-03-02 16:21:24 +000020 if minor < 26 {
21 println!("cargo:rustc-cfg=no_ops_bound");
22 if minor < 17 {
23 println!("cargo:rustc-cfg=no_collections_bound");
24 }
Yi Konge94f3922020-08-31 01:24:17 +080025 }
26
27 // core::cmp::Reverse stabilized in Rust 1.19:
28 // https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html
David LeGared1bc1012022-03-02 16:21:24 +000029 if minor < 19 {
30 println!("cargo:rustc-cfg=no_core_reverse");
Yi Konge94f3922020-08-31 01:24:17 +080031 }
32
33 // CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
34 // https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
35 // https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
David LeGared1bc1012022-03-02 16:21:24 +000036 if minor < 20 {
37 println!("cargo:rustc-cfg=no_de_boxed_c_str");
38 println!("cargo:rustc-cfg=no_de_boxed_path");
Yi Konge94f3922020-08-31 01:24:17 +080039 }
40
41 // From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21:
42 // https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>>
43 // https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>>
David LeGared1bc1012022-03-02 16:21:24 +000044 if minor < 21 {
45 println!("cargo:rustc-cfg=no_de_rc_dst");
Yi Konge94f3922020-08-31 01:24:17 +080046 }
47
48 // Duration available in core since Rust 1.25:
49 // https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations
David LeGared1bc1012022-03-02 16:21:24 +000050 if minor < 25 {
51 println!("cargo:rustc-cfg=no_core_duration");
Yi Konge94f3922020-08-31 01:24:17 +080052 }
53
54 // 128-bit integers stabilized in Rust 1.26:
55 // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
56 //
Joel Galensonda48ce92021-09-22 11:17:22 -070057 // Disabled on Emscripten targets before Rust 1.40 since
58 // Emscripten did not support 128-bit integers until Rust 1.40
59 // (https://github.com/rust-lang/rust/pull/65251)
David LeGared1bc1012022-03-02 16:21:24 +000060 if minor < 26 || emscripten && minor < 40 {
61 println!("cargo:rustc-cfg=no_integer128");
Yi Konge94f3922020-08-31 01:24:17 +080062 }
63
64 // Inclusive ranges methods stabilized in Rust 1.27:
65 // https://github.com/rust-lang/rust/pull/50758
Joel Galensonda48ce92021-09-22 11:17:22 -070066 // Also Iterator::try_for_each:
67 // https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#library-stabilizations
David LeGared1bc1012022-03-02 16:21:24 +000068 if minor < 27 {
69 println!("cargo:rustc-cfg=no_range_inclusive");
70 println!("cargo:rustc-cfg=no_iterator_try_fold");
Yi Konge94f3922020-08-31 01:24:17 +080071 }
72
73 // Non-zero integers stabilized in Rust 1.28:
74 // https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#library-stabilizations
David LeGared1bc1012022-03-02 16:21:24 +000075 if minor < 28 {
76 println!("cargo:rustc-cfg=no_num_nonzero");
Yi Konge94f3922020-08-31 01:24:17 +080077 }
78
Haibo Huangb3085842021-02-09 17:58:09 -080079 // Current minimum supported version of serde_derive crate is Rust 1.31.
David LeGared1bc1012022-03-02 16:21:24 +000080 if minor < 31 {
81 println!("cargo:rustc-cfg=no_serde_derive");
Haibo Huangb3085842021-02-09 17:58:09 -080082 }
83
Joel Galensonb32e2722021-04-13 10:05:58 -070084 // TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
85 // stabilized in Rust 1.34:
Yi Konge94f3922020-08-31 01:24:17 +080086 // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
87 // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
David LeGared1bc1012022-03-02 16:21:24 +000088 if minor < 34 {
89 println!("cargo:rustc-cfg=no_core_try_from");
90 println!("cargo:rustc-cfg=no_num_nonzero_signed");
91 println!("cargo:rustc-cfg=no_systemtime_checked_add");
92 }
Yi Konge94f3922020-08-31 01:24:17 +080093
David LeGared1bc1012022-03-02 16:21:24 +000094 // Whitelist of archs that support std::sync::atomic module. Ideally we
95 // would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
96 // Instead this is based on rustc's compiler/rustc_target/src/spec/*.rs.
97 let has_atomic64 = target.starts_with("x86_64")
98 || target.starts_with("i686")
99 || target.starts_with("aarch64")
100 || target.starts_with("powerpc64")
101 || target.starts_with("sparc64")
102 || target.starts_with("mips64el")
103 || target.starts_with("riscv64");
104 let has_atomic32 = has_atomic64 || emscripten;
105 if minor < 34 || !has_atomic64 {
106 println!("cargo:rustc-cfg=no_std_atomic64");
107 }
108 if minor < 34 || !has_atomic32 {
109 println!("cargo:rustc-cfg=no_std_atomic");
Yi Konge94f3922020-08-31 01:24:17 +0800110 }
111}
112
113fn rustc_minor_version() -> Option<u32> {
114 let rustc = match env::var_os("RUSTC") {
115 Some(rustc) => rustc,
116 None => return None,
117 };
118
119 let output = match Command::new(rustc).arg("--version").output() {
120 Ok(output) => output,
121 Err(_) => return None,
122 };
123
124 let version = match str::from_utf8(&output.stdout) {
125 Ok(version) => version,
126 Err(_) => return None,
127 };
128
129 let mut pieces = version.split('.');
130 if pieces.next() != Some("rustc 1") {
131 return None;
132 }
133
134 let next = match pieces.next() {
135 Some(next) => next,
136 None => return None,
137 };
138
139 u32::from_str(next).ok()
140}