Building gmpy2 with MSYS2
=========================

Note: These instructions are a work in progress!

From the MSYS2 home page:

http://sourceforge.net/projects/msys2/

MSYS2 is a Cygwin-derived Posix-like environment for Windows. The name is a
contraction of Minimal SYStem 2, and aims to provide support to facilitate
using the bash shell, Autotools, revision control systems and the like for
building native Windows applications using MinGW-w64 toolchains.

Installing MSYS2
================

MSYS2 installation instructions can be found at:

http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/

Some of the examples assume the 64-bit version of MSYS2 is installed. If you
install the 32-bit version of MSYS2, you will need to change any references
of C:\msys64 to C:\msys32.

After following the installation and upgrade steps documented above, you will
need to install the MinGW compiler toolchain(s). Both toolchains can be
installed at the same time. To install the 64-bit toolchain:

$ pacman -S mingw-w64-x86_64-toolchain

To install the 32-bit toolchain:

$ pacman -S mingw-w64-i686-toolchain

The following additional packages should also be installed:

$ pacman -S patch m4 lzip wget tar make diffutils

The MSYS2 build environment
===========================

The build process is split into two phases. During phase 1 we use the MSYS2
environment to build statically linked versions of GMP, MPFR, and MPC. Once
those libraries are build, we will use the Windows command prompt to build
the actual gmpy2 extension (DLL). We do not use the versions of Python
included with MSYS2 or MinGW to compile gmpy2.

Commands that must be executed from an MSYS2 shell are preceded by "$".
Commands that must be executed from a Windows command prompt are preceded
by ">". You will need to run the commands *after* "$" or ">".

The MSYS2 environment provides three different command line operating
environments.

 * msys2_shell.bat is the general-purpose shell but it does not provide
   access to the MinGW compiler suite.
 * mingw64_shell.bat provides access to the 64-bit MinGW compiler suite.
 * mingw32_shell.bat provides access to the 32-bit MinGW compiler suite.

MSYS2 does include versions of GMP, MPFR, and MPC but we will compile our
own version directly from the source. We will specify the options that are
appropriate for building a statically linked Windows DLL.

MSYS2 can support both MinGW32 and MinGW64 toolchains simultaneously. We
use /mingw32/opt and /mingw64/opt as the destinations for the 32-bit and
64-bit versions of GMP, MPFR, and MPC. The sources for GMP, MPFR, and MPC
will be placed in /mingw32/src or /mingw64/opt.

64-bit Build
============

# Start the appropriate shell: MSYS2 WinGW 64-bit

# Create a new directies as the destinations for GMP, MPFR, and MPC.

  $ mkdir /mingw64/opt
  $ mkdir /mingw64/src

# Download and uncompress GMP, MPFR, and MPC
  $ cd /mingw64/src

# Download GMP
  $ wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.lz
  $ tar xf gmp-6.1.2.tar.lz

# Download MPFR and any patches
  $ wget http://www.mpfr.org/mpfr-current/mpfr-3.1.5.tar.bz2
  $ tar xf mpfr-3.1.5.tar.bz2
  $ cd mpfr-3.1.5/
  $ wget http://www.mpfr.org/mpfr-current/allpatches
  $ patch -N -Z -p1 < allpatches
  $ cd ..

# Download MPC
  $ wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
  $ tar xf mpc-1.0.3.tar.gz

# Compile GMP
  $ cd gmp-6.1.2/
  $ ./configure --prefix=/mingw64/opt --enable-static --disable-shared --enable-fat --with-pic

    *Experimental for mingw64 build*

    mp_bitcnt_t should really be a 64-bit unsigned integer. The following sed
    command makes the change. It should only be used for 64-bit builds.

    This will require changes to gmpy2 source also. See gmpy2_convert_utils.h.

  $ mv gmp.h gmp.original
  $ sed 's/typedef\s*unsigned\s*long\s*int\s*mp_bitcnt_t/typedef unsigned long long int  mp_bitcnt_t/g' gmp.original > gmp.h

  $ make
  $ make check
  $ make install
  $ cd ..

# Compile MPFR
  $ cd mpfr-3.1.5/
  $ ./configure --prefix=/mingw64/opt --enable-static --disable-shared --disable-thread-safe --enable-gmp-internals --with-pic --with-gmp=/mingw64/opt
  $ make
  $ make check
  $ make install
  $ cd ..

# Compile MPC
  $ cd mpc-1.0.3/
  $ ./configure --prefix=/mingw64/opt --enable-static --disable-shared --with-pic --with-gmp=/mingw64/opt --with-mpfr=/mingw64/opt
  $ make
  $ make check
  $ make install
  $ cd ..

32-bit Build
============

# Start the appropriate shell: MSYS2 WinGW 32-bit

# Create a new directies as the destinations for GMP, MPFR, and MPC/

  $ mkdir /mingw32/opt
  $ mkdir /mingw32/src

# Download and uncompress GMP, MPFR, and MPC
  $ cd /mingw32/src

# Download GMP
  $ wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.lz
  $ tar xf gmp-6.1.2.tar.lz

# Download MPFR and any patches
  $ wget http://www.mpfr.org/mpfr-current/mpfr-3.1.5.tar.bz2
  $ tar xf mpfr-3.1.5.tar.bz2
  $ cd mpfr-3.1.5/
  $ wget http://www.mpfr.org/mpfr-current/allpatches
  $ patch -N -Z -p1 < allpatches
  $ cd ..

# Download MPC
  $ wget ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
  $ tar xf mpc-1.0.3.tar.gz

# Compile GMP
  $ cd gmp-6.1.2/
  $ ./configure --prefix=/mingw32/opt --enable-static --disable-shared --enable-fat --with-pic
  $ make
  $ make check
  $ make install
  $ cd ..

# Compile MPFR
  $ cd mpfr-3.1.5/
  $ ./configure --prefix=/mingw32/opt --enable-static --disable-shared --disable-thread-safe --enable-gmp-internals --with-pic --with-gmp=/mingw32/opt
  $ make
  $ make check
  $ make install
  $ cd ..

# Compile MPC
  $ cd mpc-1.0.3/
  $ ./configure --prefix=/mingw32/opt --enable-static --disable-shared --with-pic --with-gmp=/mingw32/opt --with-mpfr=/mingw32/opt
  $ make
  $ make check
  $ make install
  $ cd ..


The Windows build environment
=============================

# Configure Windows command prompt - GCC

  The MinGW toolchain must be accessible by the normal Windows installation of
  Python. The 64-bit MinGW toolchain is located in C:\msys64\mingw64\bin. The
  32-bit MinGW toolchain is located in C:\msys64\mingw32\bin. The MinGW
  location can be added to the system Path permanently or can be added manually
  after starting the Windows command prompt. We will do it manually. After
  starting the Windows command prompt, add either the 64-bit or 32-bit
  MinGW to the Path with one of the following commands:

  > set Path=C:\msys64\mingw64\bin;%Path%
  -- or --
  > set Path=C:\msys64\mingw32\bin;%Path%

  To verfiy the MinGW toolchain is accessible, the command "gcc -v" should
  return information about the gcc compiler.

  Note: All the remaining instructions assume this step has been completed and
        that "gcc -v" returns information for the correct version of gcc.

# Configure Windows command prompt - Python
  The Python interpreter does not need to be accessible via the path. You can
  specify the full path name to start a particular version. To simplify the
  compilation of multiple versions of gmpy2, I have adopted the following
  approach:

  # 32-bit versions of Python are installed in C:\32
  # 64-bit versions of Python are installed in C:\64
  # Aliases are defined to create shortcuts for invoking a Python interpreter

  I use the following batch file to configure a command prompt for 32-bit builds:

  @echo off
  doskey py26=C:\32\Python26\python.exe $*
  doskey py27=C:\32\Python27\python.exe $*
  doskey py34=C:\32\Python34\python.exe $*
  doskey py35=C:\32\Python35\python.exe $*
  doskey py36=C:\32\Python36\python.exe $*
  set Path=C:\msys64\mingw32\bin;%Path%

  I use the following batch file to configure a command prompt for 64-bit builds:

  @echo off
  doskey py26=C:\64\Python26\python.exe $*
  doskey py27=C:\64\Python27\python.exe $*
  doskey py34=C:\64\Python34\python.exe $*
  doskey py35=C:\64\Python35\python.exe $*
  doskey py36=C:\64\Python36\python.exe $*
  set Path=C:\msys64\mingw64\bin;%Path%

# Modify distutils library

  The gmpy2 extension is built using the distutils module. Recent versions of
  the gcc compiler no longer support an option that is used by older versions
  of distutils. In the file "cygwincompiler.py", remove all occurences of the
  "-mno-cygwin" option. The "-mno-cygwin" option was already removed in Python
  2.7.8, 3.3.5, and 3.4.1. The option was present in Python 2.6.6 and 3.2.5.
  You will need to check your specific version.

  In the file "cygwincompiler.py", find the definition for the class
  "Mingw32CCompiler". In the "__init__" function comment out the line:
  "self.dll_libraries = get_msvcr()".

  In the file "cygwincompiler.py", find the definition for the class
  "Mingw32CCompiler". Under self.set_executables(...), change -O to -O2 and
  add "-fno-strict-aliasing" and "-fwrapv". After changes, it should look like

    self.set_executables(compiler='gcc -O2 -Wall -fno-strict-aliasing -fwrapv',
                         compiler_so='gcc -mdll -O2 -Wall -fno-strict-aliasing -fwrapv',
                         compiler_cxx='g++ -O2 -Wall -fno-strict-aliasing -fwrapv',
                         linker_exe='gcc',
                         linker_so='%s %s %s'
                                    % (self.linker_dll, shared_option,
                                       entry_point))

# Patch C:\Python27\include\pyconfig.h

  In that file, search for the text #ifdef _WIN64 and cut out the following
  three lines:

  #ifdef _WIN64
  #define MS_WIN64
  #endif

  Search for the text #ifdef _MSC_VER earlier in the file. Paste in the
  cut-out lines, ABOVE the #ifdef _MSC_VER.

# Create a libpython.a file

  MinGW requires a libpythonXX.a file that contains information about the C-API
  functions that exist in pythonXX.dll. Follow these steps for your version of
  Python.

  Note: The DLL locations are from a Windows 10 64-bit system with both 32-bit
        and 64-bit versions of a Python version installed. Your location may
        vary.

  # For a 32-bit versions (with mingw32 on the path)
  > cd C:\32\Python27\libs
  > gendef c:\Windows\SysWOW64\python27.dll
  > dlltool --dllname c:\Windows\SysWOW64\python27.dll --def python27.def --output-lib libpython27.a

  # For a 64-bit version (with mingw64 on the path)
  > cd C:\64\Python27\libs
  > gendef c:\Windows\System32\python27.dll
  > dlltool --dllname c:\Windows\System32\python27.dll --def python27.def --output-lib libpython27.a

# To compile gmpy2

  py27 setup.py build_ext --msys2 --prefix=c:\msys64\mingwXX\opt

# To install to a local Python installation (after compiling)
  py27 setup.py install


-------------------

misc

cd C:\32\Python27\libs
gendef c:\Windows\SysWOW64\python27.dll
dlltool --dllname c:\Windows\SysWOW64\python27.dll --def python27.def --output-lib libpython27.a
cd C:\32\Python32\libs
gendef c:\Windows\SysWOW64\python32.dll
dlltool --dllname c:\Windows\SysWOW64\python32.dll --def python32.def --output-lib libpython32.a
cd C:\32\Python33\libs
gendef c:\Windows\SysWOW64\python33.dll
dlltool --dllname c:\Windows\SysWOW64\python33.dll --def python33.def --output-lib libpython33.a
cd C:\32\Python34\libs
gendef c:\Windows\SysWOW64\python34.dll
dlltool --dllname c:\Windows\SysWOW64\python34.dll --def python34.def --output-lib libpython34.a


cd C:\64\Python27\libs
gendef c:\Windows\System32\python27.dll
dlltool --dllname c:\Windows\System32\python27.dll --def python27.def --output-lib libpython27.a
cd C:\64\Python32\libs
gendef c:\Windows\System32\python32.dll
dlltool --dllname c:\Windows\System32\python32.dll --def python32.def --output-lib libpython32.a
cd C:\64\Python33\libs
gendef c:\Windows\System32\python33.dll
dlltool --dllname c:\Windows\System32\python33.dll --def python33.def --output-lib libpython33.a
cd C:\64\Python34\libs
gendef c:\Windows\System32\python34.dll
dlltool --dllname c:\Windows\System32\python34.dll --def python34.def --output-lib libpython34.a
