Cross-Compiling for MS Windows

Cross-compiling Gwyddion for MS Windows under Linux is quite similar to normal Unix compilation with certain additional setup and extra steps. Although the process is quite smooth the initial setup may seem a bit complicated. If, in addition, you are not familiar with the normal Unix compilation you might wish to start with that and attempt cross-compilation once you familiarise yourself with the basic procedure.

These instructions describe compilation under Fedora using its MinGW cross-compilation support as this is what Gwyddion developers use. In general, the instructions work on the current version of Fedora. Compilation on other versions and other RedHat-based distributions (CentOS, Scientific Linux, …) should be similar and relatively straightforward, possibly with some tweaks. Building on, for instance, openSUSE will require modifications. Reports of success (or failure) of cross-compilation of Gwyddion in other distributions and environments and namely improvements to these instructions are welcome.

Full cross-compilation has the following steps:

A script is available that automatically performs all the steps, as described below.

Setup

Before the first compilation you must set up the cross-compilation environment. This has to be done only once.

Base MinGW Packages

Run as root:

yum install mingw{32,64}-{gcc-c++,gtk2,libxml2,minizip}

to install the necessary mingw32 and mingw64 packages (several more packages will be installed as dependencies of those explicitly given here).

Gwyddion.net repository

MinGW versions of a few packages used by Gwyddion are not available in Fedora yet. You can build them using the patches and spec files at http://sourceforge.net/projects/gwyddion/files/mingw32-cross-compile/, however, it should be much easier to just install them using yum. For this, download and install the gwyddion.net repository configuration package. The installation makes available all the additional MinGW packages. After installing it you can run

yum install mingw32-{fftw,gtkglext,gtksourceview2}

Incidentally, the repository also contains a native Gwyddion package that you can install to use Gwyddion on Fedora; and a package with cross-compiled Gwyddion libraries that can be used to cross-compile modules.

Wine

Wine is the MS Windows compatibility layer/emulator for Unix. It is used to run NSIS that creates the executable Gwyddion Windows installer. Wine can also be used to run and test the cross-compiled Gwyddion, as described below.

Run

yum install wine

to install Wine.

NSIS

Nullsoft scriptable install system (NSIS) is used to create the Gwyddion installer. This is a MS Windows program, therefore, it is installed under Wine. A cross-compiled version of NSIS might be available in the distribution but we have found the original more reliable.

Download NSIS from its web page and run

wine nsis-2.46-setup.exe

replacing 2.46 with the actual version. Version 2.46 of NSIS is the oldest that has been tested.

Python

To compile pygwy you need to install Python into Wine. The steps are the same as if you just want to use pygwy, except that all packages listed in Enabling pygwy need to be installed using msiexec:

wine msiexec /i python-2.7.3.msi
wine msiexec /i pygobject-2.28.3.win32-py2.7.msi
wine msiexec /i pycairo-1.8.10.win32-py2.7.msi
wine msiexec /i pygtk-2.24.0.win32-py2.7.msi

or similarly.

Support scripts

Support scripts and data are available in mingw32-cross-compile module in the Gwyddion subversion repository. Run

svn checkout http://svn.code.sf.net/p/gwyddion/code/trunk/mingw32-cross-compile

to check it out from the repository.

The most important tool you obtain is the cross-build-32 (or cross-build-64) script that automates all the cross-compilation steps. Before you use it for the first time, review file setup32 that defines where various things are located (or setup64 for the 64-bit target). The default contents looks as follows:

source_dir=$HOME/Projects/Gwyddion/gwyddion-mingw
mingw_prefix=/usr/i686-pc-mingw32/sys-root/mingw
target_prefix=$HOME/opt/gwyddion-mingw32
python_dir=$HOME/.wine/drive_c/Python27
nsis_compiler=C:\\Program\ Files\ \(x86\)\\NSIS\\makensis.exe

Variable source_dir specifies the location of the untarred or checked-out Gwyddion source code and it will likely need to be adjusted. Variable target_prefix specifies the installation directory (staging area) for the cross-compiled Gwyddion. The default value should be reasonable and you do not need to change it unless you want to. The remaining variables, mingw32_prefix, nsis_compiler and python_dir, specify the location of MinGW files, NSIS compiler and Win32 Python, respectively. They do not need to be changed from the default values under normal circumstances although NSIS can be installed in either Program Files (x86) or Program Files by default depending on Wine configuration. Note setup is read by shell so there must not be any spaces around =.

Compilation

The setup was tedious but it was worth it because the compilation is then extremely simple. Run

./cross-build-32

in mingw32-cross-compile directory to build Win32 insstaller. That's all. If it succeeds an executable Gwyddion Windows installer with bundled GTK+ and everything will be created in $target_prefix. Similarly, the Win64 installer is built just with

./cross-build-64

Note the scripts run autogen.sh but do not clean the source code directory. You may wish to do that manually if you compile Gwyddion repeatedly.

You can make a coffee meanwhile – or study the cross-build script (it is actually quite short and clear).

Running under Wine

Compiled Gwyddion can be run under Wine. Assuming the default value of target_prefix:

wine ~/opt/gwyddion-mingw32/bin/gwyddion.exe

To run gwyddion.exe the dynamic linker must be able to find all the necessary DLLs. This is ensured in a somewhat crude way by script copysysfiles that copies all necessary MinGW files from system to $target_prefix. Since copysysfiles is executed by cross-build you normally do not need to execute it manually.

The second step that might be necessary is setting registry key

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\gwyddion.exe

to point to gwyddion.exe and the value Path to point to the bin subdirectory.

Cross-compilation of standalone modules

Cross-compilation of standalone modules requires only Gwyddion headers and Win32 development libraries. While they can be surely obtained by cross-compiling entire Gwyddion is it not necessary to do so. By compiling only the libraries you can avoid the installation of various rarer Gwyddion dependencies. This can be achieved using the patch gwyddion-2.22-build-only-libs.patch available among the build scripts.

But it gets even easier, the MinGW Gwyddion libraries are available as a RPM package mingw32-gwyddion-libs in the gwyddion.net repository.

Once you have this package installed you can try cross-compilation of the sample standalone module threshold-example that is available in the subversion repository (or as a tarball). See README therein for some more details.