Skip to content

Commit b37747d

Browse files
committed
cmake: Add option to build with address sanitizer
1 parent 57542f6 commit b37747d

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ option(GIT_REVISION
1717
option(UPDATE_CHECKER
1818
"Enable or disable both the update checker and auto-updater" ON)
1919
option(PRO_VERSION "Enable or disable Notes Pro features" ON)
20+
option(ENABLE_ASAN "Enable address sanitizer" OFF)
2021

2122
project(
2223
Notes
@@ -32,6 +33,13 @@ set(CMAKE_AUTOMOC ON)
3233
set(CMAKE_AUTOUIC ON)
3334
set(CMAKE_AUTORCC ON)
3435

36+
# address sanitizer can be enabled by running cmake with -DENABLE_ASAN=ON
37+
if(ENABLE_ASAN AND (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
38+
OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
39+
set(ADDRESS_SANITIZER_FLAGS "-fsanitize=address -fno-omit-frame-pointer")
40+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ADDRESS_SANITIZER_FLAGS}")
41+
endif()
42+
3543
if(POLICY CMP0083)
3644
cmake_policy(SET CMP0083 NEW)
3745
include(CheckPIESupported)
@@ -74,6 +82,10 @@ else()
7482
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
7583
endif()
7684

85+
if(ENABLE_ASAN)
86+
message(STATUS "Enabling AddressSanitizer")
87+
endif()
88+
7789
set(GIT_REV "")
7890
if(GIT_REVISION)
7991
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")

docs/build_options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ You should set those while invoking CMake to build the project. See the [example
1010
| `GIT_REVISION` | `OFF` | `ON` / `OFF` | Append the current git revision to the app's version string |
1111
| `UPDATE_CHECKER` | `ON` | `ON` / `OFF` | Enable or disable both the update checker and auto-updater |
1212
| `PRO_VERSION` | `ON` | `ON` / `OFF` | Enable or disable Notes Pro features |
13+
| `ENABLE_ASAN` | `OFF` | `ON` / `OFF` | Enable AddressSanitizer (ASan) for debugging |
1314

1415
### Examples
1516

0 commit comments

Comments
 (0)