1 include(CompilerRTCompile)
3 clang_compiler_add_cxx_check()
5 # FIXME: use SANITIZER_COMMON_SUPPORTED_ARCH here
6 filter_available_targets(SANITIZER_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el riscv64 sparcv9 sparc)
8 list(APPEND SANITIZER_UNITTEST_SUPPORTED_ARCH arm64)
9 darwin_filter_host_archs(SANITIZER_UNITTEST_SUPPORTED_ARCH SANITIZER_UNITTEST_SUPPORTED_ARCH)
12 set(SANITIZER_UNITTESTS
13 sanitizer_addrhashmap_test.cpp
14 sanitizer_allocator_test.cpp
15 sanitizer_array_ref_test.cpp
16 sanitizer_atomic_test.cpp
17 sanitizer_bitvector_test.cpp
18 sanitizer_block_signals.cpp
19 sanitizer_bvgraph_test.cpp
20 sanitizer_chained_origin_depot_test.cpp
21 sanitizer_common_test.cpp
22 sanitizer_deadlock_detector_test.cpp
23 sanitizer_dense_map_test.cpp
24 sanitizer_flags_test.cpp
25 sanitizer_flat_map_test.cpp
26 sanitizer_format_interceptor_test.cpp
27 sanitizer_hash_test.cpp
28 sanitizer_ioctl_test.cpp
29 sanitizer_leb128_test.cpp
30 sanitizer_libc_test.cpp
31 sanitizer_linux_test.cpp
32 sanitizer_list_test.cpp
33 sanitizer_lzw_test.cpp
34 sanitizer_mac_test.cpp
35 sanitizer_module_uuid_size.cpp
36 sanitizer_mutex_test.cpp
37 sanitizer_nolibc_test.cpp
38 sanitizer_posix_test.cpp
39 sanitizer_printf_test.cpp
40 sanitizer_procmaps_test.cpp
41 sanitizer_procmaps_mac_test.cpp
42 sanitizer_range_test.cpp
43 sanitizer_ring_buffer_test.cpp
44 sanitizer_quarantine_test.cpp
45 sanitizer_stack_store_test.cpp
46 sanitizer_stackdepot_test.cpp
47 sanitizer_stacktrace_printer_test.cpp
48 sanitizer_stacktrace_test.cpp
49 sanitizer_stoptheworld_test.cpp
50 sanitizer_suppressions_test.cpp
51 sanitizer_symbolizer_test.cpp
52 sanitizer_test_main.cpp
53 sanitizer_thread_arg_retval_test.cpp
54 sanitizer_thread_registry_test.cpp
55 sanitizer_type_traits_test.cpp
56 sanitizer_vector_test.cpp
59 set(SANITIZER_TEST_HEADERS
60 sanitizer_pthread_wrappers.h
61 sanitizer_test_config.h
62 sanitizer_test_utils.h
64 foreach(header ${SANITIZER_IMPL_HEADERS})
65 list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header})
68 set(SANITIZER_TEST_CFLAGS_COMMON
69 ${COMPILER_RT_UNITTEST_CFLAGS}
70 ${COMPILER_RT_GTEST_CFLAGS}
71 ${COMPILER_RT_GMOCK_CFLAGS}
72 ${SANITIZER_TEST_CXX_CFLAGS}
73 -I${COMPILER_RT_SOURCE_DIR}/include
74 -I${COMPILER_RT_SOURCE_DIR}/lib
75 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common
76 -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS
80 -Wno-gnu-zero-variadic-macro-arguments
83 set(SANITIZER_TEST_LINK_FLAGS_COMMON
84 ${COMPILER_RT_UNITTEST_LINK_FLAGS}
85 ${COMPILER_RT_UNWINDER_LINK_LIBS}
86 ${SANITIZER_TEST_CXX_LIBRARIES})
88 # -gline-tables-only must be enough for these tests, so use it if possible.
89 if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
90 list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gline-tables-only)
92 list(APPEND SANITIZER_TEST_CFLAGS_COMMON -g)
95 list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gcodeview)
97 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -g)
100 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
104 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -pie)
108 list(APPEND SANITIZER_TEST_CFLAGS_COMMON ${DARWIN_osx_CFLAGS})
109 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON ${DARWIN_osx_LINK_FLAGS})
111 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
112 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON ${WEAK_SYMBOL_LINK_FLAGS})
113 # For c++17 sanitizer_allocator_test requires language features introduced in macos 10.13
114 list(APPEND SANITIZER_TEST_CFLAGS_COMMON "-mmacosx-version-min=10.13")
117 # MSVC linker is allocating 1M for the stack by default, which is not
118 # enough for the unittests. Some unittests require more than 2M.
119 # The default stack size for clang is 8M.
121 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -Wl,/STACK:0xC00000)
123 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -Wl,--stack=0xC00000)
126 set(SANITIZER_TEST_LINK_LIBS)
127 append_list_if(COMPILER_RT_HAS_LIBLOG log SANITIZER_TEST_LINK_LIBS)
129 append_list_if(COMPILER_RT_HAS_LIBDL -ldl SANITIZER_TEST_LINK_FLAGS_COMMON)
130 append_list_if(COMPILER_RT_HAS_LIBRT -lrt SANITIZER_TEST_LINK_FLAGS_COMMON)
131 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SANITIZER_TEST_LINK_FLAGS_COMMON)
132 # x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also,
133 # 'libm' shall be specified explicitly to build i386 tests.
134 if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
135 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON "-lc++ -lm")
138 include_directories(..)
139 include_directories(../..)
141 # Adds static library which contains sanitizer_common object file
142 # (universal binary on Mac and arch-specific object files on Linux).
143 macro(add_sanitizer_common_lib library)
144 add_library(${library} STATIC ${ARGN})
145 set_target_properties(${library} PROPERTIES
146 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
147 FOLDER "Compiler-RT/Tests/Runtime")
150 function(get_sanitizer_common_lib_for_arch arch lib)
152 set(tgt_name "RTSanitizerCommon.test.osx")
154 set(tgt_name "RTSanitizerCommon.test.${arch}")
156 set(${lib} "${tgt_name}" PARENT_SCOPE)
159 # Sanitizer_common unit tests testsuite.
160 add_custom_target(SanitizerUnitTests)
161 set_target_properties(SanitizerUnitTests PROPERTIES FOLDER "Compiler-RT/Tests")
163 # Adds sanitizer tests for architecture.
164 macro(add_sanitizer_tests_for_arch arch)
166 if( CMAKE_SIZEOF_VOID_P EQUAL 4 )
167 list(APPEND extra_flags "-D_LARGEFILE_SOURCE=1")
168 list(APPEND extra_flags "-D_FILE_OFFSET_BITS=64")
170 get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB)
171 set(TARGET_LINK_FLAGS)
172 get_target_link_flags_for_arch(${arch} TARGET_LINK_FLAGS)
174 set(SANITIZER_TEST_OBJECTS)
175 generate_compiler_rt_tests(SANITIZER_TEST_OBJECTS SanitizerUnitTests
176 "Sanitizer-${arch}-Test" ${arch}
177 RUNTIME "${SANITIZER_COMMON_LIB}"
178 SOURCES ${SANITIZER_UNITTESTS} ${COMPILER_RT_GTEST_SOURCE} ${COMPILER_RT_GMOCK_SOURCE}
179 COMPILE_DEPS ${SANITIZER_TEST_HEADERS}
180 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${extra_flags}
181 LINK_FLAGS ${SANITIZER_TEST_LINK_FLAGS_COMMON} ${TARGET_LINK_FLAGS} ${extra_flags})
183 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND "${arch}" STREQUAL "x86_64")
184 # Test that the libc-independent part of sanitizer_common is indeed
185 # independent of libc, by linking this binary without libc (here) and
186 # executing it (unit test in sanitizer_nolibc_test.cpp).
187 get_target_flags_for_arch(${arch} TARGET_FLAGS)
188 clang_compile(sanitizer_nolibc_test_main.${arch}.o
189 sanitizer_nolibc_test_main.cpp
190 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS}
191 DEPS ${SANITIZER_TEST_COMPILE_DEPS})
192 add_compiler_rt_test(SanitizerUnitTests "Sanitizer-${arch}-Test-Nolibc" ${arch}
193 OBJECTS sanitizer_nolibc_test_main.${arch}.o
195 libRTSanitizerCommon.test.nolibc.${arch}.a
196 -Wl,-no-whole-archive
197 DEPS sanitizer_nolibc_test_main.${arch}.o
198 RTSanitizerCommon.test.nolibc.${arch}
199 LINK_FLAGS -static -nostdlib ${TARGET_FLAGS})
203 if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID)
204 # We use just-built clang to build sanitizer_common unittests, so we must
205 # be sure that produced binaries would work.
207 add_sanitizer_common_lib("RTSanitizerCommon.test.osx"
208 $<TARGET_OBJECTS:RTSanitizerCommon.osx>
209 $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>
210 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.osx>)
212 if(CAN_TARGET_x86_64)
213 add_sanitizer_common_lib("RTSanitizerCommon.test.nolibc.x86_64"
214 $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>
215 $<TARGET_OBJECTS:RTSanitizerCommonNoLibc.x86_64>)
217 foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH})
218 add_sanitizer_common_lib("RTSanitizerCommon.test.${arch}"
219 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
220 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
221 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
222 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizerInternal.${arch}>)
225 foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH})
226 add_sanitizer_tests_for_arch(${arch})
231 foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
232 add_executable(SanitizerTest
233 ${SANITIZER_UNITTESTS}
234 ${COMPILER_RT_GTEST_SOURCE}
235 ${COMPILER_RT_GMOCK_SOURCE}
236 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
237 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
238 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
239 $<TARGET_OBJECTS:RTSanitizerCommonSymbolizerInternal.${arch}>)
240 set_target_compile_flags(SanitizerTest ${SANITIZER_TEST_CFLAGS_COMMON})
241 # Setup correct output directory and link flags.
242 set_target_properties(SanitizerTest PROPERTIES
243 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
244 set_target_link_flags(SanitizerTest ${SANITIZER_TEST_LINK_FLAGS_COMMON})
245 target_link_libraries(SanitizerTest ${SANITIZER_TEST_LINK_LIBS})
246 # Add unit test to test suite.
247 add_dependencies(SanitizerUnitTests SanitizerTest)