Skip to content

Commit

Permalink
emit rustc-check-cfg only on rust 1.80+
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed May 9, 2024
1 parent 2d19b7e commit 65c5ee4
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions pyo3-build-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ fn resolve_cross_compile_config_path() -> Option<PathBuf> {
/// so this function is unstable.
#[doc(hidden)]
pub fn print_feature_cfgs() {
fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = core::str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
}

let rustc_minor_version = rustc_minor_version().unwrap_or(0);

// invalid_from_utf8 lint was added in Rust 1.74
Expand All @@ -160,6 +149,11 @@ pub fn print_feature_cfgs() {
/// - <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg>
#[doc(hidden)]
pub fn print_expected_cfgs() {
if rustc_minor_version().map_or(false, |version| version < 80) {
// rustc 1.80.0 stabilized `rustc-check-cfg` feature, don't emit before
return;
}

println!("cargo:rustc-check-cfg=cfg(Py_LIMITED_API)");
println!("cargo:rustc-check-cfg=cfg(PyPy)");
println!("cargo:rustc-check-cfg=cfg(GraalPy)");
Expand Down Expand Up @@ -231,6 +225,22 @@ pub mod pyo3_build_script_impl {
}
}

fn rustc_minor_version() -> Option<u32> {
static RUSTC_MINOR_VERSION: OnceCell<Option<u32>> = OnceCell::new();
RUSTC_MINOR_VERSION
.get_or_init(|| {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = core::str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
})
.clone()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 65c5ee4

Please sign in to comment.