CMakeLists.txt revision e236dbb5e558b174609d2d13e80685d488c129d8
1include(CheckCXXCompilerFlag)
2include(CompilerRTCompile)
3include(CompilerRTLink)
4
5include_directories(..)
6include_directories(../..)
7
8# Instrumented libcxx sources and build flags.
9file(GLOB MSAN_LIBCXX_SOURCES ${MSAN_LIBCXX_PATH}/src/*.cpp)
10set(MSAN_LIBCXX_CFLAGS
11  -I${MSAN_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)
24set(MSAN_LIBCXX_LINK_FLAGS
25  -nodefaultlibs
26  -lpthread
27  -lrt
28  -lc
29  -lstdc++
30  -fsanitize=memory)
31
32# Unittest sources and build flags.
33set(MSAN_UNITTEST_SOURCES msan_test.cc msan_test_main.cc)
34set(MSAN_LOADABLE_SOURCE msan_loadable.cc)
35set(MSAN_UNITTEST_HEADERS
36  msan_test_config.h
37  msandr_test_so.h
38  ../../../include/sanitizer/msan_interface.h
39)
40set(MSANDR_UNITTEST_SOURCE msandr_test_so.cc)
41set(MSAN_UNITTEST_COMMON_CFLAGS
42  -I${MSAN_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++0x
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
55)
56set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS
57  ${MSAN_UNITTEST_COMMON_CFLAGS}
58  -fsanitize=memory
59  -fsanitize-memory-track-origins
60  -mllvm -msan-keep-going=1
61)
62set(MSAN_UNITTEST_LINK_FLAGS
63  -fsanitize=memory
64  -ldl
65  # FIXME: we build libcxx without cxxabi and need libstdc++ to provide it.
66  -lstdc++
67)
68set(MSAN_LOADABLE_LINK_FLAGS
69  -fsanitize=memory
70  -shared
71)
72
73# Compile source for the given architecture, using compiler
74# options in ${ARGN}, and add it to the object list.
75macro(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  clang_compile(${output_obj} ${source}
80                CFLAGS ${ARGN} ${TARGET_CFLAGS}
81                DEPS gtest ${MSAN_RUNTIME_LIBRARIES} ${MSAN_UNITTEST_HEADERS})
82  list(APPEND ${obj_list} ${output_obj})
83endmacro()
84
85macro(msan_link_shared so_list so_name arch)
86  parse_arguments(SOURCE "OBJECTS;LINKFLAGS;DEPS" "" ${ARGN})
87  set(output_so "${CMAKE_CURRENT_BINARY_DIR}/${so_name}.${arch}.so")
88  get_target_flags_for_arch(${arch} TARGET_LINKFLAGS)
89  clang_link_shared(${output_so}
90                OBJECTS ${SOURCE_OBJECTS}
91                LINKFLAGS ${TARGET_LINKFLAGS} ${SOURCE_LINKFLAGS}
92                DEPS ${SOURCE_DEPS})
93  list(APPEND ${so_list} ${output_so})
94endmacro()
95
96# Link MSan unit test for a given architecture from a set
97# of objects in ${ARGN}.
98macro(add_msan_test test_suite test_name arch)
99  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
100  add_compiler_rt_test(${test_suite} ${test_name}
101                       OBJECTS ${ARGN}
102                       DEPS ${MSAN_RUNTIME_LIBRARIES} ${ARGN}
103                            ${MSAN_LOADABLE_SO}
104                       LINK_FLAGS ${MSAN_UNITTEST_LINK_FLAGS}
105                                  ${TARGET_LINK_FLAGS}
106                                  "-Wl,-rpath=${CMAKE_CURRENT_BINARY_DIR}")
107endmacro()
108
109# Main MemorySanitizer unit tests.
110add_custom_target(MsanUnitTests)
111set_target_properties(MsanUnitTests PROPERTIES FOLDER "MSan unit tests")
112
113# Adds MSan unit tests and benchmarks for architecture.
114macro(add_msan_tests_for_arch arch)
115  # Build gtest instrumented with MSan.
116  set(MSAN_INST_GTEST)
117  msan_compile(MSAN_INST_GTEST ${COMPILER_RT_GTEST_SOURCE} ${arch} 
118                               ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
119
120  # Build libcxx instrumented with MSan.
121  set(MSAN_INST_LIBCXX_OBJECTS)
122  foreach(SOURCE ${MSAN_LIBCXX_SOURCES})
123    msan_compile(MSAN_INST_LIBCXX_OBJECTS ${SOURCE} ${arch} 
124                 ${MSAN_LIBCXX_CFLAGS})
125  endforeach(SOURCE)
126
127  set(MSAN_INST_LIBCXX)
128  msan_link_shared(MSAN_INST_LIBCXX "libcxx" ${arch}
129                   OBJECTS ${MSAN_INST_LIBCXX_OBJECTS}
130                   LINKFLAGS ${MSAN_LIBCXX_LINK_FLAGS}
131                   DEPS ${MSAN_INST_LIBCXX_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
132
133  # Instrumented tests.
134  set(MSAN_INST_TEST_OBJECTS)
135  foreach (SOURCE ${MSAN_UNITTEST_SOURCES})
136    msan_compile(MSAN_INST_TEST_OBJECTS ${SOURCE} ${arch}
137                 ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
138  endforeach(SOURCE)
139
140  # Instrumented loadable module objects.
141  set(MSAN_INST_LOADABLE_OBJECTS)
142  msan_compile(MSAN_INST_LOADABLE_OBJECTS ${MSAN_LOADABLE_SOURCE} ${arch}
143               ${MSAN_UNITTEST_INSTRUMENTED_CFLAGS})
144
145  # Uninstrumented shared object for MSanDR tests.
146  set(MSANDR_TEST_OBJECTS)
147  msan_compile(MSANDR_TEST_OBJECTS ${MSANDR_UNITTEST_SOURCE} ${arch}
148               ${MSAN_UNITTEST_COMMON_CFLAGS})
149
150  # Instrumented loadable library tests.
151  set(MSAN_LOADABLE_SO)
152  msan_link_shared(MSAN_LOADABLE_SO "libmsan_loadable" ${arch}
153                   OBJECTS ${MSAN_INST_LOADABLE_OBJECTS}
154                   DEPS ${MSAN_INST_LOADABLE_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
155
156  # Uninstrumented shared library tests.
157  set(MSANDR_TEST_SO)
158  msan_link_shared(MSANDR_TEST_SO "libmsandr_test" ${arch}
159                   OBJECTS ${MSANDR_TEST_OBJECTS}
160                   DEPS ${MSANDR_TEST_OBJECTS} ${MSAN_RUNTIME_LIBRARIES})
161
162  # Link everything together.
163  add_msan_test(MsanUnitTests "Msan-${arch}-Test" ${arch}
164                ${MSAN_INST_TEST_OBJECTS} ${MSAN_INST_GTEST}
165                ${MSAN_INST_LIBCXX} ${MSANDR_TEST_SO})
166endmacro()
167
168if(COMPILER_RT_CAN_EXECUTE_TESTS AND MSAN_CAN_INSTRUMENT_LIBCXX)
169  if(CAN_TARGET_x86_64)
170    add_msan_tests_for_arch(x86_64)
171  endif()
172endif()
173