Skip to content
Myr

build.rs

This file

Path

serde_core/build.rs

113 lines · 4.2 KiB · Rust

Last changed by

Raise required compiler for serde_derive to Rust 1.71David Tolnaybefore adoption681270e

History of this file

Take it

1use std::env;2use std::fs;3use std::path::PathBuf;4use std::process::Command;5use std::str;67const PRIVATE: &str = "\8#[doc(hidden)]9pub mod __private$$ {10    #[doc(hidden)]11    pub use crate::private::*;12}13";1415// The rustc-cfg strings below are *not* public API. Please let us know by16// opening a GitHub issue if your build environment requires some way to enable17// these cfgs other than by executing our build script.18fn main() {19    println!("cargo:rerun-if-changed=build.rs");2021    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();2526    let minor = match rustc_minor_version() {27        Some(minor) => minor,28        None => return,29    };3031    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    }4344    let target = env::var("TARGET").unwrap();45    let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";4647    // 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 is51        // 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    }6768    // 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-alloc70    if minor < 64 {71        println!("cargo:rustc-cfg=no_core_cstr");72    }7374    // 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    }7879    // Support for core::num::Saturating and std::num::Saturating stabilized in Rust 1.7480    // https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html#stabilized-apis81    if minor < 74 {82        println!("cargo:rustc-cfg=no_core_num_saturating");83    }8485    // Support for core::net stabilized in Rust 1.77.86    // https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html87    if minor < 77 {88        println!("cargo:rustc-cfg=no_core_net");89    }9091    // Support for the `#[diagnostic]` tool attribute namespace92    // https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes93    if minor < 78 {94        println!("cargo:rustc-cfg=no_diagnostic_namespace");95    }9697    // The Error trait became available in core in 1.81.98    // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror99    if minor < 81 {100        println!("cargo:rustc-cfg=no_core_error");101    }102}103104fn 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}