Skip to content
Myr

build.rs

This file

Path

serde/build.rs

69 lines · 2.4 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}13use serde_core::__private$$ as serde_core_private;14";1516// The rustc-cfg strings below are *not* public API. Please let us know by17// opening a GitHub issue if your build environment requires some way to enable18// these cfgs other than by executing our build script.19fn main() {20    println!("cargo:rerun-if-changed=build.rs");2122    println!("cargo:rustc-cfg=if_docsrs_then_no_serde_core");2324    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();2829    let minor = match rustc_minor_version() {30        Some(minor) => minor,31        None => return,32    };3334    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    }4748    // 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    }5253    // Support for the `#[diagnostic]` tool attribute namespace54    // https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes55    if minor < 78 {56        println!("cargo:rustc-cfg=no_diagnostic_namespace");57    }58}5960fn 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}