From 88b7f0d991eacae483b23c6ab9ba1ed0a4a6bd43 Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Wed, 14 Dec 2022 13:36:09 +0100
Subject: [PATCH 1/6] refactor build script to use a function to set cfgs

---
 build.rs | 52 +++++++++++++++++++++++++++-------------------------
 1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/build.rs b/build.rs
index e97be1a6ab108..9a52435e49c67 100644
--- a/build.rs
+++ b/build.rs
@@ -25,94 +25,92 @@ 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 {
-        println!("cargo:rustc-cfg=libc_long_array");
+        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");
         }
     }
 }
@@ -181,3 +179,7 @@ fn which_freebsd() -> Option<i32> {
         _ => None,
     }
 }
+
+fn set_cfg(cfg: &str) {
+    println!("cargo:rustc-cfg={}", cfg);
+}

From f8a12ee0a4e96f830cdc765343aa60cbe27c5a4a Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Wed, 14 Dec 2022 13:41:49 +0100
Subject: [PATCH 2/6] ensure all cfgs used are allowed

---
 build.rs | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/build.rs b/build.rs
index 9a52435e49c67..ab299b70baa2a 100644
--- a/build.rs
+++ b/build.rs
@@ -2,6 +2,33 @@ 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: &[&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",
+];
+
 fn main() {
     // Avoid unnecessary re-building.
     println!("cargo:rerun-if-changed=build.rs");
@@ -181,5 +208,8 @@ fn which_freebsd() -> Option<i32> {
 }
 
 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);
 }

From b636da0a67185598bbe739ed2c20910e51514e4d Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Fri, 24 Feb 2023 11:05:44 +0100
Subject: [PATCH 3/6] emit rustc-check-cfg in the build script when
 LIBC_CHECK_CFG=1

---
 build.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/build.rs b/build.rs
index ab299b70baa2a..cb70d6e238291 100644
--- a/build.rs
+++ b/build.rs
@@ -38,6 +38,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!(
@@ -140,6 +141,17 @@ fn main() {
             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);
+        }
+    }
 }
 
 fn rustc_minor_nightly() -> (u32, bool) {

From e74f73544f29fe3559081250b9a657a36bc860eb Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Wed, 14 Dec 2022 14:41:50 +0100
Subject: [PATCH 4/6] support extra check-cfg

---
 build.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/build.rs b/build.rs
index cb70d6e238291..43a5d890d1b1e 100644
--- a/build.rs
+++ b/build.rs
@@ -29,6 +29,13 @@ const ALLOWED_CFGS: &[&str] = &[
     "libc_union",
 ];
 
+// Extra values to allow for check-cfg.
+const CHECK_CFG_EXTRA: &[(&str, &[&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");
@@ -151,6 +158,10 @@ fn main() {
         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);
+        }
     }
 }
 

From b9a49d442bdeeac8cac3bb097f5e224e7623e037 Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Wed, 14 Dec 2022 14:46:27 +0100
Subject: [PATCH 5/6] add ci job to test check-cfg

---
 .github/workflows/bors.yml | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/.github/workflows/bors.yml b/.github/workflows/bors.yml
index 49a06de68605a..e4e342a1b30c2 100644
--- a/.github/workflows/bors.yml
+++ b/.github/workflows/bors.yml
@@ -317,6 +317,23 @@ jobs:
         run: LIBC_CI=1 TOOLCHAIN=${{ matrix.toolchain }} WIN_TARGET=${{ matrix.target }} sh ./ci/build.sh
         shell: bash
 
+  check_cfg:
+    permissions:
+      actions: write # to cancel workflows (rust-lang/simpleinfra/github-actions/cancel-outdated-builds)
+      contents: read # to fetch code (actions/checkout)
+
+    name: "Check #[cfg]s"
+    runs-on: ubuntu-22.04
+    steps:
+      - uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
+        with:
+          github_token: "${{ secrets.GITHUB_TOKEN }}"
+      - uses: actions/checkout@v3
+      - name: Setup Rust toolchain
+        run: TOOLCHAIN=nightly sh ./ci/install-rust.sh
+      - name: Build with check-cfg
+        run: LIBC_CI=1 LIBC_CHECK_CFG=1 cargo build -Z unstable-options -Z check-cfg=features,names,values,output
+
   docs:
     permissions:
       actions: write # to cancel workflows (rust-lang/simpleinfra/github-actions/cancel-outdated-builds)

From d63eedc3a3fe078e0aa32ba5a329f10d2e63c18d Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Wed, 8 Mar 2023 09:28:12 +0100
Subject: [PATCH 6/6] compatibility with rust 1.13.0

---
 build.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/build.rs b/build.rs
index 43a5d890d1b1e..e60672a76133b 100644
--- a/build.rs
+++ b/build.rs
@@ -5,7 +5,7 @@ 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: &[&str] = &[
+const ALLOWED_CFGS: &'static [&'static str] = &[
     "freebsd10",
     "freebsd11",
     "freebsd12",
@@ -30,7 +30,7 @@ const ALLOWED_CFGS: &[&str] = &[
 ];
 
 // Extra values to allow for check-cfg.
-const CHECK_CFG_EXTRA: &[(&str, &[&str])] = &[
+const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
     ("target_os", &["switch", "aix", "ohos"]),
     ("target_env", &["illumos", "wasi", "aix", "ohos"]),
     ("target_arch", &["loongarch64"]),
@@ -158,7 +158,7 @@ fn main() {
         for cfg in ALLOWED_CFGS {
             println!("cargo:rustc-check-cfg=values({})", cfg);
         }
-        for (name, values) in CHECK_CFG_EXTRA {
+        for &(name, values) in CHECK_CFG_EXTRA {
             let values = values.join("\",\"");
             println!("cargo:rustc-check-cfg=values({},\"{}\")", name, values);
         }