SpECTRE  v2021.11.01
Build System


SpECTRE uses CMake for the build system. In this section of the guide we outline how to add new source files, libraries, unit tests, executables, and external dependencies. We also describe commonly used CMake flags.

Note that in editing CMakeLists.txt files, it is conventional to indent multiline commands by two spaces (except for the first line), and to separate most commands by blank lines.

Adding Source Files

SpECTRE organizes source files into subdirectories of src that are compiled into libraries. To add a new source file FILE.cpp to an existing library in src/PATH/DIR, just edit src/PATH/DIR/CMakeLists.txt and add FILE.cpp to the list of files in


such that the resulting <list_of_files> is in alphabetical order.

Adding Libraries

To add a source file FILE.cpp that is compiled into a new library LIB in a directory src/PATH/DIR (either in a new directory, or in an existing directory that either does not have a CMakeLists.txt file, or does not create a library in the existing CMakeLists.txt):

  • Create (if necessary) a CMakeLists.txt file in DIR, with the following two lines at the top:
    # Distributed under the MIT License.
    # See LICENSE.txt for details.
  • In the parent directory (i.e. src/PATH), (if necessary) add the following line to its CMakeLists.txt file (if necessary, recursively do the previous step and this one until you reach a CMakeLists.txt that adds the appropriate subdirectory):
    If there are already other add_subdirectory() lines in the file, place the new one so that the subdirectories are in alphabetical order.
  • Add the line:
    set(LIBRARY LIB)
    where convention is that LIB = DIR. As library names must be unique, this is not always possible, in which case the convention is to prepend the parent directory to DIR.
  • Add the lines
    add_spectre_library(${LIBRARY} ${LIBRARY_SOURCES})
    where each <list_of_X_libraries> is an alphabetized list of libraries of the form
    The libraries listed under INTERFACE are those included in at least one .hpp file in LIB but never used in any .cpp files in LIB. The libraries listed under PRIVATE are used in at least one .cpp file in LIB but not in any .hpp file in LIB. The libraries listed under PUBLIC are used in at least one .hpp file and at least one .cpp file in LIB. Note that a library counts as being used in a .cpp file if the corresponding .hpp file includes it. In other words, list a dependency as PRIVATE if it is needed only to compile the library, but not for including headers. List a dependency as INTERFACE if it is not needed to compile the library, but is needed for including headers. List a dependency as PUBLIC if it is needed for both.

Adding Unit Tests

We use the Catch testing framework for unit tests. All unit tests are housed in tests/Unit with subdirectories for each subdirectory of src. Add the cpp file to the appropriate subdirectory and also to the CMakeLists.txt in that subdirectory. Inside the source file you can create a new test by adding a SPECTRE_TEST_CASE("Unit.Dir.Component", "[Unit][Dir][Tag]"). The [Tag] is optional and you can have more than one, but the tags should be used quite sparingly. The purpose of the tags is to be able to run all unit tests or all tests of a particular set of components, e.g. ctest -L Data to run all tests inside the Data directory. Please see writing unit tests, other unit tests and the Catch documentation for more help on writing tests. Unit tests should take as short a time as possible, with a goal of less than two seconds. Please also limit the number of distinct cases (by using SECTIONs).

You can check the unit test coverage of your code by installing all the optional components and then running make unit-test-coverage (after re-running CMake). This will create the directory BUILD_DIR/docs/html/unit-test-coverage/ which is where the coverage information is located. Open the index.html file in your browser and make sure that your tests are indeed checking all lines of your code. Your pull requests might not be merged until your line coverage is over 90% (we are aiming for 100% line coverage wherever possible). Unreachable lines of code can be excluded from coverage analysis by adding the inline comment LCOV_EXCL_LINE or a block can be excluded using LCOV_EXCL_START and LCOV_EXCL_STOP. However, this should be used extremely sparingly since unreachable code paths should be removed from the code base altogether.

Adding Executables

All general executables are found in src/Executables, while those for specific evolution (elliptic) systems are found in src/Evolution/Executables (src/Elliptic/Executables). See how to create executables.

Adding External Dependencies

To add an external dependency, first add a SetupDEPENDENCY.cmake file to the cmake directory. You should model this after one of the existing ones for Catch or Brigand if you're adding a header-only library and yaml-cpp if the library is not header-only. If CMake does not already support find_package for the library you're adding you can write your own. These should be modeled after FindBrigand or FindCatch for header-only libraries, and FindYAMLCPP for compiled libraries. The SetupDEPENDENCY.cmake file must then be included in the root spectre/CMakeLists.txt. Be sure to test both that setting LIBRARY_ROOT works correctly for your library, and also that if the library is required that CMake fails gracefully if the library is not found.

Commonly Used CMake flags

The following are the most common flags used to control building with CMake. They are used by

  • ASAN
    • Whether or not to turn on the address sanitizer compile flags (-fsanitize=address) (default is OFF)
    • Build python libraries to call SpECTRE C++ code from python (default is OFF)
    • Whether shared libraries are built instead of static libraries (default is OFF)
    • The path to the build directory of Charm++
    • Sets the build type. Common options:
      • Debug (the default if the flag is not specified): sets flags that trigger additional error checking
      • Release
    • The C compiler used (defaults to whatever is determined by CMake/Modules/CMakeDetermineCCompiler.cmake, usually cc)
    • The C++ compiler used (defaults to whatever is determined by CMake/Modules/CMakeDetermineCXXCompiler.cmake, usually c++)
    • The Fortran compiler used (defaults to whatever is determined by CMake/Modules/CMakeDetermineFortranCompiler.cmake)
    • Additional flags passed to the C compiler.
    • Additional flags passed to the C++ compiler.
  • CMAKE_Fortran_FLAGS
    • Additional flags passed to the Fortran compiler.
    • Sets the directory where the library and executables are placed. By default libraries end up in <BUILD_DIR>/lib and executables in <BUILD_DIR>/bin.
    • Whether or not to use debug symbols (default is ON)
    • Disabling debug symbols will reduce compile time and total size of the build directory.
    • Enables various options to make profiling SpECTRE easier (default is OFF)
    • Whether or not warning flags are enabled (default is ON)
    • Whether to keep the frame pointer. Needed for profiling or other cases where you need to be able to figure out what the call stack is. (default is OFF)
    • Set which memory allocator to use. If there are unexplained segfaults or other memory issues, it would be worth setting MEMORY_ALLOCATOR=SYSTEM to see if that resolves the issue. It could be the case that different third-party libraries accidentally end up using different allocators, which is undefined behavior and will result in complete chaos. (default is JEMALLOC)
    • Multiply the timeout for the respective set of tests by this factor (default is 1).
    • This is useful to run tests on slower machines.
    • Whether or not to strip all symbols (default is OFF)
    • If enabled strips all extraneous symbols from libraries and executables, further reducing the size of them.
    • Replace object files from executables after linking with empty stubs (default is OFF)
    • This is useful for drastically reducing the build size in CI, but since the object files are replaced with empty stubs will generally cause linking problems if used during development.
    • Replace object files from libraries after linking with empty stubs (default is OFF)
    • This is useful for drastically reducing the build size in CI, but since the object files are replaced with empty stubs will generally cause linking problems if used during development.
    • Whether or not to turn on the undefined behavior sanitizer unsigned integer overflow flag (-fsanitize=integer) (default is OFF)
    • Whether or not to turn on the undefined behavior sanitizer undefined behavior compile flags (-fsanitize=undefined) (default is OFF)
    • Whether to build the unit-tests target as part of the test-executables target. This is used to build only the non-unit tests in the CI build that doesn't use the PCH. (default is ON)
    • Use ccache to cache build output so that rebuilding parts of the source tree is faster. The cache will use up space on disk, with the default being around 2-5GB. If you are performing a one time build to test something specific you should consider disabling ccache in order to avoid removing cached files that may be useful in other builds. (default is ON)
    • Write the source tree into HDF5 files written to disk in order to increase reproducibility of results. (default is ON)
    • Use git hooks to perform certain sanity checks so that small goofs are caught before getting to CI. Most situations where the hooks can cause problems are handled automatically. The exception is when the build directory is inside a (Singularity) container, we use Python 2 and the host machine (where we make the commits) does not have Python 2. Except for these types of cases, we strongly encourage you to keep the hooks enabled since the same checks are performed during CI and there is a good reason why the checks exist in the first place. (default is ON)
  • USE_LD
    • Override the automatically chosen linker. The options are ld, gold, and lld. (default is OFF)

Checking Dependencies

Getting dependencies of libraries correct is quite difficult. SpECTRE offers the CMake function check_spectre_libs_dependencies, defined in cmake/SpectreCheckDependencies.cmake, to check the dependencies for all libraries in the libs target. Individual target dependencies can be checked using the check_target_dependencies CMake function defined in cmake/SpectreCheckTargetDependencies.cmake. Please see those functions in the source tree for more details on how to use them.


SpECTRE's implementation of Formaline is based on, but distinct in implementation from, the original design by Erik Schnetter and Christian Ott, which embeds an archive of the source tree into the executable. The original design creates a C/C++ file with a function that returns an array/vector of chars (a byte stream). However, this results in a very large source file (50MB or more), which is very slow to compile and ends up more than doubling the link time. Instead, SpECTRE's Formaline implementation uses the linker ld to encode a file into an object, which means rather than creating a large source file, we can directly encode the source tree archive into the binary at the linking stage.

Most of SpECTRE's Formaline is implemented inside the tools/WrapExecutableLinker.sh script. Function declarations are provided in Utilities/Formaline.hpp and a small function that writes the source file to disk is defined in Utilities/Formaline.cpp. The first Formaline-related thing done in WrapExecutableLinker.sh is to archive everything in the source directory tracked by git. Once the archive is created we run ld -r -b binary -o object.o src.tar.gz (with unique names for object.o and src.tar.gz for each executable that is built to avoid name collisions) to generate an object file with the source file encoded from _binary_src_tar_gz_start to _binary_src_tar_gz_end. Next we write a C++ source file that defines a function get_archive to convert the byte stream into a std::vector<char>. We also encode the output of printenv, the various PATH environment variables, and the CMake generated BuildInfo.txt file into the source file. Finally, the generated source file is built during the linking phase and the object file containing the source archive is linked into the executable.

To further aid in reproducibility, the printenv output and BuildInfo.txt contents are written to HDF5 files as part of the h5::Header object. The archive of the source tree is written using the h5::SourceArchive object and can be extracted by running

h5dump -d /src.tar.gz -b LE -o src.tar.gz /path/to/hdf5/file.h5