1include(CompilerRTCompile)
2
3filter_available_targets(INTERCEPTION_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el)
4
5set(INTERCEPTION_UNITTESTS
6  interception_linux_test.cpp
7  interception_test_main.cpp
8  interception_win_test.cpp
9  )
10
11set(INTERCEPTION_TEST_HEADERS)
12
13set(INTERCEPTION_TEST_CFLAGS_COMMON
14  ${COMPILER_RT_UNITTEST_CFLAGS}
15  ${COMPILER_RT_GTEST_CFLAGS}
16  ${SANITIZER_TEST_CXX_CFLAGS}
17  -I${COMPILER_RT_SOURCE_DIR}/include
18  -I${COMPILER_RT_SOURCE_DIR}/lib
19  -I${COMPILER_RT_SOURCE_DIR}/lib/interception
20  -fno-rtti
21  -O2
22  -Werror=sign-compare)
23
24set(INTERCEPTION_TEST_LINK_FLAGS_COMMON
25  ${COMPILER_RT_UNITTEST_LINK_FLAGS}
26  ${COMPILER_RT_UNWINDER_LINK_LIBS}
27  ${SANITIZER_TEST_CXX_LIBRARIES})
28
29# -gline-tables-only must be enough for these tests, so use it if possible.
30if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
31  list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gline-tables-only)
32else()
33  list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -g)
34endif()
35if(MSVC)
36  list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gcodeview)
37  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON
38    -Wl,-largeaddressaware
39    -Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames
40    )
41endif()
42list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -g)
43
44if(NOT MSVC)
45  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
46endif()
47
48if(ANDROID)
49  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -pie)
50endif()
51
52set(INTERCEPTION_TEST_LINK_LIBS)
53append_list_if(COMPILER_RT_HAS_LIBLOG log INTERCEPTION_TEST_LINK_LIBS)
54# NDK r10 requires -latomic almost always.
55append_list_if(ANDROID atomic INTERCEPTION_TEST_LINK_LIBS)
56
57append_list_if(COMPILER_RT_HAS_LIBDL -ldl INTERCEPTION_TEST_LINK_FLAGS_COMMON)
58append_list_if(COMPILER_RT_HAS_LIBRT -lrt INTERCEPTION_TEST_LINK_FLAGS_COMMON)
59append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread INTERCEPTION_TEST_LINK_FLAGS_COMMON)
60# x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also,
61# 'libm' shall be specified explicitly to build i386 tests.
62if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
63  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON "-lc++ -lm")
64endif()
65
66include_directories(..)
67include_directories(../..)
68
69# Adds static library which contains interception object file
70# (universal binary on Mac and arch-specific object files on Linux).
71macro(add_interceptor_lib library)
72  add_library(${library} STATIC ${ARGN})
73  set_target_properties(${library} PROPERTIES
74    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
75    FOLDER "Compiler-RT Runtime tests")
76endmacro()
77
78function(get_interception_lib_for_arch arch lib)
79  if(APPLE)
80    set(tgt_name "RTInterception.test.osx")
81  else()
82    set(tgt_name "RTInterception.test.${arch}")
83  endif()
84  set(${lib} "${tgt_name}" PARENT_SCOPE)
85endfunction()
86
87# Interception unit tests testsuite.
88add_custom_target(InterceptionUnitTests)
89set_target_properties(InterceptionUnitTests PROPERTIES
90  FOLDER "Compiler-RT Tests")
91
92# Adds interception tests for architecture.
93macro(add_interception_tests_for_arch arch)
94  set(INTERCEPTION_TEST_OBJECTS)
95  get_interception_lib_for_arch(${arch} INTERCEPTION_COMMON_LIB)
96  generate_compiler_rt_tests(INTERCEPTION_TEST_OBJECTS
97    InterceptionUnitTests "Interception-${arch}-Test" ${arch}
98    RUNTIME ${INTERCEPTION_COMMON_LIB}
99    SOURCES ${INTERCEPTION_UNITTESTS} ${COMPILER_RT_GTEST_SOURCE}
100    COMPILE_DEPS ${INTERCEPTION_TEST_HEADERS}
101    DEPS llvm_gtest
102    CFLAGS ${INTERCEPTION_TEST_CFLAGS_COMMON}
103    LINK_FLAGS ${INTERCEPTION_TEST_LINK_FLAGS_COMMON})
104endmacro()
105
106if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID AND NOT APPLE)
107  # We use just-built clang to build interception unittests, so we must
108  # be sure that produced binaries would work.
109  if(APPLE)
110    add_interceptor_lib("RTInterception.test.osx"
111                        $<TARGET_OBJECTS:RTInterception.osx>)
112  else()
113    foreach(arch ${INTERCEPTION_UNITTEST_SUPPORTED_ARCH})
114      add_interceptor_lib("RTInterception.test.${arch}"
115                          $<TARGET_OBJECTS:RTInterception.${arch}>)
116    endforeach()
117  endif()
118  foreach(arch ${INTERCEPTION_UNITTEST_SUPPORTED_ARCH})
119    add_interception_tests_for_arch(${arch})
120  endforeach()
121endif()
122