diff --git a/Changelog.md b/Changelog.md index 36dd7ba3a..5b1b109f4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Add Linux armv7l python sysconfig in [#901](https://github.com/PyO3/maturin/pull/901) * Add NetBSD python sysconfig in [#903](https://github.com/PyO3/maturin/pull/903) * Update 'replace_needed' to reduce total calls to 'patchelf' in [#905](https://github.com/PyO3/maturin/pull/905) +* Allow use python interpreters from bundled sysconfig when not cross compiling in [#907](https://github.com/PyO3/maturin/pull/907) ## [0.12.15] - 2022-05-07 diff --git a/src/build_options.rs b/src/build_options.rs index 5981b418e..70a623fd0 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -619,7 +619,8 @@ fn find_single_python_interpreter( Ok(interpreter) } -fn find_host_interpreter( +/// Find python interpreters in the host machine +fn find_interpreter_in_host( bridge: &BridgeModel, interpreter: &[PathBuf], target: &Target, @@ -643,6 +644,51 @@ fn find_host_interpreter( Ok(interpreters) } +/// Find python interpreters in the bundled sysconfig +fn find_interpreter_in_sysconfig( + interpreter: &[PathBuf], + target: &Target, +) -> Result> { + let mut interpreters = Vec::new(); + for interp in interpreter { + let python = interp + .file_name() + .context("Invalid python interpreter")? + .to_string_lossy(); + let (python_impl, python_ver) = if let Some(ver) = python.strip_prefix("pypy") { + (InterpreterKind::PyPy, ver) + } else if let Some(ver) = python.strip_prefix("python") { + (InterpreterKind::CPython, ver) + } else { + bail!("Unsupported Python interpreter: {}", python); + }; + let (ver_major, ver_minor) = python_ver + .split_once('.') + .context("Invalid python interpreter version")?; + let ver_major = ver_major.parse::().with_context(|| { + format!( + "Invalid python interpreter major version '{}', expect a digit", + ver_major + ) + })?; + let ver_minor = ver_minor.parse::().with_context(|| { + format!( + "Invalid python interpreter minor version '{}', expect a digit", + ver_minor + ) + })?; + let sysconfig = InterpreterConfig::lookup( + target.target_os(), + target.target_arch(), + python_impl, + (ver_major, ver_minor), + ) + .context("Failed to find a python interpreter")?; + interpreters.push(PythonInterpreter::from_config(sysconfig.clone())); + } + Ok(interpreters) +} + /// Finds the appropriate amount for python versions for each [BridgeModel]. /// /// This means all for bindings, one for cffi and zero for bin. @@ -670,7 +716,7 @@ pub fn find_interpreter( { if let Some(cross_lib_dir) = std::env::var_os("PYO3_CROSS_LIB_DIR") { let host_interpreters = - find_host_interpreter(bridge, interpreter, target, min_python_minor)?; + find_interpreter_in_host(bridge, interpreter, target, min_python_minor)?; let host_python = &host_interpreters[0]; println!( "🐍 Using host {} for cross-compiling preparation", @@ -738,47 +784,23 @@ pub fn find_interpreter( if interpreter.is_empty() { bail!("Couldn't find any python interpreters. Please specify at least one with -i"); } - for interp in interpreter { - let python = interp - .file_name() - .context("Invalid python interpreter")? - .to_string_lossy(); - let (python_impl, python_ver) = - if let Some(ver) = python.strip_prefix("pypy") { - (InterpreterKind::PyPy, ver) - } else if let Some(ver) = python.strip_prefix("python") { - (InterpreterKind::CPython, ver) - } else { - bail!("Unsupported Python interpreter: {}", python); - }; - let (ver_major, ver_minor) = python_ver - .split_once('.') - .context("Invalid python interpreter version")?; - let ver_major = ver_major.parse::().with_context(|| { - format!( - "Invalid python interpreter major version '{}', expect a digit", - ver_major - ) - })?; - let ver_minor = ver_minor.parse::().with_context(|| { - format!( - "Invalid python interpreter minor version '{}', expect a digit", - ver_minor - ) - })?; - let sysconfig = InterpreterConfig::lookup( - target.target_os(), - target.target_arch(), - python_impl, - (ver_major, ver_minor), - ) - .context("Failed to find a python interpreter")?; - interpreters.push(PythonInterpreter::from_config(sysconfig.clone())); - } + interpreters = find_interpreter_in_sysconfig(interpreter, target)?; } } else { - interpreters = - find_host_interpreter(bridge, interpreter, target, min_python_minor)?; + match find_interpreter_in_host(bridge, interpreter, target, min_python_minor) { + Ok(host_interps) => interpreters = host_interps, + Err(err) => { + if !interpreter.is_empty() + && binding_name.starts_with("pyo3") + && target.is_unix() + { + interpreters = find_interpreter_in_sysconfig(interpreter, target) + .map_err(|_| err)?; + } else { + return Err(err); + } + } + } } println!(