1 include(CompilerRTCompile) 2 3 set(SANITIZER_UNITTESTS 4 sanitizer_allocator_test.cc 5 sanitizer_common_test.cc 6 sanitizer_flags_test.cc 7 sanitizer_libc_test.cc 8 sanitizer_linux_test.cc 9 sanitizer_list_test.cc 10 sanitizer_mutex_test.cc 11 sanitizer_printf_test.cc 12 sanitizer_scanf_interceptor_test.cc 13 sanitizer_stackdepot_test.cc 14 sanitizer_stacktrace_test.cc 15 sanitizer_stoptheworld_test.cc 16 sanitizer_test_main.cc 17 sanitizer_thread_registry_test.cc 18 ) 19 20 set(SANITIZER_TEST_HEADERS) 21 foreach(header ${SANITIZER_HEADERS}) 22 list(APPEND SANITIZER_TEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../${header}) 23 endforeach() 24 25 set(SANITIZER_TEST_CFLAGS_COMMON 26 ${COMPILER_RT_GTEST_INCLUDE_CFLAGS} 27 -I${COMPILER_RT_SOURCE_DIR}/include 28 -I${COMPILER_RT_SOURCE_DIR}/lib 29 -I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common 30 -DGTEST_HAS_RTTI=0 31 -O2 -g -fno-rtti 32 -Wall -Werror -Werror=sign-compare) 33 34 set(SANITIZER_TEST_LINK_FLAGS_COMMON 35 -lstdc++ -ldl) 36 37 include_directories(..) 38 include_directories(../..) 39 40 # Adds static library which contains sanitizer_common object file 41 # (universal binary on Mac and arch-specific object files on Linux). 42 macro(add_sanitizer_common_lib library) 43 add_library(${library} STATIC ${ARGN}) 44 set_target_properties(${library} PROPERTIES 45 ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 46 endmacro() 47 48 function(get_sanitizer_common_lib_for_arch arch lib lib_name) 49 if(APPLE) 50 set(tgt_name "RTSanitizerCommon.test.osx") 51 else() 52 set(tgt_name "RTSanitizerCommon.test.${arch}") 53 endif() 54 set(${lib} "${tgt_name}" PARENT_SCOPE) 55 set(${lib_name} "lib${tgt_name}.a" PARENT_SCOPE) 56 endfunction() 57 58 # Sanitizer_common unit tests testsuite. 59 add_custom_target(SanitizerUnitTests) 60 set_target_properties(SanitizerUnitTests PROPERTIES 61 FOLDER "Sanitizer unittests") 62 63 # Adds sanitizer tests for architecture. 64 macro(add_sanitizer_tests_for_arch arch) 65 get_target_flags_for_arch(${arch} TARGET_FLAGS) 66 set(SANITIZER_TEST_SOURCES ${SANITIZER_UNITTESTS} 67 ${COMPILER_RT_GTEST_SOURCE}) 68 set(SANITIZER_TEST_OBJECTS) 69 foreach(source ${SANITIZER_TEST_SOURCES}) 70 get_filename_component(basename ${source} NAME) 71 set(output_obj "${basename}.${arch}.o") 72 clang_compile(${output_obj} ${source} 73 CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS} 74 DEPS gtest ${SANITIZER_RUNTIME_LIBRARIES} 75 ${SANITIZER_TEST_HEADERS}) 76 list(APPEND SANITIZER_TEST_OBJECTS ${output_obj}) 77 endforeach() 78 get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB 79 SANITIZER_COMMON_LIB_NAME) 80 # Add unittest target. 81 set(SANITIZER_TEST_NAME "Sanitizer-${arch}-Test") 82 add_compiler_rt_test(SanitizerUnitTests ${SANITIZER_TEST_NAME} 83 OBJECTS ${SANITIZER_TEST_OBJECTS} 84 ${SANITIZER_COMMON_LIB_NAME} 85 DEPS ${SANITIZER_TEST_OBJECTS} ${SANITIZER_COMMON_LIB} 86 LINK_FLAGS ${SANITIZER_TEST_LINK_FLAGS_COMMON} 87 -lpthread ${TARGET_FLAGS}) 88 endmacro() 89 90 if(COMPILER_RT_CAN_EXECUTE_TESTS) 91 # We use just-built clang to build sanitizer_common unittests, so we must 92 # be sure that produced binaries would work. 93 if(APPLE) 94 add_sanitizer_common_lib("RTSanitizerCommon.test.osx" 95 $<TARGET_OBJECTS:RTSanitizerCommon.osx>) 96 else() 97 if(CAN_TARGET_x86_64) 98 add_sanitizer_common_lib("RTSanitizerCommon.test.x86_64" 99 $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>) 100 endif() 101 if(CAN_TARGET_i386) 102 add_sanitizer_common_lib("RTSanitizerCommon.test.i386" 103 $<TARGET_OBJECTS:RTSanitizerCommon.i386>) 104 endif() 105 endif() 106 if(CAN_TARGET_x86_64) 107 add_sanitizer_tests_for_arch(x86_64) 108 endif() 109 if(CAN_TARGET_i386) 110 add_sanitizer_tests_for_arch(i386) 111 endif() 112 113 # Run unittests as a part of lit testsuite. 114 configure_lit_site_cfg( 115 ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in 116 ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg 117 ) 118 119 add_lit_testsuite(check-sanitizer "Running sanitizer library unittests" 120 ${CMAKE_CURRENT_BINARY_DIR} 121 DEPENDS SanitizerUnitTests 122 ) 123 set_target_properties(check-sanitizer PROPERTIES FOLDER "Sanitizer unittests") 124 endif() 125 126 if(ANDROID) 127 # We assume that unit tests on Android are built in a build 128 # tree with fresh Clang as a host compiler. 129 add_executable(SanitizerTest 130 ${SANITIZER_UNITTESTS} 131 ${COMPILER_RT_GTEST_SOURCE} 132 $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>) 133 set_target_compile_flags(SanitizerTest 134 ${SANITIZER_COMMON_CFLAGS} 135 ${SANITIZER_TEST_CFLAGS_COMMON}) 136 # Setup correct output directory and link flags. 137 set_target_properties(SanitizerTest PROPERTIES 138 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 139 set_target_link_flags(SanitizerTest ${SANITIZER_TEST_LINK_FLAGS_COMMON}) 140 # Add unit test to test suite. 141 add_dependencies(SanitizerUnitTests SanitizerTest) 142 endif() 143