cmake: tsan
authorMichael Wallner <mike@php.net>
Fri, 20 Nov 2020 10:16:20 +0000 (11:16 +0100)
committerMichael Wallner <mike@php.net>
Fri, 20 Nov 2020 10:16:20 +0000 (11:16 +0100)
CMake/CheckDebug.cmake

index e3e046bf8c7be78accb56c79c458c294f4d84bbf..e4c3dcba5f4aacf85226f4b155ca17e5dc419ab2 100644 (file)
@@ -12,6 +12,28 @@ function(check_flag FLAG DEFAULT)
     endif()
 endfunction()
 
+macro(check_sanitizer VAR NAME LIB)
+    message(STATUS "Checking for sanitizer: ${NAME} (-l${LIB})")
+    if(${NAME} IN_LIST ${VAR} OR ${LIB} IN_LIST ${VAR})
+        safe_string(${LIB} ID)
+        cmake_push_check_state(RESET)
+        set(CMAKE_REQUIRED_LIBRARIES ${LIB})
+        check_cxx_compiler_flag(-fsanitize=${NAME} HAVE_${ID})
+        cmake_pop_check_state()
+        if(HAVE_${ID})
+            add_compile_definitions(HAVE_${ID})
+            add_compile_options(-fsanitize=${NAME})
+            link_libraries(-fsanitize=${NAME})
+            check_flag(-fsanitize-recover=${NAME} IGNORE)
+            message(STATUS "  OK")
+        else()
+            message(STATUS "  NO: not supported")
+        endif()
+    else()
+        message(STATUS "  NO: not requested")
+    endif()
+endmacro()
+
 function(check_debug)
     if(CMAKE_BUILD_TYPE STREQUAL "Debug")
         add_definitions(-DDEBUG=1)
@@ -45,31 +67,10 @@ function(check_debug)
             endforeach()
 
             if(ENABLE_SANITIZERS)
-                if(address IN_LIST ENABLE_SANITIZERS OR asan IN_LIST ENABLE_SANITIZERS)
-                    cmake_push_check_state(RESET)
-                    set(CMAKE_REQUIRED_LIBRARIES asan)
-                    check_cxx_compiler_flag(-fsanitize=address HAVE_ASAN)
-                    cmake_pop_check_state()
-                    if(HAVE_ASAN)
-                        add_compile_definitions(HAVE_ASAN)
-                        add_compile_options(-fsanitize=address)
-                        link_libraries(-fsanitize=address)
-                        check_flag(-fsanitize-recover=address IGNORE)
-                    endif()
-                endif()
-
-                if(undefined IN_LIST ENABLE_SANITIZERS OR ubsan IN_LIST ENABLE_SANITIZERS)
-                    cmake_push_check_state(RESET)
-                    set(CMAKE_REQUIRED_LIBRARIES ubsan)
-                    check_cxx_compiler_flag(-fsanitize=undefined HAVE_UBSAN)
-                    cmake_pop_check_state()
-                    if(HAVE_UBSAN)
-                        add_compile_definitions(HAVE_UBSAN)
-                        add_compile_options(-fsanitize=undefined)
-                        link_libraries(-fsanitize=undefined)
-                        check_flag(-fsanitize-recover=undefined IGNORE)
-                    endif()
-                endif()
+                check_sanitizer(ENABLE_SANITIZERS address asan)
+                check_sanitizer(ENABLE_SANITIZERS undefined ubsan)
+                check_sanitizer(ENABLE_SANITIZERS thread tsan)
+                check_sanitizer(ENABLE_SANITIZERS leak lsan)
             endif()
         endif()
     else()