File: Sanitizer.cmake

package info (click to toggle)
dmlc-core 0.5%2Bgit20240614.1334185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,672 kB
  • sloc: cpp: 16,312; python: 1,421; ansic: 1,014; java: 802; makefile: 236; sh: 140; xml: 138
file content (71 lines) | stat: -rw-r--r-- 2,149 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Set appropriate compiler and linker flags for sanitizers.
#
# Usage of this module:
#  enable_sanitizers("address;leak")

# Add flags
macro(enable_sanitizer sanitizer)
  if(${sanitizer} MATCHES "address")
    find_package(ASan)
    set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=address")
    if (ASan_FOUND)
      link_libraries(${ASan_LIBRARY})
    endif()

  elseif(${sanitizer} MATCHES "thread")
    find_package(TSan)
    set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=thread")
    if (TSan_FOUND)
      link_libraries(${TSan_LIBRARY})
    endif()

  elseif(${sanitizer} MATCHES "leak")
    find_package(LSan)
    set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=leak")
    if (LSan_FOUND)
      link_libraries(${LSan_LIBRARY})
    endif()

  elseif(${sanitizer} MATCHES "undefined")
    find_package(UBSan)
    set(SAN_COMPILE_FLAGS "${SAN_COMPILE_FLAGS} -fsanitize=undefined -fno-sanitize-recover=undefined")
    if (UBSan_FOUND)
      link_libraries(${UBSan_LIBRARY})
    endif()

  else()
    message(FATAL_ERROR "Santizer ${sanitizer} not supported.")
  endif()
endmacro()

macro(enable_sanitizers SANITIZERS)
  # Check sanitizers compatibility.
  # Idealy, we should use if(san IN_LIST SANITIZERS) ... endif()
  # But I haven't figure out how to make it work.
  foreach ( _san ${SANITIZERS} )
    string(TOLOWER ${_san} _san)
    if (_san MATCHES "thread")
      if (${_use_other_sanitizers})
        message(FATAL_ERROR
          "thread sanitizer is not compatible with ${_san} sanitizer.")
      endif()
      set(_use_thread_sanitizer 1)
    else ()
      if (${_use_thread_sanitizer})
        message(FATAL_ERROR
          "${_san} sanitizer is not compatible with thread sanitizer.")
      endif()
      set(_use_other_sanitizers 1)
    endif()
  endforeach()

  message("Sanitizers: ${SANITIZERS}")

  foreach( _san ${SANITIZERS} )
    string(TOLOWER ${_san} _san)
    enable_sanitizer(${_san})
  endforeach()
  message("Sanitizers compile flags: ${SAN_COMPILE_FLAGS}")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAN_COMPILE_FLAGS}")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAN_COMPILE_FLAGS}")
endmacro()