From 9eb925f72b2401ea50b69726cf079255145c82ed Mon Sep 17 00:00:00 2001 From: Albin Johansson Date: Thu, 7 Dec 2023 21:57:49 +0100 Subject: [PATCH] Add sanitizer stuff --- CMakeLists.txt | 65 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 736bb685c..a21952695 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,10 +13,14 @@ project(centurion option(CEN_USE_SYSTEM_SDL "Use the system SDL libraries" ON) option(CEN_BUILD_UNIT_TESTS "Build the unit test suite" OFF) option(CEN_BUILD_MOCK_TESTS "Build the mock test suite" OFF) +option(CEN_USE_ADDRESS_SANITIZER "Build with the address sanitizer enabled" OFF) +option(CEN_USE_UB_SANITIZER "Build with the undefined behavior sanitizer enabled" OFF) message(DEBUG "CEN_USE_SYSTEM_SDL: ${CEN_USE_SYSTEM_SDL}") message(DEBUG "CEN_BUILD_UNIT_TESTS: ${CEN_BUILD_UNIT_TESTS}") message(DEBUG "CEN_BUILD_MOCK_TESTS: ${CEN_BUILD_MOCK_TESTS}") +message(DEBUG "CEN_USE_ADDRESS_SANITIZER: ${CEN_USE_ADDRESS_SANITIZER}") +message(DEBUG "CEN_USE_UB_SANITIZER: ${CEN_USE_UB_SANITIZER}") set(CENTURION_ROOT_DIR "${PROJECT_SOURCE_DIR}") @@ -28,6 +32,39 @@ else () find_package(SDL3 REQUIRED PATHS "${CENTURION_ROOT_DIR}/external/SDL/build") endif () +if (MSVC) + list(APPEND CENTURION_COMPILER_FLAGS + /EHsc + /MP + /W4 + /permissive- + /Zc:preprocessor + /Zc:__cplusplus + ) +else () + list(APPEND CENTURION_COMPILER_FLAGS + -Wall + -Wextra + -Wpedantic + -Wconversion + -Wsign-conversion + -Wshadow + ) + + if (NOT MSVC) + if (CEN_USE_ADDRESS_SANITIZER MATCHES "ON") + list(APPEND CENTURION_COMPILER_FLAGS -fsanitize=address -fno-sanitize-recover -fno-omit-frame-pointer -g) + list(APPEND CENTURION_LINK_FLAGS -fsanitize=address -fno-sanitize-recover -fno-omit-frame-pointer -g) + elseif (CEN_USE_UB_SANITIZER MATCHES "ON") + list(APPEND CENTURION_COMPILER_FLAGS -fsanitize=undefined -fno-sanitize-recover -fno-omit-frame-pointer -g) + list(APPEND CENTURION_LINK_FLAGS -fsanitize=undefined -fno-sanitize-recover -fno-omit-frame-pointer -g) + endif () + endif () +endif () + +message(DEBUG "CENTURION_COMPILER_FLAGS: ${CENTURION_COMPILER_FLAGS}") +message(DEBUG "CENTURION_LINK_FLAGS: ${CENTURION_LINK_FLAGS}") + function(cen_apply_compiler_options target) set_target_properties(${target} PROPERTIES @@ -35,25 +72,15 @@ function(cen_apply_compiler_options target) CXX_STANDARD_EXTENSIONS OFF ) - if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - target_compile_options(${target} - PRIVATE - /EHsc - /MP - /W4 - /permissive- - /Zc:preprocessor - /Zc:__cplusplus - ) - else () - target_compile_options(${target} - PRIVATE - -Wall - -Wextra - -Wpedantic - -Wconversion - -Wsign-conversion - ) + target_compile_options(${target} PRIVATE "${CENTURION_COMPILER_FLAGS}") + target_link_options(${target} PRIVATE "${CENTURION_LINK_FLAGS}") + + if (NOT MSVC) + if (CEN_USE_ADDRESS_SANITIZER MATCHES "ON") + target_link_libraries(${target} PRIVATE -fsanitize=address) + elseif (CEN_USE_UB_SANITIZER MATCHES "ON") + target_link_libraries(${target} PRIVATE -fsanitize=undefined) + endif () endif () endfunction()