View on GitHub

gssi-training

VSC training: Getting Scientific Software Installed

Exercise: Building software with CMake and Ninja

In this exercise, you will build tblite from source using CMake to configure the build and Ninja to compile and install the software.

Get the source code from GitHub:

git clone https://github.com/tblite/tblite.git
cd tblite

Select a version, for example v0.7.0:

git switch --detach v0.7.0

Inspect the source directory:

ls

Look for files that indicate which build system is used. In this case, the presence of CMakeLists.txt shows that the project uses CMake.

CMake can generate build files for different build tools. Here, we will use Ninja as the build tool by selecting the Ninja generator with -G Ninja.

Load the required compiler environment and build tools:

module load buildenv/default-foss-2025a Ninja/1.12.1-GCCcore-14.2.0 CMake/3.31.3-GCCcore-14.2.0

buildenv modules are available on all VSC clusters, they provide a ready-to-use development environment with a predefined set of compilers and libraries matching a common toolchain, such as FOSS or Intel. We additionally load CMake and Ninja because this project uses CMake to configure the build and Ninja to compile and install the software.

Choose an installation location and configure the build:

cmake -B _build -G Ninja -DCMAKE_INSTALL_PREFIX=/path_to_tblite_dir

Here, -B _build creates a separate build directory, -G Ninja selects Ninja as the build system, and CMAKE_INSTALL_PREFIX defines where the software will be installed.

After configuring, inspect the build directory:

ls _build/

You should now see files generated by CMake, including build.ninja. This file contains the build instructions that Ninja will use.

Compile the software:

ninja -C _build

The -C _build option tells Ninja to run from the build directory.

Install the compiled files into the selected installation prefix:

ninja -C _build install

This copies the installed binaries, libraries, headers, and other files into the directory specified by CMAKE_INSTALL_PREFIX.

Add the installation’s bin directory to your PATH:

export PATH=/path_to_tblite_dir/bin:$PATH

Run a simple test, for example:

tblite run --method gfn2 /path_to_tblite_dir/test/cli/01-ala-xab.xyz

You can use cmake --help and ninja --help to inspect additional configuration and build options.

Extra exercise: use Make instead of Ninja

Try repeating the build using CMake with Make instead of Ninja. Use a new build directory for this exercise, if there is an existing _build directory already configured to use Ninja.

Configure the build with the Unix Makefiles generator:

cmake -B _build -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/path_to_tblite_dir

Compile with:

make -C _build

Install with:

make -C _build install

Both Make and Ninja execute build instructions generated by CMake. Ninja is designed to be lightweight and fast, especially for parallel and incremental builds, while Make is older, more widely known, and commonly available on Linux systems.

Depending on the project and system, Ninja can be noticeably faster than Make.