| 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 |
use serde_core::__private$$ as serde_core_private; |
| 14 |
"; |
| 15 |
|
| 16 |
// The rustc-cfg strings below are *not* public API. Please let us know by |
| 17 |
// opening a GitHub issue if your build environment requires some way to enable |
| 18 |
// these cfgs other than by executing our build script. |
| 19 |
fn main() { |
| 20 |
println!("cargo:rerun-if-changed=build.rs"); |
| 21 |
|
| 22 |
println!("cargo:rustc-cfg=if_docsrs_then_no_serde_core"); |
| 23 |
|
| 24 |
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
| 25 |
let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap(); |
| 26 |
let module = PRIVATE.replace("$$", &patch_version); |
| 27 |
fs::write(out_dir.join("private.rs"), module).unwrap(); |
| 28 |
|
| 29 |
let minor = match rustc_minor_version() { |
| 30 |
Some(minor) => minor, |
| 31 |
None => return, |
| 32 |
}; |
| 33 |
|
| 34 |
if minor >= 77 { |
| 35 |
println!("cargo:rustc-check-cfg=cfg(feature, values(\"result\"))"); |
| 36 |
println!("cargo:rustc-check-cfg=cfg(if_docsrs_then_no_serde_core)"); |
| 37 |
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)"); |
| 38 |
println!("cargo:rustc-check-cfg=cfg(no_core_error)"); |
| 39 |
println!("cargo:rustc-check-cfg=cfg(no_core_net)"); |
| 40 |
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)"); |
| 41 |
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)"); |
| 42 |
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)"); |
| 43 |
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)"); |
| 44 |
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)"); |
| 45 |
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)"); |
| 46 |
} |
| 47 |
|
| 48 |
// Current minimum supported version of serde_derive crate is Rust 1.71. |
| 49 |
if minor < 71 { |
| 50 |
println!("cargo:rustc-cfg=no_serde_derive"); |
| 51 |
} |
| 52 |
|
| 53 |
// Support for the `#[diagnostic]` tool attribute namespace |
| 54 |
// https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes |
| 55 |
if minor < 78 { |
| 56 |
println!("cargo:rustc-cfg=no_diagnostic_namespace"); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
fn rustc_minor_version() -> Option<u32> { |
| 61 |
let rustc = env::var_os("RUSTC")?; |
| 62 |
let output = Command::new(rustc).arg("--version").output().ok()?; |
| 63 |
let version = str::from_utf8(&output.stdout).ok()?; |
| 64 |
let mut pieces = version.split('.'); |
| 65 |
if pieces.next() != Some("rustc 1") { |
| 66 |
return None; |
| 67 |
} |
| 68 |
pieces.next()?.parse().ok() |
| 69 |
} |