@@ -41,6 +41,41 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
4141option (BUILD_SHARED_LIBS "Build shared libraries" ON )
4242option (BUILD_TESTING "Build tests" ON )
4343
44+ # ---------------------------------------------------------------------------
45+ # Sanitizer options
46+ # ---------------------------------------------------------------------------
47+ option (BUILD_WITH_ASAN "Enable AddressSanitizer (-fsanitize=address)" OFF )
48+ option (BUILD_WITH_VALGRIND "Enable Valgrind memcheck via CTest" OFF )
49+
50+ if (BUILD_WITH_ASAN AND BUILD_WITH_VALGRIND)
51+ message (FATAL_ERROR "BUILD_WITH_ASAN and BUILD_WITH_VALGRIND are mutually exclusive. "
52+ "ASAN instruments the binary at compile time; Valgrind instruments at runtime. "
53+ "They must not be combined." )
54+ endif ()
55+
56+ if (MSVC AND (BUILD_WITH_ASAN OR BUILD_WITH_VALGRIND))
57+ message (WARNING "Sanitizers are not yet supported on MSVC. "
58+ "BUILD_WITH_ASAN / BUILD_WITH_VALGRIND will be ignored." )
59+ elseif (NOT MSVC )
60+ if (BUILD_WITH_ASAN)
61+ message (STATUS "AddressSanitizer: ENABLED" )
62+ add_compile_options (-fsanitize=address -fno-omit-frame-pointer )
63+ add_link_options (-fsanitize=address )
64+ endif ()
65+
66+ if (BUILD_WITH_VALGRIND)
67+ find_program (VALGRIND_COMMAND valgrind )
68+ if (NOT VALGRIND_COMMAND)
69+ message (FATAL_ERROR "Valgrind not found but BUILD_WITH_VALGRIND is ON. "
70+ "Install it with: sudo apt-get install valgrind" )
71+ endif ()
72+ message (STATUS "Valgrind memcheck: ENABLED (${VALGRIND_COMMAND} )" )
73+ set (MEMORYCHECK_COMMAND ${VALGRIND_COMMAND} )
74+ set (MEMORYCHECK_COMMAND_OPTIONS
75+ "--leak-check=full --error-exitcode=1 --suppressions=${CMAKE_SOURCE_DIR} /valgrind.supp" )
76+ endif ()
77+ endif ()
78+
4479# ---------------------------------------------------------------------------
4580# CPU architecture detection (from PR #248)
4681# ---------------------------------------------------------------------------
@@ -95,12 +130,19 @@ else()
95130
96131 # Compiler optimization flags (from PR #248 / old makefile.linux)
97132 add_compile_options (
98- "$<$<CONFIG :Debug >:-O0 ;-g3 ;-D_DEBUG ;-DDEBUG ;- DLOGGING ;- fexceptions >"
133+ "$<$<CONFIG :Debug >:-O0 ;-g3 ;-D_DEBUG ;-fexceptions >"
99134 "$<$<CONFIG :Release >:-O3 ;-DNDEBUG ;-ftree -loop -vectorize >"
100135 "$<$<CONFIG :RelWithDebInfo >:-O2 ;-g ;-DNDEBUG >"
101136 "$<$<CONFIG :MinSizeRel >:-Os ;-DNDEBUG >"
102137 )
103138
139+ # Debug macros: DEBUG and LOGGING are for Debug builds only;
140+ # sanitizer builds strip them for cleaner output (less noise in reports).
141+ if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT BUILD_WITH_ASAN AND NOT BUILD_WITH_VALGRIND)
142+ add_definitions (-DDEBUG )
143+ add_definitions (-DLOGGING )
144+ endif ()
145+
104146 # SSE4.1 for x86/x86_64 (from PR #248)
105147 if (FBODBC_ARCH STREQUAL "x86" OR FBODBC_ARCH STREQUAL "i686"
106148 OR FBODBC_ARCH STREQUAL "x86_64" OR FBODBC_ARCH STREQUAL "AMD64" )
@@ -246,12 +288,6 @@ else()
246288 )
247289endif ()
248290
249- # Debug-specific definitions (matching .vcxproj: DEBUG;LOGGING for Debug configs)
250- target_compile_definitions (OdbcFb PRIVATE
251- $<$<CONFIG :Debug >:DEBUG >
252- $<$<CONFIG :Debug >:LOGGING >
253- )
254-
255291# ---------------------------------------------------------------------------
256292# Testing
257293# ---------------------------------------------------------------------------
0 commit comments