Skip to content

Commit bbc87b9

Browse files
committed
refactor: use unified USE_SANITIZER option for CI stability
1 parent eeafba8 commit bbc87b9

2 files changed

Lines changed: 23 additions & 19 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,11 @@ jobs:
4949
mkdir build
5050
cd build
5151
export CCACHE_DIR=~/.ccache
52-
if [ "${{ matrix.sanitizer }}" == "thread" ]; then
53-
SAN_FLAGS="-fsanitize=thread"
54-
else
55-
SAN_FLAGS="-fsanitize=address -fno-omit-frame-pointer"
56-
fi
5752
5853
cmake .. -G Ninja \
5954
-DCMAKE_CXX_COMPILER=${{ matrix.compiler }} \
6055
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
61-
-DCMAKE_CXX_FLAGS="$SAN_FLAGS" \
62-
-DCMAKE_EXE_LINKER_FLAGS="$SAN_FLAGS" \
63-
-DCMAKE_SHARED_LINKER_FLAGS="$SAN_FLAGS" \
56+
-DUSE_SANITIZER=${{ matrix.sanitizer }} \
6457
-DBUILD_TESTS=ON \
6558
-DBUILD_COVERAGE=${{ matrix.sanitizer == 'address' && 'ON' || 'OFF' }}
6659

CMakeLists.txt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,27 @@ set(CMAKE_CXX_EXTENSIONS OFF)
99
# Position Independent Code (required for TSan and good practice for libraries)
1010
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1111

12-
# Enable AddressSanitizer
13-
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
14-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
15-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
16-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
12+
# Build options
13+
option(BUILD_TESTS "Build tests" ON)
14+
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
15+
option(BUILD_COVERAGE "Build with coverage instrumentation" OFF)
16+
set(USE_SANITIZER "address" CACHE STRING "Sanitizer to use: address, thread, or none")
17+
18+
# Configure Sanitizers
19+
if(NOT USE_SANITIZER STREQUAL "none")
20+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
21+
if(USE_SANITIZER STREQUAL "address")
22+
set(SAN_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
23+
elseif(USE_SANITIZER STREQUAL "thread")
24+
set(SAN_FLAGS "-fsanitize=thread")
25+
endif()
26+
27+
if(SAN_FLAGS)
28+
add_compile_options(${SAN_FLAGS})
29+
add_link_options(${SAN_FLAGS})
30+
message(STATUS "Enabled sanitizer: ${USE_SANITIZER}")
31+
endif()
32+
endif()
1733
endif()
1834

1935
# Enable compiler warnings
@@ -38,11 +54,6 @@ else()
3854
message(WARNING "clang-tidy not found")
3955
endif()
4056

41-
# Build options
42-
option(BUILD_TESTS "Build tests" ON)
43-
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
44-
option(BUILD_COVERAGE "Build with coverage instrumentation" OFF)
45-
4657
if(BUILD_COVERAGE)
4758
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
4859
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
@@ -132,5 +143,5 @@ message(STATUS " Version: ${PROJECT_VERSION}")
132143
message(STATUS " C++ Standard: ${CMAKE_CXX_STANDARD}")
133144
message(STATUS " Compiler: ${CMAKE_CXX_COMPILER_ID}")
134145
message(STATUS " Build Tests: ${BUILD_TESTS}")
135-
message(STATUS " Sanitizers: AddressSanitizer enabled")
146+
message(STATUS " Sanitizer: ${USE_SANITIZER}")
136147
message(STATUS "=============================================")

0 commit comments

Comments
 (0)