Skip to content
New issue

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

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

Already on GitHub? # to your account

Generate python3.dll import libraries for Windows targets in the build script #2231

Closed
ravenexp opened this issue Mar 16, 2022 · 4 comments · Fixed by #2282
Closed

Generate python3.dll import libraries for Windows targets in the build script #2231

ravenexp opened this issue Mar 16, 2022 · 4 comments · Fixed by #2282

Comments

@ravenexp
Copy link
Contributor

I'm routinely cross-compiling Python extension modules for Windows from a Linux host using the well-supported x86_64-pc-windows-gnu rustc target. The main issue I'm having is finding a suitable python3.dll import library (for abi3 extensions) to use with PYO3_CROSS_LIB_DIR.

MinGW and MSVC compilers use incompatible import library object formats, so downloading a Windows Python distribution does not work, and compiling Python with MinGW compiler is not supported by the upstream and requires about a hundred patches to work.

I've been working around this by using shell scripts to generate the MinGW-compatible python3.dll import library using dlltool from MinGW binutils. Luckily, the CPython project maintains a list of the stable ABI symbols in a machine-readable format, which can be used to generate a DLL module definition (.def) file and the DLL import library from it.

A few weeks ago I decided to ditch the shell script hacks and package everything into a self-contained Rust crate called python3-dll-a. I'm currently using it in my own projects via build.rs, and the Windows cross-compile command is just this (no Windows Python files required):

PYO3_CROSS_LIB_DIR=target/python3-dll cargo build --target x86_64-pc-windows-gnu

Ideally, I'd like to integrate it into pyo3-build-config, so that cross-compiling abi3 extension modules for Windows would just work out of the box without messing with any PYO3_CROSS* environment variables. This should be already the case for Unix-like platforms where linking extension modules does not require a Python shared library.

The current python3-dll-a version can generate python3.dll import libraries for
x86_64-pc-windows-gnu, i686-pc-windows-gnu, x86_64-pc-windows-msvc, i686-pc-windows-msvc and aarch64-pc-windows-msvc compile targets and can support both cross and native compilation setting. As a limitation of this approach, targeting the -msvc environment ABI requires LLVM binutils to be installed on the compile host.

If there is an interest in integrating this solution, I can start working on a PR for PyO3.
This will be feature-gated at the pyo3 crate level, of course.

@messense
Copy link
Member

See also PyO3/maturin#830

@messense
Copy link
Member

messense commented Mar 16, 2022

Ideally, I'd like to integrate it into pyo3-build-config

I'm also open for integrating it into maturin. It should be doable.

@davidhewitt
Copy link
Member

I'm happy for it to go in either place. You might find it a bit easier to put it in maturin; to keep pyo3-build-config simple there's a single file impl_.rs which is compiled into both the build script and the library.

@ravenexp
Copy link
Contributor Author

I'm happy for it to go in either place. You might find it a bit easier to put it in maturin; to keep pyo3-build-config simple there's a single file impl_.rs which is compiled into both the build script and the library.

Thanks!

I'll try to go with pyo3-build-config first, because I like to think that cargo build should always work. It's also a prerequisite for cargo clippy and cargo test to work. If it is implemented in maturin only these commands will also become inaccessible.

ravenexp added a commit to ravenexp/pyo3 that referenced this issue Apr 7, 2022
Automatically generate `python3.dll` import libraries
for Windows compile targets in the build script.

Adds a new PyO3 crate feature `auto-abi3-import-lib` enabling
automatic import library generation.

Closes PyO3#2231
ravenexp added a commit to ravenexp/pyo3 that referenced this issue Apr 8, 2022
Automatically generate `python3.dll` import libraries for Windows
compile targets in the build script.

Adds a new PyO3 crate feature `generate-abi3-import-lib` enabling
automatic import library generation.

Closes PyO3#2231
ravenexp added a commit to ravenexp/pyo3 that referenced this issue Apr 10, 2022
Automatically generate `python3.dll` import libraries for Windows
compile targets in the build script.

Adds a new PyO3 crate feature `generate-abi3-import-lib` enabling
automatic import library generation.

Closes PyO3#2231
jqnatividad added a commit to dathere/qsv that referenced this issue Apr 29, 2022
jqnatividad added a commit to dathere/qsv that referenced this issue Apr 29, 2022
on Windows targets.
see PyO3/pyo3#2231 for more info
mejrs pushed a commit to mejrs/pyo3 that referenced this issue May 1, 2022
Automatically generate `python3.dll` import libraries for Windows
compile targets in the build script.

Adds a new PyO3 crate feature `generate-abi3-import-lib` enabling
automatic import library generation.

Closes PyO3#2231
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants