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:

dnf install mingw{32,64}-{gcc-c++,gtk2,libxml2,minizip,fftw,gtkglext,libwebp,OpenEXR}

to install the necessary mingw32 and mingw64 packages. Several more packages will be installed as dependencies of those explicitly given here. Note that, technically, some of the packages are optional dependencies and you can build a MS Windows installer without them (after some adjustments). Neverhteless the standard installers include these packages and the cross-compilation scripts expect them to be present by default.

Gwyddion.net repository

MinGW versions of a few packages used by Gwyddion are not available in Fedora yet (or any more), or there may be some patches we would like to include but they are not in Fedora packages. This includes HDF5 and gtksourceview2 (only used by pygwy).

You can build these additional packages 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 dnf. For this, download and install the gwyddion.net repository configuration package. The installation brings two repository configurations, for the program and for the extra MinGW packages. By default only the gwyddion repository is enabled. Enable gwyddion-manually by editing /etc/yum.repos.d/gwyddion-mingw.repo for the MinGW packages. Run the following command to install them:

dnf install mingw32-{hdf5,gtksourceview2}

Be warned the repo overrides a few Fedora MinGW packages to keep them at specific versions.

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

dnf 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-3.06-setup.exe

replacing 3.06 with the actual version. Version 3.06 is the oldest NSIS 3 that has been tested, but any version probably works.

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.16.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 unpacked 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

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

Note the cross-build scripts run autogen.sh but do not clean the source code directory. You may wish to do that manually if you compile Gwyddion repeatedly. Especially if you build for both architectures in the same directory, make sure to run

make distclean

between the builds to get the source directory back to a well-defined state.

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.