From fd5f5feb40630c6518f74797045f905cf3a071f4 Mon Sep 17 00:00:00 2001
From: Nikolaj Bjorner <nbjorner@microsoft.com>
Date: Tue, 28 Jan 2025 15:01:31 -0800
Subject: [PATCH] add cmake option to turn on asan

---
 CMakeLists.txt  | 10 ++++++++++
 README-CMake.md |  1 +
 2 files changed, 11 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 34f3caecd..9aad103ab 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -300,6 +300,16 @@ list(APPEND Z3_DEPENDENT_LIBS Threads::Threads)
 ################################################################################
 include(${PROJECT_SOURCE_DIR}/cmake/compiler_warnings.cmake)
 
+
+################################################################################
+# Address sanitization
+################################################################################
+option(Z3_ADDRESS_SANITIZE "Set address sanitization." OFF)
+
+if (Z3_ADDRESS_SANITIZE)
+  z3_add_cxx_flag("-fsanitize=address" REQUIRED)
+endif()
+
 ################################################################################
 # Save Clang optimization records
 ################################################################################
diff --git a/README-CMake.md b/README-CMake.md
index 047737186..c004f5c7c 100644
--- a/README-CMake.md
+++ b/README-CMake.md
@@ -311,6 +311,7 @@ The following useful options can be passed to CMake whilst configuring.
 * ``Z3_SAVE_CLANG_OPTIMIZATION_RECORDS`` - BOOL. If set to ``TRUE`` saves Clang optimization records by setting the compiler flag ``-fsave-optimization-record``.
 * ``Z3_SINGLE_THREADED`` - BOOL. If set to ``TRUE`` compiles Z3 for single threaded mode.
 * ``Z3_POLLING_TIMER`` - BOOL. If set to ``TRUE`` compiles Z3 to use polling based timer instead of requiring a thread. This is useful for wasm builds and avoids spawning threads that interfere with how WASM is run.
+* ``Z3_ADDRESS_SANITIZE`` - BOOL. If set to ``TRUE`` compiles Z3 with address sanitization enabled. 
 
 
 On the command line these can be passed to ``cmake`` using the ``-D`` option. In ``ccmake`` and ``cmake-gui`` these can be set in the user interface.