Overview

This documentation provides information on how to use Swarm on Windows with MinGW compilers. The following topics will be covered:

How to run a Swam model compiled with MinGW on Windows: Once you produce a Swarm model using MinGW (or someone gives one to you), how do you run it on other machines without installing Swarm there?

How to use MinGW to compile your own Swarm models, either during model development or so you can distribute working models to others. This requires installing MinGW and our binary Swarm libraries.

How to package a model compiled with MinGW so it can be installed and run by itself on Windows machines.

How to use MinGW to compile the Swarm libraries, in case you want to build your own Swarm binaries.

Each topic is more difficult than the one before it. However the topics are independent in that it is not necessary to know how to compile the Swarm library in order to run a pre-compiled Swarm model or compile your own model against a pre-compiled version of Swarm.

Using Swarm Models Compiled with MinGW

A model that has been compiled using MinGW can be installed and run from a Windows computer without installing MinGW or Swarm. An example code is this build of Heatbugs:

Unzipping that file will create a folder called swarm which should be moved somewhere that is easy to remember. The rest of this tutorial will assume this Swarm folder has been placed at C:\swarm.

Before a Swarm model can be run, the swarm\bin folder must be added to the Windows PATH so that Heatbugs can find necessary libraries when it runs. The swarm\bin folder can be temporarily added to the path by executing the following in the Windows command line (the command line is accessed using the program cmd.exe):

path=%PATH%;C:\swarm\bin

However, it is possible to have conflicts if other versions of libraries used by Swarm, such as HDF5, are already installed and in your path. And some Swarm programs require that the environment variable SWARMHOME be defined, which will typically not be true. These problems can be avoided by using the following statements instead of the above path command:

SWARMHOME=C:\swarm
path=%SWARMHOME%\bin;%PATH%

You can alternatively add the Swarm folder to your PATH permanently by editing the Windows PATH environment variable. A tool like RapidEE makes doing so easy, or you can use use Windows control panel > System > Advanced > Environment Variables to add swarm\bin to the PATH system variable.

Once the path has been set to include the swarm\bin folder, Swarm models can be started through the Windows command line, or by double-clicking on them from the Windows Explorer.

The Heatbugs model can now be run by changing the directory of the command line to the folder holding heatbugs.exe and then executing the program. For example, if heatbugs.exe is located in C:\Users\example_user\Downloads, then the model may be run as follows:

cd C:\Users\example_user\Downloads
heatbugs.exe

(or you can just double-click on heatbugs.exe from Windows Explorer, which temporarily opens a Windows command terminal while the model runs.)

You can also create a simple Windows batch file that sets SWARMHOME and the path and then executes the model. Create a file called, for example heatbugs.bat in the same directory where heatbugs.exe is, containing these lines:

SWARMHOME=C:\swarm
path=%SWARMHOME%\bin;%PATH%
heatbugs.exe

Then, just double-clicking on heatbugs.bat will start the model.

If you try this and get an error that some .dll file is not found, then the problem is likely that the path is not set correctly to include the swarm\bin folder. If instead the program crashes without an error statement, then the problem is more likely that SWARMHOME needs to be set.

Installing Swarm and MinGW to compile models

To compile Swarm models from source using MinGW, the following simple steps need to be taken to set up the build environment:

Download compiled Swarm binaries, archived as swarm-2.4.1-win32.zip. Unzip the archive and place the extracted folder at <tt>C:\swarm</tt>.

NOTE:

The archived Swarm binaries MUST be placed at <tt>C:\swarm</tt> to successfully compile Swarm models. This is because some paths had to be hard-coded by <tt>libtool</tt> during the creation of the binaries.

The final step before you can compile a Swarm model is to set the <tt>SWARMHOME</tt> environment variable so that the "make" program knows where to find the Swarm libraries when compiling your code. There are several ways to do this.

You can temporarily set SWARMHOME by entering this command in the MinGW shell:

export SWARMHOME=/c/swarm

You can edit your Makefile so it starts with:

SWARMHOME=/c/swarm

Set SWARMHOME permanently as a Windows environment variable, either by using RapidEE or Windows control panel > System > Advanced > Environment Variables to add a new (User or System) variable called SWARMHOME with the value <tt>/c/swarm</tt>.

After MinGW has been installed and the Swarm binaries have been downloaded, Swarm models may be compiled using the MinGW shell. Start up the program MinGW Shell, cd to the directory containing your code and Makefile, and enter the command "Make".

VERY IMPORTANT NOTE: MinGW produces two executables called <tt>heatbugs.exe</tt>---one in the <tt>heatbugs</tt> folder and another in the <tt>heatbugs/.libs</tt> folder. You should ignore the executable in the top-level folder, <tt>heatbugs/heatbugs.exe</tt>, as it is merely a compiled wrapper script that executes <tt>heatbugs/.libs/heatbugs.exe</tt>. When distributing compiled models, only use binaries from the <tt>.libs</tt> folder.

The resulting executable, <tt>.libs/heatbugs.exe</tt> may be executing by following the steps described above at Using Swarm Models Compiled with MinGW. These steps must include setting the Windows Path to include <tt>C:\swarm\bin</tt>.

Distributing a Swarm Model for Windows Users

A major benefit of the MinGW version of Swarm is that models can be distributed for people to use without installing Swarm. A simple way to do this, after the model has been compiled and tested using methods in the previous section, is to create a ZIP archive that contains all the files to run the model.

Create a directory tree that includes the Swarm binary files. For example, create C:\MySwarmModel and add the Swarm binaries (as described above) to it as a subdirectory tree.

Create another subdirectory that includes just the compiled model.

Create a third subdirectory that includes any input files that the model needs and a batch file that sets the path and executes the model.

Then users can run the model just by double-clicking on <tt>MySwarmModel.bat</tt>.

Building Swarm Using MinGW

This tutorial provides directions and code for compiling your own Swarm libraries for Windows. It is a bit advanced---some familiarity with compiling software from source in a Linux environment and knowledge of the UNIX command line is assumed.

Install Required Tools

The first step is to download and install tools required for compiling Swarm. These links provide directions:

patches: This folder contains patchfiles that modfy source code so that MinGW can compile it.

After downloading and extracting the source code archive, open up a MinGW shell and run the following commands:

cd /path/to/where/swarm/source/was/extracted
mkdir build

The following steps will all assume your MinGW shell is operating out of the <tt>build</tt> directory. All compiled software will be installed to <tt>/c/swarm</tt> (The Windows Explorer will see the folder appear at <tt>C:\swarm</tt>).

ZLib

ZLib will be the first library to be built as it is a common dependency for many of the other software components. From the <tt>build</tt> directory, execute the following to build Zlib:

The patchfile is inherited from the SUSE builds of XPM for MinGW and adds Makefiles and other critical components.

One major modification has been made to the SUSE patchfile---<tt>tcl.h</tt> is included in <tt>xpm.h</tt>. This is required in order to use XPM while Tk is installed. Conversely, this means that this build of XPM cannot be used without having Tk installed.

LibFFI

Starting from the <tt>build</tt> directory, LibFFI can be built and installed as follows:

At two places in the TCL source code, an assignment is made while simultaneously performing a typecast. The typecast and the assignment had to be split into separate operations in order to be acceptable to the MinGW compilers.

Tk

Starting from the <tt>build</tt> directory, Tk can be built and installed as follows:

BLT

HDF5 (Optional)

Getting HDF5 to work with MinGW is a little tricky. It is an optional dependency, so if support for reading and writing <tt>.hdf</tt> files is not needed this section can be skipped.

As of version 1.8.7, HDF5 currently does not compile under MinGW. So, we must wrap pre-compiled binaries for use with MinGW. In order to wrap the binaries, we will need an additional tool called <tt>pexports</tt>. This can be installed using <tt>mingw-get</tt>:

mingw-get install mingw-utils

Download 32-bit Windows binaries from the HDF website compiled for use with the Visual Studio compilers (these files have &quot;VS&quot; listed under the Compilers section). After extracting the archive, the HDF5 libraries can be wrapped for use with MinGW by executing the following commands:

Finally, a patch must be applied to the HDF header file <tt>H5public.h</tt> so that it doesn't define <tt>ssize_t</tt>---a constant already defined by MinGW. The required patch is contained in the Swarm MinGW source archive:

If you wrapped HDF5 up for use with MinGW, add <tt>--with-hdf5dir=/c/swarm</tt> to the list of arguments passed to <tt>configure</tt>.

Summary of patchfile changes:

<tt>swarm-2.4.1-mingw.patch</tt> modifies the Swarm source code in the following ways:

<tt>gettimeofday</tt> is no longer defined in the Swarm sources as MinGW includes this function in its standard library.

Swarm GUI functions are updated to be compatible with LibPNG 1.5.

The Swarm <tt>configure</tt> script defines <tt>DATADIR</tt> as a preprocessor macro---this conflicts with variables in the GCC Objective-C library. <tt>DATDIR</tt> is <tt>undef</tt>ed in critical parts of the source code.

The <tt>sleep</tt> function is aliased to <tt>usleep</tt>---which is the name of the MinGW implementation.

The way in which Makefiles invoke <tt>gperf</tt> is amended so that it actually produces output.

NULL checks are added to `zstrdup` in src/defobj/internal.m. Some functions in the Swarm library may attempt to call `zstrdup` on a NULL pointer. (This happens if the SWARMHOME environment variable is not set.) Failing to check for this condition can lead to segfaults. `zstrdup` now returns NULL if passed a NULL string or if `alloc` fails to allocate memory for a duplicate.

Full details are contained in the patchfile.

Tools

MinGW

MinGW is a set of software that aims to provide an environment where software developed on UNIX-like systems can be easily compiled for Windows. Since Swarm is primarily developed on Linux (a UNIX clone), MinGW can ease the task of creating Swarm models that run on Windows.

MinGW is similar to the Cygwin project. The major difference is that MinGW produces executables that depend only on native Windows libraries while Cygwin executables require special compatibility libraries that are part of the Cygwin distribution.

Installing MinGW

MinGW can be installed by downloading the &quot;Automated MinGW Installer&quot;, which usually has a name like <tt>mingw-get-inst-&lt;date&gt;.exe</tt> where <tt>&lt;date&gt;</tt> is the datestamp for when the installer was packaged. Running this executable will start the installation process. The installer lets you select which optional components to install; make sure all boxes are checked so you install all components.

The installer downloads all components from the internet. This will take a while, so have a cup of coffee or walk the dog.

See the following section about also installing an optional, more-functional shell window for MinGW.

Using MinGW

The MinGW compilers are accessible through the &quot;MinGW Shell&quot; which should be located under the MinGW folder in the Start Menu:

or by running <tt>C:\MinGW\msys\1.0\msys.bat</tt> (if MinGW was installed to its default location).

By default the MinGW shell executes inside of a Windows <tt>cmd.exe</tt> terminal. There are a couple major drawbacks to this:

You cannot resize the window to full-screen.

The shell provides limited history scrollback.

MinGW provides a couple of alternative terminals for the MinGW shell which do not have these drawbacks; one of which is called MinTTY. To install MinTTY execute the following in a MinGW shell:

mingw-get install mintty

Then the <tt>mintty</tt> command can be used to launch a MinTTY terminal. The MinGW Shell can also be set to launch MinTTY by default by editing the file <tt>C:\MinGW\msys\1.0\msys.bat</tt> and changing the following line (approximately line 58):

if &quot;x%MSYSCON%&quot; == &quot;xunknown&quot; set MSYSCON=sh.exe

to:

if &quot;x%MSYSCON%&quot; == &quot;xunknown&quot; set MSYSCON=mintty.exe

A Note About Paths

The MinGW Shell emulates a UNIX <tt>bash</tt> shell and thus uses UNIX-style paths, <tt>/path/to/file</tt>, instead of Windows-style paths, <tt>C:\path\to\file</tt>. One peculiarity of this process is that the shell must deal with the fact that Windows filesystems have multiple roots (<tt>C:</tt>, <tt>D:</tt>, etc.) while a UNIX filesystem only has one root called <tt>/</tt>. The way this is reconciled by the <tt>bash</tt> shell provided with MinGW is that <tt>/c</tt> refers to the root of the <tt>C:</tt> drive, <tt>/d</tt> refers to the root of the <tt>D:</tt> drive and so on and so forth.

The root of the filesystem itself, <tt>/</tt>, also has some caveats. <tt>/</tt> refers to the <tt>mysy</tt> folder, <tt>C:/MinGW/mysys/1.0</tt> in a default installation. <tt>/usr</tt> also refers to the same location. Thus, <tt>/usr/local</tt> and <tt>/local</tt> both refer to <tt>C:/MinGW/mysys/1.0/local</tt>.

A Note About MinGW-64

Currently Swarm has been developed and tested against the 32-bit version of MinGW. Hence Swarm models produced this way should run in any version of Windows.

The [MinGW-w64][mingw_65] project produces 64-bit MinGW compilers that can produce 64-bit executables that will only run on 64-bit versions of Windows. These executables benefit from:

Access to more memory. A 32-bit executable will only be able to allocate ~3 GB of RAM---even if the system has more RAM available.

Possibly quicker execution speeds due to matching the native architecture of the operating system---however this depends on the types of tasks that the program executes. It has been reported that 64 bit Swarm executables run quicker than 32 bit Swarm executables on Linux.

Currently, no attempt has been made to compile Swarm 2.4.1 with the 64 bit MinGW compilers. If a successful attempt is made, please report it on the Swarm mailing list.

Emacs

GNU Emacs is a text editor that is used by many programmers to edit code. However, Emacs can also be scripted to perform a variety of complicated transformations on text files. The scripts that build the Swarm libraries use Emacs to automate the process of generating some source files. Thus Emacs has to be installed in order to build Swarm from source.

Installing Emacs

Install Emacs by downloading one of the <tt>emacs-&lt;version
number&gt;-bin-i386.zip</tt> files from the download area. Don't use any of the
files containing <tt>barebin</tt>. After extracting the ZIP archive there will
be an Emacs folder containing a subfolder called <tt>bin</tt>. Add this folder
to your Windows <tt>PATH</tt>. RapidEE can help edit the
<tt>PATH</tt> variable.

GPerf

GPerf is a program that produces C code containing hash functions. The scripts
that build the Swarm libraries use GPerf to generate some source code files.
Download <tt>gperf.exe</tt> from the GnuWin32 site on Sourceforge and place it
somewhere on your Windows <tt>PATH</tt>. Again, RapidEE can help set the <tt>PATH</tt> variable.

RapidEE

The Rapid Environment Editor (RapidEE) is a spiffy tool that makes it easy to inspect and alter the value of Windows environment variables, such as the <tt>PATH</tt>. RapidEE may be used to edit &quot;System Variables&quot; which will affect all user accounts or &quot;User Variables&quot; which will affect just the current account. Administrative privileges may be required to edit the System Variables.

Right-clicking on the <tt>Path</tt> variable in the System Variables or User Variables will bring up a menu that presents the option to &quot;Add value&quot;:

Once the new entry has been added, RapidEE must be told to save the changes. Command line shells, such as <tt>cmd.exe</tt>, must be restarted in order for them to notice the changes.