Skip to content

Commit

Permalink
Add cmake options to sanitizer (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: jparisu <javierparis@eprosima.com>

Signed-off-by: jparisu <javierparis@eprosima.com>
  • Loading branch information
jparisu authored and cferreiragonz committed Apr 11, 2024
1 parent 675d2ff commit 043bb3a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,65 @@ foreach( version_var IN LISTS version_unset)
endforeach()
unset(version_unset)

###############################################################################
# Activate Sanitizers
###############################################################################
option(SANITIZER "Enable Thread or Address sanitizers" OFF)
string(TOUPPER ${SANITIZER} SANITIZER)

if (SANITIZER)
if(${SANITIZER} STREQUAL "ADDRESS")
message(STATUS "Enabling address sanitizer...")
# Warning/Error messages
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
message(WARNING "Address sanitizer results with an optimized (non-Debug) build may be misleading")
endif()

if("${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
message(STATUS "Building with llvm Address sanitizer Tools")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")

elseif(CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "Building with Address sanitizer Tools")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")

else()
message(FATAL_ERROR "Address sanitizer requires Clang or GCC. Aborting.")
endif()

elseif(${SANITIZER} STREQUAL "THREAD")
message(STATUS "Enabling thread sanitizer...")
# Warning/Error messages
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
message(WARNING "Thread sanitizer results with an optimized (non-Debug) build may be misleading")
endif()

if("${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
message(STATUS "Building with llvm Thread sanitizer Tools")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")

elseif(CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "Building with Thread sanitizer Tools")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer")

else()
message(FATAL_ERROR "Thread sanitizer requires Clang or GCC. Aborting.")
endif()

else()
message(WARNING "Sanitizer option not supported. Continuing without any code instrumentation... ")

endif()
endif()

###############################################################################
# Compilation
###############################################################################
Expand Down

0 comments on commit 043bb3a

Please sign in to comment.