diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 755b5779a83f0a8bbba42c07d253a1fd551a28ad..fe735161de552fb33119df9519bddf894884209a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,6 +48,13 @@ check-recipes: - cmake --build 08_build - ctest --test-dir 08_build || FAILED=true + - cmake --preset dev -S 10_presets -B 10_dev + - cmake --build 10_dev + - ./10_dev/app + - cmake --preset production -S 10_presets -B 10_production + - cmake --build 10_production + - ./10_production/app + check-links: before_script: - module purge diff --git a/10_presets/CMakeLists.txt b/10_presets/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6cfcee07653dacbc5d758f4a08640ca5e08ea9f3 --- /dev/null +++ b/10_presets/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20) + +project(10_presets + LANGUAGES CXX) + +add_executable(app main.cpp) + diff --git a/10_presets/CMakePresets.json b/10_presets/CMakePresets.json new file mode 100644 index 0000000000000000000000000000000000000000..c299dc637e6fcbea4c94739620e63f06ec1ba267 --- /dev/null +++ b/10_presets/CMakePresets.json @@ -0,0 +1,28 @@ +{ + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 20, + "patch": 0 + }, + "configurePresets": [ + { + "name": "dev", + "displayName": "development settings", + "generator": "Unix Makefiles", + "cacheVariables": { + "CMAKE_CXX_COMPILER": "g++", + "CMAKE_CXX_FLAGS": "-Wall -Wextra" + } + }, + { + "name": "production", + "displayName": "production settings", + "generator": "Unix Makefiles", + "cacheVariables": { + "CMAKE_CXX_COMPILER": "g++", + "CMAKE_CXX_FLAGS": "-march=native" + } + } + ] +} diff --git a/10_presets/README.md b/10_presets/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4cf1bd1b708b9ebe4f08cad0375889034c48fedc --- /dev/null +++ b/10_presets/README.md @@ -0,0 +1,22 @@ +# How to add compilation configurations to your project? + +## Brief +[Presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html) are able to replace your custom shell scripts that build your application on different platforms with different settings. Since 3.19 CMake supports presets. + +## Description +Presets are based on two main files. + * `CMakePresets.json` holds the project-wide presets that are checked into the repository. + * `CMakeUserPresets.json` contains developer specific settings and should not be part of the repository.\ + (In fact, you should at it to your `.gitignore` file.) + +Presets have a name and specify flags for the configure, build, test, install, etc stage. +To invoke a preset configuration during the configure stage use e.g. +`cmake --preset <ConfigurePreset> -S <SourceDirectory> -B <BuildDirectory>` +or during the build stage use +`cmake --preset <BuildPreset> --build <BuildDirectory>`. + +## Example +This recipe contains two configure presets as an example. The development preset can be selected like this, +`cmake --preset dev -S <SourceDirectory> -B <BuildDirectory>`. It uses `g++` as a compiler and enables compiler warnings. +If you build you will see a warning printed. In contrast, the `production` preset does not enable compiler warnings, +but optimization flags. A build with this configuration does not show warnings. \ No newline at end of file diff --git a/10_presets/main.cpp b/10_presets/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78aad3a99ecb5587c7988939c5016c4ae0b3ba8a --- /dev/null +++ b/10_presets/main.cpp @@ -0,0 +1,7 @@ +#include <iostream> + +int main() +{ + int a; + std::cout << a << std::endl; +} diff --git a/README.md b/README.md index c62d535d2419e3e3a0d4ee96ea00e0b63677f948..58c37df63eec6aff0c07c3f6d488196ed7d2d7a7 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ we run them in our CI. * [How to get the current git hash at configure time?](06_git_configure) * [How to get the current git hash at build time?](07_git_build) * [How to manage all the tests in your project?](08_ctest) + * [How to add compilation configurations to your project?](10_presets) ## Why CMake? * Automatically search for programs, libraries, and header files