Echo Writes Code

CrucibleEnableRuntimeSanitizers.cmake

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
72
include_guard(GLOBAL)

option(CRUCIBLE_ENABLE_ASAN "Enable AddressSanitizer if supported by the toolchain" OFF)
option(CRUCIBLE_ENABLE_TSAN "Enable ThreadSanitizer if supported by the toolchain" OFF)
option(CRUCIBLE_ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer if supported by the toolchain" OFF)

function(crucible_enable_runtime_sanitizers TARGET_NAME)
  get_target_property(TARGET_TYPE "${TARGET_NAME}" TYPE)

  if(TARGET_TYPE STREQUAL INTERFACE_LIBRARY)
    return()
  endif()

  if(TARGET_TYPE STREQUAL STATIC_LIBRARY OR TARGET_TYPE STREQUAL OBJECT_LIBRARY)
    set(COMPILE_OPTIONS_SCOPE PUBLIC)
    set(LINK_OPTIONS_SCOPE INTERFACE)
  else()
    set(COMPILE_OPTIONS_SCOPE PRIVATE)
    set(LINK_OPTIONS_SCOPE PRIVATE)
  endif()

  if(MSVC)
    if(CRUCIBLE_ENABLE_TSAN)
      message(FATAL_ERROR "ThreadSanitizer is not supported by the MSVC toolchain")
    endif()

    if(CRUCIBLE_ENABLE_UBSAN)
      message(FATAL_ERROR "UndefinedBehaviorSanitizer is not supported by the MSVC toolchain")
    endif()
  endif()

  if(CRUCIBLE_ENABLE_ASAN AND CRUCIBLE_ENABLE_TSAN)
    message(FATAL_ERROR "Cannot enable both AddressSanitizer and ThreadSanitizer")
  endif()

  if(CRUCIBLE_ENABLE_ASAN)
    target_compile_options("${TARGET_NAME}" "${COMPILE_OPTIONS_SCOPE}"
      $<$<CXX_COMPILER_ID:AppleClang>:-fsanitize=address -fno-omit-frame-pointer>
      $<$<CXX_COMPILER_ID:Clang>:-fsanitize=address -fno-omit-frame-pointer>
      $<$<CXX_COMPILER_ID:GNU>:-fsanitize=address -fno-omit-frame-pointer>
      $<$<CXX_COMPILER_ID:MSVC>:/fsanitize=address>)

    target_link_options("${TARGET_NAME}" "${LINK_OPTIONS_SCOPE}"
      $<$<CXX_COMPILER_ID:AppleClang>:-fsanitize=address>
      $<$<CXX_COMPILER_ID:Clang>:-fsanitize=address>
      $<$<CXX_COMPILER_ID:GNU>:-fsanitize=address>)
  endif()

  if(CRUCIBLE_ENABLE_TSAN)
    target_compile_options("${TARGET_NAME}" "${COMPILE_OPTIONS_SCOPE}"
      $<$<CXX_COMPILER_ID:AppleClang>:-fsanitize=thread>
      $<$<CXX_COMPILER_ID:Clang>:-fsanitize=thread>
      $<$<CXX_COMPILER_ID:GNU>:-fsanitize=thread>)

    target_link_options("${TARGET_NAME}" "${LINK_OPTIONS_SCOPE}"
      $<$<CXX_COMPILER_ID:AppleClang>:-fsanitize=thread>
      $<$<CXX_COMPILER_ID:Clang>:-fsanitize=thread>
      $<$<CXX_COMPILER_ID:GNU>:-fsanitize=thread>)
  endif()

  if(CRUCIBLE_ENABLE_UBSAN)
    target_compile_options("${TARGET_NAME}" "${COMPILE_OPTIONS_SCOPE}"
      $<$<CXX_COMPILER_ID:AppleClang>:-fsanitize=undefined>
      $<$<CXX_COMPILER_ID:Clang>:-fsanitize=undefined>
      $<$<CXX_COMPILER_ID:GNU>:-fsanitize=undefined>)

    target_link_options("${TARGET_NAME}" "${LINK_OPTIONS_SCOPE}"
      $<$<CXX_COMPILER_ID:AppleClang>:-fsanitize=undefined>
      $<$<CXX_COMPILER_ID:Clang>:-fsanitize=undefined>
      $<$<CXX_COMPILER_ID:GNU>:-fsanitize=undefined>)
  endif()
endfunction()