• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include_directories(../rtl)
2
3add_custom_target(TsanUnitTests)
4set_target_properties(TsanUnitTests PROPERTIES
5  FOLDER "TSan unittests")
6
7set(TSAN_UNITTEST_CFLAGS
8  ${TSAN_CFLAGS}
9  ${COMPILER_RT_UNITTEST_CFLAGS}
10  ${COMPILER_RT_GTEST_CFLAGS}
11  -I${COMPILER_RT_SOURCE_DIR}/lib
12  -I${COMPILER_RT_SOURCE_DIR}/lib/tsan/rtl
13  -DGTEST_HAS_RTTI=0)
14
15set(TSAN_RTL_HEADERS)
16foreach (header ${TSAN_HEADERS})
17  list(APPEND TSAN_RTL_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header})
18endforeach()
19
20# tsan_compile(obj_list, source, arch, {headers})
21macro(tsan_compile obj_list source arch)
22  get_filename_component(basename ${source} NAME)
23  set(output_obj "${basename}.${arch}.o")
24  get_target_flags_for_arch(${arch} TARGET_CFLAGS)
25  set(COMPILE_DEPS ${TSAN_RTL_HEADERS} ${ARGN})
26  if(NOT COMPILER_RT_STANDALONE_BUILD)
27    list(APPEND COMPILE_DEPS gtest tsan)
28  endif()
29  clang_compile(${output_obj} ${source}
30          CFLAGS ${TSAN_UNITTEST_CFLAGS} ${TARGET_CFLAGS}
31          DEPS ${COMPILE_DEPS})
32  list(APPEND ${obj_list} ${output_obj})
33endmacro()
34
35macro(add_tsan_unittest testname)
36  set(TSAN_TEST_ARCH ${TSAN_SUPPORTED_ARCH})
37  if(APPLE)
38    darwin_filter_host_archs(TSAN_SUPPORTED_ARCH TSAN_TEST_ARCH)
39  endif()
40  if(UNIX)
41    foreach(arch ${TSAN_TEST_ARCH})
42      cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
43      set(TEST_OBJECTS)
44      foreach(SOURCE ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE})
45        tsan_compile(TEST_OBJECTS ${SOURCE} ${arch} ${TEST_HEADERS})
46      endforeach()
47      get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
48      set(TEST_DEPS ${TEST_OBJECTS})
49      if(NOT COMPILER_RT_STANDALONE_BUILD)
50        list(APPEND TEST_DEPS tsan)
51      endif()
52      if(NOT APPLE)
53        # FIXME: Looks like we should link TSan with just-built runtime,
54        # and not rely on -fsanitize=thread, as these tests are essentially
55        # unit tests.
56        add_compiler_rt_test(TsanUnitTests ${testname}
57                OBJECTS ${TEST_OBJECTS}
58                DEPS ${TEST_DEPS}
59                LINK_FLAGS ${TARGET_LINK_FLAGS}
60                           -fsanitize=thread
61                           -lstdc++ -lm)
62      else()
63        set(TSAN_TEST_RUNTIME_OBJECTS
64          $<TARGET_OBJECTS:RTTsan_dynamic.osx>
65          $<TARGET_OBJECTS:RTInterception.osx>
66          $<TARGET_OBJECTS:RTSanitizerCommon.osx>
67          $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>
68          $<TARGET_OBJECTS:RTUbsan.osx>)
69        set(TSAN_TEST_RUNTIME RTTsanTest.${testname}.${arch})
70        add_library(${TSAN_TEST_RUNTIME} STATIC ${TSAN_TEST_RUNTIME_OBJECTS})
71        set_target_properties(${TSAN_TEST_RUNTIME} PROPERTIES
72          ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
73        list(APPEND TEST_OBJECTS lib${TSAN_TEST_RUNTIME}.a)
74        list(APPEND TEST_DEPS ${TSAN_TEST_RUNTIME})
75        # Intentionally do *not* link with `-fsanitize=thread`. We already link
76        # against a static version of the runtime, and we don't want the dynamic
77        # one.
78        add_compiler_rt_test(TsanUnitTests "${testname}-${arch}-Test"
79                OBJECTS ${TEST_OBJECTS}
80                DEPS ${TEST_DEPS}
81                LINK_FLAGS ${TARGET_LINK_FLAGS}
82                           -lc++)
83      endif()
84    endforeach()
85  endif()
86endmacro()
87
88if(COMPILER_RT_CAN_EXECUTE_TESTS)
89  add_subdirectory(rtl)
90  add_subdirectory(unit)
91endif()
92