CMake: Add WITH_COMPILER_ASAN option
authorCampbell Barton <ideasman42@gmail.com>
Fri, 18 May 2018 13:21:18 +0000 (15:21 +0200)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 18 May 2018 13:22:16 +0000 (15:22 +0200)
This supports easy toggling of Address Sanitizer.

CMakeLists.txt

index 3e23b5f9986c2d0103d44056abd9da322bd17c0e..0388e251b6fff87f50c6ec9b08b5631b3bead0f5 100644 (file)
@@ -527,6 +527,41 @@ if(CMAKE_COMPILER_IS_GNUCC)
        mark_as_advanced(WITH_LINKER_GOLD)
 endif()
 
+if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
+       option(WITH_COMPILER_ASAN "Build and link against address sanitizer (only for Debug & RelWithDebInfo targets)." OFF)
+       mark_as_advanced(WITH_COMPILER_ASAN)
+
+       if(WITH_COMPILER_ASAN)
+               set(_asan_defaults "\
+-fsanitize=address \
+-fsanitize=bool \
+-fsanitize=bounds \
+-fsanitize=enum \
+-fsanitize=float-cast-overflow \
+-fsanitize=float-divide-by-zero \
+-fsanitize=leak \
+-fsanitize=nonnull-attribute \
+-fsanitize=object-size \
+-fsanitize=returns-nonnull-attribute \
+-fsanitize=signed-integer-overflow \
+-fsanitize=undefined \
+-fsanitize=vla-bound \
+-fno-sanitize=alignment \
+")
+
+               set(COMPILER_ASAN_CFLAGS "${_asan_defaults}" CACHE STRING "C flags for address sanitizer")
+               mark_as_advanced(COMPILER_ASAN_CFLAGS)
+               set(COMPILER_ASAN_CXXFLAGS "${_asan_defaults}" CACHE STRING "C++ flags for address sanitizer")
+               mark_as_advanced(COMPILER_ASAN_CXXFLAGS)
+
+               unset(_asan_defaults)
+
+               find_library(COMPILER_ASAN_LIBRARY asan ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES})
+               mark_as_advanced(COMPILER_ASAN_LIBRARY)
+
+       endif()
+endif()
+
 # Dependency graph
 option(WITH_LEGACY_DEPSGRAPH "Build Blender with legacy dependency graph" ON)
 mark_as_advanced(WITH_LEGACY_DEPSGRAPH)
@@ -822,6 +857,15 @@ set(PLATFORM_LINKLIBS "")
 set(PLATFORM_LINKFLAGS "")
 set(PLATFORM_LINKFLAGS_DEBUG "")
 
+if(WITH_COMPILER_ASAN)
+       set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${COMPILER_ASAN_CFLAGS}")
+       set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${COMPILER_ASAN_CFLAGS}")
+
+       set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${COMPILER_ASAN_CXXFLAGS}")
+       set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${COMPILER_ASAN_CXXFLAGS}")
+
+       set(PLATFORM_LINKFLAGS_DEBUG "${COMPILER_ASAN_LIBRARY}")
+endif()
 
 #-----------------------------------------------------------------------------
 #Platform specifics