Skip to content

compiling the source code

alemart edited this page Jan 6, 2025 · 1 revision

Compiling the Source Code

Overview

This guide will help you compile Open Surge. Open Surge uses Allegro 5, a game programming library written mostly in C. Allegro 5 is supported on multiple platforms, including: Windows, Linux, macOS and Android. Regardless of your platform, you'll need CMake and a C compiler.

Note

You normally don't need to compile Open Surge from the source code, unless you're an advanced user or a package maintainer.

Important

Package maintainers: it's recommended to create two separate packages for distribution: one for Open Surge and the other for SurgeScript. SurgeScript is a scripting language for games. It's conceived as a separate project by the same author, providing shared and static libraries, and it's used massively in Open Surge. SurgeScript features a runtime engine of its own that doesn't depend on Open Surge.

Cross-compiling

We'll compile a Windows build of the game from a Linux machine. We'll first compile Allegro 5 and its dependencies. Next, we'll compile SurgeScript. Finally, we'll compile Open Surge.

Since we're building everything from the sources, the instructions below can be adapted to build the game on other platforms, such as Linux and macOS (there are hints throughout the guide). Also, if you came here from the web, this guide can help you cross-compile Allegro 5 as well.

Note

Before we begin, create an empty directory for compiling the game and then get in there (cd /path/to/dir). We'll be downloading packages and installing things. Avoid using directories with whitespaces or special characters in the path.

Install the cross-compiler

First of all, we need to install CMake and the cross-compiler. We'll be using MinGW-w64. Get MinGW-w64 from its website and CMake from cmake.org. On Debian-based distributions such as Ubuntu, you can simply run:

sudo apt-get install mingw-w64 cmake

Now, check where the cross-compiler has been installed. Then, run the following commands in a shell (adapt as necessary):

# Name of the cross compiler  
# Usually one of the following: i686-w64-mingw32, i686-pc-mingw32, i386-mingw32 (32 bit)  
export TOOLSET=i686-w64-mingw32  
  
# Location of the cross compiler  
# Usually in /usr (please check) - it should have subdirectories: bin/, include/, lib/  
export TOOLCHAIN=/usr/$TOOLSET

We'll also set some environment variables. The programs below must be available in your PATH:

# These tools should be available in your PATH  
export CC=$TOOLSET-gcc  
export CXX=$TOOLSET-g++  
export RANLIB=$TOOLSET-ranlib  
export LD=$TOOLSET-ld  
export AR=$TOOLSET-ar  
export AS=$TOOLSET-as  
export STRIP=$TOOLSET-strip

Download Allegro 5

Let's download Allegro 5 (source code). The most recent version of the library can be found at liballeg.org. In this example, we're using Allegro 5.2.8.

wget https://github.com/liballeg/allegro5/releases/download/5.2.8.0/allegro-5.2.8.0.tar.gz  
tar xvzf allegro-5.2.8.0.tar.gz  
cd allegro-5.2.8.0

In order to compile Allegro 5, we must first install its dependencies. We'll install the dependencies into a special folder called deps. This folder is secretly recognized by Allegro. Set the variables below:

export ALLEGRO_FOLDER="$(pwd)"  
export ALLEGRO_BUILD_FOLDER="$ALLEGRO_FOLDER/build"  
export ALLEGRO_DEPS_FOLDER="$ALLEGRO_FOLDER/deps"  
export LDFLAGS="-L$ALLEGRO_DEPS_FOLDER/lib"  
export CFLAGS="-I$ALLEGRO_DEPS_FOLDER/include"  
export CPPFLAGS="-I$ALLEGRO_DEPS_FOLDER/include"

Let's create the deps folder:

mkdir -p $ALLEGRO_DEPS_FOLDER
cd $ALLEGRO_DEPS_FOLDER

In that folder, we'll create a helper file called toolchain-mingw.cmake:

cat > toolchain-mingw.cmake << EOF  
set(CMAKE_SYSTEM_NAME Windows)  
set(CMAKE_C_COMPILER ${TOOLSET}-gcc)  
set(CMAKE_CXX_COMPILER ${TOOLSET}-g++)  
set(CMAKE_RC_COMPILER ${TOOLSET}-windres)  
set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN})  
set(CMAKE_AR:FILEPATH `which ${TOOLSET}-ar`)  
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)  
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)  
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)  
EOF

Now we're ready to compile the dependencies.

Compile zlib

zlib can be obtained from http://zlib.net

wget https://zlib.net/zlib-1.2.13.tar.gz  
tar xvzf zlib-1.2.13.tar.gz && cd zlib-1.2.13  
# ./configure --prefix=$ALLEGRO_DEPS_FOLDER --static && make # skip this (not win32)  
sed -e s/"PREFIX ="/"PREFIX = ${TOOLSET}-"/ -i win32/Makefile.gcc  
BINARY_PATH=${ALLEGRO_DEPS_FOLDER}/bin \  
  INCLUDE_PATH=${ALLEGRO_DEPS_FOLDER}/include \  
  LIBRARY_PATH=${ALLEGRO_DEPS_FOLDER}/lib \  
  make -f win32/Makefile.gcc install  
make install  
cd ..

Compile libpng

libpng can be obtained from http://libpng.org

wget https://downloads.sourceforge.net/project/libpng/libpng16/1.6.38/libpng-1.6.38.tar.gz  
tar xvzf libpng-1.6.38.tar.gz && cd libpng-1.6.38  
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static  
make  
make install  
cd ..

Compile freetype

The FreeType library can be obtained from http://freetype.org

wget http://downloads.sourceforge.net/project/freetype/freetype2/2.12.1/freetype-2.12.1.tar.gz  
tar xvzf freetype-2.12.1.tar.gz && cd freetype-2.12.1  
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static \  
  --without-png --without-zlib --without-harfbuzz --with-bzip2=no --with-brotli=no  
make  
make install  
cd ..

Compile libjpeg

We'll be using libjpeg: http://www.ijg.org

wget https://www.ijg.org/files/jpegsrc.v9e.tar.gz  
tar xvzf jpegsrc.v9e.tar.gz && cd jpeg-9e  
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static  
make  
make install  
cd ..

You may alternatively use libjpeg-turbo.

Compile libogg

libogg can be obtained from http://xiph.org

wget http://downloads.xiph.org/releases/ogg/libogg-1.3.5.tar.gz  
tar xvzf libogg-1.3.5.tar.gz && cd libogg-1.3.5  
# sed -e s/"-mno-ieee-fp"/""/g -i ./configure # skip this (clang only, x86)  
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static  
make  
make install  
cd ..

Compile libvorbis

libvorbis can also be obtained from http://xiph.org

wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.7.tar.gz  
tar xvzf libvorbis-1.3.7.tar.gz && cd libvorbis-1.3.7  
# sed -e s/"-mno-ieee-fp"/""/g -i ./configure # skip this (clang only, x86)  
./configure --host=$TOOLSET --target=$TOOLSET --prefix=$ALLEGRO_DEPS_FOLDER --disable-shared --enable-static \  
  --without-ogg --with-ogg=$ALLEGRO_DEPS_FOLDER --with-ogg-libraries=$ALLEGRO_DEPS_FOLDER/lib \  
  --with-ogg-includes=$ALLEGRO_DEPS_FOLDER/include  
make  
make install  
cd ..

Compile libphysfs

libphysfs can be downloaded from https://github.com/icculus/physfs/

wget https://github.com/icculus/physfs/archive/refs/tags/release-3.2.0.tar.gz  
tar xvzf release-3.2.0.tar.gz && cd physfs-release-3.2.0  
mkdir build && cd build  
cmake .. -DCMAKE_INSTALL_PREFIX=$ALLEGRO_DEPS_FOLDER \  
  -DCMAKE_TOOLCHAIN_FILE=$ALLEGRO_DEPS_FOLDER/toolchain-mingw.cmake \  
  -DPHYSFS_BUILD_SHARED=off -DPHYSFS_BUILD_STATIC=on  
make  
make install  
cd ../..

Optional libraries

Other libraries such as: libflac, libdumb, libopus, libtheora can be added to Allegro, but are currently not required by Open Surge.

libopenal

libopenal is required on OSX, but optional otherwise. We'll be cross-compiling OpenAL Soft, a software implementation of the OpenAL API. It's available at https://kcat.strangesoft.net/

wget https://kcat.strangesoft.net/openal-releases/openal-soft-1.17.2.tar.bz2  
tar xvjf openal-soft-1.17.2.tar.bz2 && pushd openal-soft-1.17.2/build  
cmake .. -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_C_COMPILER=$(which $CC) -DCMAKE_CXX_COMPILER=$(which $CXX) \  
  -DCMAKE_FIND_ROOT_PATH=$TOOLCHAIN -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \  
  -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY -DCMAKE_INSTALL_PREFIX=$ALLEGRO_DEPS_FOLDER -DLIBTYPE=STATIC  
make  
make install  
popd

Compile Allegro 5

In this tutorial, we'll compile the static version of Allegro 5 (monolith). YMMV, but we'll be using this configuration for the Windows build of the game. Note that other platforms may not support linking Allegro 5 statically.

mkdir -p $ALLEGRO_BUILD_FOLDER  
cd $ALLEGRO_BUILD_FOLDER  
cmake .. -DCMAKE_TOOLCHAIN_FILE="$ALLEGRO_FOLDER/cmake/Toolchain-mingw.cmake" -DCMAKE_BUILD_TYPE=Release \  
  -DWANT_MONOLITH=on -DSHARED=off -DPREFER_STATIC_DEPS=on -DWANT_DOCS=off \  
  -DWANT_EXAMPLES=on -DWANT_DEMO=on -DWANT_TESTS=off -DWANT_MP3=off -DWANT_MODAUDIO=off \  
  -DWANT_STATIC_RUNTIME=on -DCMAKE_VERBOSE_MAKEFILE=on  
# Cross fingers and pray.  
 make -jnproc   
sudo make install

After compiling Allegro 5, go back to the base folder:

cd $ALLEGRO_FOLDER/..

Compile SurgeScript

Compiling SurgeScript should be fairly easy. Get the sources on GitHub. File cmake/toolchain-mingw.cmake tries to automatically locate the cross-compiler (adapt if necessary).

# extract the sources to surgescript/  
pushd surgescript  
mkdir -p build && cd build  
cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain-mingw.cmake  
make  
sudo make install  
popd

Tip

You may install SurgeScript into a non-standard folder by modifying CMAKE_INSTALL_PREFIX (via the command-line or using ccmake | cmake-gui). However, we cover only the default setup in this guide.

Compile Open Surge

If everything went well so far, you're ready to build Open Surge! The sources are available on GitHub. Adapt file src/misc/toolchain-mingw.cmake if necessary.

# extract the sources to opensurge/  
pushd opensurge  
mkdir -p build && cd build  
cmake .. -DCMAKE_TOOLCHAIN_FILE=../src/misc/toolchain-mingw.cmake \  
  -DCMAKE_SYSROOT=$ALLEGRO_DEPS_FOLDER -DALLEGRO_STATIC=ON -DALLEGRO_MONOLITH=ON -DSURGESCRIPT_STATIC=ON  
make  
# sudo make install # the Linux build uses this  
popd

The Open Surge executable will be available in the opensurge folder.