Home | History | Annotate | Line # | Download | only in tests
      1 include_directories(..)
      2 
      3 add_custom_target(XRayUnitTests)
      4 set_target_properties(XRayUnitTests PROPERTIES FOLDER "XRay unittests")
      5 
      6 # Sanity check XRAY_ALL_SOURCE_FILES_ABS_PATHS
      7 list(LENGTH XRAY_ALL_SOURCE_FILES_ABS_PATHS XASFAP_LENGTH)
      8 if (${XASFAP_LENGTH} EQUAL 0)
      9   message(FATAL_ERROR "XRAY_ALL_SOURCE_FILES_ABS_PATHS cannot be empty")
     10 endif()
     11 unset(XASFAP_LENGTH)
     12 foreach (src_file ${XRAY_ALL_SOURCE_FILES_ABS_PATHS})
     13   if (NOT EXISTS "${src_file}")
     14     message(FATAL_ERROR "Source file \"${src_file}\" does not exist")
     15   endif()
     16 endforeach()
     17 
     18 set(XRAY_UNITTEST_CFLAGS
     19   ${XRAY_CFLAGS}
     20   ${COMPILER_RT_UNITTEST_CFLAGS}
     21   ${COMPILER_RT_GTEST_CFLAGS}
     22   ${COMPILER_RT_GMOCK_CFLAGS}
     23   -I${COMPILER_RT_SOURCE_DIR}/include
     24   -I${COMPILER_RT_SOURCE_DIR}/lib/xray
     25   -I${COMPILER_RT_SOURCE_DIR}/lib
     26   )
     27 
     28 # We add the include directories one at a time in our CFLAGS.
     29 foreach (DIR ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
     30   list(APPEND XRAY_UNITTEST_CFLAGS -I${DIR})
     31 endforeach()
     32 
     33 function(add_xray_lib library)
     34   add_library(${library} STATIC ${ARGN})
     35   set_target_properties(${library} PROPERTIES
     36     ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
     37     FOLDER "Compiler-RT Runtime tests")
     38 endfunction()
     39 
     40 function(get_xray_lib_for_arch arch lib)
     41   if(APPLE)
     42     set(tgt_name "RTXRay.test.osx")
     43   else()
     44     set(tgt_name "RTXRay.test.${arch}")
     45   endif()
     46   set(${lib} "${tgt_name}" PARENT_SCOPE)
     47 endfunction()
     48 
     49 set(XRAY_TEST_ARCH ${XRAY_SUPPORTED_ARCH})
     50 set(XRAY_UNITTEST_LINK_FLAGS
     51   ${CMAKE_THREAD_LIBS_INIT}
     52   -l${SANITIZER_CXX_ABI_LIBRARY})
     53 
     54 if (NOT APPLE)
     55   # Needed by LLVMSupport.
     56   append_list_if(
     57     COMPILER_RT_HAS_TERMINFO
     58     -l${COMPILER_RT_TERMINFO_LIB} XRAY_UNITTEST_LINK_FLAGS)
     59 
     60   if (COMPILER_RT_STANDALONE_BUILD)
     61     append_list_if(COMPILER_RT_HAS_LLVMXRAY ${LLVM_XRAY_LDFLAGS} XRAY_UNITTEST_LINK_FLAGS)
     62     append_list_if(COMPILER_RT_HAS_LLVMXRAY ${LLVM_XRAY_LIBLIST} XRAY_UNITTEST_LINK_FLAGS)
     63     append_list_if(COMPILER_RT_HAS_LLVMTESTINGSUPPORT
     64       ${LLVM_TESTINGSUPPORT_LDFLAGS} XRAY_UNITTEST_LINK_FLAGS)
     65     append_list_if(COMPILER_RT_HAS_LLVMTESTINGSUPPORT
     66       ${LLVM_TESTINGSUPPORT_LIBLIST} XRAY_UNITTEST_LINK_FLAGS)
     67   else()
     68     # We add the library directories one at a time in our CFLAGS.
     69     foreach (DIR ${LLVM_LIBRARY_DIR})
     70       list(APPEND XRAY_UNITTEST_LINK_FLAGS -L${DIR})
     71     endforeach()
     72 
     73     # We also add the actual libraries to link as dependencies.
     74     list(APPEND XRAY_UNITTEST_LINK_FLAGS -lLLVMXRay -lLLVMSupport -lLLVMTestingSupport)
     75   endif()
     76 
     77   append_list_if(COMPILER_RT_HAS_LIBM -lm XRAY_UNITTEST_LINK_FLAGS)
     78   append_list_if(COMPILER_RT_HAS_LIBRT -lrt XRAY_UNITTEST_LINK_FLAGS)
     79   append_list_if(COMPILER_RT_HAS_LIBDL -ldl XRAY_UNITTEST_LINK_FLAGS)
     80   append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread XRAY_UNITTEST_LINK_FLAGS)
     81 endif()
     82 
     83 macro(add_xray_unittest testname)
     84   cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
     85   if(UNIX AND NOT APPLE)
     86     set(CMAKE_DL_LIBS_INIT "")
     87     foreach(arch ${XRAY_TEST_ARCH})
     88       set(TEST_OBJECTS)
     89       get_xray_lib_for_arch(${arch} XRAY_RUNTIME_LIBS)
     90       generate_compiler_rt_tests(TEST_OBJECTS
     91         XRayUnitTests "${testname}-${arch}-Test" "${arch}"
     92         SOURCES ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}
     93                 ${COMPILER_RT_GMOCK_SOURCE}
     94 
     95         # Note that any change in the implementations will cause all the unit
     96         # tests to be re-built. This is by design, but may be cumbersome during
     97         # the build/test cycle.
     98         COMPILE_DEPS ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}
     99         ${XRAY_HEADERS} ${XRAY_ALL_SOURCE_FILES_ABS_PATHS}
    100         "test_helpers.h"
    101         RUNTIME "${XRAY_RUNTIME_LIBS}"
    102         DEPS gtest xray llvm-xray LLVMXRay LLVMTestingSupport
    103         CFLAGS ${XRAY_UNITTEST_CFLAGS}
    104         LINK_FLAGS ${TARGET_LINK_FLAGS} ${XRAY_UNITTEST_LINK_FLAGS}
    105         )
    106       set_target_properties(XRayUnitTests
    107         PROPERTIES
    108         RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    109     endforeach()
    110   endif()
    111 endmacro()
    112 
    113 if(COMPILER_RT_CAN_EXECUTE_TESTS)
    114   if (APPLE)
    115     add_xray_lib("RTXRay.test.osx"
    116       $<TARGET_OBJECTS:RTXray.osx>
    117       $<TARGET_OBJECTS:RTXrayFDR.osx>
    118       $<TARGET_OBJECTS:RTXrayPROFILING.osx>
    119       $<TARGET_OBJECTS:RTSanitizerCommon.osx>
    120       $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>)
    121   else()
    122   foreach(arch ${XRAY_SUPPORTED_ARCH})
    123     add_xray_lib("RTXRay.test.${arch}"
    124       $<TARGET_OBJECTS:RTXray.${arch}>
    125       $<TARGET_OBJECTS:RTXrayFDR.${arch}>
    126       $<TARGET_OBJECTS:RTXrayPROFILING.${arch}>
    127       $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
    128       $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
    129   endforeach()
    130   endif()
    131   add_subdirectory(unit)
    132 endif()
    133