| 1 |
use std::env; |
| 2 |
use std::fs; |
| 3 |
use std::path::PathBuf; |
| 4 |
use std::process::Command; |
| 5 |
use std::str; |
| 6 |
|
| 7 |
const PRIVATE: &str = "\ |
| 8 |
#[doc(hidden)] |
| 9 |
pub mod __private$$ { |
| 10 |
#[doc(hidden)] |
| 11 |
pub use crate::private::*; |
| 12 |
} |
| 13 |
"; |
| 14 |
|
| 15 |
// The rustc-cfg strings below are *not* public API. Please let us know by |
| 16 |
// opening a GitHub issue if your build environment requires some way to enable |
| 17 |
// these cfgs other than by executing our build script. |
| 18 |
fn main() { |
| 19 |
println!("cargo:rerun-if-changed=build.rs"); |
| 20 |
|
| 21 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 22 |
let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap(); |
| 23 |
let module = PRIVATE.replace("$$", &patch_version); |
| 24 |
fs::write(out_dir.join("private.rs"), module).unwrap(); |
| 25 |
|
| 26 |
let minor = match rustc_minor_version() { |
| 27 |
Some(minor) => minor, |
| 28 |
None => return, |
| 29 |
}; |
| 30 |
|
| 31 |
if minor >= 77 { |
| 32 |
println!("cargo:rustc-check-cfg=cfg(if_docsrs_then_no_serde_core)"); |
| 33 |
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)"); |
| 34 |
println!("cargo:rustc-check-cfg=cfg(no_core_error)"); |
| 35 |
println!("cargo:rustc-check-cfg=cfg(no_core_net)"); |
| 36 |
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)"); |
| 37 |
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)"); |
| 38 |
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)"); |
| 39 |
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)"); |
| 40 |
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)"); |
| 41 |
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)"); |
| 42 |
} |
| 43 |
|
| 44 |
let target = env::var("TARGET").unwrap(); |
| 45 |
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; |
| 46 |
|
| 47 |
// Support for #[cfg(target_has_atomic = "...")] stabilized in Rust 1.60. |
| 48 |
if minor < 60 { |
| 49 |
println!("cargo:rustc-cfg=no_target_has_atomic"); |
| 50 |
// Allowlist of archs that support std::sync::atomic module. This is |
| 51 |
// based on rustc's compiler/rustc_target/src/spec/*.rs. |
| 52 |
let has_atomic64 = target.starts_with("x86_64") |
| 53 |
|| target.starts_with("i686") |
| 54 |
|| target.starts_with("aarch64") |
| 55 |
|| target.starts_with("powerpc64") |
| 56 |
|| target.starts_with("sparc64") |
| 57 |
|| target.starts_with("mips64el") |
| 58 |
|| target.starts_with("riscv64"); |
| 59 |
let has_atomic32 = has_atomic64 || emscripten; |
| 60 |
if minor < 34 || !has_atomic64 { |
| 61 |
println!("cargo:rustc-cfg=no_std_atomic64"); |
| 62 |
} |
| 63 |
if minor < 34 || !has_atomic32 { |
| 64 |
println!("cargo:rustc-cfg=no_std_atomic"); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Support for core::ffi::CStr and alloc::ffi::CString stabilized in Rust 1.64. |
| 69 |
// https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#c-compatible-ffi-types-in-core-and-alloc |
| 70 |
if minor < 64 { |
| 71 |
println!("cargo:rustc-cfg=no_core_cstr"); |
| 72 |
} |
| 73 |
|
| 74 |
// Current minimum supported version of serde_derive crate is Rust 1.71. |
| 75 |
if minor < 71 { |
| 76 |
println!("cargo:rustc-cfg=no_serde_derive"); |
| 77 |
} |
| 78 |
|
| 79 |
// Support for core::num::Saturating and std::num::Saturating stabilized in Rust 1.74 |
| 80 |
// https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#stabilized-apis |
| 81 |
if minor < 74 { |
| 82 |
println!("cargo:rustc-cfg=no_core_num_saturating"); |
| 83 |
} |
| 84 |
|
| 85 |
// Support for core::net stabilized in Rust 1.77. |
| 86 |
// https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html |
| 87 |
if minor < 77 { |
| 88 |
println!("cargo:rustc-cfg=no_core_net"); |
| 89 |
} |
| 90 |
|
| 91 |
// Support for the `#[diagnostic]` tool attribute namespace |
| 92 |
// https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes |
| 93 |
if minor < 78 { |
| 94 |
println!("cargo:rustc-cfg=no_diagnostic_namespace"); |
| 95 |
} |
| 96 |
|
| 97 |
// The Error trait became available in core in 1.81. |
| 98 |
// https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror |
| 99 |
if minor < 81 { |
| 100 |
println!("cargo:rustc-cfg=no_core_error"); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
fn rustc_minor_version() -> Option<u32> { |
| 105 |
let rustc = env::var_os("RUSTC")?; |
| 106 |
let output = Command::new(rustc).arg("--version").output().ok()?; |
| 107 |
let version = str::from_utf8(&output.stdout).ok()?; |
| 108 |
let mut pieces = version.split('.'); |
| 109 |
if pieces.next() != Some("rustc 1") { |
| 110 |
return None; |
| 111 |
} |
| 112 |
pieces.next()?.parse().ok() |
| 113 |
} |