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_bvgraph_test.cpp
19 sanitizer_chained_origin_depot_test.cpp
20 sanitizer_common_test.cpp
21 sanitizer_deadlock_detector_test.cpp
22 sanitizer_dense_map_test.cpp
23 sanitizer_flags_test.cpp
24 sanitizer_flat_map_test.cpp
25 sanitizer_format_interceptor_test.cpp
26 sanitizer_hash_test.cpp
27 sanitizer_ioctl_test.cpp
28 sanitizer_leb128_test.cpp
29 sanitizer_libc_test.cpp
30 sanitizer_linux_test.cpp
31 sanitizer_list_test.cpp
32 sanitizer_lzw_test.cpp
33 sanitizer_mac_test.cpp
34 sanitizer_module_uuid_size.cpp
35 sanitizer_mutex_test.cpp
36 sanitizer_nolibc_test.cpp
37 sanitizer_posix_test.cpp
38 sanitizer_printf_test.cpp
39 sanitizer_procmaps_test.cpp
40 sanitizer_procmaps_mac_test.cpp
41 sanitizer_range_test.cpp
42 sanitizer_ring_buffer_test.cpp
43 sanitizer_quarantine_test.cpp
44 sanitizer_stack_store_test.cpp
45 sanitizer_stackdepot_test.cpp
46 sanitizer_stacktrace_printer_test.cpp
47 sanitizer_stacktrace_test.cpp
48 sanitizer_stoptheworld_test.cpp
49 sanitizer_suppressions_test.cpp
50 sanitizer_symbolizer_test.cpp
51 sanitizer_test_main.cpp
52 sanitizer_thread_arg_retval_test.cpp
53 sanitizer_thread_registry_test.cpp
54 sanitizer_type_traits_test.cpp
55 sanitizer_vector_test.cpp
58 set(SANITIZER_TEST_HEADERS
59 sanitizer_pthread_wrappers.h
60 sanitizer_test_config.h
61 sanitizer_test_utils.h
63 foreach(header ${SANITIZER_IMPL_HEADERS})
64 list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header})
67 set(SANITIZER_TEST_CFLAGS_COMMON
68 ${COMPILER_RT_UNITTEST_CFLAGS}
69 ${COMPILER_RT_GTEST_CFLAGS}
70 ${COMPILER_RT_GMOCK_CFLAGS}
71 ${SANITIZER_TEST_CXX_CFLAGS}
72 -I${COMPILER_RT_SOURCE_DIR}/include
73 -I${COMPILER_RT_SOURCE_DIR}/lib
74 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common
75 -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS
79 -Wno-gnu-zero-variadic-macro-arguments
82 set(SANITIZER_TEST_LINK_FLAGS_COMMON
83 ${COMPILER_RT_UNITTEST_LINK_FLAGS}
84 ${COMPILER_RT_UNWINDER_LINK_LIBS}
85 ${SANITIZER_TEST_CXX_LIBRARIES})
87 # -gline-tables-only must be enough for these tests, so use it if possible.
88 if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
89 list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gline-tables-only)
91 list(APPEND SANITIZER_TEST_CFLAGS_COMMON -g)
94 list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gcodeview)
96 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -g)
99 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
103 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -pie)
107 list(APPEND SANITIZER_TEST_CFLAGS_COMMON ${DARWIN_osx_CFLAGS})
108 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON ${DARWIN_osx_LINK_FLAGS})
110 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
111 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON ${WEAK_SYMBOL_LINK_FLAGS})
112 # For c++17 sanitizer_allocator_test requires language features introduced in macos 10.13
113 list(APPEND SANITIZER_TEST_CFLAGS_COMMON "-mmacosx-version-min=10.13")
116 # MSVC linker is allocating 1M for the stack by default, which is not
117 # enough for the unittests. Some unittests require more than 2M.
118 # The default stack size for clang is 8M.
120 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -Wl,/STACK:0xC00000)
122 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -Wl,--stack=0xC00000)
125 set(SANITIZER_TEST_LINK_LIBS)
126 append_list_if(COMPILER_RT_HAS_LIBLOG log SANITIZER_TEST_LINK_LIBS)
128 append_list_if(COMPILER_RT_HAS_LIBDL -ldl SANITIZER_TEST_LINK_FLAGS_COMMON)
129 append_list_if(COMPILER_RT_HAS_LIBRT -lrt SANITIZER_TEST_LINK_FLAGS_COMMON)
130 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SANITIZER_TEST_LINK_FLAGS_COMMON)
131 # x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also,
132 # 'libm' shall be specified explicitly to build i386 tests.
133 if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
134 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON "-lc++ -lm")
137 include_directories(..)
138 include_directories(../..)
140 # Adds static library which contains sanitizer_common object file
141 # (universal binary on Mac and arch-specific object files on Linux).
142 macro(add_sanitizer_common_lib library)
143 add_library(${library} STATIC ${ARGN})
144 set_target_properties(${library} PROPERTIES
145 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
146 FOLDER "Compiler-RT Runtime tests")
149 function(get_sanitizer_common_lib_for_arch arch lib)
151 set(tgt_name "RTSanitizerCommon.test.osx")
153 set(tgt_name "RTSanitizerCommon.test.${arch}")
155 set(${lib} "${tgt_name}" PARENT_SCOPE)
158 # Sanitizer_common unit tests testsuite.
159 add_custom_target(SanitizerUnitTests)
160 set_target_properties(SanitizerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
162 # Adds sanitizer tests for architecture.
163 macro(add_sanitizer_tests_for_arch arch)
165 if( CMAKE_SIZEOF_VOID_P EQUAL 4 )
166 list(APPEND extra_flags "-D_LARGEFILE_SOURCE=1")
167 list(APPEND extra_flags "-D_FILE_OFFSET_BITS=64")
169 get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB)
170 set(TARGET_LINK_FLAGS)
171 get_target_link_flags_for_arch(${arch} TARGET_LINK_FLAGS)
173 set(SANITIZER_TEST_OBJECTS)
174 generate_compiler_rt_tests(SANITIZER_TEST_OBJECTS SanitizerUnitTests
175 "Sanitizer-${arch}-Test" ${arch}
176 RUNTIME "${SANITIZER_COMMON_LIB}"
177 SOURCES ${SANITIZER_UNITTESTS} ${COMPILER_RT_GTEST_SOURCE} ${COMPILER_RT_GMOCK_SOURCE}
178 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)