Skip to content

Commit a5cc956

Browse files
committed
Major refactor
1 parent a126947 commit a5cc956

22 files changed

+384
-251
lines changed

.clang-tidy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ Checks: >
1010
-cppcoreguidelines-owning-memory
1111
1212
UseColor: true
13-
WarningsAsErrors: ''
13+
WarningsAsErrors: '*'
1414
HeaderFilterRegex: '^(include|src|tests|benchmarks)/'
1515
FormatStyle: file
1616
CheckOptions:
1717
- key: modernize-use-nullptr.NullMacros
1818
value: 'NULL'
1919
- key: readability-identifier-naming.VariableCase
2020
value: camelBack
21+
- key: readability-identifier-naming.ClassCase
22+
value: CamelCase
23+
- key: readability-identifier-naming.ConstantCase
24+
value: UPPER_CASE
25+
- key: readability-identifier-naming.ParameterCase
26+
value: camelBack

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
matrix:
2929
os: [ubuntu-latest]
3030
compiler: [gcc, clang]
31-
build_type: [RelWithDebInfo, Sanitize]
31+
build_type: [RelWithDebInfo]
3232

3333
steps:
3434
- name: Checkout code
@@ -55,4 +55,4 @@ jobs:
5555
run: cmake --build --preset ${{ matrix.compiler }}-${{ matrix.build_type }}
5656

5757
- name: Run tests
58-
run: ctest --preset ${{ matrix.compiler }}-${{ matrix.build_type }} --output-on-failure
58+
run: ctest --preset ${{ matrix.compiler }}-${{ matrix.build_type }}

.github/workflows/clang-format.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
run: cmake --preset gcc-RelWithDebInfo
3333

3434
- name: Run clang-format
35-
run: cmake --build build/gcc-RelWithDebInfo/ --target clang-format
35+
run: cmake --build --preset gcc-RelWithDebInfo --target clang-format-check

.github/workflows/clang-tidy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
run: cmake --preset gcc-RelWithDebInfo
3737

3838
- name: Run clang-tidy
39-
run: cmake --build build/gcc-RelWithDebInfo/ --target clang-tidy
39+
run: cmake --build --preset gcc-RelWithDebInfo --target clang-tidy

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
"C_Cpp.autoAddFileAssociations": false,
33
// Use CMake presets instead of manual configuration
44
"cmake.useCMakePresets": "always",
5+
"cmake.configureOnOpen": false,
6+
"cmake.autoReload": false,
57
"cmake.configurePreset": "gcc-RelWithDebInfo",
68
"cmake.buildPreset": "gcc-RelWithDebInfo",
79
"cmake.testPreset": "gcc-RelWithDebInfo",
8-
"cmake.autoReload": false,
910
// Disable Microsoft IntelliSense (we use clangd)
1011
"C_Cpp.intelliSenseEngine": "disabled",
1112
"C_Cpp.enabled": false,
@@ -30,4 +31,4 @@
3031
"editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd",
3132
"editor.formatOnSave": true,
3233
}
33-
}
34+
}

CMakeLists.txt

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,91 @@
11
cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
22
project(modern_cpp_project LANGUAGES CXX VERSION 0.1.0)
33

4-
# Enable C++20 with strict enforcement
4+
# === C++ Standard ===
55
set(CMAKE_CXX_STANDARD 20)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

8-
# Export compile_commands.json for clangd and clang-tidy
8+
# === Compile Commands ===
99
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1010

11-
# Options
12-
option(ENABLE_TESTING "Build tests" ON)
13-
option(ENABLE_BENCHMARKS "Build benchmarks" ON)
14-
option(BUILD_APP "Build the demo application" ON)
15-
option(BUILD_DOCS "Build documentation via Doxygen" ON)
16-
17-
# Add custom "Sanitize" build type
11+
# === Custom Build Types ===
1812
if(CMAKE_CONFIGURATION_TYPES)
19-
list(APPEND CMAKE_CONFIGURATION_TYPES Sanitize)
13+
foreach(build_type IN ITEMS Sanitize Coverage)
14+
list(APPEND CMAKE_CONFIGURATION_TYPES ${build_type})
15+
endforeach()
2016
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
2117
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "" FORCE)
2218
endif()
2319

24-
# Add custom "Coverage" build type
25-
if(CMAKE_CONFIGURATION_TYPES)
26-
list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
27-
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
28-
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "" FORCE)
20+
# === Project Options ===
21+
option(ENABLE_WARNINGS "Enable compiler warnings" ON)
22+
option(ENABLE_STRICT_WARNINGS "Treat warnings as errors" ON)
23+
24+
option(ENABLE_SANITIZERS "Enable sanitizer instrumentation" OFF)
25+
26+
option(ENABLE_TESTING "Build unit tests" ON)
27+
option(ENABLE_BENCHMARKS "Build benchmarks" ON)
28+
option(ENABLE_COVERAGE "Enable coverage instrumentation" OFF)
29+
30+
option(BUILD_APP "Build the demo app" ON)
31+
option(BUILD_DOCS "Build Doxygen documentation" ON)
32+
33+
option(ENABLE_CPPCHECK "Enable cppcheck analysis target" ON)
34+
option(ENABLE_CLANG_TIDY "Enable clang-tidy analysis target" ON)
35+
option(ENABLE_CLANG_FORMAT "Enable clang-format target" ON)
36+
37+
# === Normalize Build Type (e.g., for string comparison) ===
38+
if(CMAKE_BUILD_TYPE)
39+
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_LOWER)
40+
else()
41+
set(BUILD_TYPE_LOWER "release")
42+
endif()
43+
44+
# === Auto-toggle Options for Special Build Types ===
45+
if(BUILD_TYPE_LOWER STREQUAL "sanitize")
46+
set(ENABLE_SANITIZERS ON CACHE BOOL "Enable sanitizers" FORCE)
2947
endif()
3048

31-
# Load cmake modules
49+
if(BUILD_TYPE_LOWER STREQUAL "coverage")
50+
set(ENABLE_COVERAGE ON CACHE BOOL "Enable coverage" FORCE)
51+
set(BUILD_APP OFF CACHE BOOL "Disable building app in coverage builds" FORCE)
52+
set(ENABLE_BENCHMARKS OFF CACHE BOOL "Disable benchmarks in coverage builds" FORCE)
53+
endif()
54+
55+
# === CMake Module Path ===
3256
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
3357

34-
# Warnings, Sanitizers, Link Time Optimization (LTO)
35-
include(options/LTO)
58+
# === Core Configuration ===
3659
include(options/Warnings)
3760
include(options/Sanitizers)
3861

39-
# Coverage
62+
# === Optional Tooling ===
4063
include(tools/Coverage)
64+
include(tools/Cppcheck)
65+
include(tools/ClangTidy)
66+
include(tools/ClangFormat)
4167

42-
# === Libraries ===
43-
add_subdirectory(src) # modern_cpp_project::math
68+
# === Core library ===
69+
add_subdirectory(src) # modern_cpp_project::math
4470

4571
# === Application ===
46-
if(BUILD_APP AND NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
47-
add_subdirectory(app) # modern_cpp_project_app
72+
if(BUILD_APP)
73+
add_subdirectory(app) # modern_cpp_project_app
4874
endif()
4975

50-
# === Tests ===
76+
# === Unit Tests ===
5177
if(ENABLE_TESTING)
5278
enable_testing()
5379
add_subdirectory(tests)
5480
endif()
5581

82+
# === Benchmarks ===
5683
if(ENABLE_BENCHMARKS)
57-
if (NOT CMAKE_BUILD_TYPE STREQUAL "Coverage" AND NOT CMAKE_BUILD_TYPE STREQUAL "Sanitize")
58-
add_subdirectory(benchmarks)
59-
endif()
84+
add_subdirectory(benchmarks)
6085
endif()
6186

62-
# === Docs ===
63-
if(BUILD_DOCS)
64-
include(tools/Doxygen)
65-
endif()
87+
# === Documentation ===
88+
include(tools/Doxygen)
6689

6790
# === Installation ===
6891
include(install/InstallConfig)
69-
70-
# === Formatting ===
71-
include(tools/ClangFormat)
72-
73-
# === Linting ===
74-
include(tools/ClangTidy)
75-
include(tools/Cppcheck)

CMakePresets.json

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"binaryDir": "${sourceDir}/build/${presetName}",
1313
"cacheVariables": {
1414
"ENABLE_TESTING": "ON",
15-
"ENABLE_BENCHMARKS": "ON",
16-
"BUILD_APP": "ON",
17-
"BUILD_DOCS": "ON",
15+
"ENABLE_BENCHMARKS": "OFF",
16+
"BUILD_APP": "OFF",
17+
"BUILD_DOCS": "OFF",
18+
"ENABLE_WARNINGS": "ON",
19+
"ENABLE_STRICT_WARNINGS": "ON",
1820
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
1921
},
2022
"description": "Shared settings for all configurations"
@@ -68,8 +70,7 @@
6870
"inherits": "conf-base-gcc",
6971
"cacheVariables": {
7072
"CMAKE_BUILD_TYPE": "Sanitize",
71-
"ENABLE_BENCHMARKS": "OFF",
72-
"BUILD_APP": "OFF"
73+
"ENABLE_SANITIZERS": "ON"
7374
},
7475
"description": "GCC build with sanitizers"
7576
},
@@ -78,8 +79,7 @@
7879
"inherits": "conf-base-gcc",
7980
"cacheVariables": {
8081
"CMAKE_BUILD_TYPE": "Coverage",
81-
"ENABLE_BENCHMARKS": "OFF",
82-
"BUILD_APP": "OFF"
82+
"ENABLE_COVERAGE": "ON"
8383
},
8484
"description": "GCC build for coverage analysis"
8585
},
@@ -112,8 +112,7 @@
112112
"inherits": "conf-base-clang",
113113
"cacheVariables": {
114114
"CMAKE_BUILD_TYPE": "Sanitize",
115-
"ENABLE_BENCHMARKS": "OFF",
116-
"BUILD_APP": "OFF"
115+
"ENABLE_SANITIZERS": "ON"
117116
},
118117
"description": "Clang build with sanitizers"
119118
},
@@ -122,8 +121,7 @@
122121
"inherits": "conf-base-clang",
123122
"cacheVariables": {
124123
"CMAKE_BUILD_TYPE": "Coverage",
125-
"ENABLE_BENCHMARKS": "OFF",
126-
"BUILD_APP": "OFF"
124+
"ENABLE_COVERAGE": "ON"
127125
},
128126
"description": "Clang build for coverage analysis"
129127
}

README.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- ✅ CMake presets with Clang & GCC support
1515
- ✅ C++20 (extendable to C++23)
1616
- ✅ GoogleTest and Google Benchmarks (with `FetchContent`)
17-
- ✅ Sanitizers, Link Time Optimization (LTO)
17+
- ✅ Sanitizers, Coverage (`lcov` + `genhtml`)
1818
- ✅ Clang-format + Clang-tidy + Cppcheck + Pre-commit hook
1919
- ✅ GitHub Actions CI for builds, tests, docs, and formatting
2020
- ✅ Install targets (`find_package(...)` supported)
@@ -29,29 +29,21 @@ This template uses [CMake Presets](https://cmake.org/cmake/help/latest/manual/cm
2929
### Build and Test
3030

3131
```bash
32-
cmake --preset gcc-RelWithDebInfo
33-
cmake --build --preset gcc-RelWithDebInfo
34-
ctest --preset gcc-RelWithDebInfo
32+
cmake --preset gcc-Release
33+
cmake --build --preset gcc-Release
34+
ctest --preset gcc-Release
3535
```
3636

3737
You can list available presets:
3838
```bash
3939
cmake --list-presets
4040
```
4141

42-
#### Enable Sanitizers
42+
#### Sanitizers
4343
Use the special `Sanitize` build type to catch runtime issues:
4444
```bash
4545
cmake --preset gcc-Sanitize
4646
cmake --build --preset gcc-Sanitize
47-
ctest --preset gcc-Sanitize
48-
```
49-
50-
#### Link Time Optimization
51-
LTO is enabled via a dedicated preset:
52-
```bash
53-
cmake --preset gcc-RelWithDebInfo-lto
54-
cmake --build --preset gcc-RelWithDebInfo-lto
5547
```
5648

5749
#### Testing
@@ -63,9 +55,23 @@ cmake --build --preset gcc-RelWithDebInfo-lto
6355

6456
Run tests:
6557
```bash
58+
cmake --preset gcc-Sanitize
59+
cmake --build --preset gcc-Sanitize
6660
ctest --preset gcc-Sanitize
6761
```
6862

63+
#### Coverage
64+
65+
This project supports code coverage analysis using `lcov` and `genhtml`. To enable coverage:
66+
```bash
67+
cmake --preset gcc-Coverage
68+
cmake --build --preset gcc-Coverage
69+
ctest --preset gcc-Coverage
70+
cmake --build --preset gcc-Coverage --target coverage
71+
```
72+
73+
The report will be saved in `<project_root>/build/gcc-Coverage/coverage-report/index.html`.
74+
6975
### Code Style and Linting
7076

7177
This project uses:
@@ -76,16 +82,17 @@ If `clang-format` or `clang-tidy` is not found, the corresponding target will be
7682

7783
Run checks manually using custom CMake targets:
7884
```bash
79-
cmake --build --preset gcc-RelWithDebInfo --target clang-format
80-
cmake --build --preset gcc-RelWithDebInfo --target clang-tidy
85+
cmake --preset gcc-Release
86+
cmake --build --preset gcc-Release --target clang-format-check
87+
cmake --build --preset gcc-Release --target clang-tidy
8188
```
8289

8390
### Installation
8491

8592
Install includes, libs, and CMake config files:
8693
```bash
87-
cmake --preset gcc-RelWithDebInfo
88-
cmake --build --preset gcc-RelWithDebInfo
94+
cmake --preset gcc-Release
95+
cmake --build --preset gcc-Release
8996
cmake --install build/gcc-RelWithDebInfo --prefix install # or /usr/local
9097
```
9198

@@ -110,8 +117,8 @@ Documentation is generated using [Doxygen](https://www.doxygen.nl). Docs are bui
110117

111118
Generate docs locally via custom CMake target:
112119
```bash
113-
cmake --preset gcc-RelWithDebInfo
114-
cmake --build --preset gcc-RelWithDebInfo --target docs
120+
cmake --preset gcc-Release -DBUILD_DOCS=ON
121+
cmake --build --preset gcc-Release --target docs
115122
```
116123

117124
View documentation online: [Modern C++ template](https://ramsafin.github.io/modern-cpp-project-template/html).

app/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ target_link_libraries(${PROJECT_NAME}_app
66
PRIVATE ${PROJECT_NAME}::math
77
)
88

9-
enable_lto(${PROJECT_NAME}_app)
109
enable_sanitizers(${PROJECT_NAME}_app)
1110
enable_strict_warnings(${PROJECT_NAME}_app)

0 commit comments

Comments
 (0)