Skip to content

Compiling using CMake

Jasmin B. Maglic edited this page Jul 11, 2023 · 4 revisions

CMake is a cross-platform tool that helps manage the build process for applications. Once set up, CMake allows creating various different builds, for instance release/debug builds. Visit https://cmake.org/install/ to download the installation files, or install CMake through homebrew.

Note that this article follows macOS, but should be just as applicable to Linux.

To create a MoloVol build, navigate to the root directory of the cloned repository, create a new build directory, and enter it. Afterwards, run cmake with your desired options.

myname:MoloVol$ mkdir build
myname:MoloVol$ cd build
myname:build$ cmake -DCMAKE_BUILD_TYPE=RELEASE ..

It is recommended to build the release build, because running calculations can take a long time without the optimisation flags enabled. CMake now creates a Makefile for compilation. To compile the code run make inside your build folder.

myname:build$ make

The compiled binary file, i.e., executable, will be located in the same directory and can be executed.

myname:build$ ./MoloVol

Notable options

CMake options can be set either when creating the build files for the first time (1) or afterwards (2) by changing cache variables. This is done with a leading -D and by specifying the value with an equals sign.

(1) myname:build$ cmake -DCMAKE_OPTION=ON ..
(2) myname:build$ cmake . -DCMAKE_OPTION=OFF

If you are building on a Mac with Apple Silicon (arm64 architecture), you need to manually set the target architecture to arm64. Otherwise, the executable will be built for x86_64 architecture.

myname:build$ cmake . -DCMAKE_OSX_ARCHITECTURES=arm64

To build a Mac OS app bundle you need to enable this option.

myname:build$ cmake . -DCMAKE_MAXOSX_BUNDLE=ON

There are three additional MoloVol-specific build options. Each is set to OFF by default.

  • MOLOVOL_ABS_RESOURCE_PATH - If enabled, the paths to the 'elements' and space group files are set to a platform specific absolute path
  • MOLOVOL_OSX_FAT_FILE - Builds an arm64/x86_64 fat file on macOS
  • MOLOVOL_BUILD_TESTING - Enables MoloVol unit tests

For unit tests to work, it is additionally necessary to enable the higher level option BUILD_TESTING.

For development on macOS it is recommended to use the following configuration:

myname:build$ cmake .. -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_BUILD_TYPE=RELEASE -DMOLOVOL_BUILD_TESTING=ON -DBUILD_TESTING=ON