Skip to content

Commit 7790dab

Browse files
authored
emit rustc-check-cfg only on rust 1.80+ (#4168)
1 parent 10152a7 commit 7790dab

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

pyo3-build-config/src/lib.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::{
1818

1919
use std::{env, process::Command, str::FromStr};
2020

21-
#[cfg(feature = "resolve-config")]
2221
use once_cell::sync::OnceCell;
2322

2423
pub use impl_::{
@@ -135,17 +134,6 @@ fn resolve_cross_compile_config_path() -> Option<PathBuf> {
135134
/// so this function is unstable.
136135
#[doc(hidden)]
137136
pub fn print_feature_cfgs() {
138-
fn rustc_minor_version() -> Option<u32> {
139-
let rustc = env::var_os("RUSTC")?;
140-
let output = Command::new(rustc).arg("--version").output().ok()?;
141-
let version = core::str::from_utf8(&output.stdout).ok()?;
142-
let mut pieces = version.split('.');
143-
if pieces.next() != Some("rustc 1") {
144-
return None;
145-
}
146-
pieces.next()?.parse().ok()
147-
}
148-
149137
let rustc_minor_version = rustc_minor_version().unwrap_or(0);
150138

151139
// invalid_from_utf8 lint was added in Rust 1.74
@@ -160,6 +148,11 @@ pub fn print_feature_cfgs() {
160148
/// - <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg>
161149
#[doc(hidden)]
162150
pub fn print_expected_cfgs() {
151+
if rustc_minor_version().map_or(false, |version| version < 80) {
152+
// rustc 1.80.0 stabilized `rustc-check-cfg` feature, don't emit before
153+
return;
154+
}
155+
163156
println!("cargo:rustc-check-cfg=cfg(Py_LIMITED_API)");
164157
println!("cargo:rustc-check-cfg=cfg(PyPy)");
165158
println!("cargo:rustc-check-cfg=cfg(GraalPy)");
@@ -233,6 +226,20 @@ pub mod pyo3_build_script_impl {
233226
}
234227
}
235228

229+
fn rustc_minor_version() -> Option<u32> {
230+
static RUSTC_MINOR_VERSION: OnceCell<Option<u32>> = OnceCell::new();
231+
*RUSTC_MINOR_VERSION.get_or_init(|| {
232+
let rustc = env::var_os("RUSTC")?;
233+
let output = Command::new(rustc).arg("--version").output().ok()?;
234+
let version = core::str::from_utf8(&output.stdout).ok()?;
235+
let mut pieces = version.split('.');
236+
if pieces.next() != Some("rustc 1") {
237+
return None;
238+
}
239+
pieces.next()?.parse().ok()
240+
})
241+
}
242+
236243
#[cfg(test)]
237244
mod tests {
238245
use super::*;

0 commit comments

Comments
 (0)