Setting up a MinGW-w64 build environment

From ASCEND
Revision as of 13:01, 12 August 2013 by Jpye (talk | contribs) (SWIG)
Jump to navigation Jump to search

This page documents a suggested C/C++/Fortran/Python software build environment for Windows, tested on Windows 7 Professional 64-bit. The build environment is capable of building both 64-bit and 32-bit software, although some manual editing of configuration files is required to switch between the two. With some fairly obvious changes, these instructions will also work for building 32-bit software using Windows XP 32-bit.

The suggested build environment includes

  • 7-zip file archive tool
  • Python 2.7.5 scripting language
  • MSYS 20111123, plus findutils 4.4.2
  • GCC 4.x compiler suite
  • SCons 2.3.0 software build tool
  • gedit 2.30.1 text editor
  • SWIG 2.0.10 wrapper interface generator
  • NSIS installation package builder
  • Dependency Walker 2.2 library/program file dependency checker
  • Subversion 1.8.0 version control software

These steps were completed by a user with administrator privileges. It is known that the Python, MinGW etc can be installed by non-administrator users, but we haven't yet tested that scenario, unfortunately. Problems may potentially arise in relation to 7-zip, gedit, NSIS or Subversion if administrator privileges are not available (but that's just guessing).

First steps

  • Download and install the 64-bit version of 7-zip. We used the 64-bit version, 7z920-x64.msi.
  • Download and install the latest Python 2.7 release. We used the 64-bit version if 2.7.5, python-2.7.5.amd64.msi from this page. If you want to build Python extensions, as we do with ASCEND, then you need to use the 64-bit version of Python with the 64-bit C/C++ compiler, or the 32-bit version of Python with the 32-bit C/C++ compiler. If you install the 32-bit version of Python, we suggest you select the installation directory as c:\Python27_32, whereas for the 64-bit version, use the default c:\Python27.
  • Download and install the gedit text editor for Windows. We used this package: gedit-setup-2.30.1-1.exe (32-bit)
  • Download and install NSIS. We used nsis-2.46-setup.exe. You can tell the installer not to run NSIS, no need for that.

MinGW-w64

MinGW-w64 (Minimalist GNU for Windows, 64-bit) is a version of the earlier MinGW version of GCC (GNU Compiler Collection: C, C++ and Fortran compilers and associated tools) that was carefully extended to include support for 64-bit Windows. It works with both 32-bit and 64-bit Windows, but you need to make sure you download the right package according to which platform you want to target (and which platform you are building on, too). We're aiming for the following file structure:

c:
  \mingw
        \64
        \32
        \msys

Download x86_64-w64-mingw32-gcc-4.8.0-win64_rubenvb.7z, which provides you with the 64-bit MinGW-w64 GCC compiler targeting 64-bit Windows. Once you have the downloaded file, right-click it, select "7-zip" and "Open archive". Double click 'mingw64', then select all of the sub-files and subfolders under there (bin, lib, include, and the rest). Click "Extract" and type in c:\mingw\64. Then check in Windows Explorer that the files have appeared as expected. You should have c:\mingw\64\bin, c:\mingw\64\lib, and so on.

Optional. If you also want to be able to build 32-bit stuff, download i686-w64-mingw32-gcc-4.8.0-win32_rubenvb.7z and extract the contents, using the same method above, into c:\mingw\32. This is the compiler for making 32-bit packages, and it only requires 32-bit Windows to run, so all of the above should work fine even on native 32-bit Windows XP, for example (but for 32-bit windows, you need to also select 32-bit Python and 7-zip above).

MSYS

MSYS provides a command-line interface to MinGW that looks and feels like Linux/Unix terminal and the Mac OSX terminal. It includes a large number of very useful utilities like 'wget', 'grep', and many others which will be useful when building software with GCC.

  • Download MSYS-20111123.zip and extract the contents into c:\mingw\msys. This can be done by locating the downloaded MSYS-20111123.zip file, double-clicking it, then when you see the folder 'msys', copy it and paste it into the directory c:\mingw (navigate into c:\ then mingw then paste).
  • In Windows Explorer, navigate to c:\mingw\msys\postinstall and run the batch-file pi (aka pi.bat). This will pop up a console with some questions in it. You need to say that you have installed MinGW and that its location is c:/mingw/64. The script should complete with a few minor messages.
  • Open gedit (from the Start menu) and check the contents of the file c:\mingw\msys\etc\fstab (no filename extension). It should contain simply the text as shown here:
c:/mingw/64   /mingw
  • In Windows Explorer, navigate to c:\mingw\msys and create a shortcut to the batch-file msys.bat. Right-click the resulting shortcut and select to change its icon to c:\mingw\msys\msys.ico. Move the resulting shortcut to your desktop.
  • Test that MSYS is working correctly:

  • In gedit, open the file c:\mingw\msys\home\yourusername\.profile (no file extension). In that file, enter the following text, and save the resulting file.
export PATH=/c/Python27:/c/Python27/Scripts:$PATH
alias gedit="/c/Program\ Files\ \(x86\)/gedit/bin/gedit"

SCons

tar zxvf scons-2.3.0.tar.gz
python setup.py bdist_wininst
start dist/scons-2.3.0-win-amd64.exe 
  • Step through the installer process, then return to your MSYS window and type
cp /c/Python27/Scripts/scons.py /c/Python27/Scripts/scons
  • Test that SCons is working by typing scons --version. You should get a copyright message including text to show that the you have version 2.3.0 of SCons running.

Dependency Walker

  • Download the 'x64' version of Dependency Walker. We downloaded this file: depends22_x64.zip. If you are also building 32-bit applications, you will also need the i686 version of Dependency Walker.
  • Unpack the .exe and .dll files into the directory c:\mingw\64\bin, for the 64-bit version and/or c:\mingw\32\bin for the 32-bit version. By utting the files here you will automatically get access to the correct version of depends corresponding to the 32- or 64-bit mode you are running via /etc/fstab.
  • Test that it's working by typing into MSYS the command depends /mingw/bin/gcc.exe. A window should pop up showing the DLLs that your GCC compiler depends on.

SWIG

  • Download a pre-compiled 'swigwin' copy of SWIG. We used swigwin-2.0.10.zip. As SWIG is a code pre-processor, it doesn't matter whether it's 32-bit or 64-bit, they will be perfectly equivalent.
  • Create a folder c:\mingw\msys\opt using Windows Explorer, then copy the 'swigwin-2.0.10' folder out of the Zip archive and into the new location c:\mingw\msys\opt\swigwin-2.0.10
  • In MSYS, type gedit ~/.profile, then add the SWIG directory to your PATH in your profile, so that it reads:
export PATH=/c/Python27:/c/Python27/Scripts:$PATH
alias gedit="/c/Program\ Files\ \(x86\)/gedit/bin/gedit"
export PATH=$PATH:/opt/swigwin-2.0.10
  • Close your MSYS window and reopen it, so that the ~/.profile will be re-read.
  • Test that SWIG is accessible by typing swig -version. You should see the version number and some other information output.

Setup Python for compilation of extensions

Before you can built any Python extensions using MinGW-w64 (which includes via SWIG), some preparation is required. One C header file, pyconfig.h has to be patched, and linker definitions for python27.dll have to be prepared for the compiler. Follow these instructions to sort it out.

First download, build, and install a utility called 'gendef' which builds library *.def files that MinGW-w64 can use when linhing to DLLs. This is because the python27.lib file distributed with Python doesn't work with MinGW-w64. In your MSYS shell, type

svn co  svn://svn.code.sf.net/p/mingw-w64/code/trunk/mingw-w64-tools/gendef -r5774 ~/gendef
cd ~/gendef 
./configure --prefix=/mingw
make -j4 && make install
gendef --help

Now prepare a new python27.def file from the installed copy of python27.dll in c:\windows\system32:

  • Using Windows Explorer, copy the file c:\windows\system32\python27.dll into the folder c:\Python27\Libs
  • Using MSYS, enter the following:
cd /c/Python27/libs
mv python27.lib old-python27.lib
gendef python27.dll

If this works OK, you should have a new file c:\Python27\Libs\python27.def.

Next, patch the c:\Python27\include\pyconfig.h file:

  • Open the file c:\Python27\include\pyconfig.h using gedit
  • Search for the text #ifdef _WIN64, which in our copy of this file was at line 141, and cut out the following three lines:
#ifdef _WIN64
#define MS_WIN64
#endif
  • Search for the text #ifdef _MSC_VER, which in our copy of this file was at line 107. Paste in the cut-out lines, ABOVE the #ifdef _MSC_VER.

This issue is reported as python bug 4709, in the Python bug tracker, so possibly it will have been fixed if you use a newer version of Python than we tested with.

findutils

The find and xargs tools in Linux are very useful for searching for files and are furthermore occasionally used in build scripts, so are good to have around. As an example, to selectively remove all "*.o" (compilers object files) in a particular directory, you can (carefully) use find . -name "*.o" | xargs rm

  • Download findutils-4.4.2-2-msys-1.0.13-bin.tar.lzma and save the file to your c:\mingw\msys\home\yourusername folder (a good way with the command-line is to right-click, and copy the URL, then use wget in MSYS)
  • In MSYS, unpack the tarball into your MSYS executable path:
cd /
tar Jxvf ~/findutils-4.4.2-2-msys-1.0.13-bin.tar.lzma
  • Test that it's working using (you should see your ~/.profile filename listed as output):
cd ~
find . -name ".*file"

GTK+2

GTK+ is useful for developing cross-platform graphical user interface (GUI) programs. It works well under Windows, and it is the native GUI toolkit for Ubuntu, Fedora, Mint, and other Linux distributions. It has also been ported to Mac, although getting up and running on that platform is still a bit difficult. The ASCEND project provides some pre-compiled GTK+ installers that make getting started with GTK+ very straightforward.

export PATH=/c/Python27:/c/Python27/Scripts:$PATH
alias gedit="/c/Program\ Files\ \(x86\)/gedit/bin/gedit"
export PATH=$PATH:/opt/swigwin-2.0.10
export PATH=$PATH:/c/Program\ Files/GTK+-2.22/bin
  • Close your MSYS and re-open it so that the updated ~/.profile is loaded.
  • Test that GTK+ is working by typing either pkg-config gtk+-2.0 --libs or gtk-demo. For the latter, you should see a GUI window pop up that shows off the various GTK+ features.

Note that GTK+3 is now available, however we haven't tested it on Windows up to now.

Subversion

Subversion is a version control software. ASCEND uses it for hosting our source code, as do many other projects, especially on SourceForge.net and Google Code.

  • Download and install the 64-bit version of SlikSVN, Slik-Subversion-1.8.0-x64.msi from the SlikSVN website. You can use the 'typical' settings and default installation location, which is c:\Program Files\SlikSvn
  • Edit your ~/.profile, adding the final line so that you have
export PATH=/c/Python27:/c/Python27/Scripts:$PATH
alias gedit="/c/Program\ Files\ \(x86\)/gedit/bin/gedit"
export PATH=$PATH:/opt/swigwin-2.0.10
export PATH=$PATH:/c/Program\ Files/GTK+-2.22/bin
export PATH=$PATH:/c/Program\ Files/SlikSvn/bin
  • Close your MSYS and re-open it so that the updated ~/.profile is loaded.
  • Check that subversion is working by typing svn --version.

Usage tips

All done! You may wish to make a backup of your directory c:\mingw, which contains your complete MSYS and MinGW setup, for easy reinstallation later.

When building software on this platform using GNU AutoTools, (ie, the typical './configure && make && make install' instructions), you should specify

./configure --prefix=/mingw
make 
make install

This will have the result that libraries and new tools will be installed into c:\mingw\64.

If you ever want to switch building 32-bit code, you simply edit c:\mingw\msys\etc\fstab and change c:/mingw/64 /mingw to c:/mingw/32. Obviously you must install the MinGW-w64 32-bit compiler into that other location in that case. You also need to edit your ~/.profile to point to the alternative Python location, so that you can link against 64-bit Python in any packages you are going to build (also so that SCons scripts correctly determine your 64-bit target architecture). (We're going to try to figure out a way to make these changes simpler, eg by changing only /etc/fstab and not also ~/.profile.)

Example of building a simple program

If everything is working, you should be able to enter the following simple C program into gedit, and save it into c:\mingw\msys\home\yourusername\test.c:

#include <stdio.h>
int main(){
  printf("Hello, world!\n");
  return 0;
}

Then compile the file and run it by typing

gcc test.c
./a.exe

And you should see

Hello, world!

TODO: expand with simple MinGW-w64 example using SCons as the build tool.