1include(CompilerRTCompile) 2 3clang_compiler_add_cxx_check() 4 5# FIXME: use SANITIZER_COMMON_SUPPORTED_ARCH here 6filter_available_targets(SANITIZER_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el) 7 8set(SANITIZER_UNITTESTS 9 sanitizer_allocator_test.cc 10 sanitizer_atomic_test.cc 11 sanitizer_bitvector_test.cc 12 sanitizer_bvgraph_test.cc 13 sanitizer_common_test.cc 14 sanitizer_deadlock_detector_test.cc 15 sanitizer_flags_test.cc 16 sanitizer_format_interceptor_test.cc 17 sanitizer_ioctl_test.cc 18 sanitizer_libc_test.cc 19 sanitizer_linux_test.cc 20 sanitizer_list_test.cc 21 sanitizer_mutex_test.cc 22 sanitizer_nolibc_test.cc 23 sanitizer_posix_test.cc 24 sanitizer_printf_test.cc 25 sanitizer_procmaps_test.cc 26 sanitizer_stackdepot_test.cc 27 sanitizer_stacktrace_printer_test.cc 28 sanitizer_stacktrace_test.cc 29 sanitizer_stoptheworld_test.cc 30 sanitizer_suppressions_test.cc 31 sanitizer_symbolizer_test.cc 32 sanitizer_test_main.cc 33 sanitizer_thread_registry_test.cc) 34 35set(SANITIZER_TEST_HEADERS 36 sanitizer_pthread_wrappers.h 37 sanitizer_test_config.h 38 sanitizer_test_utils.h) 39foreach(header ${SANITIZER_HEADERS}) 40 list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header}) 41endforeach() 42 43set(SANITIZER_TEST_CFLAGS_COMMON 44 ${COMPILER_RT_TEST_CFLAGS} 45 ${COMPILER_RT_GTEST_CFLAGS} 46 -I${COMPILER_RT_SOURCE_DIR}/include 47 -I${COMPILER_RT_SOURCE_DIR}/lib 48 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common 49 -fno-rtti 50 -O2 51 -Werror=sign-compare 52 -Wno-non-virtual-dtor) 53 54# -gline-tables-only must be enough for these tests, so use it if possible. 55if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang") 56 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gline-tables-only) 57else() 58 list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -g) 59endif() 60 61if(NOT MSVC) 62 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON --driver-mode=g++) 63endif() 64 65set(SANITIZER_TEST_LINK_LIBS) 66append_list_if(ANDROID log SANITIZER_TEST_LINK_LIBS) 67# NDK r10 requires -latomic almost always. 68append_list_if(ANDROID atomic SANITIZER_TEST_LINK_LIBS) 69 70append_list_if(COMPILER_RT_HAS_LIBDL -ldl SANITIZER_TEST_LINK_FLAGS_COMMON) 71append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SANITIZER_TEST_LINK_FLAGS_COMMON) 72# x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also, 73# 'libm' shall be specified explicitly to build i386 tests. 74if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE") 75 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON "-lc++ -lm") 76endif() 77 78include_directories(..) 79include_directories(../..) 80 81# Adds static library which contains sanitizer_common object file 82# (universal binary on Mac and arch-specific object files on Linux). 83macro(add_sanitizer_common_lib library) 84 add_library(${library} STATIC ${ARGN}) 85 set_target_properties(${library} PROPERTIES 86 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 87endmacro() 88 89function(get_sanitizer_common_lib_for_arch arch lib lib_name) 90 if(APPLE) 91 set(tgt_name "RTSanitizerCommon.test.osx") 92 else() 93 set(tgt_name "RTSanitizerCommon.test.${arch}") 94 endif() 95 set(${lib} "${tgt_name}" PARENT_SCOPE) 96 if(NOT MSVC) 97 set(${lib_name} "lib${tgt_name}.a" PARENT_SCOPE) 98 else() 99 set(${lib_name} "${tgt_name}.lib" PARENT_SCOPE) 100 endif() 101endfunction() 102 103# Sanitizer_common unit tests testsuite. 104add_custom_target(SanitizerUnitTests) 105set_target_properties(SanitizerUnitTests PROPERTIES 106 FOLDER "Sanitizer unittests") 107 108# Adds sanitizer tests for architecture. 109macro(add_sanitizer_tests_for_arch arch) 110 get_target_flags_for_arch(${arch} TARGET_FLAGS) 111 set(SANITIZER_TEST_SOURCES ${SANITIZER_UNITTESTS} 112 ${COMPILER_RT_GTEST_SOURCE}) 113 set(SANITIZER_TEST_COMPILE_DEPS ${SANITIZER_TEST_HEADERS}) 114 if(NOT COMPILER_RT_STANDALONE_BUILD) 115 list(APPEND SANITIZER_TEST_COMPILE_DEPS gtest) 116 endif() 117 set(SANITIZER_TEST_OBJECTS) 118 foreach(source ${SANITIZER_TEST_SOURCES}) 119 get_filename_component(basename ${source} NAME) 120 set(output_obj "${basename}.${arch}.o") 121 clang_compile(${output_obj} ${source} 122 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS} 123 DEPS ${SANITIZER_TEST_COMPILE_DEPS}) 124 list(APPEND SANITIZER_TEST_OBJECTS ${output_obj}) 125 endforeach() 126 get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB 127 SANITIZER_COMMON_LIB_NAME) 128 # Add unittest target. 129 set(SANITIZER_TEST_NAME "Sanitizer-${arch}-Test") 130 add_compiler_rt_test(SanitizerUnitTests ${SANITIZER_TEST_NAME} 131 OBJECTS ${SANITIZER_TEST_OBJECTS} 132 ${SANITIZER_COMMON_LIB_NAME} 133 DEPS ${SANITIZER_TEST_OBJECTS} ${SANITIZER_COMMON_LIB} 134 LINK_FLAGS ${SANITIZER_TEST_LINK_FLAGS_COMMON} 135 ${TARGET_FLAGS}) 136 137 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND "${arch}" STREQUAL "x86_64") 138 # Test that the libc-independent part of sanitizer_common is indeed 139 # independent of libc, by linking this binary without libc (here) and 140 # executing it (unit test in sanitizer_nolibc_test.cc). 141 clang_compile(sanitizer_nolibc_test_main.${arch}.o 142 sanitizer_nolibc_test_main.cc 143 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS} 144 DEPS ${SANITIZER_TEST_COMPILE_DEPS}) 145 add_compiler_rt_test(SanitizerUnitTests "Sanitizer-${arch}-Test-Nolibc" 146 OBJECTS sanitizer_nolibc_test_main.${arch}.o 147 -Wl,-whole-archive 148 libRTSanitizerCommon.test.nolibc.${arch}.a 149 -Wl,-no-whole-archive 150 DEPS sanitizer_nolibc_test_main.${arch}.o 151 RTSanitizerCommon.test.nolibc.${arch} 152 LINK_FLAGS -nostdlib ${TARGET_FLAGS}) 153 endif() 154endmacro() 155 156if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID) 157 # We use just-built clang to build sanitizer_common unittests, so we must 158 # be sure that produced binaries would work. 159 if(APPLE) 160 add_sanitizer_common_lib("RTSanitizerCommon.test.osx" 161 $<TARGET_OBJECTS:RTSanitizerCommon.osx>) 162 else() 163 if(CAN_TARGET_x86_64) 164 add_sanitizer_common_lib("RTSanitizerCommon.test.nolibc.x86_64" 165 $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>) 166 endif() 167 foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH}) 168 add_sanitizer_common_lib("RTSanitizerCommon.test.${arch}" 169 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}> 170 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>) 171 endforeach() 172 endif() 173 foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH}) 174 add_sanitizer_tests_for_arch(${arch}) 175 endforeach() 176endif() 177 178if(ANDROID) 179 foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH}) 180 add_executable(SanitizerTest 181 ${SANITIZER_UNITTESTS} 182 ${COMPILER_RT_GTEST_SOURCE} 183 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}> 184 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>) 185 set_target_compile_flags(SanitizerTest 186 ${SANITIZER_COMMON_CFLAGS} 187 ${SANITIZER_TEST_CFLAGS_COMMON}) 188 # Setup correct output directory and link flags. 189 set_target_properties(SanitizerTest PROPERTIES 190 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 191 set_target_link_flags(SanitizerTest ${SANITIZER_TEST_LINK_FLAGS_COMMON}) 192 target_link_libraries(SanitizerTest ${SANITIZER_TEST_LINK_LIBS}) 193 # Add unit test to test suite. 194 add_dependencies(SanitizerUnitTests SanitizerTest) 195 endforeach() 196endif() 197