From f346d1c86faa5f4f109f97c045f567a38d8d046d Mon Sep 17 00:00:00 2001 From: mejrs Date: Fri, 18 Feb 2022 19:20:32 +0100 Subject: [PATCH] Move emitting cfgs to pyo3-build-config --- build.rs | 27 ++++----------------------- pyo3-build-config/src/lib.rs | 31 +++++++++++++++++++++++++++++++ pyo3-ffi/build.rs | 23 +++-------------------- 3 files changed, 38 insertions(+), 43 deletions(-) diff --git a/build.rs b/build.rs index cfaf06f6..78cebf9a 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ -use std::{env, process::Command}; +use std::env; use pyo3_build_config::pyo3_build_script_impl::{cargo_env_var, errors::Result}; -use pyo3_build_config::{bail, InterpreterConfig}; +use pyo3_build_config::{bail, print_feature_cfgs, InterpreterConfig}; fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<()> { if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() { @@ -32,17 +32,6 @@ fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<( Ok(()) } -fn rustc_minor_version() -> Option { - 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() -} - /// Prepares the PyO3 crate for compilation. /// /// This loads the config from pyo3-build-config and then makes some additional checks to improve UX @@ -55,18 +44,10 @@ fn configure_pyo3() -> Result<()> { interpreter_config.emit_pyo3_cfgs(); - let rustc_minor_version = rustc_minor_version().unwrap_or(0); ensure_auto_initialize_ok(interpreter_config)?; - // Enable use of const generics on Rust 1.51 and greater - if rustc_minor_version >= 51 { - println!("cargo:rustc-cfg=min_const_generics"); - } - - // Enable use of std::ptr::addr_of! on Rust 1.51 and greater - if rustc_minor_version >= 51 { - println!("cargo:rustc-cfg=addr_of"); - } + // Emit cfgs like `addr_of` and `min_const_generics` + print_feature_cfgs(); Ok(()) } diff --git a/pyo3-build-config/src/lib.rs b/pyo3-build-config/src/lib.rs index 0a4a147c..5a89ad9d 100644 --- a/pyo3-build-config/src/lib.rs +++ b/pyo3-build-config/src/lib.rs @@ -10,6 +10,7 @@ mod impl_; #[cfg(feature = "resolve-config")] use std::io::Cursor; +use std::{env, process::Command}; #[cfg(feature = "resolve-config")] use once_cell::sync::OnceCell; @@ -115,6 +116,36 @@ fn abi3_config() -> InterpreterConfig { interpreter_config } +/// Use certain features if we detect the compiler being used supports them. +/// +/// Features may be removed or added as MSRV gets bumped or new features become available, +/// so this function is unstable. +#[doc(hidden)] +pub fn print_feature_cfgs() { + fn rustc_minor_version() -> Option { + 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); + + // Enable use of const generics on Rust 1.51 and greater + if rustc_minor_version >= 51 { + println!("cargo:rustc-cfg=min_const_generics"); + } + + // Enable use of std::ptr::addr_of! on Rust 1.51 and greater + if rustc_minor_version >= 51 { + println!("cargo:rustc-cfg=addr_of"); + } +} + /// Private exports used in PyO3's build.rs /// /// Please don't use these - they could change at any time. diff --git a/pyo3-ffi/build.rs b/pyo3-ffi/build.rs index feb9d4d8..c8d65d5d 100644 --- a/pyo3-ffi/build.rs +++ b/pyo3-ffi/build.rs @@ -1,5 +1,5 @@ use pyo3_build_config::{ - bail, ensure, + bail, ensure, print_feature_cfgs, pyo3_build_script_impl::{ cargo_env_var, env_var, errors::Result, resolve_interpreter_config, InterpreterConfig, PythonVersion, @@ -100,29 +100,12 @@ fn configure_pyo3() -> Result<()> { println!("{}", line); } - let rustc_minor_version = rustc_minor_version().unwrap_or(0); - - // Enable use of std::ptr::addr_of! on Rust 1.51 and greater - if rustc_minor_version >= 51 { - println!("cargo:rustc-cfg=addr_of"); - } + // Emit cfgs like `addr_of` and `min_const_generics` + print_feature_cfgs(); Ok(()) } -fn rustc_minor_version() -> Option { - use std::{env, process::Command}; - - 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() -} - fn print_config_and_exit(config: &InterpreterConfig) { println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --"); config