Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / xray / tests / CMakeLists.txt
blob732f982c932f04f16c59f9f8b2f2287989454be9
1 include_directories(..)
3 add_custom_target(XRayUnitTests)
4 set_target_properties(XRayUnitTests PROPERTIES FOLDER "XRay unittests")
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()
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   )
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()
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()
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()
49 set(XRAY_TEST_ARCH ${XRAY_SUPPORTED_ARCH})
50 set(XRAY_UNITTEST_LINK_FLAGS
51   ${COMPILER_RT_UNITTEST_LINK_FLAGS}
52   ${CMAKE_THREAD_LIBS_INIT}
53   ${COMPILER_RT_UNWINDER_LINK_LIBS}
54   ${COMPILER_RT_CXX_LINK_LIBS})
56 if (NOT APPLE)
57   # Needed by LLVMSupport.
58   append_list_if(
59     LLVM_ENABLE_TERMINFO
60     -l${COMPILER_RT_TERMINFO_LIB} XRAY_UNITTEST_LINK_FLAGS)
62   # We add the library directories one at a time in our CFLAGS.
63   foreach (DIR ${LLVM_LIBRARY_DIR})
64     list(APPEND XRAY_UNITTEST_LINK_FLAGS -L${DIR})
65   endforeach()
67   if (COMPILER_RT_STANDALONE_BUILD)
68     if (COMPILER_RT_HAS_LLVMXRAY OR COMPILER_RT_HAS_LLVMTESTINGSUPPORT)
69       if (LLVM_LINK_LLVM_DYLIB)
70         list(APPEND XRAY_UNITTEST_LINK_FLAGS -lLLVM)
71       endif()
72     else()
73       if (COMPILER_RT_HAS_LLVMXRAY)
74         list(APPEND XRAY_UNITTEST_LINK_FLAGS -lLLVMXRay)
75       endif()
76       if (COMPILER_RT_HAS_TESTINGSUPPORT)
77         list(APPEND XRAY_UNITTEST_LINK_FLAGS -lLLVMTestingSupport)
78       endif()
79       list(APPEND XRAY_UNITTEST_LINK_FLAGS -lLLVMSupport -lLLVMDemangle)
80     endif()
81   else()
82     # We also add the actual libraries to link as dependencies.
83     list(APPEND XRAY_UNITTEST_LINK_FLAGS -lLLVMXRay -lLLVMSupport -lLLVMDemangle -lLLVMTestingSupport)
84   endif()
86   append_list_if(COMPILER_RT_HAS_LIBM -lm XRAY_UNITTEST_LINK_FLAGS)
87   append_list_if(COMPILER_RT_HAS_LIBRT -lrt XRAY_UNITTEST_LINK_FLAGS)
88   append_list_if(COMPILER_RT_HAS_LIBDL -ldl XRAY_UNITTEST_LINK_FLAGS)
89   append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread XRAY_UNITTEST_LINK_FLAGS)
90   append_list_if(COMPILER_RT_HAS_LIBEXECINFO -lexecinfo XRAY_UNITTEST_LINK_FLAGS)
91 endif()
93 macro(add_xray_unittest testname)
94   cmake_parse_arguments(TEST "" "" "SOURCES;HEADERS" ${ARGN})
95   if(UNIX AND NOT APPLE)
96     set(CMAKE_DL_LIBS_INIT "")
97     foreach(arch ${XRAY_TEST_ARCH})
98       set(TEST_OBJECTS)
99       get_xray_lib_for_arch(${arch} XRAY_RUNTIME_LIBS)
100       generate_compiler_rt_tests(TEST_OBJECTS
101         XRayUnitTests "${testname}-${arch}-Test" "${arch}"
102         SOURCES ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}
103                 ${COMPILER_RT_GMOCK_SOURCE}
105         # Note that any change in the implementations will cause all the unit
106         # tests to be re-built. This is by design, but may be cumbersome during
107         # the build/test cycle.
108         COMPILE_DEPS ${TEST_SOURCES} ${COMPILER_RT_GTEST_SOURCE}
109         ${XRAY_HEADERS} ${XRAY_ALL_SOURCE_FILES_ABS_PATHS}
110         "test_helpers.h"
111         RUNTIME "${XRAY_RUNTIME_LIBS}"
112         DEPS llvm_gtest xray llvm-xray LLVMXRay LLVMTestingSupport
113         CFLAGS ${XRAY_UNITTEST_CFLAGS}
114         LINK_FLAGS ${TARGET_LINK_FLAGS} ${XRAY_UNITTEST_LINK_FLAGS}
115         )
116       set_target_properties(XRayUnitTests
117         PROPERTIES
118         RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
119     endforeach()
120   endif()
121 endmacro()
123 if(COMPILER_RT_CAN_EXECUTE_TESTS)
124   if (APPLE)
125     add_xray_lib("RTXRay.test.osx"
126       $<TARGET_OBJECTS:RTXray.osx>
127       $<TARGET_OBJECTS:RTXrayFDR.osx>
128       $<TARGET_OBJECTS:RTXrayPROFILING.osx>
129       $<TARGET_OBJECTS:RTSanitizerCommon.osx>
130       $<TARGET_OBJECTS:RTSanitizerCommonLibc.osx>)
131   else()
132   foreach(arch ${XRAY_SUPPORTED_ARCH})
133     add_xray_lib("RTXRay.test.${arch}"
134       $<TARGET_OBJECTS:RTXray.${arch}>
135       $<TARGET_OBJECTS:RTXrayFDR.${arch}>
136       $<TARGET_OBJECTS:RTXrayPROFILING.${arch}>
137       $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
138       $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
139   endforeach()
140   endif()
141   add_subdirectory(unit)
142 endif()