summaryrefslogtreecommitdiff
path: root/lib/tsan/tests/CMakeLists.txt
blob: 4b9bc6ae9854f14f070b9e852cd495381553d154 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
include_directories(../rtl)

add_custom_target(TsanUnitTests)
set_target_properties(TsanUnitTests PROPERTIES
  FOLDER "TSan unittests")

set(TSAN_UNITTEST_CFLAGS
  ${TSAN_CFLAGS}
  ${COMPILER_RT_UNITTEST_CFLAGS}
  ${COMPILER_RT_GTEST_CFLAGS}
  -I${COMPILER_RT_SOURCE_DIR}/include
  -I${COMPILER_RT_SOURCE_DIR}/lib
  -I${COMPILER_RT_SOURCE_DIR}/lib/tsan/rtl
  -DGTEST_HAS_RTTI=0)

set(TSAN_TEST_ARCH ${TSAN_SUPPORTED_ARCH})
if(APPLE)
  darwin_filter_host_archs(TSAN_SUPPORTED_ARCH TSAN_TEST_ARCH)
  list(APPEND TSAN_UNITTEST_CFLAGS ${DARWIN_osx_CFLAGS})

  # Create a static library for test dependencies.
  set(TSAN_TEST_RUNTIME_OBJECTS
    $<TARGET_OBJECTS:RTTsan_dynamic.osx>
    $<TARGET_OBJECTS:RTInterception.osx>
    $<TARGET_OBJECTS:RTSanitizerCommon.osx>
    $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>
    $<TARGET_OBJECTS:RTUbsan.osx>)
  set(TSAN_TEST_RUNTIME RTTsanTest)
  add_library(${TSAN_TEST_RUNTIME} STATIC ${TSAN_TEST_RUNTIME_OBJECTS})
  set_target_properties(${TSAN_TEST_RUNTIME} PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif()

set(TSAN_RTL_HEADERS)
foreach (header ${TSAN_HEADERS})
  list(APPEND TSAN_RTL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header})
endforeach()

# add_tsan_unittest(<name>
#                   SOURCES <sources list>
#                   HEADERS <extra headers list>)
macro(add_tsan_unittest testname)
  cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
  if(UNIX)
    foreach(arch ${TSAN_TEST_ARCH})
      set(TEST_OBJECTS)
      foreach(SOURCE ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE})
        sanitizer_test_compile(
          TEST_OBJECTS ${SOURCE} ${arch}
          COMPILE_DEPS ${TSAN_RTL_HEADERS} ${TEST_HEADERS}
          DEPS gtest tsan
          CFLAGS ${TSAN_UNITTEST_CFLAGS}
          )
      endforeach()
      get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
      set(TEST_DEPS ${TEST_OBJECTS})
      if(NOT APPLE)
        # FIXME: Looks like we should link TSan with just-built runtime,
        # and not rely on -fsanitize=thread, as these tests are essentially
        # unit tests.
        add_compiler_rt_test(TsanUnitTests ${testname}
                OBJECTS ${TEST_OBJECTS}
                DEPS ${TEST_DEPS}
                LINK_FLAGS ${TARGET_LINK_FLAGS}
                           -fsanitize=thread
                           -lstdc++ -lm)
      else()
        list(APPEND TEST_OBJECTS $<TARGET_FILE:${TSAN_TEST_RUNTIME}>)
        list(APPEND TEST_DEPS ${TSAN_TEST_RUNTIME})

        add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
        add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)

        # Intentionally do *not* link with `-fsanitize=thread`. We already link
        # against a static version of the runtime, and we don't want the dynamic
        # one.
        add_compiler_rt_test(TsanUnitTests "${testname}-${arch}-Test"
                OBJECTS ${TEST_OBJECTS}
                DEPS ${TEST_DEPS}
                LINK_FLAGS ${TARGET_LINK_FLAGS} ${DARWIN_osx_LINK_FLAGS}
                           ${WEAK_SYMBOL_LINK_FLAGS} -lc++)
      endif()
    endforeach()
  endif()
endmacro()

if(COMPILER_RT_CAN_EXECUTE_TESTS)
  add_subdirectory(rtl)
  add_subdirectory(unit)
endif()