Compiling BerryBots app on Mac OS X with XCode

From BerryBots Wiki
Revision as of 02:56, 9 November 2013 by Voidious (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


The BerryBots source code is available at the GitHub repo or on the Downloads page.

Install build tools

First, you need to install XCode. You may also find it on your OS X install disc.

SFML for Mac depends on Freetype. The easiest way to get this is to just install XQuartz X Window system for Mac. You could also rip freetype.framework from the Frameworks directory of the binary release, and drop it in /Library/Frameworks.

Note that this is much more involved than Compiling BerryBots on Mac OS X command line. If you don't mind (or prefer) launching BerryBots from the command line, just do that instead. The app will function identically besides some app icons and menubar titles.

Compile dependencies

To compile BerryBots on Mac OS X, we need to link against four external libraries: Freetype, SFML 2.0, wxWidgets 2.9.4, and libarchive. We'll also compile BerryBots' modified version of LuaJIT. Once we've compiled these dependencies, we can start configuring our XCode project.


BerryBots uses SFML 2.0 for the main game graphics. Note that SFML has their own instructions for building SFML 2.0 with CMake, which may be helpful.

First, download and install CMake from During installation, you'll be asked about installing the command line tools - install them where ever you like and add them to your PATH. If you don't know how to do that, there are a bunch of Google results that might help.

There are some notable OS X resizing/scaling issues in SFML 2.1+ ([1]), so you should use SFML 2.0 for now. Download and extract the source, then open a terminal and change into the SFML source directory. Use CMake to generate the build files that are used for actual compilation, then use make to compile SFML.

~> cd ~/SFML-2.0
~/SFML-2.0> mkdir build
~/SFML-2.0> cd build
~/SFML-2.0/build> cmake ..
~/SFML-2.0/build> make

When it completes, you should have a lib subdirectory with the SFML library files.


We use wxWidgets 2.9.x for everything besides the main game window - all the dialogs and menus for configuring battles, packaging ships and stages, and viewing ship/stage output consoles. wxWidgets has their own instructions for compiling wxWidgets on OS X in a terminal, which may be helpful.

Download wxWidgets 2.9.x source code from the wxWidgets downloads page and extract it. Open a terminal and navigate to the wxWidgets directory. Then compile wxWidgets with configure and make.

~> cd ~/wxWidgets-2.9.4
~/wxWidgets-2.9.4> ./configure --with-osx_cocoa
~/wxWidgets-2.9.4> make

This will take a few minutes to compile. When it completes, you should have a lib subdirectory with the wxWidgets library files.


We use libarchive with zlib for packaging ships and stages as tarred / gzipped (.tar.gz) files. The gzip compression itself is handled by zlib, which should already be installed in Mac OS X.

Download libarchive from - v3.1.x is used for the official BerryBots binary releases. Extract it, open a terminal, and change into the libarchive directory. Then compile libarchive with configure and make. Copy libarchive.a to a non-hidden directory for later.

~> cd ~/libarchive-3.1.2
~/libarchive-3.1.2> ./configure
~/libarchive-3.1.2> make
~/libarchive-3.1.2> cp ./.libs/libarchive.a .

When it completes, you should have .libs/libarchive.a under the build directory. (Note that .libs is a hidden directory.)


BerryBots includes a modified version of LuaJIT 2.0.x. Extract the BerryBots source code, change into the berrybots/luajit directory, and run make.

~> cd ~/berrybots/luajit
~/berrybots/luajit> make

When this completes, you'll have libluajit.a under berrybots/luajit/src.

Configure XCode project

Once we've compiled the dependencies, we can start configuring everything in XCode. Unfortunately this is ridiculously complicated compared to just setting some paths and running make. Hopefully I can figure out a way to share an XCode project or template to make this easier in the future.

Download the source from the downloads page or the GitHub repo. Extract it where ever you like. Launch XCode and create a New Project of type OS X > Application > Cocoa Application. Name it "BerryBots" and uncheck "Use Automatic Reference Counting". Choose whatever base directory you like - we can pull in the BerryBots source files from where ever.

Import libraries

  • Drag libarchive/libarchive.a and berrybots/luajit/src/libluajit.a into the project. Make sure to check "Copy items into destination's group folder".
  • Drag sfml/build/lib into the project. Uncheck "Copy items...". Select "Create groups for any added folders". Rename it to something else in XCode, like sfml-lib.
  • Do the same for wxWidgets/lib.
  • Click "BerryBots" in the XCode left panel (Project Navigator) and choose the BerryBots target. Under the Summary tab, click the “+” under "Linked Frameworks and Libraries".
    • Add libz.dylib and libiconv.dylib.
    • Add Foundation.framework.
    • Remove all the SFML (libsfml*) and wxWidgets (libwx*) libraries from this list.

Compiler settings

Go back to the BerryBots build target - select BerryBots at the top of the Project Navigator and select "BerryBots" under "Targets".

  • Click on the “Summary” tab. Drag the resources/icon.iconset folder from Finder to “App Icon”.
  • Click on the "Build Settings" tab.
  • Switch “C++ Standard Library” from “libc++” to “libstdc++”.
  • Select “Framework Search Paths” and add /Library/Frameworks
  • Select “Other C Flags” to add some header paths (-I/yada) and settings.
    • Add the path to libarchive, e.g.: -I/Users/voidious/libarchive-3.1.2
    • Add the path to SFML, e.g.: -I/Users/voidious/SFML-2.0/include
    • Add the path and settings for wxWidgets. Open a terminal run wx-config --cflags from the wxWidgets build directory, e.g. /Users/voidious/wxWidgets-2.9.4/wx-config --cflags. Copy the output verbatim.
  • Select “Other Linker Flags”.
    • Add the following flags: -lsfml-graphics -lsfml-window -lsfml-system -pagezero_size 10000 -image_base 100000000 -ldl
    • Add the linker flags for wxWidgets. Open a terminal run wx-config --libs from the wxWidgets build directory, e.g. /Users/voidious/wxWidgets-2.9.4/wx-config --libs. Copy the output verbatim, minus the -L/path/to/wxWidgets. We’re going to instead link against a modified version of the libs that we’ll package with the app.
  • Select “Header Search Paths”. Add the paths to STLSoft and LuaJIT headers included with BerryBots source, e.g.: /Users/voidious/berrybots/stlsoft-1.9.116/include /Users/voidious/berrybots/luajit/src.
  • Select "Library Search Paths". Remove the SFML and wxWidgets lib paths and add ${BUILT_PRODUCTS_DIR}/${SHARED_SUPPORT_FOLDER_PATH}. This will link against our modified copy of the SFML and wxWidgets libraries after they are processed by

Add source files and resources

  • Drag all the source code files (.m, .mm, .c, .h, .cpp, .hpp) from the BerryBots source directory to the "BerryBots" folder in your XCode project. Uncheck "Copy items...".
  • Delete the following files.
    • AppDelegate.* and MainMenu.xib (part of default XCode project)
    • main.m from “Supporting Files”
    • bbpi*
    • bbsfmlmain.cpp
    • cli*
    • eglstate.h
    • fontinfo.h
    • libshapes.c
    • oglinit.c
    • relative*
    • shapes.h
    • tarzipper.*
  • Drag apidocs, bots, stages, and resources directories into your project. Check "Copy items..." and select "Create folder references for any added folders".
  • Drag scripts/ into your project root. Uncheck "Copy items...".

Build phases

Under the BerryBots build target, go to the “Build Phases” tab.

  • Click “Add Build Phase” / “Add Copy Files”. Drag it to the 2nd spot under “Target Dependencies”. Select destination “Shared Support”. Click “+” and add all the *.dylib files for wxWidgets and SFML. Right now it’s 63 files.
  • Add another “Add Copy Files” phase after the first one. Select destination “Frameworks”. Click “+” and “Add Other”, navigate to /Library/Frameworks/freetype.framework.
  • Add a “Run Script” phase after the “Copy Files” phases. Drag from the file list on the left into the first text area.
  • Under the “Compile Sources” phase, make sure you have all the BerryBots source files. If not, click “+” and add them.
  • Under “Link Binary With Libraries”, add freetype.framework.
  • Under “Copy Bundle Resources” phase, make sure apidocs, bots, stages, and resources are included.

Build and run

You should be all set! You can modify BerryBots, “Run” with the button in the top left, or go to Product > Archive > Distribute to package it as an app.

Personal tools