Skip to content

Commit

Permalink
Add support for generating PyPy Windows import library
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jul 14, 2022
1 parent 402018e commit ef650cb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Rust `std::cmp::Ordering` comparison. [#2460](https://github.com/PyO3/pyo3/pull/2460)
- Supprt `#[pyo3(name)]` on enum variants [#2457](https://github.com/PyO3/pyo3/pull/2457)
- Add `PySuper` object [#2049](https://github.com/PyO3/pyo3/issues/2049)
- Add support for generating PyPy Windows import library. [#2506](https://github.com/PyO3/pyo3/pull/2506)

### Changed

Expand Down
4 changes: 2 additions & 2 deletions pyo3-build-config/Cargo.toml
Expand Up @@ -12,11 +12,11 @@ edition = "2018"

[dependencies]
once_cell = "1"
python3-dll-a = { version = "0.2.2", optional = true }
python3-dll-a = { version = "0.2.4", optional = true }
target-lexicon = "0.12"

[build-dependencies]
python3-dll-a = { version = "0.2.2", optional = true }
python3-dll-a = { version = "0.2.4", optional = true }
target-lexicon = "0.12"

[features]
Expand Down
53 changes: 42 additions & 11 deletions pyo3-build-config/src/impl_.rs
Expand Up @@ -471,7 +471,8 @@ print("mingw", get_platform().startswith("mingw"))
if self.lib_dir.is_none() {
let target = target_triple_from_env();
let py_version = if self.abi3 { None } else { Some(self.version) };
self.lib_dir = import_lib::generate_import_lib(&target, py_version)?;
self.lib_dir =
import_lib::generate_import_lib(&target, self.implementation, py_version)?;
}
Ok(())
}
Expand Down Expand Up @@ -1427,7 +1428,13 @@ fn default_cross_compile(cross_compile_config: &CrossCompileConfig) -> Result<In
#[cfg(feature = "python3-dll-a")]
if lib_dir.is_none() {
let py_version = if abi3 { None } else { Some(version) };
lib_dir = self::import_lib::generate_import_lib(&cross_compile_config.target, py_version)?;
lib_dir = self::import_lib::generate_import_lib(
&cross_compile_config.target,
cross_compile_config
.implementation
.unwrap_or(PythonImplementation::CPython),
py_version,
)?;
}

Ok(InterpreterConfig {
Expand Down Expand Up @@ -1554,13 +1561,24 @@ fn default_lib_name_windows(
abi3: bool,
mingw: bool,
) -> String {
if abi3 && !implementation.is_pypy() {
WINDOWS_ABI3_LIB_NAME.to_owned()
} else if mingw {
// https://packages.msys2.org/base/mingw-w64-python
format!("python{}.{}", version.major, version.minor)
} else {
format!("python{}{}", version.major, version.minor)
match implementation {
PythonImplementation::CPython => {
if abi3 {
WINDOWS_ABI3_LIB_NAME.to_owned()
} else if mingw {
// https://packages.msys2.org/base/mingw-w64-python
format!("python{}.{}", version.major, version.minor)
} else {
format!("python{}{}", version.major, version.minor)
}
}
PythonImplementation::PyPy => {
if (version.major, version.minor) >= (3, 9) {
format!("libpypy{}.{}-c", version.major, version.minor)
} else {
"libpypy3-c".to_string()
}
}
}
}

Expand Down Expand Up @@ -1765,7 +1783,11 @@ pub fn make_interpreter_config() -> Result<InterpreterConfig> {
} else {
Some(interpreter_config.version)
};
interpreter_config.lib_dir = self::import_lib::generate_import_lib(&host, py_version)?;
interpreter_config.lib_dir = self::import_lib::generate_import_lib(
&host,
interpreter_config.implementation,
py_version,
)?;
}

Ok(interpreter_config)
Expand Down Expand Up @@ -2299,7 +2321,16 @@ mod tests {
true,
false
),
"python37",
"libpypy3-c",
);
assert_eq!(
super::default_lib_name_windows(
PythonVersion { major: 3, minor: 9 },
PyPy,
true,
false
),
"libpypy3.9-c",
);
}

Expand Down
8 changes: 7 additions & 1 deletion pyo3-build-config/src/import_lib.rs
Expand Up @@ -7,7 +7,7 @@ use python3_dll_a::ImportLibraryGenerator;

use crate::errors::{Context, Result};

use super::{Architecture, OperatingSystem, PythonVersion, Triple};
use super::{Architecture, OperatingSystem, PythonImplementation, PythonVersion, Triple};

/// Generates the `python3.dll` or `pythonXY.dll` import library for Windows targets.
///
Expand All @@ -17,6 +17,7 @@ use super::{Architecture, OperatingSystem, PythonVersion, Triple};
/// Does nothing if the target OS is not Windows.
pub(super) fn generate_import_lib(
target: &Triple,
py_impl: PythonImplementation,
py_version: Option<PythonVersion>,
) -> Result<Option<String>> {
if target.operating_system != OperatingSystem::Windows {
Expand All @@ -38,9 +39,14 @@ pub(super) fn generate_import_lib(
};

let env = target.environment.to_string();
let implementation = match py_impl {
PythonImplementation::CPython => python3_dll_a::PythonImplementation::CPython,
PythonImplementation::PyPy => python3_dll_a::PythonImplementation::PyPy,
};

ImportLibraryGenerator::new(&arch, &env)
.version(py_version.map(|v| (v.major, v.minor)))
.implementation(implementation)
.generate(&out_lib_dir)
.context("failed to generate python3.dll import library")?;

Expand Down

0 comments on commit ef650cb

Please sign in to comment.