1include(CompilerRTCompile)
2
3filter_available_targets(INTERCEPTION_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el)
4
5set(INTERCEPTION_UNITTESTS
6  interception_linux_test.cc
7  interception_test_main.cc
8  interception_win_test.cc
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  -Wno-non-virtual-dtor)
23
24# -gline-tables-only must be enough for these tests, so use it if possible.
25if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
26  list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gline-tables-only)
27else()
28  list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -g)
29endif()
30if(MSVC)
31  list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gcodeview)
32  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -Wl,-largeaddressaware)
33endif()
34list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -g)
35
36if(NOT MSVC)
37  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
38endif()
39
40if(ANDROID)
41  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -pie)
42endif()
43
44set(INTERCEPTION_TEST_LINK_LIBS)
45append_list_if(COMPILER_RT_HAS_LIBLOG log INTERCEPTION_TEST_LINK_LIBS)
46# NDK r10 requires -latomic almost always.
47append_list_if(ANDROID atomic INTERCEPTION_TEST_LINK_LIBS)
48
49append_list_if(COMPILER_RT_HAS_LIBDL -ldl INTERCEPTION_TEST_LINK_FLAGS_COMMON)
50append_list_if(COMPILER_RT_HAS_LIBRT -lrt INTERCEPTION_TEST_LINK_FLAGS_COMMON)
51append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread INTERCEPTION_TEST_LINK_FLAGS_COMMON)
52# x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also,
53# 'libm' shall be specified explicitly to build i386 tests.
54if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
55  list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON "-lc++ -lm")
56endif()
57
58include_directories(..)
59include_directories(../..)
60
61# Adds static library which contains interception object file
62# (universal binary on Mac and arch-specific object files on Linux).
63macro(add_interceptor_lib library)
64  add_library(${library} STATIC ${ARGN})
65  set_target_properties(${library} PROPERTIES
66    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
67    FOLDER "Compiler-RT Runtime tests")
68endmacro()
69
70function(get_interception_lib_for_arch arch lib lib_name)
71  if(APPLE)
72    set(tgt_name "RTInterception.test.osx")
73  else()
74    set(tgt_name "RTInterception.test.${arch}")
75  endif()
76  set(${lib} "${tgt_name}" PARENT_SCOPE)
77  if(CMAKE_CONFIGURATION_TYPES)
78   set(configuration_path "${CMAKE_CFG_INTDIR}/")
79  else()
80   set(configuration_path "")
81  endif()
82  if(NOT MSVC)
83    set(${lib_name} "${configuration_path}lib${tgt_name}.a" PARENT_SCOPE)
84  else()
85    set(${lib_name} "${configuration_path}${tgt_name}.lib" PARENT_SCOPE)
86  endif()
87endfunction()
88
89# Interception unit tests testsuite.
90add_custom_target(InterceptionUnitTests)
91set_target_properties(InterceptionUnitTests PROPERTIES
92  FOLDER "Compiler-RT Tests")
93
94# Adds interception tests for architecture.
95macro(add_interception_tests_for_arch arch)
96  get_target_flags_for_arch(${arch} TARGET_FLAGS)
97  set(INTERCEPTION_TEST_SOURCES ${INTERCEPTION_UNITTESTS}
98                             ${COMPILER_RT_GTEST_SOURCE})
99  set(INTERCEPTION_TEST_COMPILE_DEPS ${INTERCEPTION_TEST_HEADERS})
100  if(NOT COMPILER_RT_STANDALONE_BUILD)
101    list(APPEND INTERCEPTION_TEST_COMPILE_DEPS gtest)
102  endif()
103  set(INTERCEPTION_TEST_OBJECTS)
104  foreach(source ${INTERCEPTION_TEST_SOURCES})
105    get_filename_component(basename ${source} NAME)
106    if(CMAKE_CONFIGURATION_TYPES)
107      set(output_obj "${CMAKE_CFG_INTDIR}/${basename}.${arch}.o")
108    else()
109      set(output_obj "${basename}.${arch}.o")
110    endif()
111    clang_compile(${output_obj} ${source}
112                  CFLAGS ${INTERCEPTION_TEST_CFLAGS_COMMON} ${TARGET_FLAGS}
113                  DEPS ${INTERCEPTION_TEST_COMPILE_DEPS})
114    list(APPEND INTERCEPTION_TEST_OBJECTS ${output_obj})
115  endforeach()
116  get_interception_lib_for_arch(${arch} INTERCEPTION_COMMON_LIB
117                                INTERCEPTION_COMMON_LIB_NAME)
118  # Add unittest target.
119  set(INTERCEPTION_TEST_NAME "Interception-${arch}-Test")
120  add_compiler_rt_test(InterceptionUnitTests ${INTERCEPTION_TEST_NAME}
121                       OBJECTS ${INTERCEPTION_TEST_OBJECTS}
122                               ${INTERCEPTION_COMMON_LIB_NAME}
123                       DEPS ${INTERCEPTION_TEST_OBJECTS} ${INTERCEPTION_COMMON_LIB}
124                       LINK_FLAGS ${INTERCEPTION_TEST_LINK_FLAGS_COMMON}
125                                  ${TARGET_FLAGS})
126endmacro()
127
128if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID AND NOT APPLE)
129  # We use just-built clang to build interception unittests, so we must
130  # be sure that produced binaries would work.
131  if(APPLE)
132    add_interceptor_lib("RTInterception.test.osx"
133                        $<TARGET_OBJECTS:RTInterception.osx>)
134  else()
135    foreach(arch ${INTERCEPTION_UNITTEST_SUPPORTED_ARCH})
136      add_interceptor_lib("RTInterception.test.${arch}"
137                          $<TARGET_OBJECTS:RTInterception.${arch}>)
138    endforeach()
139  endif()
140  foreach(arch ${INTERCEPTION_UNITTEST_SUPPORTED_ARCH})
141    add_interception_tests_for_arch(${arch})
142  endforeach()
143endif()
144