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