Move emitting cfgs to pyo3-build-config

This commit is contained in:
mejrs 2022-02-18 19:20:32 +01:00
parent c40bd98825
commit f346d1c86f
3 changed files with 38 additions and 43 deletions

View File

@ -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::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<()> { fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<()> {
if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() { if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() {
@ -32,17 +32,6 @@ fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<(
Ok(()) Ok(())
} }
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()
}
/// Prepares the PyO3 crate for compilation. /// Prepares the PyO3 crate for compilation.
/// ///
/// This loads the config from pyo3-build-config and then makes some additional checks to improve UX /// 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(); interpreter_config.emit_pyo3_cfgs();
let rustc_minor_version = rustc_minor_version().unwrap_or(0);
ensure_auto_initialize_ok(interpreter_config)?; ensure_auto_initialize_ok(interpreter_config)?;
// Enable use of const generics on Rust 1.51 and greater // Emit cfgs like `addr_of` and `min_const_generics`
if rustc_minor_version >= 51 { print_feature_cfgs();
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");
}
Ok(()) Ok(())
} }

View File

@ -10,6 +10,7 @@ mod impl_;
#[cfg(feature = "resolve-config")] #[cfg(feature = "resolve-config")]
use std::io::Cursor; use std::io::Cursor;
use std::{env, process::Command};
#[cfg(feature = "resolve-config")] #[cfg(feature = "resolve-config")]
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
@ -115,6 +116,36 @@ fn abi3_config() -> InterpreterConfig {
interpreter_config 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<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);
// 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 /// Private exports used in PyO3's build.rs
/// ///
/// Please don't use these - they could change at any time. /// Please don't use these - they could change at any time.

View File

@ -1,5 +1,5 @@
use pyo3_build_config::{ use pyo3_build_config::{
bail, ensure, bail, ensure, print_feature_cfgs,
pyo3_build_script_impl::{ pyo3_build_script_impl::{
cargo_env_var, env_var, errors::Result, resolve_interpreter_config, InterpreterConfig, cargo_env_var, env_var, errors::Result, resolve_interpreter_config, InterpreterConfig,
PythonVersion, PythonVersion,
@ -100,29 +100,12 @@ fn configure_pyo3() -> Result<()> {
println!("{}", line); println!("{}", line);
} }
let rustc_minor_version = rustc_minor_version().unwrap_or(0); // Emit cfgs like `addr_of` and `min_const_generics`
print_feature_cfgs();
// Enable use of std::ptr::addr_of! on Rust 1.51 and greater
if rustc_minor_version >= 51 {
println!("cargo:rustc-cfg=addr_of");
}
Ok(()) Ok(())
} }
fn rustc_minor_version() -> Option<u32> {
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) { fn print_config_and_exit(config: &InterpreterConfig) {
println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --"); println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --");
config config