| 1 |
use std::env; |
| 2 |
use std::process::Command; |
| 3 |
use std::str; |
| 4 |
|
| 5 |
fn main() { |
| 6 |
println!("cargo:rerun-if-changed=build.rs"); |
| 7 |
|
| 8 |
let compiler = match rustc_minor_version() { |
| 9 |
Some(compiler) => compiler, |
| 10 |
None => return, |
| 11 |
}; |
| 12 |
|
| 13 |
if compiler < 33 { |
| 14 |
// Exhaustive integer patterns. On older compilers, a final `_` arm is |
| 15 |
// required even if every possible integer value is otherwise covered. |
| 16 |
// https://github.com/rust-lang/rust/issues/50907 |
| 17 |
println!("cargo:rustc-cfg=no_exhaustive_int_match"); |
| 18 |
} |
| 19 |
|
| 20 |
if compiler < 36 { |
| 21 |
// extern crate alloc. |
| 22 |
// https://blog.rust-lang.org/2019/07/04/Rust-1.36.0.html#the-alloc-crate-is-stable |
| 23 |
println!("cargo:rustc-cfg=no_alloc_crate"); |
| 24 |
} |
| 25 |
|
| 26 |
if compiler < 39 { |
| 27 |
// const Vec::new. |
| 28 |
// https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new |
| 29 |
println!("cargo:rustc-cfg=no_const_vec_new"); |
| 30 |
} |
| 31 |
|
| 32 |
if compiler < 40 { |
| 33 |
// #[non_exhaustive]. |
| 34 |
// https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html#non_exhaustive-structs-enums-and-variants |
| 35 |
println!("cargo:rustc-cfg=no_non_exhaustive"); |
| 36 |
} |
| 37 |
|
| 38 |
if compiler < 45 { |
| 39 |
// String::strip_prefix. |
| 40 |
// https://doc.rust-lang.org/std/primitive.str.html#method.strip_prefix |
| 41 |
println!("cargo:rustc-cfg=no_str_strip_prefix"); |
| 42 |
} |
| 43 |
|
| 44 |
if compiler < 46 { |
| 45 |
// #[track_caller]. |
| 46 |
// https://blog.rust-lang.org/2020/08/27/Rust-1.46.0.html#track_caller |
| 47 |
println!("cargo:rustc-cfg=no_track_caller"); |
| 48 |
} |
| 49 |
|
| 50 |
if compiler < 52 { |
| 51 |
// #![deny(unsafe_op_in_unsafe_fn)]. |
| 52 |
// https://github.com/rust-lang/rust/issues/71668 |
| 53 |
println!("cargo:rustc-cfg=no_unsafe_op_in_unsafe_fn_lint"); |
| 54 |
} |
| 55 |
|
| 56 |
if compiler < 53 { |
| 57 |
// Efficient intrinsics for count-leading-zeros and count-trailing-zeros |
| 58 |
// on NonZero integers stabilized in 1.53.0. On many architectures these |
| 59 |
// are more efficient than counting zeros on ordinary zeroable integers. |
| 60 |
// https://doc.rust-lang.org/std/num/struct.NonZeroU64.html#method.leading_zeros |
| 61 |
// https://doc.rust-lang.org/std/num/struct.NonZeroU64.html#method.trailing_zeros |
| 62 |
println!("cargo:rustc-cfg=no_nonzero_bitscan"); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
fn rustc_minor_version() -> Option<u32> { |
| 67 |
let rustc = env::var_os("RUSTC")?; |
| 68 |
let output = Command::new(rustc).arg("--version").output().ok()?; |
| 69 |
let version = str::from_utf8(&output.stdout).ok()?; |
| 70 |
let mut pieces = version.split('.'); |
| 71 |
if pieces.next() != Some("rustc 1") { |
| 72 |
return None; |
| 73 |
} |
| 74 |
pieces.next()?.parse().ok() |
| 75 |
} |