Skip to content

Commit

Permalink
cmake support SANITIZER
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Jan 31, 2024
1 parent ca691e3 commit fcca6d6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v3

- name: make ut
run: cmake -DCOV=ON -DCMAKE_CXX_COMPILER="g++" . && make -j
run: cmake -DCOV=1 -DCMAKE_CXX_COMPILER="g++" . && make -j

- name: ut
run: SLICE=always-malloc G_DEBUG=gc-friendly valgrind -v --tool=memcheck --leak-check=full --num-callers=40 --log-file=ut/gcovr/cov.valgrind.log --error-exitcode=6 ./ut_exe --gtest_brief=1
Expand Down
31 changes: 30 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.12)
# ------------------------------------------------------------------------------------------------
project(ut_exe CXX) # as early as possible, eg must before CMAKE_CXX_FLAGS

if(COV)
if(COV STREQUAL "1")
message("!!! coverage enabled")
set(CMAKE_CXX_STANDARD 20) # if gtest fix bug, try c++20/23 to see cov rate inc or not

Expand All @@ -17,11 +17,40 @@ if(COV)

add_compile_definitions(SMART_LOG)
add_compile_definitions(WITH_HID_LOG) # more info to debug ci

elseif(COV STREQUAL "asan")
set(CMAKE_CXX_STANDARD 14) # low c++ version for wide usage
message("!!! SANITIZE for: use-after-free, double-free, out-of-bound")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address") # compile
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address") # link
set(lib_san "-lasan")

elseif(COV STREQUAL "lsan")
set(CMAKE_CXX_STANDARD 14)
message("!!! SANITIZE for: leak")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=leak")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=leak")
set(lib_san "-llsan")

elseif(COV STREQUAL "tsan")
set(CMAKE_CXX_STANDARD 14)
message("!!! SANITIZE for: thread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=thread")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")

elseif(COV STREQUAL "ubsan")
set(CMAKE_CXX_STANDARD 14)
message("!!! SANITIZE for: undefined behavior")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=undefined")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")

else()
message("!!! UT only - no cov, no SANITIZE check etc")
set(CMAKE_CXX_STANDARD 14) # low c++ version for wide usage
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1") # fastest build+ut
#add_compile_definitions(WITH_HID_LOG) # cov no HID; easy to turn-on
endif()

set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") # inc branch coverage
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ target_include_directories(lib_domino PUBLIC
)

target_link_libraries(lib_domino PUBLIC
${lib_san}
pthread
)
1 change: 1 addition & 0 deletions ut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target_include_directories(lib_ut PUBLIC
)

target_link_libraries(lib_ut PUBLIC
${lib_san}
gtest
gmock
)

0 comments on commit fcca6d6

Please sign in to comment.