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_UNITTEST_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 if(MSVC)
     69   list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gcodeview)
     70 endif()
     71 list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -g)
     72 
     73 if(NOT MSVC)
     74   list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
     75 endif()
     76 
     77 if(ANDROID)
     78   list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -pie)
     79 endif()
     80 
     81 # MSVC linker is allocating 1M for the stack by default, which is not
     82 # enough for the unittests. Some unittests require more than 2M.
     83 # The default stack size for clang is 8M.
     84 if(MSVC)
     85   list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -Wl,/STACK:0xC00000)
     86 endif()
     87 
     88 set(SANITIZER_TEST_LINK_LIBS)
     89 append_list_if(COMPILER_RT_HAS_LIBLOG log SANITIZER_TEST_LINK_LIBS)
     90 # NDK r10 requires -latomic almost always.
     91 append_list_if(ANDROID atomic SANITIZER_TEST_LINK_LIBS)
     92 
     93 append_list_if(COMPILER_RT_HAS_LIBDL -ldl SANITIZER_TEST_LINK_FLAGS_COMMON)
     94 append_list_if(COMPILER_RT_HAS_LIBRT -lrt SANITIZER_TEST_LINK_FLAGS_COMMON)
     95 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SANITIZER_TEST_LINK_FLAGS_COMMON)
     96 # x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also,
     97 # 'libm' shall be specified explicitly to build i386 tests.
     98 if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
     99   list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON "-lc++ -lm")
    100 endif()
    101 
    102 include_directories(..)
    103 include_directories(../..)
    104 
    105 # Adds static library which contains sanitizer_common object file
    106 # (universal binary on Mac and arch-specific object files on Linux).
    107 macro(add_sanitizer_common_lib library)
    108   add_library(${library} STATIC ${ARGN})
    109   set_target_properties(${library} PROPERTIES
    110     ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    111     FOLDER "Compiler-RT Runtime tests")
    112 endmacro()
    113 
    114 function(get_sanitizer_common_lib_for_arch arch lib lib_name)
    115   if(APPLE)
    116     set(tgt_name "RTSanitizerCommon.test.osx")
    117   else()
    118     set(tgt_name "RTSanitizerCommon.test.${arch}")
    119   endif()
    120   set(${lib} "${tgt_name}" PARENT_SCOPE)
    121   if(CMAKE_CONFIGURATION_TYPES)
    122    set(configuration_path "${CMAKE_CFG_INTDIR}/")
    123   else()
    124    set(configuration_path "")
    125   endif()
    126   if(NOT MSVC)
    127     set(${lib_name} "${configuration_path}lib${tgt_name}.a" PARENT_SCOPE)
    128   else()
    129     set(${lib_name} "${configuration_path}${tgt_name}.lib" PARENT_SCOPE)
    130   endif()
    131 endfunction()
    132 
    133 # Sanitizer_common unit tests testsuite.
    134 add_custom_target(SanitizerUnitTests)
    135 set_target_properties(SanitizerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
    136 
    137 # Adds sanitizer tests for architecture.
    138 macro(add_sanitizer_tests_for_arch arch)
    139   get_target_flags_for_arch(${arch} TARGET_FLAGS)
    140   set(SANITIZER_TEST_SOURCES ${SANITIZER_UNITTESTS}
    141                              ${COMPILER_RT_GTEST_SOURCE})
    142   set(SANITIZER_TEST_COMPILE_DEPS ${SANITIZER_TEST_HEADERS})
    143   if(NOT COMPILER_RT_STANDALONE_BUILD)
    144     list(APPEND SANITIZER_TEST_COMPILE_DEPS gtest)
    145   endif()
    146   set(SANITIZER_TEST_OBJECTS)
    147   foreach(source ${SANITIZER_TEST_SOURCES})
    148     get_filename_component(basename ${source} NAME)
    149     if(CMAKE_CONFIGURATION_TYPES)
    150       set(output_obj "${CMAKE_CFG_INTDIR}/${basename}.${arch}.o")
    151     else()
    152       set(output_obj "${basename}.${arch}.o")
    153     endif()
    154     clang_compile(${output_obj} ${source}
    155                   CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS}
    156                   DEPS ${SANITIZER_TEST_COMPILE_DEPS})
    157     list(APPEND SANITIZER_TEST_OBJECTS ${output_obj})
    158   endforeach()
    159   get_sanitizer_common_lib_for_arch(${arch} SANITIZER_COMMON_LIB
    160                                     SANITIZER_COMMON_LIB_NAME)
    161   # Add unittest target.
    162   set(SANITIZER_TEST_NAME "Sanitizer-${arch}-Test")
    163   add_compiler_rt_test(SanitizerUnitTests ${SANITIZER_TEST_NAME}
    164                        OBJECTS ${SANITIZER_TEST_OBJECTS}
    165                                ${SANITIZER_COMMON_LIB_NAME}
    166                        DEPS ${SANITIZER_TEST_OBJECTS} ${SANITIZER_COMMON_LIB}
    167                        LINK_FLAGS ${SANITIZER_TEST_LINK_FLAGS_COMMON}
    168                                   ${TARGET_FLAGS})
    169 
    170   if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND "${arch}" STREQUAL "x86_64")
    171     # Test that the libc-independent part of sanitizer_common is indeed
    172     # independent of libc, by linking this binary without libc (here) and
    173     # executing it (unit test in sanitizer_nolibc_test.cc).
    174     clang_compile(sanitizer_nolibc_test_main.${arch}.o
    175                   sanitizer_nolibc_test_main.cc
    176                   CFLAGS ${SANITIZER_TEST_CFLAGS_COMMON} ${TARGET_FLAGS}
    177                   DEPS ${SANITIZER_TEST_COMPILE_DEPS})
    178     add_compiler_rt_test(SanitizerUnitTests "Sanitizer-${arch}-Test-Nolibc"
    179                          OBJECTS sanitizer_nolibc_test_main.${arch}.o
    180                                  -Wl,-whole-archive
    181                                  libRTSanitizerCommon.test.nolibc.${arch}.a
    182                                  -Wl,-no-whole-archive
    183                          DEPS sanitizer_nolibc_test_main.${arch}.o
    184                               RTSanitizerCommon.test.nolibc.${arch}
    185                          LINK_FLAGS -nostdlib ${TARGET_FLAGS})
    186   endif()
    187 endmacro()
    188 
    189 if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID)
    190   # We use just-built clang to build sanitizer_common unittests, so we must
    191   # be sure that produced binaries would work.
    192   if(APPLE)
    193     add_sanitizer_common_lib("RTSanitizerCommon.test.osx"
    194                              $<TARGET_OBJECTS:RTSanitizerCommon.osx>
    195                              $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>)
    196   else()
    197     if(CAN_TARGET_x86_64)
    198       add_sanitizer_common_lib("RTSanitizerCommon.test.nolibc.x86_64"
    199                                $<TARGET_OBJECTS:RTSanitizerCommon.x86_64>
    200                                $<TARGET_OBJECTS:RTSanitizerCommonNoLibc.x86_64>)
    201     endif()
    202     foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH})
    203       add_sanitizer_common_lib("RTSanitizerCommon.test.${arch}"
    204                                $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
    205                                $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
    206     endforeach()
    207   endif()
    208   foreach(arch ${SANITIZER_UNITTEST_SUPPORTED_ARCH})
    209     add_sanitizer_tests_for_arch(${arch})
    210   endforeach()
    211 endif()
    212 
    213 if(ANDROID)
    214   foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
    215     add_executable(SanitizerTest
    216       ${SANITIZER_UNITTESTS}
    217       ${COMPILER_RT_GTEST_SOURCE}
    218       $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
    219       $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
    220     set_target_compile_flags(SanitizerTest
    221       ${SANITIZER_COMMON_CFLAGS}
    222       ${SANITIZER_TEST_CFLAGS_COMMON})
    223     # Setup correct output directory and link flags.
    224     set_target_properties(SanitizerTest PROPERTIES
    225       RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    226     set_target_link_flags(SanitizerTest ${SANITIZER_TEST_LINK_FLAGS_COMMON})
    227     target_link_libraries(SanitizerTest ${SANITIZER_TEST_LINK_LIBS})
    228     # Add unit test to test suite.
    229     add_dependencies(SanitizerUnitTests SanitizerTest)
    230   endforeach()
    231 endif()
    232