Skip to content
Snippets Groups Projects
Commit 1fa270d7 authored by Sebastian Eibl's avatar Sebastian Eibl
Browse files

Merge branch '10_presets' into 'main'

add example for cmake presets

See merge request !5
parents e25dffb4 62f773db
No related branches found
No related tags found
1 merge request!5add example for cmake presets
Pipeline #157810 passed
...@@ -48,6 +48,13 @@ check-recipes: ...@@ -48,6 +48,13 @@ check-recipes:
- cmake --build 08_build - cmake --build 08_build
- ctest --test-dir 08_build || FAILED=true - 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: check-links:
before_script: before_script:
- module purge - module purge
......
cmake_minimum_required(VERSION 3.20)
project(10_presets
LANGUAGES CXX)
add_executable(app main.cpp)
{
"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"
}
}
]
}
# 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
#include <iostream>
int main()
{
int a;
std::cout << a << std::endl;
}
...@@ -18,6 +18,7 @@ we run them in our CI. ...@@ -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 configure time?](06_git_configure)
* [How to get the current git hash at build time?](07_git_build) * [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 manage all the tests in your project?](08_ctest)
* [How to add compilation configurations to your project?](10_presets)
## Why CMake? ## Why CMake?
* Automatically search for programs, libraries, and header files * Automatically search for programs, libraries, and header files
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment