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