Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / ubsan / CMakeLists.txt
blob3f1e12ed9ac66fc453e9553b478eb809268bd287
1 # Build for the undefined behavior sanitizer runtime support library.
3 set(UBSAN_SOURCES
4   ubsan_diag.cpp
5   ubsan_init.cpp
6   ubsan_flags.cpp
7   ubsan_handlers.cpp
8   ubsan_monitor.cpp
9   ubsan_value.cpp
10   )
12 set(UBSAN_STANDALONE_SOURCES
13   ubsan_diag_standalone.cpp
14   ubsan_init_standalone.cpp
15   ubsan_signals_standalone.cpp
16   )
18 set(UBSAN_CXXABI_SOURCES
19   ubsan_handlers_cxx.cpp
20   ubsan_type_hash.cpp
21   ubsan_type_hash_itanium.cpp
22   ubsan_type_hash_win.cpp
23  )
25 set(UBSAN_HEADERS
26   ubsan_checks.inc
27   ubsan_diag.h
28   ubsan_flags.h
29   ubsan_flags.inc
30   ubsan_handlers.h
31   ubsan_handlers_cxx.h
32   ubsan_init.h
33   ubsan_interface.inc
34   ubsan_monitor.h
35   ubsan_platform.h
36   ubsan_signals_standalone.h
37   ubsan_type_hash.h
38   ubsan_value.h
39   )
41 include_directories(..)
43 set(UBSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
44 append_rtti_flag(OFF UBSAN_CFLAGS)
45 append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_CFLAGS)
47 # Too many existing bugs, needs cleanup.
48 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format UBSAN_CFLAGS)
50 set(UBSAN_STANDALONE_CFLAGS ${SANITIZER_COMMON_CFLAGS})
51 append_rtti_flag(OFF UBSAN_STANDALONE_CFLAGS)
52 append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_STANDALONE_CFLAGS)
54 set(UBSAN_CXXFLAGS ${SANITIZER_COMMON_CFLAGS})
55 append_rtti_flag(ON UBSAN_CXXFLAGS)
56 append_list_if(SANITIZER_CAN_USE_CXXABI -DUBSAN_CAN_USE_CXXABI UBSAN_CXXFLAGS)
58 # Silence warnings in system headers with MSVC.
59 if(NOT CLANG_CL)
60   append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" UBSAN_CXXFLAGS)
61 endif()
63 set(UBSAN_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
65 set(UBSAN_DYNAMIC_LIBS
66   ${COMPILER_RT_UNWINDER_LINK_LIBS}
67   ${SANITIZER_CXX_ABI_LIBRARIES}
68   ${SANITIZER_COMMON_LINK_LIBS})
70 append_list_if(COMPILER_RT_HAS_LIBDL dl UBSAN_DYNAMIC_LIBS)
71 append_list_if(COMPILER_RT_HAS_LIBLOG log UBSAN_DYNAMIC_LIBS)
72 append_list_if(COMPILER_RT_HAS_LIBRT rt UBSAN_DYNAMIC_LIBS)
73 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread UBSAN_DYNAMIC_LIBS)
74 if (COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
75   append_list_if(COMPILER_RT_HAS_LIBM m UBSAN_DYNAMIC_LIBS)
76 endif()
78 add_compiler_rt_component(ubsan)
80 if(APPLE)
81   set(UBSAN_COMMON_SOURCES ${UBSAN_SOURCES})
82   if(SANITIZER_CAN_USE_CXXABI)
83     list(APPEND UBSAN_COMMON_SOURCES ${UBSAN_CXXABI_SOURCES})
84   endif()
86   # Common parts of UBSan runtime.
87   add_compiler_rt_object_libraries(RTUbsan
88     OS ${SANITIZER_COMMON_SUPPORTED_OS}
89     ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH}
90     SOURCES ${UBSAN_COMMON_SOURCES}
91     ADDITIONAL_HEADERS ${UBSAN_HEADERS}
92     CFLAGS ${UBSAN_CXXFLAGS})
94   if(COMPILER_RT_HAS_UBSAN)
95     # Initializer of standalone UBSan runtime.
96     add_compiler_rt_object_libraries(RTUbsan_standalone
97       OS ${UBSAN_SUPPORTED_OS}
98       ARCHS ${UBSAN_SUPPORTED_ARCH}
99       SOURCES ${UBSAN_STANDALONE_SOURCES}
100       ADDITIONAL_HEADERS ${UBSAN_HEADERS}
101       CFLAGS ${UBSAN_STANDALONE_CFLAGS})
103     add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
104     add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
106     add_compiler_rt_runtime(clang_rt.ubsan
107       SHARED
108       OS ${UBSAN_SUPPORTED_OS}
109       ARCHS ${UBSAN_SUPPORTED_ARCH}
110       OBJECT_LIBS RTUbsan
111                   RTUbsan_standalone
112                   RTSanitizerCommon
113                   RTSanitizerCommonLibc
114                   RTSanitizerCommonCoverage
115                   RTSanitizerCommonSymbolizer
116                   RTInterception
117       LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
118       PARENT_TARGET ubsan)
120     if (NOT APPLE)
121       add_compiler_rt_runtime(clang_rt.ubsan
122         STATIC
123         OS ${UBSAN_SUPPORTED_OS}
124         ARCHS ${UBSAN_SUPPORTED_ARCH}
125         OBJECT_LIBS RTUbsan
126                     RTUbsan_standalone
127                     RTSanitizerCommonNoHooks
128                     RTSanitizerCommonLibcNoHooks
129                     RTSanitizerCommonCoverage
130                     RTSanitizerCommonSymbolizerNoHooks
131                     RTInterception
132         LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
133         PARENT_TARGET ubsan)
134     endif()
135   endif()
137 else()
138   # Common parts of UBSan runtime.
139   add_compiler_rt_object_libraries(RTUbsan
140     ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH}
141     SOURCES ${UBSAN_SOURCES}
142     ADDITIONAL_HEADERS ${UBSAN_HEADERS}
143     CFLAGS ${UBSAN_CFLAGS})
145   if(SANITIZER_CAN_USE_CXXABI)
146     # C++-specific parts of UBSan runtime. Requires a C++ ABI library.
147     set(UBSAN_CXX_SOURCES ${UBSAN_CXXABI_SOURCES})
148   else()
149     # Dummy target if we don't have C++ ABI library.
150     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cxx_dummy.cpp "")
151     set(UBSAN_CXX_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/cxx_dummy.cpp)
152   endif()
154   add_compiler_rt_object_libraries(RTUbsan_cxx
155     ARCHS ${UBSAN_COMMON_SUPPORTED_ARCH}
156     SOURCES ${UBSAN_CXX_SOURCES}
157     ADDITIONAL_HEADERS ${UBSAN_HEADERS}
158     CFLAGS ${UBSAN_CXXFLAGS})
160   if (WIN32)
161     add_compiler_rt_object_libraries(UbsanWeakInterception
162       ${SANITIZER_COMMON_SUPPORTED_OS}
163       ARCHS ${UBSAN_SUPPORTED_ARCH}
164       SOURCES
165         ubsan_win_weak_interception.cpp
166       CFLAGS ${UBSAN_CFLAGS} -DSANITIZER_DYNAMIC
167       DEFS ${UBSAN_COMMON_DEFINITIONS})
169     add_compiler_rt_object_libraries(UbsanDllThunk
170       ${SANITIZER_COMMON_SUPPORTED_OS}
171       ARCHS ${UBSAN_SUPPORTED_ARCH}
172       SOURCES
173         ubsan_win_dll_thunk.cpp
174       CFLAGS ${UBSAN_CFLAGS} -DSANITIZER_DLL_THUNK
175       DEFS ${UBSAN_COMMON_DEFINITIONS})
177     set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
178     if(MSVC)
179       list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
180     elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
181       list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
182     endif()
183     add_compiler_rt_object_libraries(UbsanDynamicRuntimeThunk
184       ${SANITIZER_COMMON_SUPPORTED_OS}
185       ARCHS ${UBSAN_SUPPORTED_ARCH}
186       SOURCES
187         ubsan_win_dynamic_runtime_thunk.cpp
188       CFLAGS ${UBSAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
189       DEFS ${UBSAN_COMMON_DEFINITIONS})
190   endif()
192   if(COMPILER_RT_HAS_UBSAN)
193     add_compiler_rt_object_libraries(RTUbsan_standalone
194       ARCHS ${UBSAN_SUPPORTED_ARCH}
195       SOURCES ${UBSAN_STANDALONE_SOURCES}
196       ADDITIONAL_HEADERS ${UBSAN_HEADERS}
197       CFLAGS ${UBSAN_STANDALONE_CFLAGS})
199     # Standalone UBSan runtimes.
200     add_compiler_rt_runtime(clang_rt.ubsan_standalone
201       STATIC
202       ARCHS ${UBSAN_SUPPORTED_ARCH}
203       SOURCES
204         ubsan_init_standalone_preinit.cpp
205       ADDITIONAL_HEADERS ${UBSAN_HEADERS}
206       OBJECT_LIBS RTSanitizerCommon
207               RTSanitizerCommonLibc
208               RTSanitizerCommonCoverage
209               RTSanitizerCommonSymbolizer
210               RTSanitizerCommonSymbolizerInternal
211               RTUbsan
212               RTUbsan_standalone
213               RTInterception
214       CFLAGS ${UBSAN_CFLAGS}
215       PARENT_TARGET ubsan)
217     add_compiler_rt_runtime(clang_rt.ubsan_standalone_cxx
218       STATIC
219       ARCHS ${UBSAN_SUPPORTED_ARCH}
220       OBJECT_LIBS RTUbsan_cxx
221       CFLAGS ${UBSAN_CXXFLAGS}
222       PARENT_TARGET ubsan)
224     if (COMPILER_RT_HAS_VERSION_SCRIPT)
225       file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
226       add_compiler_rt_object_libraries(RTUbsan_dynamic_version_script_dummy
227         ARCHS ${UBSAN_SUPPORTED_ARCH}
228         SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
229         CFLAGS ${UBSAN_CFLAGS})
231       foreach(arch ${UBSAN_SUPPORTED_ARCH})
232         add_sanitizer_rt_version_list(clang_rt.ubsan_standalone-dynamic-${arch}
233                                       LIBS clang_rt.ubsan_standalone-${arch}
234                                            clang_rt.ubsan_standalone_cxx-${arch}
235                                       EXTRA ubsan.syms.extra)
236         set(VERSION_SCRIPT_FLAG
237             -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.ubsan_standalone-dynamic-${arch}.vers)
238         # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
239         # but requires a special option to enable it.
240         if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
241             list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
242         endif()
243         set_property(SOURCE
244           ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
245           APPEND PROPERTY
246           OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.ubsan_standalone-dynamic-${arch}.vers)
248         add_compiler_rt_runtime(clang_rt.ubsan_standalone
249           SHARED
250           ARCHS ${arch}
251           OBJECT_LIBS RTSanitizerCommon
252                 RTSanitizerCommonLibc
253                 RTSanitizerCommonCoverage
254                 RTSanitizerCommonSymbolizer
255                 RTSanitizerCommonSymbolizerInternal
256                 RTUbsan
257                 RTUbsan_cxx
258                 RTUbsan_standalone
259                 RTInterception
260                 RTUbsan_dynamic_version_script_dummy
261           CFLAGS ${UBSAN_CFLAGS}
262           LINK_FLAGS ${UBSAN_LINK_FLAGS} ${VERSION_SCRIPT_FLAG}
263           LINK_LIBS ${UBSAN_DYNAMIC_LIBS}
264           PARENT_TARGET ubsan)
265       endforeach()
267       set(ARCHS_FOR_SYMBOLS ${UBSAN_SUPPORTED_ARCH})
268       list(REMOVE_ITEM ARCHS_FOR_SYMBOLS i386)
269       add_sanitizer_rt_symbols(clang_rt.ubsan_standalone
270         ARCHS ${ARCHS_FOR_SYMBOLS}
271         PARENT_TARGET ubsan
272         EXTRA ubsan.syms.extra)
273       add_sanitizer_rt_symbols(clang_rt.ubsan_standalone_cxx
274         ARCHS ${ARCHS_FOR_SYMBOLS}
275         PARENT_TARGET ubsan
276         EXTRA ubsan.syms.extra)
277     endif()
278   endif()
279 endif()