Home | History | Annotate | Download | only in tests
      1 include(CompilerRTCompile)
      2 
      3 filter_available_targets(INTERCEPTION_UNITTEST_SUPPORTED_ARCH x86_64 i386 mips64 mips64el)
      4 
      5 set(INTERCEPTION_UNITTESTS
      6   interception_linux_test.cc
      7   interception_test_main.cc
      8   interception_win_test.cc
      9 )
     10 
     11 set(INTERCEPTION_TEST_HEADERS)
     12 
     13 set(INTERCEPTION_TEST_CFLAGS_COMMON
     14   ${COMPILER_RT_UNITTEST_CFLAGS}
     15   ${COMPILER_RT_GTEST_CFLAGS}
     16   -I${COMPILER_RT_SOURCE_DIR}/include
     17   -I${COMPILER_RT_SOURCE_DIR}/lib
     18   -I${COMPILER_RT_SOURCE_DIR}/lib/interception
     19   -fno-rtti
     20   -O2
     21   -Werror=sign-compare
     22   -Wno-non-virtual-dtor)
     23 
     24 # -gline-tables-only must be enough for these tests, so use it if possible.
     25 if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
     26   list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gline-tables-only)
     27 else()
     28   list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -g)
     29 endif()
     30 if(MSVC)
     31   list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gcodeview)
     32 endif()
     33 list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -g)
     34 
     35 if(NOT MSVC)
     36   list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON --driver-mode=g++)
     37 endif()
     38 
     39 if(ANDROID)
     40   list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON -pie)
     41 endif()
     42 
     43 set(INTERCEPTION_TEST_LINK_LIBS)
     44 append_list_if(COMPILER_RT_HAS_LIBLOG log INTERCEPTION_TEST_LINK_LIBS)
     45 # NDK r10 requires -latomic almost always.
     46 append_list_if(ANDROID atomic INTERCEPTION_TEST_LINK_LIBS)
     47 
     48 append_list_if(COMPILER_RT_HAS_LIBDL -ldl INTERCEPTION_TEST_LINK_FLAGS_COMMON)
     49 append_list_if(COMPILER_RT_HAS_LIBRT -lrt INTERCEPTION_TEST_LINK_FLAGS_COMMON)
     50 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread INTERCEPTION_TEST_LINK_FLAGS_COMMON)
     51 # x86_64 FreeBSD 9.2 additionally requires libc++ to build the tests. Also,
     52 # 'libm' shall be specified explicitly to build i386 tests.
     53 if(CMAKE_SYSTEM MATCHES "FreeBSD-9.2-RELEASE")
     54   list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON "-lc++ -lm")
     55 endif()
     56 
     57 include_directories(..)
     58 include_directories(../..)
     59 
     60 # Adds static library which contains interception object file
     61 # (universal binary on Mac and arch-specific object files on Linux).
     62 macro(add_interceptor_lib library)
     63   add_library(${library} STATIC ${ARGN})
     64   set_target_properties(${library} PROPERTIES
     65     ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
     66     FOLDER "Compiler-RT Runtime tests")
     67 endmacro()
     68 
     69 function(get_interception_lib_for_arch arch lib lib_name)
     70   if(APPLE)
     71     set(tgt_name "RTInterception.test.osx")
     72   else()
     73     set(tgt_name "RTInterception.test.${arch}")
     74   endif()
     75   set(${lib} "${tgt_name}" PARENT_SCOPE)
     76   if(CMAKE_CONFIGURATION_TYPES)
     77    set(configuration_path "${CMAKE_CFG_INTDIR}/")
     78   else()
     79    set(configuration_path "")
     80   endif()
     81   if(NOT MSVC)
     82     set(${lib_name} "${configuration_path}lib${tgt_name}.a" PARENT_SCOPE)
     83   else()
     84     set(${lib_name} "${configuration_path}${tgt_name}.lib" PARENT_SCOPE)
     85   endif()
     86 endfunction()
     87 
     88 # Interception unit tests testsuite.
     89 add_custom_target(InterceptionUnitTests)
     90 set_target_properties(InterceptionUnitTests PROPERTIES
     91   FOLDER "Compiler-RT Tests")
     92 
     93 # Adds interception tests for architecture.
     94 macro(add_interception_tests_for_arch arch)
     95   get_target_flags_for_arch(${arch} TARGET_FLAGS)
     96   set(INTERCEPTION_TEST_SOURCES ${INTERCEPTION_UNITTESTS}
     97                              ${COMPILER_RT_GTEST_SOURCE})
     98   set(INTERCEPTION_TEST_COMPILE_DEPS ${INTERCEPTION_TEST_HEADERS})
     99   if(NOT COMPILER_RT_STANDALONE_BUILD)
    100     list(APPEND INTERCEPTION_TEST_COMPILE_DEPS gtest)
    101   endif()
    102   set(INTERCEPTION_TEST_OBJECTS)
    103   foreach(source ${INTERCEPTION_TEST_SOURCES})
    104     get_filename_component(basename ${source} NAME)
    105     if(CMAKE_CONFIGURATION_TYPES)
    106       set(output_obj "${CMAKE_CFG_INTDIR}/${basename}.${arch}.o")
    107     else()
    108       set(output_obj "${basename}.${arch}.o")
    109     endif()
    110     clang_compile(${output_obj} ${source}
    111                   CFLAGS ${INTERCEPTION_TEST_CFLAGS_COMMON} ${TARGET_FLAGS}
    112                   DEPS ${INTERCEPTION_TEST_COMPILE_DEPS})
    113     list(APPEND INTERCEPTION_TEST_OBJECTS ${output_obj})
    114   endforeach()
    115   get_interception_lib_for_arch(${arch} INTERCEPTION_COMMON_LIB
    116                                 INTERCEPTION_COMMON_LIB_NAME)
    117   # Add unittest target.
    118   set(INTERCEPTION_TEST_NAME "Interception-${arch}-Test")
    119   add_compiler_rt_test(InterceptionUnitTests ${INTERCEPTION_TEST_NAME}
    120                        OBJECTS ${INTERCEPTION_TEST_OBJECTS}
    121                                ${INTERCEPTION_COMMON_LIB_NAME}
    122                        DEPS ${INTERCEPTION_TEST_OBJECTS} ${INTERCEPTION_COMMON_LIB}
    123                        LINK_FLAGS ${INTERCEPTION_TEST_LINK_FLAGS_COMMON}
    124                                   ${TARGET_FLAGS})
    125 endmacro()
    126 
    127 if(COMPILER_RT_CAN_EXECUTE_TESTS AND NOT ANDROID AND NOT APPLE)
    128   # We use just-built clang to build interception unittests, so we must
    129   # be sure that produced binaries would work.
    130   if(APPLE)
    131     add_interceptor_lib("RTInterception.test.osx"
    132                         $<TARGET_OBJECTS:RTInterception.osx>)
    133   else()
    134     foreach(arch ${INTERCEPTION_UNITTEST_SUPPORTED_ARCH})
    135       add_interceptor_lib("RTInterception.test.${arch}"
    136                           $<TARGET_OBJECTS:RTInterception.${arch}>)
    137     endforeach()
    138   endif()
    139   foreach(arch ${INTERCEPTION_UNITTEST_SUPPORTED_ARCH})
    140     add_interception_tests_for_arch(${arch})
    141   endforeach()
    142 endif()
    143