Skip to content

Commit 886ae31

Browse files
author
Anton Pantyukhin
committed
Add CMake preset
1 parent 1a59144 commit 886ae31

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@ build*/
33
_build*/
44
cmake-build*/
55

6+
# CMake
7+
CMakeUserPresets.json
8+
9+
# IDEs
10+
.idea/
11+
.vs/
12+
.vscode/
13+
614
# Mac
715
.DS_Store

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ set_target_properties(mylib PROPERTIES
9292
SOVERSION ${PROJECT_VERSION_MAJOR}
9393
VERSION ${PROJECT_VERSION})
9494

95-
if(MYLIB_INSTALL)
95+
if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
9696
configure_package_config_file(cmake/mylib-config.cmake.in mylib-config.cmake
9797
INSTALL_DESTINATION "${MYLIB_INSTALL_CMAKEDIR}")
9898

CMakePresets.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"version": 3,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 14,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "dev",
11+
"description": "Base preset for library developers",
12+
"binaryDir": "${sourceDir}/build",
13+
"hidden": true,
14+
"cacheVariables": {
15+
"MYLIB_BUILD_TESTS": "ON",
16+
"MYLIB_BUILD_EXAMPLES": "ON"
17+
}
18+
},
19+
{
20+
"name": "dev-win",
21+
"description": "Windows preset for library developers",
22+
"hidden": false,
23+
"inherits": ["dev"],
24+
"cacheVariables": {
25+
"CMAKE_CXX_FLAGS": "/W4 /EHsc /w14242 /w14254 /w14263 /w14265 /w14287 /w14289 /w14296 /w14311 /w14545 /w14546 /w14547 /w14549 /w14555 /w14640 /w14826 /w14928 /WX"
26+
}
27+
},
28+
{
29+
"name": "dev-linux",
30+
"description": "Linux preset for library developers",
31+
"hidden": false,
32+
"inherits": ["dev"],
33+
"cacheVariables": {
34+
"CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wsign-conversion -Wcast-align -Wcast-qual -Wnull-dereference -Woverloaded-virtual -Wformat=2 -Werror"
35+
}
36+
}
37+
]
38+
}

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# Overview
22

3-
Simple template repository to be used as a base for creating C++ libraries built with CMake. This repository provides:
3+
Template repository to be used as a base for creating C++ libraries built with CMake. This repository provides:
44
* Commonly accepted directory layout: `cmake` for CMake utilities and package config, `include/<libname>` for public headers, `src` for library sources and private headers, `examples` and `tests` for library examples and tests correspondingly.
55
* `CMakeLists.txt` files thoroughly implemented with the following ideas in mind:
66
* user should be able to build library both as subproject (probably fetched with CMake's [FetchContent_MakeAvailable](https://cmake.org/cmake/help/latest/module/FetchContent.html)) and as a stand-alone project
77
* user should be able to build examples/tests as part of the library build or as a stand-alone projects
88
* multi-configuration CMake generators should be supported
99
* package systems (conan, vcpkg and others) should be respected, which means that library `CMakeLists.txt` file should contain only **build requirements**, i.e. should not hardcode any compiler/linker flags unless they are absolutely required to build the library
1010
* Basic [preset](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html) file `CMakePresets.json` which is a modern way (since CMake 3.19) for specifying build options (instead of hardcoding them in the `CMakeLists.txt` or setting on the command line)
11-
* My personal worth trying `.clang-format` for code-style
11+
* My personal `.clang-format` for code-style worth trying
1212

1313
Note, that I prefer [googletest](https://github.com/google/googletest) as a testing framework so it's the one used in this repository. Still it's not a big deal to replace it with any other of your choice.
14-
15-
See also [cmake-init](https://github.com/friendlyanon/cmake-init) tool which is developed under the same principles as this repository but offers much more: CMake executable/library project generation, integrated support for linter/coverage tools for generated projects, etc.

cmake/utils.cmake

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# set_if_undefined(<variable> [<value>]...) - set variable if it is not defined
1+
# set_if_undefined(<variable> [<value>]...)
2+
#
3+
# Set variable if it is not defined.
24
macro(set_if_undefined variable)
3-
if(NOT DEFINED ${variable})
4-
set(${variable} ${ARGN})
5+
if(NOT DEFINED "${variable}")
6+
set("${variable}" ${ARGN})
57
endif()
68
endmacro()

examples/add/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <mylib/mylib.h>
22

3-
int main(int argc, char* argv[])
3+
int main(int, char*[])
44
{
55
return 0;
66
}

0 commit comments

Comments
 (0)