Skip to content

Commit 59f27a0

Browse files
committed
Clean and correct CMake's ASAN code a bit
1 parent ed2cf9b commit 59f27a0

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

CMakeLists.txt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ project(Nabla LANGUAGES CXX C)
1717

1818
option(NBL_STATIC_BUILD "" ON) # ON for static builds, OFF for shared
1919
option(NBL_DYNAMIC_MSVC_RUNTIME "" OFF)
20+
option(NBL_SANITIZE_ADDRESS OFF)
2021

2122
if(NBL_STATIC_BUILD)
2223
message(STATUS "Static Nabla build enabled!")
@@ -87,9 +88,12 @@ set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;")
8788
set(LLVM_USE_CRT_DEBUG MTd CACHE STRING "Nabla developer note: No idea what subfolder provides this")
8889
set(LLVM_USE_CRT_RELEASE MT CACHE STRING "Nabla developer note: No idea what subfolder provides this")
8990

90-
unset(CMAKE_CXX_FLAGS_DEBUG CACHE)
91-
unset(CMAKE_CXX_FLAGS_DEBUG)
92-
#set(CMAKE_CXX_FLAGS_DEBUG /Zi /Ob0 /Od /MTd)
91+
if(NBL_SANITIZE_ADDRESS) # get rid of RTC1 flag provided by CMake for Debug and RelWithDebInfo configurations for ASAN purposes
92+
unset(CMAKE_CXX_FLAGS_DEBUG CACHE)
93+
unset(CMAKE_CXX_FLAGS_DEBUG)
94+
unset(CMAKE_CXX_FLAGS_RELWITHDEBINFO CACHE)
95+
unset(CMAKE_CXX_FLAGS_RELWITHDEBINFO)
96+
endif()
9397

9498
if(NOT CMAKE_BUILD_TYPE)
9599
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type: Debug or Release" FORCE)
@@ -102,16 +106,6 @@ else()
102106
endif()
103107

104108
macro(nbl_adjust_flags)
105-
option(NBL_SANITIZE_ADDRESS OFF)
106-
107-
# test
108-
add_compile_options("/fsanitize=adress")
109-
add_compile_options("-fsanitize=adress")
110-
#add_link_options("/fsanitize=address")
111-
112-
# TODO: AnastaZIuk please clean it later or somebody
113-
#message(FATAL_ERROR "${CMAKE_CXX_FLAGS_DEBUG}")
114-
115109
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
116110
option(NBL_SANITIZE_THREAD OFF)
117111
endif()
@@ -195,11 +189,9 @@ macro(nbl_adjust_flags)
195189
endif()
196190

197191
elseif(MSVC) # /arch:sse3 or anything like this is not needed on x64 on MSVC for enabling sse3 instructions
198-
if (NBL_SANITIZE_ADDRESS)
199-
set(NBL_MSVC_FLAG_SANITIZE_ADDRESS "/fsanitize=address")
200-
#message(WARNING "Due to https://github.com/google/sanitizers/issues/328 expect not to be able to use the Debug target!")
201-
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /wholearchive:clang_rt.asan_dbg-x86_64.lib /wholearchive:clang_rt.asan_cxx_dbg-x86_64.lib -fno-omit-frame-pointer -fsanitize=address")
202-
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /wholearchive:clang_rt.asan-x86_64.lib /wholearchive:clang_rt.asan_cxx-x86_64.lib -fno-omit-frame-pointer -fsanitize=address")
192+
if(NBL_SANITIZE_ADDRESS)
193+
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /wholearchive:clang_rt.asan_dbg-x86_64.lib /wholearchive:clang_rt.asan_cxx_dbg-x86_64.lib -fno-omit-frame-pointer -fsanitize=address /fsanitize=address")
194+
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /wholearchive:clang_rt.asan-x86_64.lib /wholearchive:clang_rt.asan_cxx-x86_64.lib -fno-omit-frame-pointer -fsanitize=address /fsanitize=address")
203195
endif()
204196

205197
# debug

cmake/common.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ macro(nbl_create_executable_project _EXTRA_SOURCES _EXTRA_OPTIONS _EXTRA_INCLUDE
7676

7777
add_compile_options(${_EXTRA_OPTIONS})
7878

79-
target_compile_options(${EXECUTABLE_NAME} PUBLIC "-fsanitize=address")
79+
if(NBL_SANITIZE_ADDRESS)
80+
target_compile_options(${EXECUTABLE_NAME} PUBLIC "-fsanitize=address /fsanitize=address")
81+
endif()
8082

8183
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
8284
# add_compile_options("-msse4.2 -mfpmath=sse") ????

0 commit comments

Comments
 (0)