Compiling with CMake (OpenXcom)

From UFOpaedia
Jump to navigation Jump to search

CMake is a meta-build system. It doesn't build the code itself. Instead, it autogenerates files for other build systems, like Make or Visual Studio. It is an excellent tool for maintaining consistent build environments across multiple operating systems.

Before building

In addition to the OpenXcom dependencies, you will need CMake version 2.8.0 or later.

CMake can be used to build OpenXcom with differents tools. So far only the Makefile and Visual Studio have been tried. Other Generators (Xcode/Code blocks, ...) might work, but if you choose to use them, we currently have no way to make sure that they work. If broken, patches are welcome to make them work.

Note : Once a project has been generated, you don't need to regenerate it after updating the source. CMake will automatically check for modifications and regenerate the project for you.

Source dir/build dir: CMake allows you to separate the source directory (where the source resides) from the build directory (where the project is generated). This allows you to always have a clean source directory (no clutter left from builds). The source and build directories can be the same, but it's generally recommended to separate them. The standard name for the build directory is "build", which you must create yourself under the top level OpenXcom directory (where the file is).

Generate a build project

Using CMake GUI tool

  • Launch the CMake configuration tools. First specify the path to the OpenXcom source tree.
  • Then specify where the OpenXcom project should be generated (e.g. the "build" directory that you created).
  • Click on configure. The first time, CMake will ask you which kind of build tool you wish to use.
  • Now CMake will analyze your system and once finished, you will be presented with a list of options. If all is fine for you, then click on generate.

Using CMake interactive command line tools

ccmake is a command line tool which allows you to configure the build interactively. You can launch it with:

 mkdir -p build
 cmake -B build -G 'Generator name' .

Generator name, is the name of the build system you wish to use. You can see possible values with:

 cmake --help

If omitted, cmake will default to 'Unix Makefile'. Note that this needed only the first time you generate the project.

As with the GUI, tweak the options as needed and then configure the project before generating it.

Using CMake command line tools

 mkdir -p build
 cmake -B build -G 'Generator name' -DOPTION_NAME=OPTION_VALUE ..

You can check options value with:

 cmake -L

To change the value of an option:


For example:

 cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/bin/openxcom.inst .

Once the project is generated, you can use it as you would have done without using CMake.

End-to-end example

This will get you up and running from scratch to a fully functional installation. This example is written with Linux users in mind, but can be adapted easily for Windows or OSX. It shows an alternative way of cd'ing into the build dir and running the compilation commands from there.

git clone openxcom
   OR "git clone openxcom" if the above doesn't work for whatever reason
{copy XCOM1 assets into openxcom/bin/UFO and XCOM2 assets into openxcom/bin/TFTD, do not forget to apply the patchfiles}
mkdir openxcom/build
cd openxcom/build
cmake ..
ccmake ..
{set CMAKE_BUILD_TYPE to Release (or Debug if you want useful stack traces, but that will slow the game /way/ down)}
{set CMAKE_INSTALL_PREFIX to /home/yourusername/bin/OpenXcom (or whatever)}
{configure, generate, and exit}
make -j3
make install

Automated patching and compiling

It can also be fully automated, in case you want fast compilation e. g. for testing/using nightlies on a regular basis:

git clone openxcom
cp -r /path/to/UFO_vanilla_with_patch_files_applied/* openxcom/bin/UFO/
mkdir openxcom/build
cd openxcom/build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j10
sudo make install

This version downloads the sources, then copies prepared vanilla-files (should already include the patchfiles) to the right place. One could as well decompress a prepared 7zip-archive like 7za e /path/to/UFO_vanilla_with_patch_files_applied.7z -oopenxcom/bin/UFO/ -y. After that, it compiles automatically as »Release«, also speeding up the process a bit, by using 10 jobs parallel, instead of just 3. As OpenXcom usually is installed automatically in a folder, where users don't have write-access (unless you also change the CMAKE_INSTALL_PREFIX variable accordingly, see -DCMAKE_BUILD_TYPE=Release-line), the sudo before the last command makes sure, a legitimation is requested (if the script is not started already with sudo). If you let it be installed somewhere you have the write permissions, with the mentioned prepared vanilla- and patch-files somewhere laying around, you won't even need the sudo, so you don't need to enter anything for a compilation run. Saving this example adjusted to your need, e. g. as and making it executable, you have an installation-skript, which needs very little attention. This can save a lot of time, if you compile nightlies more often.

Then there are two ways for getting a well playable/startable game:

Starting script

Create an executable shell script in your $HOME/bin directory (ensure it is in your PATH) named openxcom with the contents:

"$HOME/bin/OpenXcom/bin/openxcom" -data "$HOME/bin/OpenXcom/share/openxcom/data"

now just run openxcom to play. : )


On Ubuntu-Systems, or in general, using GNOME, you also might want to create a *.desktop-file, which shows a nice icon and starts your OpenXcom as well as that starting-script above. An example would be:

[Desktop Entry]
Comment=Open-source clone of UFO: Enemy Unknown

You can save it as openxcom.desktop either somewhere, you want to start it from (e. g. your desktop) or put it in the suitable folder, where GNOME-Search will find it, e. g. ~/.local/share/applications, if you want it only for your own user, or /usr/share/applications, if it is meant for every user. It may be necessary, to make this file executable.