Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PYO3_NO_PYTHON hack for pyo3 >= 0.16.4 #875

Merged
merged 1 commit into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Stop setting `PYO3_NO_PYTHON` environment variable for pyo3 0.16.4 and later in [#875](https://github.com/PyO3/maturin/pull/875)

## [0.12.12] - 2022-04-07

* Migrate docker image to GitHub container registry in [#845](https://github.com/PyO3/maturin/pull/845)
Expand Down
33 changes: 30 additions & 3 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::str;

/// The first version of pyo3 that supports building Windows abi3 wheel
/// without `PYO3_NO_PYTHON` environment variable
const PYO3_ABI3_NO_PYTHON_VERSION: (u64, u64, u64) = (0, 16, 4);

/// Builds the rust crate into a native module (i.e. an .so or .dll) for a
/// specific python version. Returns a mapping from crate type (e.g. cdylib)
/// to artifact location.
Expand Down Expand Up @@ -218,9 +222,13 @@ fn compile_target(
.map(|p| p.interpreter_kind == InterpreterKind::PyPy)
.unwrap_or(false);
if !is_pypy && !target.is_windows() {
// This will make pyo3's build script only set some predefined linker
// arguments without trying to read any python configuration
build_command.env("PYO3_NO_PYTHON", "1");
let pyo3_ver = pyo3_version(&context.cargo_metadata)
.context("Failed to get pyo3 version from cargo metadata")?;
if pyo3_ver < PYO3_ABI3_NO_PYTHON_VERSION {
// This will make old pyo3's build script only set some predefined linker
// arguments without trying to read any python configuration
build_command.env("PYO3_NO_PYTHON", "1");
}
}
}

Expand Down Expand Up @@ -386,3 +394,22 @@ pub fn warn_missing_py_init(artifact: &Path, module_name: &str) -> Result<()> {

Ok(())
}

fn pyo3_version(cargo_metadata: &cargo_metadata::Metadata) -> Option<(u64, u64, u64)> {
let packages: HashMap<&str, &cargo_metadata::Package> = cargo_metadata
.packages
.iter()
.filter_map(|pkg| {
let name = &pkg.name;
if name == "pyo3" || name == "pyo3-ffi" {
Some((name.as_ref(), pkg))
} else {
None
}
})
.collect();
packages
.get("pyo3")
.or_else(|| packages.get("pyo3-ffi"))
.map(|pkg| (pkg.version.major, pkg.version.minor, pkg.version.patch))
}