Skip to content

Commit ad2fc82

Browse files
committed
Major refactor: configured sanitizer build/testing
1 parent 5a762de commit ad2fc82

File tree

7 files changed

+75
-88
lines changed

7 files changed

+75
-88
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
matrix:
1515
os: [ubuntu-latest]
1616
compiler: [gcc, clang]
17-
build_type: [RelWithDebInfo]
17+
build_type: [RelWithDebInfo, Sanitize]
1818

1919
env:
2020
ENABLE_TESTING: ON
@@ -37,16 +37,11 @@ jobs:
3737
echo "CXX=g++" >> $GITHUB_ENV
3838
fi
3939
40-
- name: Configure with CMake
41-
run: >
42-
cmake -S . -B build
43-
-G Ninja
44-
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
45-
-DENABLE_TESTING=${{ env.ENABLE_TESTING }}
46-
${{ matrix.build_type == 'RelWithDebInfo' && '-DENABLE_ASAN=ON -DENABLE_UBSAN=ON' || '' }}
40+
- name: Configure with CMake Preset
41+
run: cmake --preset ${{ matrix.compiler }}-${{ matrix.build_type }}
4742

4843
- name: Build
49-
run: cmake --build build
44+
run: cmake --build --preset ${{ matrix.compiler }}-${{ matrix.build_type }}
5045

5146
- name: Run tests
52-
run: ctest --test-dir build --output-on-failure
47+
run: ctest --preset ${{ matrix.compiler }}-${{ matrix.build_type }} --output-on-failure

CMakeLists.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
22
project(modern_cpp_project LANGUAGES CXX)
33

44
# Enable C++20 with strict enforcement
@@ -8,14 +8,18 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
# Export compile_commands.json for clangd and clang-tidy
99
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1010

11-
# Sanitizer options
12-
option(ENABLE_ASAN "Enable AddressSanitizer" ON)
13-
option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer" ON)
11+
# Add custom "Sanitize" build type for multi-config generators (e.g., Visual Studio)
12+
if(CMAKE_CONFIGURATION_TYPES)
13+
list(APPEND CMAKE_CONFIGURATION_TYPES Sanitize)
14+
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
15+
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "" FORCE)
16+
endif()
1417

18+
# Load sanitizer flags if needed
1519
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
1620
include(Sanitizers)
1721

18-
# Targets
22+
# === Application target ===
1923
add_executable(${PROJECT_NAME}
2024
src/main.cpp
2125
)
@@ -28,11 +32,11 @@ if(TARGET sanitizers)
2832
target_link_libraries(${PROJECT_NAME} PRIVATE sanitizers)
2933
endif()
3034

31-
# Testing options
35+
# === Unit Testing ===
3236
option(ENABLE_TESTING "Build tests" ON)
3337

3438
if(ENABLE_TESTING)
35-
message(STATUS "Enabled unit tests")
39+
message(STATUS "Enabled unit tests")
3640
enable_testing()
3741
add_subdirectory(tests)
3842
endif()

CMakePresets.json

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,85 @@
1212
"generator": "Ninja",
1313
"binaryDir": "${sourceDir}/build/${presetName}",
1414
"cacheVariables": {
15-
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
16-
"ENABLE_TESTING": "ON"
15+
"ENABLE_TESTING": "ON",
16+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
1717
}
1818
},
1919
{
20-
"name": "gcc-release",
20+
"name": "gcc-RelWithDebInfo",
2121
"inherits": "base",
22-
"description": "Build using GCC in Release mode",
22+
"description": "GCC with RelWithDebInfo",
2323
"cacheVariables": {
24-
"CMAKE_BUILD_TYPE": "Release",
24+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
2525
"CMAKE_C_COMPILER": "/usr/bin/gcc",
2626
"CMAKE_CXX_COMPILER": "/usr/bin/g++"
2727
}
2828
},
2929
{
30-
"name": "gcc-relwithdebinfo",
30+
"name": "gcc-Sanitize",
3131
"inherits": "base",
32-
"description": "GCC with RelWithDebInfo and sanitizers",
32+
"description": "GCC with ASan/UBSan (custom Sanitize build type)",
3333
"cacheVariables": {
34-
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
35-
"ENABLE_ASAN": "ON",
36-
"ENABLE_UBSAN": "ON",
34+
"CMAKE_BUILD_TYPE": "Sanitize",
3735
"CMAKE_C_COMPILER": "/usr/bin/gcc",
3836
"CMAKE_CXX_COMPILER": "/usr/bin/g++"
3937
}
4038
},
4139
{
42-
"name": "clang-release",
40+
"name": "clang-RelWithDebInfo",
4341
"inherits": "base",
44-
"description": "Build using Clang in Release mode",
42+
"description": "Clang with RelWithDebInfo",
4543
"cacheVariables": {
46-
"CMAKE_BUILD_TYPE": "Release",
44+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
4745
"CMAKE_C_COMPILER": "/usr/bin/clang",
4846
"CMAKE_CXX_COMPILER": "/usr/bin/clang++"
4947
}
5048
},
5149
{
52-
"name": "clang-relwithdebinfo",
50+
"name": "clang-Sanitize",
5351
"inherits": "base",
54-
"description": "Clang with RelWithDebInfo and sanitizers",
52+
"description": "Clang with ASan/UBSan (custom Sanitize build type)",
5553
"cacheVariables": {
56-
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
57-
"ENABLE_ASAN": "ON",
58-
"ENABLE_UBSAN": "ON",
54+
"CMAKE_BUILD_TYPE": "Sanitize",
5955
"CMAKE_C_COMPILER": "/usr/bin/clang",
6056
"CMAKE_CXX_COMPILER": "/usr/bin/clang++"
6157
}
6258
}
6359
],
6460
"buildPresets": [
6561
{
66-
"name": "gcc-release",
67-
"configurePreset": "gcc-release"
62+
"name": "gcc-RelWithDebInfo",
63+
"configurePreset": "gcc-RelWithDebInfo"
64+
},
65+
{
66+
"name": "gcc-Sanitize",
67+
"configurePreset": "gcc-Sanitize"
68+
},
69+
{
70+
"name": "clang-RelWithDebInfo",
71+
"configurePreset": "clang-RelWithDebInfo"
72+
},
73+
{
74+
"name": "clang-Sanitize",
75+
"configurePreset": "clang-Sanitize"
76+
}
77+
],
78+
"testPresets": [
79+
{
80+
"name": "gcc-RelWithDebInfo",
81+
"configurePreset": "gcc-RelWithDebInfo"
6882
},
6983
{
70-
"name": "gcc-relwithdebinfo",
71-
"configurePreset": "gcc-relwithdebinfo"
84+
"name": "gcc-Sanitize",
85+
"configurePreset": "gcc-Sanitize"
7286
},
7387
{
74-
"name": "clang-release",
75-
"configurePreset": "clang-release"
88+
"name": "clang-RelWithDebInfo",
89+
"configurePreset": "clang-RelWithDebInfo"
7690
},
7791
{
78-
"name": "clang-relwithdebinfo",
79-
"configurePreset": "clang-relwithdebinfo"
92+
"name": "clang-Sanitize",
93+
"configurePreset": "clang-Sanitize"
8094
}
8195
]
8296
}

cmake/Sanitizers.cmake

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
# Optional CMake variables:
2-
# - ENABLE_ASAN=ON
3-
# - ENABLE_UBSAN=ON
4-
5-
# Create sanitizer interface library if enabled
6-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
7-
set(SANITIZER_FLAGS "")
8-
9-
if(ENABLE_ASAN)
10-
list(APPEND SANITIZER_FLAGS -fsanitize=address)
11-
endif()
12-
13-
if(ENABLE_UBSAN)
14-
list(APPEND SANITIZER_FLAGS -fsanitize=undefined)
15-
endif()
16-
17-
if(SANITIZER_FLAGS)
18-
add_library(sanitizers INTERFACE)
19-
target_compile_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
20-
target_link_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
21-
message(STATUS "Sanitizer interface created with flags: ${SANITIZER_FLAGS}")
22-
endif()
1+
# Enable AddressSanitizer + UndefinedBehaviorSanitizer only for "Sanitize" build type
2+
if(CMAKE_BUILD_TYPE STREQUAL "Sanitize")
3+
set(SANITIZER_FLAGS
4+
-fsanitize=address
5+
-fsanitize=undefined
6+
-O1
7+
-g
8+
)
9+
10+
add_library(sanitizers INTERFACE)
11+
target_compile_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
12+
target_link_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
13+
14+
message(STATUS "Sanitize build: enabled with flags: ${SANITIZER_FLAGS}")
2315
endif()
24-

tests/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
include(FetchContent)
22

3+
# Download and configure GoogleTest
34
FetchContent_Declare(
45
googletest
56
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
67
DOWNLOAD_EXTRACT_TIMESTAMP TRUE # Prevents timestamp-related rebuild issues
78
)
89

9-
# Windows-specific: avoid CRT conflicts
10+
# Avoid static/shared CRT mismatch on Windows
1011
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
1112

1213
FetchContent_MakeAvailable(googletest)
1314

14-
# Unit tests
15+
# === Unit Test Target ===
1516
add_executable(unit_tests
16-
main.cpp
17+
test_main.cpp
1718
test_core.cpp
18-
test_sanitizers.cpp
1919
)
2020

2121
target_link_libraries(unit_tests
@@ -27,7 +27,7 @@ target_include_directories(unit_tests
2727
)
2828

2929
if(TARGET sanitizers)
30-
message(STATUS "Enabled sanitizers for unit tests")
30+
message(STATUS "Enabled sanitizers for unit tests")
3131
target_link_libraries(unit_tests PRIVATE sanitizers)
3232
endif()
3333

File renamed without changes.

tests/test_sanitizers.cpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)