From d0adf785df4b1ee28f21dd07c7aece3c506918cb Mon Sep 17 00:00:00 2001 From: messense Date: Wed, 5 Apr 2023 17:16:51 +0800 Subject: [PATCH] Fix wrong `EXT_SUFFIX` when cross compiling musllinux wheels for Python 3.11 --- Changelog.md | 1 + src/build_options.rs | 13 +++------ src/python_interpreter/config.rs | 49 +++++++++++++++++++++++++------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Changelog.md b/Changelog.md index 4ff354a03..c25751109 100644 --- a/Changelog.md +++ b/Changelog.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Bump MSRV to 1.64.0 in [#1528](https://github.com/PyO3/maturin/pull/1528) * Add wildcards support to publish/upload commands on Windows in [#1534](https://github.com/PyO3/maturin/pull/1534) * Add support for configuring macOS deployment target version in `pyproject.toml` in [#1536](https://github.com/PyO3/maturin/pull/1536) +* Fix wrong `EXT_SUFFIX` when cross compiling musllinux wheels for Python 3.11 in [#1560](https://github.com/PyO3/maturin/pull/1560) ## [0.14.16] - 2023-03-26 diff --git a/src/build_options.rs b/src/build_options.rs index c66f93014..c353590d1 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -1149,15 +1149,10 @@ fn find_interpreter_in_sysconfig( let ver_minor = ver_minor.parse::().with_context(|| { format!("Invalid python interpreter minor version '{ver_minor}', expect a digit") })?; - let sysconfig = InterpreterConfig::lookup( - target.target_os(), - target.target_arch(), - python_impl, - (ver_major, ver_minor), - ) - .with_context(|| { - format!("Failed to find a {python_impl} {ver_major}.{ver_minor} interpreter") - })?; + let sysconfig = InterpreterConfig::lookup(target, python_impl, (ver_major, ver_minor)) + .with_context(|| { + format!("Failed to find a {python_impl} {ver_major}.{ver_minor} interpreter") + })?; debug!( "Found {} {}.{} in bundled sysconfig", sysconfig.interpreter_kind, sysconfig.major, sysconfig.minor, diff --git a/src/python_interpreter/config.rs b/src/python_interpreter/config.rs index feec2848a..b157efed6 100644 --- a/src/python_interpreter/config.rs +++ b/src/python_interpreter/config.rs @@ -75,16 +75,28 @@ pub struct InterpreterConfig { impl InterpreterConfig { /// Lookup a wellknown sysconfig for a given Python interpreter pub fn lookup( - os: Os, - arch: Arch, + target: &Target, python_impl: InterpreterKind, python_version: (usize, usize), - ) -> Option<&'static Self> { + ) -> Option { let (major, minor) = python_version; - if let Some(os_sysconfigs) = WELLKNOWN_SYSCONFIG.get(&os) { - if let Some(sysconfigs) = os_sysconfigs.get(&arch) { - return sysconfigs.iter().find(|s| { - s.interpreter_kind == python_impl && s.major == major && s.minor == minor + if let Some(os_sysconfigs) = WELLKNOWN_SYSCONFIG.get(&target.target_os()) { + if let Some(sysconfigs) = os_sysconfigs.get(&target.target_arch()) { + return sysconfigs.iter().find_map(|s| { + if s.interpreter_kind == python_impl && s.major == major && s.minor == minor { + if python_version >= (3, 11) && target.is_musl_target() { + // See https://github.com/pypa/auditwheel/issues/349 + let mut musl_config = s.clone(); + musl_config.ext_suffix = s + .ext_suffix + .replace("-gnu", &format!("-{}", target.target_env())); + Some(musl_config) + } else { + Some(s.clone()) + } + } else { + None + } }); } } @@ -255,10 +267,27 @@ mod test { #[test] fn test_pyo3_config_file() { - let sysconfig = - InterpreterConfig::lookup(Os::Linux, Arch::X86_64, InterpreterKind::CPython, (3, 10)) - .unwrap(); + let sysconfig = InterpreterConfig::lookup( + &Target::from_target_triple(Some("x86_64-unknown-linux-gnu".to_string())).unwrap(), + InterpreterKind::CPython, + (3, 10), + ) + .unwrap(); + assert_eq!(sysconfig.ext_suffix, ".cpython-310-x86_64-linux-gnu.so"); let config_file = sysconfig.pyo3_config_file(); assert_eq!(config_file, "implementation=CPython\nversion=3.10\nshared=true\nabi3=false\nbuild_flags=WITH_THREAD\nsuppress_build_script_link_lines=false\npointer_width=64"); } + + #[test] + fn test_pyo3_config_file_musl_python_3_11() { + let sysconfig = InterpreterConfig::lookup( + &Target::from_target_triple(Some("x86_64-unknown-linux-musl".to_string())).unwrap(), + InterpreterKind::CPython, + (3, 11), + ) + .unwrap(); + assert_eq!(sysconfig.ext_suffix, ".cpython-311-x86_64-linux-musl.so"); + let config_file = sysconfig.pyo3_config_file(); + assert_eq!(config_file, "implementation=CPython\nversion=3.11\nshared=true\nabi3=false\nbuild_flags=WITH_THREAD\nsuppress_build_script_link_lines=false\npointer_width=64"); + } }