From 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:39 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/libc/build.rs | 108 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 84 insertions(+), 24 deletions(-) (limited to 'vendor/libc/build.rs') diff --git a/vendor/libc/build.rs b/vendor/libc/build.rs index bbee2d28a..e60672a76 100644 --- a/vendor/libc/build.rs +++ b/vendor/libc/build.rs @@ -2,6 +2,40 @@ use std::env; use std::process::Command; use std::str; +// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we +// need to know all the possible cfgs that this script will set. If you need to set another cfg +// make sure to add it to this list as well. +const ALLOWED_CFGS: &'static [&'static str] = &[ + "freebsd10", + "freebsd11", + "freebsd12", + "freebsd13", + "freebsd14", + "libc_align", + "libc_cfg_target_vendor", + "libc_const_extern_fn", + "libc_const_extern_fn_unstable", + "libc_const_size_of", + "libc_core_cvoid", + "libc_deny_warnings", + "libc_int128", + "libc_long_array", + "libc_non_exhaustive", + "libc_packedN", + "libc_priv_mod_use", + "libc_ptr_addr_of", + "libc_thread_local", + "libc_underscore_const_names", + "libc_union", +]; + +// Extra values to allow for check-cfg. +const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[ + ("target_os", &["switch", "aix", "ohos"]), + ("target_env", &["illumos", "wasi", "aix", "ohos"]), + ("target_arch", &["loongarch64"]), +]; + fn main() { // Avoid unnecessary re-building. println!("cargo:rerun-if-changed=build.rs"); @@ -11,6 +45,7 @@ fn main() { let align_cargo_feature = env::var("CARGO_FEATURE_ALIGN").is_ok(); let const_extern_fn_cargo_feature = env::var("CARGO_FEATURE_CONST_EXTERN_FN").is_ok(); let libc_ci = env::var("LIBC_CI").is_ok(); + let libc_check_cfg = env::var("LIBC_CHECK_CFG").is_ok(); if env::var("CARGO_FEATURE_USE_STD").is_ok() { println!( @@ -25,89 +60,107 @@ fn main() { // On CI, we detect the actual FreeBSD version and match its ABI exactly, // running tests to ensure that the ABI is correct. match which_freebsd() { - Some(10) if libc_ci || rustc_dep_of_std => { - println!("cargo:rustc-cfg=freebsd10") - } - Some(11) if libc_ci => println!("cargo:rustc-cfg=freebsd11"), - Some(12) if libc_ci => println!("cargo:rustc-cfg=freebsd12"), - Some(13) if libc_ci => println!("cargo:rustc-cfg=freebsd13"), - Some(14) if libc_ci => println!("cargo:rustc-cfg=freebsd14"), - Some(_) | None => println!("cargo:rustc-cfg=freebsd11"), + Some(10) if libc_ci || rustc_dep_of_std => set_cfg("freebsd10"), + Some(11) if libc_ci => set_cfg("freebsd11"), + Some(12) if libc_ci => set_cfg("freebsd12"), + Some(13) if libc_ci => set_cfg("freebsd13"), + Some(14) if libc_ci => set_cfg("freebsd14"), + Some(_) | None => set_cfg("freebsd11"), } // On CI: deny all warnings if libc_ci { - println!("cargo:rustc-cfg=libc_deny_warnings"); + set_cfg("libc_deny_warnings"); } // Rust >= 1.15 supports private module use: if rustc_minor_ver >= 15 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_priv_mod_use"); + set_cfg("libc_priv_mod_use"); } // Rust >= 1.19 supports unions: if rustc_minor_ver >= 19 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_union"); + set_cfg("libc_union"); } // Rust >= 1.24 supports const mem::size_of: if rustc_minor_ver >= 24 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_const_size_of"); + set_cfg("libc_const_size_of"); } // Rust >= 1.25 supports repr(align): if rustc_minor_ver >= 25 || rustc_dep_of_std || align_cargo_feature { - println!("cargo:rustc-cfg=libc_align"); + set_cfg("libc_align"); } // Rust >= 1.26 supports i128 and u128: if rustc_minor_ver >= 26 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_int128"); + set_cfg("libc_int128"); } // Rust >= 1.30 supports `core::ffi::c_void`, so libc can just re-export it. // Otherwise, it defines an incompatible type to retaining // backwards-compatibility. if rustc_minor_ver >= 30 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_core_cvoid"); + set_cfg("libc_core_cvoid"); } // Rust >= 1.33 supports repr(packed(N)) and cfg(target_vendor). if rustc_minor_ver >= 33 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_packedN"); - println!("cargo:rustc-cfg=libc_cfg_target_vendor"); + set_cfg("libc_packedN"); + set_cfg("libc_cfg_target_vendor"); } // Rust >= 1.40 supports #[non_exhaustive]. if rustc_minor_ver >= 40 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_non_exhaustive"); + set_cfg("libc_non_exhaustive"); + } + + // Rust >= 1.47 supports long array: + if rustc_minor_ver >= 47 || rustc_dep_of_std { + set_cfg("libc_long_array"); } if rustc_minor_ver >= 51 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_ptr_addr_of"); + set_cfg("libc_ptr_addr_of"); } // Rust >= 1.37.0 allows underscores as anonymous constant names. if rustc_minor_ver >= 37 || rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_underscore_const_names"); + set_cfg("libc_underscore_const_names"); } // #[thread_local] is currently unstable if rustc_dep_of_std { - println!("cargo:rustc-cfg=libc_thread_local"); + set_cfg("libc_thread_local"); } // Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C". if rustc_minor_ver >= 62 { - println!("cargo:rustc-cfg=libc_const_extern_fn"); + set_cfg("libc_const_extern_fn"); } else { // Rust < 1.62.0 requires a crate feature and feature gate. if const_extern_fn_cargo_feature { if !is_nightly || rustc_minor_ver < 40 { panic!("const-extern-fn requires a nightly compiler >= 1.40"); } - println!("cargo:rustc-cfg=libc_const_extern_fn_unstable"); - println!("cargo:rustc-cfg=libc_const_extern_fn"); + set_cfg("libc_const_extern_fn_unstable"); + set_cfg("libc_const_extern_fn"); + } + } + + // check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the + // codebase. libc can configure it if the appropriate environment variable is passed. Since + // rust-lang/rust enforces it, this is useful when using a custom libc fork there. + // + // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg + if libc_check_cfg { + for cfg in ALLOWED_CFGS { + println!("cargo:rustc-check-cfg=values({})", cfg); + } + for &(name, values) in CHECK_CFG_EXTRA { + let values = values.join("\",\""); + println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values); } } } @@ -176,3 +229,10 @@ fn which_freebsd() -> Option { _ => None, } } + +fn set_cfg(cfg: &str) { + if !ALLOWED_CFGS.contains(&cfg) { + panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg); + } + println!("cargo:rustc-cfg={}", cfg); +} -- cgit v1.2.3