[Main Page] Main Page | Recent changes | View source | Page history

Printable version | All content dual-licensed under the GNU FDL and the Creative Commons Share-alike Attribution license. | Privacy policy | Latest revision

Not logged in
Log in | Help
 

Swarm and MinGW

(Difference between revisions)

(Fix a couple of links)
m (Building Swarm Using MinGW)
Line 66: Line 66:
  
 
This tutorial is a bit advanced---some familiarity with compiling software from source in a Linux environment and knowledge of the UNIX command line is assumed.
 
This tutorial is a bit advanced---some familiarity with compiling software from source in a Linux environment and knowledge of the UNIX command line is assumed.
 +
 +
PUT A LINK TO THIS FROM SOME PLACE ABOUT INSTALLING FROM SOURCE
  
 
== Install Required Tools ==
 
== Install Required Tools ==

Revision as of 09:48, 26 July 2011

Contents

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 use MinGW to compile the Swarm library, 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 the library.

Using Swarm Models Compiled with MinGW

A model that has been compiled using MinGW, such as this build of Heatbugs:

https://github.com/downloads/Sharpie/Swarm/heatbugs.exe

Requires some support programs and libraries. These are available as swarm-0.4.1-win32.zip:

https://github.com/downloads/Sharpie/Swarm/swarm-2.4.1-win32.zip

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 the swarm folder has been placed at C:\swarm.

Most Swarm models are started through the Windows command line. The command line is accessed using the program cmd.exe. Before the Heatbugs model can be run, the contents of 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:

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

A tool like RapidEE can be used to permanently add the folder to your PATH.

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\Dowloads, then the model may be run as follows:

cd C:\Users\example_user\Downloads
heatbugs.exe
Running the Heatbugs model

Compiling Swarm Models with MinGW

In order to compile Swarm models from source using MinGW, the following steps need to be taken in order to setup the build environment:

NOTE:

The archived Swarm binaries MUST be placed at C:\swarm in order to successfully compile Swarm models. This is due to some paths that were hard-coded by libtool during the creation of the binaries.

After MinGW has been installed and the Swarm binaries have been downloaded, swarm models may be compiled using the MinGW shell. Before compiling a Swarm model from source, set the SWARMHOME environment value in the MinGW shell:

export SWARMHOME=/c/swarm

This variable can be set permanently using RapidEE.

An archive of official Swarm example programs can be downloaded from:

http://ftp.swarm.org/pub/swarm/src/swarm/swarmapps-2.3.0.tar.gz

We will use the "Heatbugs" model as an example of how to compile a Swarm application:

tar xzf /path/to/dowloaded/swarmapps-2.3.0.tar.gz
cd swarmapps-2.3.0/objc/heatbugs

make

This produces two executables called heatbugs.exe---one in the heatbugs folder and another in the heatbugs/.libs folder. It is strongly suggested to ignore the executable in the top-level folder, heatbugs/heatbugs.exe, as it is merely a compiled wrapper script that executes heatbugs/.libs/heatbugs.exe. When distributing compiled models, only use binaries from the .libs folder.

The resulting executable, .libs/heatbugs.exe may be executing by following the steps described in Using Swarm Models Compiled with MinGW.

Building Swarm Using MinGW

This tutorial is a bit advanced---some familiarity with compiling software from source in a Linux environment and knowledge of the UNIX command line is assumed.

PUT A LINK TO THIS FROM SOME PLACE ABOUT INSTALLING FROM SOURCE

Install Required Tools

The first step is to download and install tools required for compiling Swarm:

Build Support Libraries

The next steps are to build and install the libraries that Swarm depends on. A ZIP archive containing all the source code required can be found at:

https://github.com/downloads/Sharpie/Swarm/Swarm-2.4.1-MinGW-sources.zip

This archive contains two subdirectories:

  • sources: This folder contains the source code for each component.
  • 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 build directory. All compiled software will be installed to /c/swarm (The Windows Explorer will see the folder appear at C:\swarm).

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 build directory, execute the following to build Zlib:

tar xzf ../sources/zlib-1.2.5.tar.gz
cd zlib-1.2.5
make -f win32/Makefile.gcc

And then the following commands to install ZLib to /c/swarm:

mkdir -p /c/swarm/bin
mkdir -p /c/swarm/include
mkdir -p /c/swarm/lib

cp zlib1.dll /c/swarm/bin/
cp zconf.h zlib.h /c/swarm/include/
mv libzdll.a libz.dll.a
cp *.a /c/swarm/lib

LibPNG

LibPNG will be built next as its only dependency is ZLib. From the build directory, execute the following steps to build and install LibPNG:

tar xjf ../sources/libpng-1.5.4.tar.bz2
cd libpng-1.5.4

export CPPFLAGS='-I/c/swarm/include'
export LDFLAGS='-L/c/swarm/lib'
./configure --prefix=/c/swarm --with-zlib-prefix=/c/swarm

make
make install

XPM

Starting from the build directory, the XPM library can be built and installed as follows:

tar xjf ../sources/xpm-nox-4.2.0.tar.bz2
cd xpm-nox-4.2.0
patch -p1 < ../../patches/xpm-nox-4.2.0-mingw-tcl.patch

make CC=gcc
make install prefix=/c/swarm

Summary of patchfile changes:

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---tcl.h is included in xpm.h. 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 build directory, LibFFI can be built and installed as follows:

tar xzf ../sources/libffi-3.0.9.tar.gz
cd libffi-3.0.9

./configure --prefix=/c/swarm
make
make install

The LibFFI headers get installed in a weird place.

mv /c/swarm/lib/libffi-3.0.9/include/*.h /c/swarm/include
rm -rf /c/swarm/libffi-3.0.9

Tcl

Starting from the build directory, Tcl can be built and installed as follows:

tar xzf ../sources/tcl8.4.19-src.tar.gz
cd tcl8.4.19
patch -p1 < ../../patches/tcl-8.4.19-fix-lvalues.patch
cd win

./configure --prefix=/c/swarm
make
make install

Summary of patchfile changes:

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 build directory, Tk can be built and installed as follows:

tar xzf ../sources/tk8.4.19-src.tar.gz
cd tk8.4.19/win

./configure --prefix=/c/swarm
make
make install

BLT

Starting from the build directory, BLT can be built and installed as follows:

tar xzf ../sources/BLT2.4z.tar.gz
cd blt2.4z

./configure --prefix=/c/swarm --without-x
make
make install

HDF5 (Optional)

Getting HDF5 to work with MinGW is a little tricky. It is an optional dependency, so if support for reading and writing .hdf 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 pexports. This can be installed using mingw-get:

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 "VS" listed under the Compilers section). After extracting the archive, the HDF5 libraries can be wrapped for use with MinGW by executing the following commands:

cd path/to/where/HDF5/was/extracted

for dll in `ls dll/*dll`; do
  def_file=`basename $dll .dll`.def
  lib_file=lib`basename $dll dll.dll`.a
  pexports $dll > $def_file
  dlltool -d $def_file -l lib/$lib_file
done

Next, move the libraries and header files needed to use HDF5 from C into C:\swarm:

cp dll/hdf5dll.dll dll/szip.dll /c/swarm/bin
cp include/* /c/swarm/include
cp lib/libhdf5.a /c/swarm/lib

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

cd /path/to/where/swarm/source/was/extracted
patch -p1 -d /c/swarm < patches/hdf5-1.8.7-mingw.patch

Build Swarm

Finally, the Swarm libraries themselves can be built. From the build directory, execute the following

tar xzf ../sources/swarm-2.4.1.tar.gz
cd swarm-2.4.1
patch -p1 ../../patches/swarm-2.4.1-mingw.patch

export CPPFLAGS='-I/c/swarm/include'
export LDFLAGS='-L/c/swarm/lib'
./configure --prefix=/c/swarm \
  --with-zlibdir=/c/swarm \
  --with-pngdir=/c/swarm \
  --with-xpmdir=/c/swarm \
  --with-ffidir=/c/swarm \
  --with-tcl=/c/swarm/lib \
  --with-tk=/c/swarm/lib \
  --with-bltdir=/c/swarm

cd avcall
gcc -S avcall-i386.c
cd ..

make
make install

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

Summary of patchfile changes:

swarm-2.4.1-mingw.patch modifies the Swarm source code in the following ways:

  • gettimeofday 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 configure script defines DATADIR as a preprocessor macro---this conflicts with variables in the GCC Objective-C library. DATDIR is undefed in critical parts of the source code.
  • The sleep function is aliased to usleep---which is the name of the MinGW implementation.
  • The way in which Makefiles invoke gperf is ammended so that it actually produces output.

Full details are contained in the patchfile.

Tools

MinGW

Homepage: http://www.mingw.org

Download Area: http://sourceforge.net/projects/mingw/files

Version Used: 20110530

License: GPL

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 "Automated MinGW Installer" which usually has a name like mingw-get-inst-<date>.exe where <date> is the datestamp on which the installer was packaged. Running this executable will start the installation process. The installer offers the option to configure which components are installed, make sure all boxes are checked:

Install all available MinGW components

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

Using MinGW

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

Location of MinGW Shell

Or by running C:\MinGW\msys\1.0\msys.bat (if MinGW was allowed to install to its default location).

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

  • Cannot resize the window to full-screen.
  • Limited history scrollback.

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

mingw-get install mintty

Then the mintty 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 C:\MinGW\msys\1.0\msys.bat and changing the following line:

if "x%MSYSCON%" == "xunknown" set MSYSCON=sh.exe

to:

if "x%MSYSCON%" == "xunknown" set MSYSCON=mintty.exe

A Note About Paths

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

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

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

Homepage: http://www.gnu.org/software/emacs

Version Used: 23.3

Download Area: http://ftp.gnu.org/gnu/emacs/windows

License: GPL

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 emacs-<version number>-bin-i386.zip files from the download area. Don't use any of the files containing barebin. After extracting the ZIP archive there will be an Emacs folder containing a subfolder called bin. Add this folder to your Windows PATH. RapidEE can help edit the PATH variable.


GPerf

Homepage: http://www.gnu.org/software/gperf/

Download Area: http://sourceforge.net/projects/gnuwin32/files/gperf

Version Used: 3.0.1

License: GPL

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 gperf.exe from the GnuWin32 site on Sourceforge and place it somewhere on your Windows PATH. Again, RapidEE can help set the PATH variable.


RapidEE

Homepage: http://www.rapidee.com

Version Used: 5.0

License: Freeware

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 PATH. RapidEE may be used to edit "System Variables" which will affect all user accounts or "User Variables" which will affect just the current account. Administrative privileges may be required to edit the System Variables.

The different variables RapidEE can edit

Right-clicking on the Path variable in the System Variables or User Variables will bring up a menu that presents the option to "Add value":

Adding a new value to the PATH

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

Adding and saving a value

Depends

Homepage: http://www.dependencywalker.com/

Version Used: 2.2

License: Freeware

Depends is a usefull utility that scans Windows executables and reports information concerning the Dynamic-link Libraries (DLLs) that those executables require in order to run.

Software Used to Compile Swarm 2.4.1

Name Verson Used License Homepage Download Area
ZLib 1.2.5 ZLib/PNG License http://www.zlib.net --
LibFFI 3.0.9 MIT http://sourceware.org/libffi --
LibPNG 1.5.4 ZLib/PNG http://www.libpng.org/pub/png/libpng.html --
XPM-NoX 4.2.0 MIT unknown http://build.opensuse.org/package/files?package=mingw32-xpm-nox&project=windows%3Amingw%3Awin32
Tcl 8.4.19 Tcl http://www.tcl.tk --
Tk 8.4.19 Tcl http://www.tcl.tk --
BLT 2.4z BSD http://blt.sourceforge.net --
HDF5 1.8.7 BSD http://www.hdfgroup.org/HDF5 --
Swarm 2.4.1 GPL http://www.swarm.org http://ftp.swarm.org/pub/swarm/src/swarm/swarmapps-2.3.0.tar.gz