Make sanitizers optional.

This commit is contained in:
tastytea 2021-08-20 18:23:40 +02:00
parent 552df1a49e
commit ef77a9e4fb
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 18 additions and 19 deletions

View File

@ -15,6 +15,7 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# Project build options. # Project build options.
option(WITH_TESTS "Compile tests." NO) option(WITH_TESTS "Compile tests." NO)
option(FALLBACK_BUNDLED "Fall back to bundled libs." YES) 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 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)

View File

@ -22,25 +22,17 @@
"inherits": "common", "inherits": "common",
"cacheVariables": { "cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug", "CMAKE_BUILD_TYPE": "Debug",
"WITH_TESTS": true "WITH_TESTS": true,
"WITH_SANITIZERS": false
} }
}, },
{ {
"name": "dev_gcc", "name": "dev_san",
"displayName": "Developer config, GCC", "displayName": "Developer config, with sanitizers",
"description": "Build using GCC with debug symbols and tests enabled", "description": "Build with debug symbols, tests enabled and sanitizers enabled",
"inherits": "dev", "inherits": "dev",
"cacheVariables": { "cacheVariables": {
"CMAKE_CXX_COMPILER": "g++" "WITH_SANITIZERS": true
}
},
{
"name": "dev_clang",
"displayName": "Developer config, clang",
"description": "Build using clang with debug symbols and tests enabled",
"inherits": "dev",
"cacheVariables": {
"CMAKE_CXX_COMPILER": "clang++"
} }
}, },
{ {

View File

@ -144,6 +144,7 @@ can then install it with `apt install ./epubgrep-*.deb`.
* `-DWITH_TESTS=YES` if you want to compile the tests. * `-DWITH_TESTS=YES` if you want to compile the tests.
* `-DXGETTEXT_CMD=String` The program to use instead of `xgettext`. * `-DXGETTEXT_CMD=String` The program to use instead of `xgettext`.
* `-DFALLBACK_BUNDLED=NO` if you don't want to fall back on bundled libraries. * `-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 == Similar projects

View File

@ -24,10 +24,13 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
"-Wdouble-promotion" "-Wdouble-promotion"
"-Wformat=2" "-Wformat=2"
"-ftrapv" "-ftrapv"
"-fsanitize=undefined"
"-fsanitize=address"
"-Og" "-Og"
"-fno-omit-frame-pointer") "-fno-omit-frame-pointer")
if(WITH_SANITIZERS)
list(APPEND tmp_CXXFLAGS
"-fsanitize=undefined"
"-fsanitize=address")
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
list(APPEND tmp_CXXFLAGS list(APPEND tmp_CXXFLAGS
"-Wlogical-op" "-Wlogical-op"
@ -45,9 +48,11 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"
endif() endif()
add_compile_options("$<$<CONFIG:Debug>:${tmp_CXXFLAGS}>") add_compile_options("$<$<CONFIG:Debug>:${tmp_CXXFLAGS}>")
list(APPEND tmp_LDFLAGS if(WITH_SANITIZERS)
"-fsanitize=undefined" list(APPEND tmp_LDFLAGS
"-fsanitize=address") "-fsanitize=undefined"
"-fsanitize=address")
endif()
# add_link_options was introduced in version 3.13. # add_link_options was introduced in version 3.13.
if(${CMAKE_VERSION} VERSION_LESS 3.13) if(${CMAKE_VERSION} VERSION_LESS 3.13)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${tmp_LDFLAGS}") set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${tmp_LDFLAGS}")