|
1 | 1 | cmake_minimum_required(VERSION 3.23 FATAL_ERROR) |
2 | 2 | project(modern_cpp_project LANGUAGES CXX VERSION 0.1.0) |
3 | 3 |
|
4 | | -# Enable C++20 with strict enforcement |
| 4 | +# === C++ Standard === |
5 | 5 | set(CMAKE_CXX_STANDARD 20) |
6 | 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
7 | 7 |
|
8 | | -# Export compile_commands.json for clangd and clang-tidy |
| 8 | +# === Compile Commands === |
9 | 9 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
10 | 10 |
|
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 === |
18 | 12 | 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() |
20 | 16 | list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES) |
21 | 17 | set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "" FORCE) |
22 | 18 | endif() |
23 | 19 |
|
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) |
29 | 47 | endif() |
30 | 48 |
|
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 === |
32 | 56 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") |
33 | 57 |
|
34 | | -# Warnings, Sanitizers, Link Time Optimization (LTO) |
35 | | -include(options/LTO) |
| 58 | +# === Core Configuration === |
36 | 59 | include(options/Warnings) |
37 | 60 | include(options/Sanitizers) |
38 | 61 |
|
39 | | -# Coverage |
| 62 | +# === Optional Tooling === |
40 | 63 | include(tools/Coverage) |
| 64 | +include(tools/Cppcheck) |
| 65 | +include(tools/ClangTidy) |
| 66 | +include(tools/ClangFormat) |
41 | 67 |
|
42 | | -# === Libraries === |
43 | | -add_subdirectory(src) # modern_cpp_project::math |
| 68 | +# === Core library === |
| 69 | +add_subdirectory(src) # modern_cpp_project::math |
44 | 70 |
|
45 | 71 | # === 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 |
48 | 74 | endif() |
49 | 75 |
|
50 | | -# === Tests === |
| 76 | +# === Unit Tests === |
51 | 77 | if(ENABLE_TESTING) |
52 | 78 | enable_testing() |
53 | 79 | add_subdirectory(tests) |
54 | 80 | endif() |
55 | 81 |
|
| 82 | +# === Benchmarks === |
56 | 83 | 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) |
60 | 85 | endif() |
61 | 86 |
|
62 | | -# === Docs === |
63 | | -if(BUILD_DOCS) |
64 | | - include(tools/Doxygen) |
65 | | -endif() |
| 87 | +# === Documentation === |
| 88 | +include(tools/Doxygen) |
66 | 89 |
|
67 | 90 | # === Installation === |
68 | 91 | include(install/InstallConfig) |
69 | | - |
70 | | -# === Formatting === |
71 | | -include(tools/ClangFormat) |
72 | | - |
73 | | -# === Linting === |
74 | | -include(tools/ClangTidy) |
75 | | -include(tools/Cppcheck) |
|
0 commit comments