From ef77a9e4fb15ef0dec61ac801700057fd4e647db Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 20 Aug 2021 18:23:40 +0200 Subject: [PATCH] Make sanitizers optional. --- CMakeLists.txt | 1 + CMakePresets.json | 20 ++++++-------------- README.adoc | 1 + cmake/debug_flags.cmake | 15 ++++++++++----- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b70cf29..b993763 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") # Project build options. option(WITH_TESTS "Compile tests." NO) option(FALLBACK_BUNDLED "Fall back to bundled libs." YES) +option(WITH_SANITIZERS "Use sanitizers in debug builds." NO) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/CMakePresets.json b/CMakePresets.json index ba77f42..c40a6e5 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -22,25 +22,17 @@ "inherits": "common", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", - "WITH_TESTS": true + "WITH_TESTS": true, + "WITH_SANITIZERS": false } }, { - "name": "dev_gcc", - "displayName": "Developer config, GCC", - "description": "Build using GCC with debug symbols and tests enabled", + "name": "dev_san", + "displayName": "Developer config, with sanitizers", + "description": "Build with debug symbols, tests enabled and sanitizers enabled", "inherits": "dev", "cacheVariables": { - "CMAKE_CXX_COMPILER": "g++" - } - }, - { - "name": "dev_clang", - "displayName": "Developer config, clang", - "description": "Build using clang with debug symbols and tests enabled", - "inherits": "dev", - "cacheVariables": { - "CMAKE_CXX_COMPILER": "clang++" + "WITH_SANITIZERS": true } }, { diff --git a/README.adoc b/README.adoc index 1f00703..091e76d 100644 --- a/README.adoc +++ b/README.adoc @@ -144,6 +144,7 @@ can then install it with `apt install ./epubgrep-*.deb`. * `-DWITH_TESTS=YES` if you want to compile the tests. * `-DXGETTEXT_CMD=String` The program to use instead of `xgettext`. * `-DFALLBACK_BUNDLED=NO` if you don't want to fall back on bundled libraries. +* `-DWITH_SANITIZER=YES` to use sanitizers in debug builds. == Similar projects diff --git a/cmake/debug_flags.cmake b/cmake/debug_flags.cmake index 52abc89..9f1434d 100644 --- a/cmake/debug_flags.cmake +++ b/cmake/debug_flags.cmake @@ -24,10 +24,13 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" "-Wdouble-promotion" "-Wformat=2" "-ftrapv" - "-fsanitize=undefined" - "-fsanitize=address" "-Og" "-fno-omit-frame-pointer") + if(WITH_SANITIZERS) + list(APPEND tmp_CXXFLAGS + "-fsanitize=undefined" + "-fsanitize=address") + endif() if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") list(APPEND tmp_CXXFLAGS "-Wlogical-op" @@ -45,9 +48,11 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" endif() add_compile_options("$<$:${tmp_CXXFLAGS}>") - list(APPEND tmp_LDFLAGS - "-fsanitize=undefined" - "-fsanitize=address") + if(WITH_SANITIZERS) + list(APPEND tmp_LDFLAGS + "-fsanitize=undefined" + "-fsanitize=address") + endif() # add_link_options was introduced in version 3.13. if(${CMAKE_VERSION} VERSION_LESS 3.13) set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${tmp_LDFLAGS}")