aleaxit / gmpy

General Multi-Precision arithmetic for Python 2.6+/3+ (GMP, MPIR, MPFR, MPC)
https://gmpy2.readthedocs.io/en/latest/
GNU Lesser General Public License v3.0
513 stars 87 forks source link

Document procedure for building Cython extension on Windows. #320

Open casevh opened 2 years ago

casevh commented 2 years ago

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 commented 2 years ago

Sorry. I can't get it to work

phletic commented 2 years ago

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

GBillotey commented 2 years ago

@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

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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 11 months ago

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 commented 10 months ago

Can you try compiling gmpy2 from source? After cloning the source, run the following command(s) using the "x64 Native Tools Command Prompt".

py -3.12 setup.py build_ext --force bdist_wheel

or

py -3.12 -m pip install build py -3.12 -m build --wheel

Both commands should result in a binary wheel.

Please let me know if this doesn't work.

casevh

On Mon, Oct 23, 2023 at 9:51 PM Kalanyr @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/aleaxit/gmpy/issues/320#issuecomment-1776535338, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMR23ZQP5WUTOTQWRTMABDYA5CMVAVCNFSM5KPKKEX2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZXGY2TGNJTGM4A . You are receiving this because you were mentioned.Message ID: @.***>