From 164f8883d2b7271069afa7f71b401f7855f73b22 Mon Sep 17 00:00:00 2001 From: messense Date: Thu, 5 May 2022 16:05:51 +0800 Subject: [PATCH] Add default value for `ext_suffix` --- src/build_options.rs | 2 +- src/python_interpreter/config.rs | 49 ++++++++++++++++++++++++++++---- src/target.rs | 18 ++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/build_options.rs b/src/build_options.rs index 7de44ef0b..9c377a246 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -726,7 +726,7 @@ pub fn find_interpreter( }); } else if let Some(config_file) = env::var_os("PYO3_CONFIG_FILE") { let interpreter_config = - InterpreterConfig::from_pyo3_config(config_file.as_ref()) + InterpreterConfig::from_pyo3_config(config_file.as_ref(), target) .context("Invalid PYO3_CONFIG_FILE")?; interpreters.push(PythonInterpreter::from_config(interpreter_config)); } else { diff --git a/src/python_interpreter/config.rs b/src/python_interpreter/config.rs index 2a15cd9e7..ea7120993 100644 --- a/src/python_interpreter/config.rs +++ b/src/python_interpreter/config.rs @@ -1,5 +1,6 @@ use super::InterpreterKind; use crate::target::{Arch, Os}; +use crate::Target; use anyhow::{format_err, Context, Result}; use fs_err as fs; use once_cell::sync::Lazy; @@ -67,7 +68,7 @@ impl InterpreterConfig { } /// Construct a new InterpreterConfig from a pyo3 config file - pub fn from_pyo3_config(config_file: &Path) -> Result { + pub fn from_pyo3_config(config_file: &Path, target: &Target) -> Result { let config_file = fs::File::open(config_file)?; let reader = BufReader::new(config_file); let lines = reader.lines(); @@ -125,16 +126,52 @@ impl InterpreterConfig { })?; let implementation = implementation.unwrap_or_else(|| "cpython".to_string()); let interpreter_kind = implementation.parse().map_err(|e| format_err!("{}", e))?; - let ext_suffix = ext_suffix.context("missing value for ext_suffix")?; let abi_tag = match interpreter_kind { InterpreterKind::CPython => { if (major, minor) >= (3, 8) { - abi_tag.or_else(|| Some(format!("{}{}", major, minor))) + abi_tag.unwrap_or_else(|| format!("{}{}", major, minor)) } else { - abi_tag.or_else(|| Some(format!("{}{}m", major, minor))) + abi_tag.unwrap_or_else(|| format!("{}{}m", major, minor)) } } - InterpreterKind::PyPy => abi_tag.or_else(|| Some("pp73".to_string())), + InterpreterKind::PyPy => abi_tag.unwrap_or_else(|| "pp73".to_string()), + }; + let file_ext = if target.is_windows() { "pyd" } else { "so" }; + let ext_suffix = if target.is_linux() || target.is_macos() { + // See https://github.com/pypa/auditwheel/issues/349 + let target_env = if (major, minor) >= (3, 11) { + target.target_env().to_string() + } else { + "gnu".to_string() + }; + match interpreter_kind { + InterpreterKind::CPython => ext_suffix.unwrap_or_else(|| { + // Eg: .cpython-38-x86_64-linux-gnu.so + format!( + ".cpython-{}-{}-{}-{}.{}", + abi_tag, + target.get_python_arch(), + target.get_python_os(), + target_env, + file_ext, + ) + }), + InterpreterKind::PyPy => ext_suffix.unwrap_or_else(|| { + // Eg: .pypy38-pp73-x86_64-linux-gnu.so + format!( + ".pypy{}{}-{}-{}-{}-{}.{}", + major, + minor, + abi_tag, + target.get_python_arch(), + target.get_python_os(), + target_env, + file_ext, + ) + }), + } + } else { + ext_suffix.context("missing value for ext_suffix")? }; Ok(Self { major, @@ -142,7 +179,7 @@ impl InterpreterConfig { interpreter_kind, abiflags: abiflags.unwrap_or_default(), ext_suffix, - abi_tag, + abi_tag: Some(abi_tag), pointer_width, }) } diff --git a/src/target.rs b/src/target.rs index 29752ee1b..d03b2f7b8 100644 --- a/src/target.rs +++ b/src/target.rs @@ -296,6 +296,19 @@ impl Target { Ok(tag) } + /// Returns the name python uses in `sys.platform` for this architecture. + pub fn get_python_arch(&self) -> &str { + match self.arch { + Arch::Aarch64 => "aarch64", + Arch::Armv7L => "armv7l", + Arch::Powerpc64Le => "powerpc64le", + Arch::Powerpc64 => "powerpc64", + Arch::X86 => "i386", + Arch::X86_64 => "x86_64", + Arch::S390X => "s390x", + } + } + /// Returns the name python uses in `sys.platform` for this os pub fn get_python_os(&self) -> &str { match self.os { @@ -364,6 +377,11 @@ impl Target { self.arch } + /// Returns target environment + pub fn target_env(&self) -> Environment { + self.env + } + /// Returns true if the current platform is linux pub fn is_linux(&self) -> bool { self.os == Os::Linux