Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / msan / tests / CMakeLists.txt
blob6ef63ff8216638e731f569556275b8d9fa3afc31
1 include(CheckCXXCompilerFlag)
2 include(CompilerRTCompile)
3 include(CompilerRTLink)
5 include_directories(..)
6 include_directories(../..)
8 set(MSAN_LIBCXX_CFLAGS
9   -fsanitize=memory
10   -fsanitize-memory-track-origins
11   -fno-sanitize-memory-param-retval  # unittests test mostly this mode.
12   -Wno-pedantic
13   -Xclang -fdepfile-entry=${COMPILER_RT_OUTPUT_DIR}/share/msan_ignorelist.txt
14   )
16 # Unittest sources and build flags.
17 set(MSAN_UNITTEST_SOURCES
18   msan_test.cpp
19   msan_test_main.cpp
20   )
21 set(MSAN_LOADABLE_SOURCE
22   msan_loadable.cpp
23   )
24 set(MSAN_UNITTEST_HEADERS
25   msan_test_config.h
26   ../../../include/sanitizer/msan_interface.h
27   )
28 set(MSAN_UNITTEST_COMMON_CFLAGS
29   -nostdinc++
30   ${COMPILER_RT_UNITTEST_CFLAGS}
31   ${COMPILER_RT_GTEST_CFLAGS}
32   -I${COMPILER_RT_SOURCE_DIR}/include
33   -I${COMPILER_RT_SOURCE_DIR}/lib
34   -I${COMPILER_RT_SOURCE_DIR}/lib/msan
35   -g
36   -O2
37   -fno-omit-frame-pointer
38   -mno-omit-leaf-frame-pointer
39   -Wno-deprecated-declarations
40   -Wno-unused-variable
41   -Wno-zero-length-array
42   -Wno-uninitialized
43   -Werror=sign-compare
44   -Wno-gnu-zero-variadic-macro-arguments
45   -fno-sanitize-memory-param-retval  # unittests test mostly this mode.
47 # Remove -stdlib= which is unused when passing -nostdinc++.
48 string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
50 set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
51   ${MSAN_UNITTEST_COMMON_CFLAGS}
52   -fsanitize=memory
53   -fsanitize-memory-track-origins
54   -mllvm -msan-keep-going=1
56 set(MSAN_UNITTEST_LINK_FLAGS
57   -nostdlib++
58   ${COMPILER_RT_UNITTEST_LINK_FLAGS}
59   ${COMPILER_RT_UNWINDER_LINK_LIBS}
60   -fsanitize=memory
61   # Don't need -stdlib=libc++ because we explicitly list libc++.a in the linker
62   # inputs.
65 append_list_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)
67 macro(msan_compile obj_list source arch kind cflags)
68   sanitizer_test_compile(
69     ${obj_list} ${source} ${arch}
70     KIND ${kind}
71     COMPILE_DEPS ${MSAN_UNITTEST_HEADERS}
72     DEPS llvm_gtest msan
73     CFLAGS -isystem ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch}/include/c++/v1
74            ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS} ${cflags}
75   )
76 endmacro()
78 macro(msan_link_shared so_list so_name arch kind)
79   cmake_parse_arguments(SOURCE "" "" "OBJECTS;LINK_FLAGS;DEPS" ${ARGN})
80   set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}${kind}.so")
81   get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
82   list(APPEND SOURCE_DEPS msan)
83   clang_link_shared(${output_so}
84                 OBJECTS ${SOURCE_OBJECTS}
85                 LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS} ${TARGET_LINK_FLAGS} ${SOURCE_LINK_FLAGS}
86                 DEPS ${SOURCE_DEPS})
87   list(APPEND ${so_list} ${output_so})
88 endmacro()
90 # Main MemorySanitizer unit tests.
91 add_custom_target(MsanUnitTests)
92 set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
94 # Adds MSan unit tests and benchmarks for architecture.
95 macro(add_msan_tests_for_arch arch kind cflags)
96   # Build gtest instrumented with MSan.
97   set(MSAN_INST_GTEST)
98   msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} "${kind}"
99                                "${cflags}")
101   # Instrumented tests.
102   set(MSAN_INST_TEST_OBJECTS)
103   foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
104     msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch} "${kind}" "${cflags}")
105   endforeach(SOURCE)
107   # Instrumented loadable module objects.
108   set(MSAN_INST_LOADABLE_OBJECTS)
109   msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch} "${kind}"
110                "-fPIC;${cflags}")
112   # Instrumented loadable library tests.
113   set(MSAN_LOADABLE_SO)
114   msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch} "${kind}"
115                    OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
116                    DEPS ${MSAN_INST_LOADABLE_OBJECTS})
118   set(MSAN_TEST_OBJECTS ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST})
119   set(MSAN_TEST_DEPS ${MSAN_TEST_OBJECTS} libcxx_msan_${arch}-build
120                      ${MSAN_LOADABLE_SO}
121                      "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a")
122   if(NOT COMPILER_RT_STANDALONE_BUILD)
123     list(APPEND MSAN_TEST_DEPS msan)
124   endif()
125   get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
126   add_compiler_rt_test(MsanUnitTests "Msan-${arch}${kind}-Test" ${arch}
127     OBJECTS ${MSAN_TEST_OBJECTS} "${MSAN_LIBCXX_DIR}/libc++.a" "${MSAN_LIBCXX_DIR}/libc++abi.a"
128     DEPS ${MSAN_TEST_DEPS}
129     LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
130                ${TARGET_LINK_FLAGS})
131 endmacro()
133 # We should only build MSan unit tests if we can build instrumented libcxx.
134 if(COMPILER_RT_CAN_EXECUTE_TESTS AND
135    COMPILER_RT_LIBCXX_PATH AND
136    COMPILER_RT_LIBCXXABI_PATH)
137   foreach(arch ${MSAN_SUPPORTED_ARCH})
138     get_target_flags_for_arch(${arch} TARGET_CFLAGS)
139     set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/../libcxx_msan_${arch})
140     add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}
141       DEPS ${MSAN_RUNTIME_LIBRARIES}
142       CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS}
143       USE_TOOLCHAIN)
144     set(MSAN_LIBCXX_DIR ${LIBCXX_PREFIX}/lib/)
146     add_msan_tests_for_arch(${arch} "" "")
147     add_msan_tests_for_arch(${arch} "-with-call"
148                             "-mllvm;-msan-instrumentation-with-call-threshold=0")
149   endforeach()
150 endif()