[compiler-rt] Silence warnings when building with MSVC
[llvm-project.git] / compiler-rt / lib / asan / CMakeLists.txt
blobf2d99be6db2ec25306ddb8d8186486e1ac0b1508
1 # Build for the AddressSanitizer runtime support library.
3 set(ASAN_SOURCES
4   asan_allocator.cpp
5   asan_activation.cpp
6   asan_debugging.cpp
7   asan_descriptions.cpp
8   asan_errors.cpp
9   asan_fake_stack.cpp
10   asan_flags.cpp
11   asan_fuchsia.cpp
12   asan_globals.cpp
13   asan_globals_win.cpp
14   asan_interceptors.cpp
15   asan_interceptors_memintrinsics.cpp
16   asan_linux.cpp
17   asan_mac.cpp
18   asan_malloc_linux.cpp
19   asan_malloc_mac.cpp
20   asan_malloc_win.cpp
21   asan_memory_profile.cpp
22   asan_poisoning.cpp
23   asan_posix.cpp
24   asan_premap_shadow.cpp
25   asan_report.cpp
26   asan_rtl.cpp
27   asan_shadow_setup.cpp
28   asan_stack.cpp
29   asan_stats.cpp
30   asan_suppressions.cpp
31   asan_thread.cpp
32   asan_win.cpp
33   )
35 if (NOT WIN32 AND NOT APPLE)
36   list(APPEND ASAN_SOURCES
37     asan_interceptors_vfork.S
38     )
39 endif()
41 set(ASAN_CXX_SOURCES
42   asan_new_delete.cpp
43   )
45 set(ASAN_STATIC_SOURCES
46   asan_rtl_static.cpp
47   )
49 if (NOT WIN32 AND NOT APPLE)
50   list(APPEND ASAN_STATIC_SOURCES
51     asan_rtl_x86_64.S
52   )
53 endif()
55 set(ASAN_PREINIT_SOURCES
56   asan_preinit.cpp
57   )
59 SET(ASAN_HEADERS
60   asan_activation.h
61   asan_activation_flags.inc
62   asan_allocator.h
63   asan_descriptions.h
64   asan_errors.h
65   asan_fake_stack.h
66   asan_flags.h
67   asan_flags.inc
68   asan_init_version.h
69   asan_interceptors.h
70   asan_interceptors_memintrinsics.h
71   asan_interface.inc
72   asan_interface_internal.h
73   asan_internal.h
74   asan_lock.h
75   asan_mapping.h
76   asan_poisoning.h
77   asan_premap_shadow.h
78   asan_report.h
79   asan_scariness_score.h
80   asan_stack.h
81   asan_stats.h
82   asan_suppressions.h
83   asan_thread.h
84   )
86 include_directories(..)
88 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
89 set(ASAN_COMMON_DEFINITIONS ${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION})
91 append_rtti_flag(OFF ASAN_CFLAGS)
93 # Silence warnings in system headers with MSVC.
94 append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external /external:W0 /external:anglebrackets" ASAN_CFLAGS)
96 # Too many existing bugs, needs cleanup.
97 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
99 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
101 if(ANDROID)
102 # Put most Sanitizer shared libraries in the global group. For more details, see
103 # android-changes-for-ndk-developers.md#changes-to-library-search-order
104   if (COMPILER_RT_HAS_Z_GLOBAL)
105     list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
106   endif()
107 endif()
109 set(ASAN_DYNAMIC_DEFINITIONS
110   ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
111 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
113 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
114 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
115   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
116 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
118 set(ASAN_DYNAMIC_LIBS ${SANITIZER_CXX_ABI_LIBRARIES} ${SANITIZER_COMMON_LINK_LIBS})
120 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
121 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
122 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
123 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
124 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
125 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
127 # Compile ASan sources into an object library.
129 add_compiler_rt_object_libraries(RTAsan_dynamic
130   OS ${SANITIZER_COMMON_SUPPORTED_OS}
131   ARCHS ${ASAN_SUPPORTED_ARCH}
132   SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
133   ADDITIONAL_HEADERS ${ASAN_HEADERS}
134   CFLAGS ${ASAN_DYNAMIC_CFLAGS}
135   DEFS ${ASAN_DYNAMIC_DEFINITIONS})
137 if(NOT APPLE)
138   add_compiler_rt_object_libraries(RTAsan
139     ARCHS ${ASAN_SUPPORTED_ARCH}
140     SOURCES ${ASAN_SOURCES}
141     ADDITIONAL_HEADERS ${ASAN_HEADERS}
142     CFLAGS ${ASAN_CFLAGS}
143     DEFS ${ASAN_COMMON_DEFINITIONS})
144   add_compiler_rt_object_libraries(RTAsan_cxx
145     ARCHS ${ASAN_SUPPORTED_ARCH}
146     SOURCES ${ASAN_CXX_SOURCES}
147     ADDITIONAL_HEADERS ${ASAN_HEADERS}
148     CFLAGS ${ASAN_CFLAGS}
149     DEFS ${ASAN_COMMON_DEFINITIONS})
150   add_compiler_rt_object_libraries(RTAsan_static
151     ARCHS ${ASAN_SUPPORTED_ARCH}
152     SOURCES ${ASAN_STATIC_SOURCES}
153     ADDITIONAL_HEADERS ${ASAN_HEADERS}
154     CFLAGS ${ASAN_CFLAGS}
155     DEFS ${ASAN_COMMON_DEFINITIONS})
156   add_compiler_rt_object_libraries(RTAsan_preinit
157     ARCHS ${ASAN_SUPPORTED_ARCH}
158     SOURCES ${ASAN_PREINIT_SOURCES}
159     ADDITIONAL_HEADERS ${ASAN_HEADERS}
160     CFLAGS ${ASAN_CFLAGS}
161     DEFS ${ASAN_COMMON_DEFINITIONS})
163   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
164   add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
165     ARCHS ${ASAN_SUPPORTED_ARCH}
166     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
167     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
168     DEFS ${ASAN_DYNAMIC_DEFINITIONS})
169 endif()
171 # Build ASan runtimes shipped with Clang.
172 add_compiler_rt_component(asan)
174 if(APPLE)
175   add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
176   add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
177   add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
178   add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
179   add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
181   add_compiler_rt_runtime(clang_rt.asan
182     SHARED
183     OS ${SANITIZER_COMMON_SUPPORTED_OS}
184     ARCHS ${ASAN_SUPPORTED_ARCH}
185     OBJECT_LIBS RTAsan_dynamic
186                 RTInterception
187                 RTSanitizerCommon
188                 RTSanitizerCommonLibc
189                 RTSanitizerCommonCoverage
190                 RTSanitizerCommonSymbolizer
191                 RTLSanCommon
192                 RTUbsan
193     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
194     LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
195     DEFS ${ASAN_DYNAMIC_DEFINITIONS}
196     PARENT_TARGET asan)
198   add_compiler_rt_runtime(clang_rt.asan_static
199     STATIC
200     ARCHS ${ASAN_SUPPORTED_ARCH}
201     OBJECT_LIBS RTAsan_static
202     CFLAGS ${ASAN_CFLAGS}
203     DEFS ${ASAN_COMMON_DEFINITIONS}
204     PARENT_TARGET asan)
205 else()
206   # Build separate libraries for each target.
208   set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
209     RTInterception
210     RTSanitizerCommon
211     RTSanitizerCommonLibc
212     RTSanitizerCommonCoverage
213     RTSanitizerCommonSymbolizer
214     RTLSanCommon
215     RTUbsan)
217   add_compiler_rt_runtime(clang_rt.asan
218     STATIC
219     ARCHS ${ASAN_SUPPORTED_ARCH}
220     OBJECT_LIBS RTAsan_preinit
221                 RTAsan
222                 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
223     CFLAGS ${ASAN_CFLAGS}
224     DEFS ${ASAN_COMMON_DEFINITIONS}
225     PARENT_TARGET asan)
227   add_compiler_rt_runtime(clang_rt.asan_cxx
228     STATIC
229     ARCHS ${ASAN_SUPPORTED_ARCH}
230     OBJECT_LIBS RTAsan_cxx
231                 RTUbsan_cxx
232     CFLAGS ${ASAN_CFLAGS}
233     DEFS ${ASAN_COMMON_DEFINITIONS}
234     PARENT_TARGET asan)
236   add_compiler_rt_runtime(clang_rt.asan_static
237     STATIC
238     ARCHS ${ASAN_SUPPORTED_ARCH}
239     OBJECT_LIBS RTAsan_static
240     CFLAGS ${ASAN_CFLAGS}
241     DEFS ${ASAN_COMMON_DEFINITIONS}
242     PARENT_TARGET asan)
244   add_compiler_rt_runtime(clang_rt.asan-preinit
245     STATIC
246     ARCHS ${ASAN_SUPPORTED_ARCH}
247     OBJECT_LIBS RTAsan_preinit
248     CFLAGS ${ASAN_CFLAGS}
249     DEFS ${ASAN_COMMON_DEFINITIONS}
250     PARENT_TARGET asan)
252   foreach(arch ${ASAN_SUPPORTED_ARCH})
253     if (COMPILER_RT_HAS_VERSION_SCRIPT)
254       add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
255                                     LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
256                                     EXTRA asan.syms.extra)
257       set(VERSION_SCRIPT_FLAG
258            -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
259       # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
260       # but requires a special option to enable it.
261       if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
262           list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
263       endif()
264       set_property(SOURCE
265         ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
266         APPEND PROPERTY
267         OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
268     else()
269       set(VERSION_SCRIPT_FLAG)
270     endif()
272     set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
273     if (WIN32)
274       add_compiler_rt_object_libraries(AsanWeakInterception
275         ${SANITIZER_COMMON_SUPPORTED_OS}
276         ARCHS ${arch}
277         SOURCES asan_win_weak_interception.cpp
278         CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
279         DEFS ${ASAN_COMMON_DEFINITIONS})
280       set(ASAN_DYNAMIC_WEAK_INTERCEPTION
281           AsanWeakInterception
282           UbsanWeakInterception
283           SancovWeakInterception
284           SanitizerCommonWeakInterception)
285     endif()
287     add_compiler_rt_runtime(clang_rt.asan
288       SHARED
289       ARCHS ${arch}
290       OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
291               RTAsan_dynamic
292               # The only purpose of RTAsan_dynamic_version_script_dummy is to
293               # carry a dependency of the shared runtime on the version script.
294               # Replacing it with a straightforward
295               # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
296               # generates an order-only dependency in ninja.
297               RTAsan_dynamic_version_script_dummy
298               RTUbsan_cxx
299               ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
300       CFLAGS ${ASAN_DYNAMIC_CFLAGS}
301       LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
302                 ${VERSION_SCRIPT_FLAG}
303       LINK_LIBS ${ASAN_DYNAMIC_LIBS}
304       DEFS ${ASAN_DYNAMIC_DEFINITIONS}
305       PARENT_TARGET asan)
307     if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
308       add_sanitizer_rt_symbols(clang_rt.asan_cxx
309         ARCHS ${arch})
310       add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
311       add_sanitizer_rt_symbols(clang_rt.asan
312         ARCHS ${arch}
313         EXTRA asan.syms.extra)
314       add_dependencies(asan clang_rt.asan-${arch}-symbols)
315     endif()
317     if (WIN32)
318       add_compiler_rt_object_libraries(AsanDllThunk
319         ${SANITIZER_COMMON_SUPPORTED_OS}
320         ARCHS ${arch}
321         SOURCES asan_globals_win.cpp
322                 asan_win_dll_thunk.cpp
323         CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
324         DEFS ${ASAN_COMMON_DEFINITIONS})
326       add_compiler_rt_runtime(clang_rt.asan_dll_thunk
327         STATIC
328         ARCHS ${arch}
329         OBJECT_LIBS AsanDllThunk
330                     UbsanDllThunk
331                     SancovDllThunk
332                     SanitizerCommonDllThunk
333         SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
334         PARENT_TARGET asan)
336       set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
337       if(MSVC)
338         list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
339       elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
340         list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
341       endif()
343       add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
344         ${SANITIZER_COMMON_SUPPORTED_OS}
345         ARCHS ${arch}
346         SOURCES asan_globals_win.cpp
347                 asan_win_dynamic_runtime_thunk.cpp
348         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
349         DEFS ${ASAN_COMMON_DEFINITIONS})
351       add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
352         STATIC
353         ARCHS ${arch}
354         OBJECT_LIBS AsanDynamicRuntimeThunk
355                     UbsanDynamicRuntimeThunk
356                     SancovDynamicRuntimeThunk
357                     SanitizerCommonDynamicRuntimeThunk
358         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
359         DEFS ${ASAN_COMMON_DEFINITIONS}
360         PARENT_TARGET asan)
361     endif()
362   endforeach()
363 endif()
365 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
367 add_subdirectory(scripts)
369 if(COMPILER_RT_INCLUDE_TESTS)
370   add_subdirectory(tests)
371 endif()