perf(build-rs): Always emit check-cfg

These didn't require an MSRV bump.  Worse case, some metadata will be
emitted but that shouldn't impact things too negatively.
This commit is contained in:
Ed Page 2024-12-09 10:05:42 -06:00
parent 0f62101676
commit 5e833bfbbb
3 changed files with 13 additions and 51 deletions

View File

@ -1,30 +0,0 @@
use std::{process::Command, sync::OnceLock};
fn cargo_version_minor() -> u32 {
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
*VERSION_MINOR.get_or_init(|| {
let out = Command::new(crate::input::cargo())
.arg("-V")
.output()
.expect("running `cargo -V` should succeed");
assert!(out.status.success(), "running `cargo -V` should succeed");
// > cargo -V # example output
// cargo 1.82.0 (8f40fc59f 2024-08-21)
let out = std::str::from_utf8(&out.stdout).expect("`cargo -V` should output valid UTF-8");
let version = out.split(' ').nth(1).unwrap();
version_minor(version).unwrap()
})
}
fn version_minor(version: &str) -> Option<u32> {
let minor = version.split('.').nth(1)?;
let minor = minor.parse().unwrap();
Some(minor)
}
pub(crate) fn check_cfg() -> bool {
// emit check-cfg if the toolchain being used supports it
cargo_version_minor() >= 80
}

View File

@ -40,7 +40,6 @@ MSRV: Respected as of "#,
};
}
mod allow_use;
mod ident;
pub mod input;

View File

@ -6,10 +6,7 @@
//!
//! Reference: <https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script>
use crate::{
allow_use,
ident::{is_ascii_ident, is_ident},
};
use crate::ident::{is_ascii_ident, is_ident};
use std::{ffi::OsStr, fmt::Display, fmt::Write, path::Path, str};
fn emit(directive: &str, value: impl Display) {
@ -309,13 +306,11 @@ pub fn rustc_check_cfgs(keys: &[&str]) {
}
}
if allow_use::check_cfg() {
let mut directive = keys[0].to_string();
for key in &keys[1..] {
write!(directive, ", {key}").expect("writing to string should be infallible");
}
emit("rustc-check-cfg", format_args!("cfg({directive})"));
let mut directive = keys[0].to_string();
for key in &keys[1..] {
write!(directive, ", {key}").expect("writing to string should be infallible");
}
emit("rustc-check-cfg", format_args!("cfg({directive})"));
}
/// Add to the list of expected config names that is used when checking the
@ -339,17 +334,15 @@ pub fn rustc_check_cfg_values(key: &str, values: &[&str]) {
return;
}
if allow_use::check_cfg() {
let mut directive = format!("\"{}\"", values[0].escape_default());
for value in &values[1..] {
write!(directive, ", \"{}\"", value.escape_default())
.expect("writing to string should be infallible");
}
emit(
"rustc-check-cfg",
format_args!("cfg({key}, values({directive}))"),
);
let mut directive = format!("\"{}\"", values[0].escape_default());
for value in &values[1..] {
write!(directive, ", \"{}\"", value.escape_default())
.expect("writing to string should be infallible");
}
emit(
"rustc-check-cfg",
format_args!("cfg({key}, values({directive}))"),
);
}
/// The `rustc-env` instruction tells Cargo to set the given environment variable