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