• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     let target = env::var("TARGET").unwrap();
17     let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
18 
19     // std::collections::Bound was stabilized in Rust 1.17
20     // but it was moved to core::ops later in Rust 1.26:
21     // https://doc.rust-lang.org/core/ops/enum.Bound.html
22     if minor < 26 {
23         println!("cargo:rustc-cfg=no_ops_bound");
24         if minor < 17 {
25             println!("cargo:rustc-cfg=no_collections_bound");
26         }
27     }
28 
29     // core::cmp::Reverse stabilized in Rust 1.19:
30     // https://doc.rust-lang.org/stable/core/cmp/struct.Reverse.html
31     if minor < 19 {
32         println!("cargo:rustc-cfg=no_core_reverse");
33     }
34 
35     // CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
36     // https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
37     // https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
38     if minor < 20 {
39         println!("cargo:rustc-cfg=no_de_boxed_c_str");
40         println!("cargo:rustc-cfg=no_de_boxed_path");
41     }
42 
43     // From<Box<T>> for Rc<T> / Arc<T> stabilized in Rust 1.21:
44     // https://doc.rust-lang.org/std/rc/struct.Rc.html#impl-From<Box<T>>
45     // https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<Box<T>>
46     if minor < 21 {
47         println!("cargo:rustc-cfg=no_de_rc_dst");
48     }
49 
50     // Duration available in core since Rust 1.25:
51     // https://blog.rust-lang.org/2018/03/29/Rust-1.25.html#library-stabilizations
52     if minor < 25 {
53         println!("cargo:rustc-cfg=no_core_duration");
54     }
55 
56     // 128-bit integers stabilized in Rust 1.26:
57     // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html
58     //
59     // Disabled on Emscripten targets before Rust 1.40 since
60     // Emscripten did not support 128-bit integers until Rust 1.40
61     // (https://github.com/rust-lang/rust/pull/65251)
62     if minor < 26 || emscripten && minor < 40 {
63         println!("cargo:rustc-cfg=no_integer128");
64     }
65 
66     // Inclusive ranges methods stabilized in Rust 1.27:
67     // https://github.com/rust-lang/rust/pull/50758
68     // Also Iterator::try_for_each:
69     // https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#library-stabilizations
70     if minor < 27 {
71         println!("cargo:rustc-cfg=no_range_inclusive");
72         println!("cargo:rustc-cfg=no_iterator_try_fold");
73     }
74 
75     // Non-zero integers stabilized in Rust 1.28:
76     // https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#library-stabilizations
77     if minor < 28 {
78         println!("cargo:rustc-cfg=no_num_nonzero");
79     }
80 
81     // TryFrom, Atomic types, non-zero signed integers, and SystemTime::checked_add
82     // stabilized in Rust 1.34:
83     // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#tryfrom-and-tryinto
84     // https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
85     if minor < 34 {
86         println!("cargo:rustc-cfg=no_core_try_from");
87         println!("cargo:rustc-cfg=no_num_nonzero_signed");
88         println!("cargo:rustc-cfg=no_systemtime_checked_add");
89         println!("cargo:rustc-cfg=no_relaxed_trait_bounds");
90     }
91 
92     // Current minimum supported version of serde_derive crate is Rust 1.56.
93     if minor < 56 {
94         println!("cargo:rustc-cfg=no_serde_derive");
95     }
96 
97     // Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60.
98     if minor < 60 {
99         println!("cargo:rustc-cfg=no_target_has_atomic");
100         // Allowlist of archs that support std::sync::atomic module. This is
101         // based on rustc's compiler/rustc_target/src/spec/*.rs.
102         let has_atomic64 = target.starts_with("x86_64")
103             || target.starts_with("i686")
104             || target.starts_with("aarch64")
105             || target.starts_with("powerpc64")
106             || target.starts_with("sparc64")
107             || target.starts_with("mips64el")
108             || target.starts_with("riscv64");
109         let has_atomic32 = has_atomic64 || emscripten;
110         if minor < 34 || !has_atomic64 {
111             println!("cargo:rustc-cfg=no_std_atomic64");
112         }
113         if minor < 34 || !has_atomic32 {
114             println!("cargo:rustc-cfg=no_std_atomic");
115         }
116     }
117 
118     // Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64.
119     // https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#c-compatible-ffi-types-in-core-and-alloc
120     if minor < 64 {
121         println!("cargo:rustc-cfg=no_core_cstr");
122     }
123 }
124 
rustc_minor_version() -> Option<u32>125 fn rustc_minor_version() -> Option<u32> {
126     let rustc = match env::var_os("RUSTC") {
127         Some(rustc) => rustc,
128         None => return None,
129     };
130 
131     let output = match Command::new(rustc).arg("--version").output() {
132         Ok(output) => output,
133         Err(_) => return None,
134     };
135 
136     let version = match str::from_utf8(&output.stdout) {
137         Ok(version) => version,
138         Err(_) => return None,
139     };
140 
141     let mut pieces = version.split('.');
142     if pieces.next() != Some("rustc 1") {
143         return None;
144     }
145 
146     let next = match pieces.next() {
147         Some(next) => next,
148         None => return None,
149     };
150 
151     u32::from_str(next).ok()
152 }
153