[CMake] Build sanitizer unit tests with -std=c++11
[blocksruntime.git] / lib / msan / tests / CMakeLists.txt
blob84f789c2f9e5ec364cd9762554b3a27291fa4cad
1 include(CheckCXXCompilerFlag)
2 include(CompilerRTCompile)
3 include(CompilerRTLink)
5 include_directories(..)
6 include_directories(../..)
8 # Instrumented libcxx sources and build flags.
9 file(GLOB MSAN_LIBCXX_SOURCES ${COMPILER_RT_LIBCXX_PATH}/src/*.cpp)
10 set(MSAN_LIBCXX_CFLAGS
11   -I${COMPILER_RT_LIBCXX_PATH}/include
12   -fsanitize=memory
13   -fsanitize-memory-track-origins
14   -fPIC
15   -Wno-\#warnings
16   -g
17   -O2
18   -std=c++0x
19   -fstrict-aliasing
20   -fno-exceptions
21   -nostdinc++
22   -fno-omit-frame-pointer
23   -mno-omit-leaf-frame-pointer)
24 set(MSAN_LIBCXX_LINK_FLAGS
25   -nodefaultlibs
26   -lrt
27   -lc
28   -lstdc++
29   -fsanitize=memory)
30 append_if(COMPILER_RT_HAS_LIBPTHREAD -lpthread MSAN_LIBCXX_LINK_FLAGS)
32 # Unittest sources and build flags.
33 set(MSAN_UNITTEST_SOURCES msan_test.cc msan_test_main.cc)
34 set(MSAN_LOADABLE_SOURCE msan_loadable.cc)
35 set(MSAN_UNITTEST_HEADERS
36   msan_test_config.h
37   msandr_test_so.h
38   ../../../include/sanitizer/msan_interface.h
40 set(MSANDR_UNITTEST_SOURCE msandr_test_so.cc)
41 set(MSAN_UNITTEST_COMMON_CFLAGS
42   -I${COMPILER_RT_LIBCXX_PATH}/include
43   ${COMPILER_RT_GTEST_INCLUDE_CFLAGS}
44   -I${COMPILER_RT_SOURCE_DIR}/include
45   -I${COMPILER_RT_SOURCE_DIR}/lib
46   -I${COMPILER_RT_SOURCE_DIR}/lib/msan
47   -std=c++11
48   -stdlib=libc++
49   -g
50   -O2
51   -fno-exceptions
52   -fno-omit-frame-pointer
53   -mno-omit-leaf-frame-pointer
54   -Wno-deprecated-declarations
56 set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
57   ${MSAN_UNITTEST_COMMON_CFLAGS}
58   -fsanitize=memory
59   -fsanitize-memory-track-origins
60   -mllvm -msan-keep-going=1
62 set(MSAN_UNITTEST_LINK_FLAGS
63   -fsanitize=memory
64   # FIXME: we build libcxx without cxxabi and need libstdc++ to provide it.
65   -lstdc++
67 append_if(COMPILER_RT_HAS_LIBDL -ldl MSAN_UNITTEST_LINK_FLAGS)
68 set(MSAN_LOADABLE_LINK_FLAGS
69   -fsanitize=memory
70   -shared
73 # Compile source for the given architecture, using compiler
74 # options in ${ARGN}, and add it to the object list.
75 macro(msan_compile obj_list source arch)
76   get_filename_component(basename ${source} NAME)
77   set(output_obj "${basename}.${arch}.o")
78   get_target_flags_for_arch(${arch} TARGET_CFLAGS)
79   set(COMPILE_DEPS ${MSAN_UNITTEST_HEADERS})
80   if(NOT COMPILER_RT_STANDALONE_BUILD)
81     list(APPEND COMPILE_DEPS gtest msan)
82   endif()
83   clang_compile(${output_obj} ${source}
84                 CFLAGS ${ARGN} ${TARGET_CFLAGS}
85                 DEPS ${COMPILE_DEPS})
86   list(APPEND ${obj_list} ${output_obj})
87 endmacro()
89 macro(msan_link_shared so_list so_name arch)
90   parse_arguments(SOURCE "OBJECTS;LINKFLAGS;DEPS" "" ${ARGN})
91   set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}.so")
92   get_target_flags_for_arch(${arch} TARGET_LINKFLAGS)
93   if(NOT COMPILER_RT_STANDALONE_BUILD)
94     list(APPEND SOURCE_DEPS msan)
95   endif()
96   clang_link_shared(${output_so}
97                 OBJECTS ${SOURCE_OBJECTS}
98                 LINKFLAGS ${TARGET_LINKFLAGS} ${SOURCE_LINKFLAGS}
99                 DEPS ${SOURCE_DEPS})
100   list(APPEND ${so_list} ${output_so})
101 endmacro()
103 # Link MSan unit test for a given architecture from a set
104 # of objects in ${ARGN}.
105 macro(add_msan_test test_suite test_name arch)
106   get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
107   set(TEST_DEPS ${ARGN} ${MSAN_LOADABLE_SO})
108   if(NOT COMPILER_RT_STANDALONE_BUILD)
109     list(APPEND TEST_DEPS msan)
110   endif()
111   add_compiler_rt_test(${test_suite} ${test_name}
112                        OBJECTS ${ARGN}
113                        DEPS ${TEST_DEPS}
114                        LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
115                                   ${TARGET_LINK_FLAGS}
116                                   "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}")
117 endmacro()
119 # Main MemorySanitizer unit tests.
120 add_custom_target(MsanUnitTests)
121 set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
123 # Adds MSan unit tests and benchmarks for architecture.
124 macro(add_msan_tests_for_arch arch)
125   # Build gtest instrumented with MSan.
126   set(MSAN_INST_GTEST)
127   msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} 
128                                ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
130   # Build libcxx instrumented with MSan.
131   set(MSAN_INST_LIBCXX_OBJECTS)
132   foreach(SOURCE ${MSAN_LIBCXX_SOURCES})
133     msan_compile(MSAN_INST_LIBCXX_OBJECTS ${SOURCE} ${arch} 
134                  ${MSAN_LIBCXX_CFLAGS})
135   endforeach(SOURCE)
137   set(MSAN_INST_LIBCXX)
138   msan_link_shared(MSAN_INST_LIBCXX "libcxx" ${arch}
139                    OBJECTS ${MSAN_INST_LIBCXX_OBJECTS}
140                    LINKFLAGS ${MSAN_LIBCXX_LINK_FLAGS}
141                    DEPS ${MSAN_INST_LIBCXX_OBJECTS})
143   # Instrumented tests.
144   set(MSAN_INST_TEST_OBJECTS)
145   foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
146     msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch}
147                  ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
148   endforeach(SOURCE)
150   # Instrumented loadable module objects.
151   set(MSAN_INST_LOADABLE_OBJECTS)
152   msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch}
153                ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
155   # Uninstrumented shared object for MSanDR tests.
156   set(MSANDR_TEST_OBJECTS)
157   msan_compile(MSANDR_TEST_OBJECTS ${MSANDR_UNITTEST_SOURCE} ${arch}
158                ${MSAN_UNITTEST_COMMON_CFLAGS})
160   # Instrumented loadable library tests.
161   set(MSAN_LOADABLE_SO)
162   msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch}
163                    OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
164                    DEPS ${MSAN_INST_LOADABLE_OBJECTS})
166   # Uninstrumented shared library tests.
167   set(MSANDR_TEST_SO)
168   msan_link_shared(MSANDR_TEST_SO "libmsandr_test" ${arch}
169                    OBJECTS ${MSANDR_TEST_OBJECTS}
170                    DEPS ${MSANDR_TEST_OBJECTS})
172   # Link everything together.
173   add_msan_test(MsanUnitTests "Msan-${arch}-Test" ${arch}
174                 ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST}
175                 ${MSAN_INST_LIBCXX} ${MSANDR_TEST_SO})
176 endmacro()
178 # We should only build MSan unit tests if we can build instrumented libcxx.
179 if(COMPILER_RT_CAN_EXECUTE_TESTS AND COMPILER_RT_HAS_LIBCXX_SOURCES)
180   if(CAN_TARGET_x86_64)
181     add_msan_tests_for_arch(x86_64)
182   endif()
183 endif()