.. _cmakepresets: cmake presets -------------- CMake presets are |FOURC|'s recommended way to configure and manage different configurations of |FOURC|. This small article will go through a few of them. The experts should also read the `official CMake presets documentation `_ to get all convenient tricks. Configuration from a terminal ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Suppose you are in the build directory where you want to build |FOURC|. You can configure a release build of |FOURC| with:: cmake --preset=lnm_workstation ../path/to/source **Hint:** The global configurations of cmake are stored in the ``CMakeCache.txt`` within the build folder and it's sometimes helpful to remove it with ``rm CMakeCache.txt``, before configuring the project new. There is a number of available preset files, which can be retrieved by ``cmake <4C_sourcedir> --list-presets``. This is the current output of this command: .. literalinclude:: 4C-cmake-presets.txt In general, it is highly recommended to create your own preset, see below. Defining your own CMake presets ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CMake presets allow you to also create your own configuration. You need to put a ``CMakeUserPresets.json``-file (**important:** ``User``) in the source-directory of |FOURC|. This file will not be part of the repository as it is listed in ``.gitignore``. In this file, you can define your own configurations. Particularly, you may define the binary directory, so you don't need to go to your binary directory in order to configure |FOURC|. CMake presets integrate well with recent releases of IDEs. You can define as many configurations as you need. Note that you can inherit from other configurations by using the keyword ``inherits``. Such a local preset could look like this:: { "version": 5, "configurePresets": [ { "name": "myworkstation", "displayName": "Release build for my workstation", "binaryDir": "<4C-execdir>/4C", "generator": "Ninja", "inherits": [ "lnm_workstation" ], "cacheVariables": { "CMAKE_CXX_COMPILER": "g++", "CMAKE_CXX_COMPILER_LAUNCHER": "ccache", "FOUR_C_WITH_GOOGLETEST": "OFF", "FOUR_C_BUILD_DOCUMENTATION": "ON", "FOUR_C_BUILD_DOXYGEN": "ON", "FOUR_C_ENABLE_DEVELOPER_MODE": "ON", } } ] } Don't be overwhelmed by the options you could potentially set. - For a basic build of |FOURC|, you should start with `CMAKE_BUILD_TYPE` (either `RELEASE` or `DEBUG`) and maybe a compiler. - If you are developing |FOURC| with this configuration, you usually want to turn on ``FOUR_C_ENABLE_DEVELOPER_MODE`` which optimizes the build setup for iterative development cycles. - We try to detect reasonable defaults for you internally. Over time you might realize that you want to turn on additional dependencies or features. To see which other options you can set, consult the console output of CMake or run `ccmake .` in the build folder. Alternatively, all options are also printed with their ``ON`` or ``OFF`` state whenever ``cmake`` runs. **Remark:** Variables either start with the prefix `FOUR_C_` indicating that this variable only affects |FOURC| itself, or they start with `CMAKE_` indicating that the variable (potentially) affects all dependent projects in a way specified directly in the CMake documentation. Configuration from the IDE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Our recommended IDEs (VS Code and CLion) already support cmake presets natively. Here is a screenshot taken from VS Code: .. figure:: figures/vs-code-cmake-preset.png :alt: CMake preset selection within VS Code :width: 100% Hints for VS Code: You need to install the extensions "CMake Tools" from Microsoft. For CMake maintainers ~~~~~~~~~~~~~~~~~~~~~ This section documents how we write the CMake code. For users and developers who do not work on the build system, this section is not necessary. Conventions ........... - In general, do not modify `CMAKE_` variables inside CMake files! They affect downstream packages (e.g. from `fetch_content`) as well and we cannot easily know whether these packages can handle our settings. - Prefer the modern CMake way and use targets over variables when handling requirements and dependencies. - Every variable that is supposed to be set from outside has to start with `FOUR_C_`. Variables that toggle a dependency are named `FOUR_C_WITH_`. Further options for a package are specified by `FOUR_C__