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

Document procedure for building Cython extension on Windows. #320

Open
casevh opened this issue Dec 21, 2021 · 8 comments
Open

Document procedure for building Cython extension on Windows. #320

casevh opened this issue Dec 21, 2021 · 8 comments

Comments

@casevh
Copy link
Collaborator

casevh commented Dec 21, 2021

It is possible to build a Cython extension on Windows using the GMP, MPFR, and MPC DLLs included with the gmpy2 binary wheel.

Here is the rough outline:

  1. Download the gmpy2 repository - either by "git clone" or download the zip file.
  2. Download and install the mingw64 compiler from www.winlibs.com. I used the following version to compile GMP, MPFR, and MPC.

https://github.com/brechtsanders/winlibs_mingw/releases/download/10.3.0-12.0.0-9.0.0-r2/winlibs-x86_64-posix-seh-gcc-10.3.0-llvm-12.0.0-mingw-w64-9.0.0-r2.zip

  1. Replace cygwinccompiler.py
  2. Create libpythonXXX.a file
  3. Compile using:

py39 setup_cython.py build_ext -cmingw32 -I..\mingw64\shared\include -L..\mingw64\shared\lib --inplace

@phletic
Copy link

phletic commented Dec 21, 2021

Sorry. I can't get it to work

@phletic
Copy link

phletic commented Dec 21, 2021

Nevermind. I just got a linux VM and it works... Sorry for the trouble....

@GBillotey
Copy link

@casevh

First, thanks a lot for this input which gave me the energy to give a try myself.

I have been able to build a Cython extension with MS Visual studio linked to the GMP, MPFR, and MPC dlls included with the last gmpy2 binary and also numpy libraries.
This build was run on a github-runner and the script is available, (as I do not have access to a windows machine it included a few shots in the dark)...

The cython extension code :
https://github.com/GBillotey/Fractalshades/blob/master/src/fractalshades/mpmath_utils/FP_loop.pyx

The key parts

  • includes the headers in gmpy2 installation directory
  • An import library (.lib file) is necessary when calling functions in a dll - for MS Visual studio - it provides the stubs that hook up to the DLL at runtime. To generate these .lib I used dumpbin & lib executables provided by visual studio, see the dll2lib.bat which I call through the python script patch_gmpy2.py in win_build subfolder

I think including the headers and .lib files as data files within gmpy2 windows distribution would ease a lot the process, that could be run with a standard python setup.

The final content of gmpy2 install folder before running the build is

executed, folder content:
['gmp.h', 'gmpy2.cp38-win_amd64.pyd', 'gmpy2.h', 'gmpy2.pxd', 'libgcc_s_seh-1.dll', 'libgcc_s_seh-1.lib', 'libgmp-10.dll', 'libgmp-10.lib', 'libmpc-3.dll', 'libmpc-3.lib', 'libmpfr-6.dll', 'libmpfr-6.lib', 'libwinpthread-1.dll', 'libwinpthread-1.lib', 'mpc.h', 'mpf2mpfr.h', 'mpfr.h', '__init__.pxd', '__init__.py', '__pycache__']

The windows test suite still have unrelated fails, however the keys tests that call this library passed.
When the script is more polished I would be happy to share or contribute to gmpy2 documentation if it is considered a useful point.

@casevh
Copy link
Collaborator Author

casevh commented Jan 21, 2022

@GBillotey

Thank you! You gave me a nudge in the right direction. I'll try to look at it this weekend.

BTW, fractalshades looks cool.

casevh

@GBillotey
Copy link

Thanks for the feedback on fractalshades! This is a "home-entertainment" application of gmpy, in a way ;)
Being able to compile it under windows would probably make it usable by more people.

@GBillotey
Copy link

Just a quick update : I pushed the Windows binary wheels for this project to pypi, and can confirm a "pip install" just works - gmpy2 is specified as an install dependency for the package.

https://pypi.org/project/fractalshades/0.4.3/#files

@Kalanyr
Copy link

Kalanyr commented Oct 24, 2023

This issue is occurring again as of Python 3.12 ( I assume because there's no pre-built wheel for Windows for 3.12 ) , I have Visual Studio 22 installed but pip can't find the gmp.h file to build

@casevh
Copy link
Collaborator Author

casevh commented Oct 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants