1 # Build for the AddressSanitizer runtime support library.
4 # Don't set rpath for the ASan libraries. Developers are encouraged to ship
5 # their binaries together with the corresponding ASan runtime libraries,
6 # so they'll anyway need to fix the rpath and the install name.
7 set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
33 set(ASAN_PREINIT_SOURCES
36 include_directories(..)
39 include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
42 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
43 append_no_rtti_flag(ASAN_CFLAGS)
45 set(ASAN_COMMON_DEFINITIONS
46 ASAN_HAS_EXCEPTIONS=1)
49 list(APPEND ASAN_COMMON_DEFINITIONS
53 set(ASAN_DYNAMIC_DEFINITIONS
54 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
56 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
57 append_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
58 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
60 set(ASAN_DYNAMIC_LIBS stdc++ m c)
61 append_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
62 append_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
64 # Compile ASan sources into an object library.
66 foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
67 add_compiler_rt_darwin_object_library(RTAsan ${os}
68 ARCH ${ASAN_SUPPORTED_ARCH}
69 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
71 DEFS ${ASAN_COMMON_DEFINITIONS})
74 add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES} ${ASAN_CXX_SOURCES})
75 set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
76 set_property(TARGET RTAsan.arm.android APPEND PROPERTY
77 COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
79 foreach(arch ${ASAN_SUPPORTED_ARCH})
80 add_compiler_rt_object_library(RTAsan ${arch}
81 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
82 DEFS ${ASAN_COMMON_DEFINITIONS})
83 add_compiler_rt_object_library(RTAsan_cxx ${arch}
84 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
85 DEFS ${ASAN_COMMON_DEFINITIONS})
86 add_compiler_rt_object_library(RTAsan_preinit ${arch}
87 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
88 DEFS ${ASAN_COMMON_DEFINITIONS})
89 if (COMPILER_RT_BUILD_SHARED_ASAN)
90 add_compiler_rt_object_library(RTAsan_dynamic ${arch}
91 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
92 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
93 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
98 # Build ASan runtimes shipped with Clang.
99 add_custom_target(asan)
101 foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
102 add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
103 ARCH ${ASAN_SUPPORTED_ARCH}
104 SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
105 $<TARGET_OBJECTS:RTInterception.${os}>
106 $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
107 $<TARGET_OBJECTS:RTLSanCommon.${os}>
108 CFLAGS ${ASAN_CFLAGS}
109 DEFS ${ASAN_COMMON_DEFINITIONS})
110 add_dependencies(asan clang_rt.asan_${os}_dynamic)
114 add_library(clang_rt.asan-arm-android SHARED
115 $<TARGET_OBJECTS:RTAsan.arm.android>
116 $<TARGET_OBJECTS:RTInterception.arm.android>
117 $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
118 set_target_compile_flags(clang_rt.asan-arm-android
120 set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
121 COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
122 target_link_libraries(clang_rt.asan-arm-android dl log)
123 add_dependencies(asan clang_rt.asan-arm-android)
124 install(TARGETS clang_rt.asan-arm-android
125 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
126 LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
128 # Build separate libraries for each target.
129 foreach(arch ${ASAN_SUPPORTED_ARCH})
130 set(ASAN_COMMON_RUNTIME_OBJECTS
131 $<TARGET_OBJECTS:RTInterception.${arch}>
132 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
133 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
135 # We can't build Leak Sanitizer on Windows yet.
136 list(APPEND ASAN_COMMON_RUNTIME_OBJECTS
137 $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
140 add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
141 SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
142 $<TARGET_OBJECTS:RTAsan.${arch}>
143 ${ASAN_COMMON_RUNTIME_OBJECTS}
144 CFLAGS ${ASAN_CFLAGS}
145 DEFS ${ASAN_COMMON_DEFINITIONS})
146 add_dependencies(asan clang_rt.asan-${arch})
148 add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
149 SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
150 CFLAGS ${ASAN_CFLAGS}
151 DEFS ${ASAN_COMMON_DEFINITIONS})
152 add_dependencies(asan clang_rt.asan_cxx-${arch})
154 if (COMPILER_RT_BUILD_SHARED_ASAN)
155 add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
156 SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
157 CFLAGS ${ASAN_CFLAGS}
158 DEFS ${ASAN_COMMON_DEFINITIONS})
159 add_dependencies(asan clang_rt.asan-preinit-${arch})
161 add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
162 OUTPUT_NAME clang_rt.asan-${arch}
163 SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
164 ${ASAN_COMMON_RUNTIME_OBJECTS}
165 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
166 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
167 target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
168 add_dependencies(asan clang_rt.asan-dynamic-${arch})
171 if (UNIX AND NOT ${arch} STREQUAL "i386")
172 add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
173 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
174 add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
175 add_dependencies(asan clang_rt.asan-${arch}-symbols)
179 add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
180 SOURCES asan_dll_thunk.cc
181 $<TARGET_OBJECTS:RTInterception.${arch}>
182 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK -DSANITIZER_DLL_THUNK
183 DEFS ${ASAN_COMMON_DEFINITIONS})
184 add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
189 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
190 add_dependencies(asan asan_blacklist)
191 add_dependencies(compiler-rt asan)
193 add_subdirectory(scripts)
195 if(COMPILER_RT_INCLUDE_TESTS)
196 add_subdirectory(tests)