]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/asan/CMakeLists.txt
New upstream version 1.19.0+dfsg1
[rustc.git] / src / compiler-rt / lib / asan / CMakeLists.txt
1 # Build for the AddressSanitizer runtime support library.
2
3 set(ASAN_SOURCES
4 asan_allocator.cc
5 asan_activation.cc
6 asan_debugging.cc
7 asan_descriptions.cc
8 asan_errors.cc
9 asan_fake_stack.cc
10 asan_flags.cc
11 asan_globals.cc
12 asan_globals_win.cc
13 asan_interceptors.cc
14 asan_linux.cc
15 asan_mac.cc
16 asan_malloc_linux.cc
17 asan_malloc_mac.cc
18 asan_malloc_win.cc
19 asan_memory_profile.cc
20 asan_poisoning.cc
21 asan_posix.cc
22 asan_report.cc
23 asan_rtl.cc
24 asan_stack.cc
25 asan_stats.cc
26 asan_suppressions.cc
27 asan_thread.cc
28 asan_win.cc)
29
30 set(ASAN_CXX_SOURCES
31 asan_new_delete.cc)
32
33 set(ASAN_PREINIT_SOURCES
34 asan_preinit.cc)
35
36 include_directories(..)
37
38 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
39 append_rtti_flag(OFF ASAN_CFLAGS)
40
41 set(ASAN_DYNAMIC_LINK_FLAGS)
42
43 if(ANDROID)
44 # On Android, -z global does not do what it is documented to do.
45 # On Android, -z global moves the library ahead in the lookup order,
46 # placing it right after the LD_PRELOADs. This is used to compensate for the fact
47 # that Android linker does not look at the dependencies of the main executable
48 # that aren't dependencies of the current DSO when resolving symbols from said DSO.
49 # As a net result, this allows running ASan executables without LD_PRELOAD-ing the
50 # ASan runtime library.
51 # The above is applicable to L MR1 or newer.
52 if (COMPILER_RT_HAS_Z_GLOBAL)
53 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
54 endif()
55 endif()
56
57 set(ASAN_DYNAMIC_DEFINITIONS
58 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
59 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
60
61 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
62 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
63 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
64 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
65
66 append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
67 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
68 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
69 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
70 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
71 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
72 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
73
74 # Compile ASan sources into an object library.
75
76 add_compiler_rt_object_libraries(RTAsan_dynamic
77 OS ${SANITIZER_COMMON_SUPPORTED_OS}
78 ARCHS ${ASAN_SUPPORTED_ARCH}
79 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
80 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
81 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
82
83 if(NOT APPLE)
84 add_compiler_rt_object_libraries(RTAsan
85 ARCHS ${ASAN_SUPPORTED_ARCH}
86 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
87 DEFS ${ASAN_COMMON_DEFINITIONS})
88 add_compiler_rt_object_libraries(RTAsan_cxx
89 ARCHS ${ASAN_SUPPORTED_ARCH}
90 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
91 DEFS ${ASAN_COMMON_DEFINITIONS})
92 add_compiler_rt_object_libraries(RTAsan_preinit
93 ARCHS ${ASAN_SUPPORTED_ARCH}
94 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
95 DEFS ${ASAN_COMMON_DEFINITIONS})
96
97 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
98 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
99 ARCHS ${ASAN_SUPPORTED_ARCH}
100 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
101 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
102 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
103 endif()
104
105 # Build ASan runtimes shipped with Clang.
106 add_compiler_rt_component(asan)
107
108 if(APPLE)
109 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
110 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
111 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
112
113 add_compiler_rt_runtime(clang_rt.asan
114 SHARED
115 OS ${SANITIZER_COMMON_SUPPORTED_OS}
116 ARCHS ${ASAN_SUPPORTED_ARCH}
117 OBJECT_LIBS RTAsan_dynamic
118 RTInterception
119 RTSanitizerCommon
120 RTSanitizerCommonLibc
121 RTLSanCommon
122 RTUbsan
123 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
124 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
125 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
126 PARENT_TARGET asan)
127 else()
128 # Build separate libraries for each target.
129
130 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
131 RTInterception
132 RTSanitizerCommon
133 RTSanitizerCommonLibc
134 RTLSanCommon
135 RTUbsan)
136
137 add_compiler_rt_runtime(clang_rt.asan
138 STATIC
139 ARCHS ${ASAN_SUPPORTED_ARCH}
140 OBJECT_LIBS RTAsan_preinit
141 RTAsan
142 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
143 CFLAGS ${ASAN_CFLAGS}
144 DEFS ${ASAN_COMMON_DEFINITIONS}
145 PARENT_TARGET asan)
146
147 add_compiler_rt_runtime(clang_rt.asan_cxx
148 STATIC
149 ARCHS ${ASAN_SUPPORTED_ARCH}
150 OBJECT_LIBS RTAsan_cxx
151 RTUbsan_cxx
152 CFLAGS ${ASAN_CFLAGS}
153 DEFS ${ASAN_COMMON_DEFINITIONS}
154 PARENT_TARGET asan)
155
156 add_compiler_rt_runtime(clang_rt.asan-preinit
157 STATIC
158 ARCHS ${ASAN_SUPPORTED_ARCH}
159 OBJECT_LIBS RTAsan_preinit
160 CFLAGS ${ASAN_CFLAGS}
161 DEFS ${ASAN_COMMON_DEFINITIONS}
162 PARENT_TARGET asan)
163
164 foreach(arch ${ASAN_SUPPORTED_ARCH})
165 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
166 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
167 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
168 EXTRA asan.syms.extra)
169 set(VERSION_SCRIPT_FLAG
170 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
171 set_source_files_properties(
172 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
173 PROPERTIES
174 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
175 else()
176 set(VERSION_SCRIPT_FLAG)
177 endif()
178
179 add_compiler_rt_runtime(clang_rt.asan
180 SHARED
181 ARCHS ${arch}
182 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
183 RTAsan_dynamic
184 # The only purpose of RTAsan_dynamic_version_script_dummy is to carry
185 # a dependency of the shared runtime on the version script. With CMake
186 # 3.1 or later it can be replaced with a straightforward
187 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
188 RTAsan_dynamic_version_script_dummy
189 RTUbsan_cxx
190 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
191 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
192 ${VERSION_SCRIPT_FLAG}
193 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
194 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
195 PARENT_TARGET asan)
196
197 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
198 add_sanitizer_rt_symbols(clang_rt.asan_cxx
199 ARCHS ${arch})
200 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
201 add_sanitizer_rt_symbols(clang_rt.asan
202 ARCHS ${arch}
203 EXTRA asan.syms.extra)
204 add_dependencies(asan clang_rt.asan-${arch}-symbols)
205 endif()
206
207 if (WIN32)
208 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
209 STATIC
210 ARCHS ${arch}
211 SOURCES asan_win_dll_thunk.cc
212 asan_globals_win.cc
213 $<TARGET_OBJECTS:RTInterception.${arch}>
214 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
215 DEFS ${ASAN_COMMON_DEFINITIONS}
216 PARENT_TARGET asan)
217
218 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DASAN_DYNAMIC_RUNTIME_THUNK")
219 if(MSVC)
220 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
221 elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
222 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
223 endif()
224
225 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
226 STATIC
227 ARCHS ${arch}
228 SOURCES asan_win_dynamic_runtime_thunk.cc
229 asan_globals_win.cc
230 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
231 DEFS ${ASAN_COMMON_DEFINITIONS}
232 PARENT_TARGET asan)
233 endif()
234 endforeach()
235 endif()
236
237 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt asan)
238
239 add_subdirectory(scripts)
240
241 if(COMPILER_RT_INCLUDE_TESTS)
242 add_subdirectory(tests)
243 endif()