CMakeLists.txt revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1# Build for the AddressSanitizer runtime support library.
2
3if(APPLE)
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.
7set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
8endif()
9
10set(ASAN_SOURCES
11  asan_allocator2.cc
12  asan_activation.cc
13  asan_fake_stack.cc
14  asan_globals.cc
15  asan_interceptors.cc
16  asan_linux.cc
17  asan_mac.cc
18  asan_malloc_linux.cc
19  asan_malloc_mac.cc
20  asan_malloc_win.cc
21  asan_poisoning.cc
22  asan_posix.cc
23  asan_report.cc
24  asan_rtl.cc
25  asan_stack.cc
26  asan_stats.cc
27  asan_thread.cc
28  asan_win.cc)
29
30set(ASAN_CXX_SOURCES
31  asan_new_delete.cc)
32
33set(ASAN_PREINIT_SOURCES
34  asan_preinit.cc)
35
36include_directories(..)
37
38if(ANDROID)
39  include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
40endif()
41
42set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
43append_no_rtti_flag(ASAN_CFLAGS)
44
45set(ASAN_COMMON_DEFINITIONS
46  ASAN_HAS_EXCEPTIONS=1)
47
48if(ANDROID)
49  list(APPEND ASAN_COMMON_DEFINITIONS
50    ASAN_LOW_MEMORY=1)
51endif()
52
53set(ASAN_DYNAMIC_DEFINITIONS
54  ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
55
56set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
57append_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
58  -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
59
60set(ASAN_DYNAMIC_LIBS stdc++ m c)
61append_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
62append_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
63
64if (NOT MSVC)
65  set(ASAN_ASM_SOURCES asan_asm_instrumentation.S)
66  set_source_files_properties(${ASAN_ASM_SOURCES} PROPERTIES LANGUAGE C)
67  list(APPEND ASAN_SOURCES ${ASAN_ASM_SOURCES})
68endif()
69
70# Compile ASan sources into an object library.
71if(APPLE)
72  foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
73    add_compiler_rt_darwin_object_library(RTAsan ${os}
74      ARCH ${ASAN_SUPPORTED_ARCH}
75      SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
76      CFLAGS ${ASAN_CFLAGS}
77      DEFS ${ASAN_COMMON_DEFINITIONS})
78  endforeach()
79elseif(ANDROID)
80  add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES} ${ASAN_CXX_SOURCES})
81  set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
82  set_property(TARGET RTAsan.arm.android APPEND PROPERTY
83    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
84else()
85  foreach(arch ${ASAN_SUPPORTED_ARCH})
86    add_compiler_rt_object_library(RTAsan ${arch}
87      SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
88      DEFS ${ASAN_COMMON_DEFINITIONS})
89    add_compiler_rt_object_library(RTAsan_cxx ${arch}
90      SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
91      DEFS ${ASAN_COMMON_DEFINITIONS})
92    add_compiler_rt_object_library(RTAsan_preinit ${arch}
93      SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
94      DEFS ${ASAN_COMMON_DEFINITIONS})
95    if (COMPILER_RT_BUILD_SHARED_ASAN)
96      add_compiler_rt_object_library(RTAsan_dynamic ${arch}
97        SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
98        CFLAGS ${ASAN_DYNAMIC_CFLAGS}
99        DEFS ${ASAN_DYNAMIC_DEFINITIONS})
100    endif()
101  endforeach()
102endif()
103
104# Build ASan runtimes shipped with Clang.
105add_custom_target(asan)
106if(APPLE)
107  foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
108    add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
109      ARCH ${ASAN_SUPPORTED_ARCH}
110      SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
111              $<TARGET_OBJECTS:RTInterception.${os}>
112              $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
113              $<TARGET_OBJECTS:RTLSanCommon.${os}>
114      CFLAGS ${ASAN_CFLAGS}
115      DEFS ${ASAN_COMMON_DEFINITIONS})
116    add_dependencies(asan clang_rt.asan_${os}_dynamic)
117  endforeach()
118
119elseif(ANDROID)
120  add_library(clang_rt.asan-arm-android SHARED
121    $<TARGET_OBJECTS:RTAsan.arm.android>
122    $<TARGET_OBJECTS:RTInterception.arm.android>
123    $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
124  set_target_compile_flags(clang_rt.asan-arm-android
125    ${ASAN_CFLAGS})
126  set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
127    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
128  target_link_libraries(clang_rt.asan-arm-android dl log)
129  add_dependencies(asan clang_rt.asan-arm-android)
130else()
131  # Build separate libraries for each target.
132  foreach(arch ${ASAN_SUPPORTED_ARCH})
133    set(ASAN_COMMON_RUNTIME_OBJECTS
134      $<TARGET_OBJECTS:RTInterception.${arch}>
135      $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
136      $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
137    if (NOT WIN32)
138      # We can't build Leak Sanitizer on Windows yet.
139      list(APPEND ASAN_COMMON_RUNTIME_OBJECTS
140           $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
141    endif()
142
143    add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
144      SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
145              $<TARGET_OBJECTS:RTAsan.${arch}>
146              ${ASAN_COMMON_RUNTIME_OBJECTS}
147      CFLAGS ${ASAN_CFLAGS}
148      DEFS ${ASAN_COMMON_DEFINITIONS})
149    add_dependencies(asan clang_rt.asan-${arch})
150
151    add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
152      SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
153      CFLAGS ${ASAN_CFLAGS}
154      DEFS ${ASAN_COMMON_DEFINITIONS})
155    add_dependencies(asan clang_rt.asan_cxx-${arch})
156
157    if (COMPILER_RT_BUILD_SHARED_ASAN)
158      add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
159        SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
160        CFLAGS ${ASAN_CFLAGS}
161        DEFS ${ASAN_COMMON_DEFINITIONS})
162      add_dependencies(asan clang_rt.asan-preinit-${arch})
163
164      add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
165        OUTPUT_NAME clang_rt.asan-${arch}
166        SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
167                ${ASAN_COMMON_RUNTIME_OBJECTS}
168        CFLAGS ${ASAN_DYNAMIC_CFLAGS}
169        DEFS ${ASAN_DYNAMIC_DEFINITIONS})
170      target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
171      add_dependencies(asan clang_rt.asan-dynamic-${arch})
172    endif()
173
174    if (UNIX AND NOT ${arch} STREQUAL "i386")
175      add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
176      add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
177      add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
178      add_dependencies(asan clang_rt.asan-${arch}-symbols)
179    endif()
180
181    if (WIN32)
182      add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
183        SOURCES asan_dll_thunk.cc
184                $<TARGET_OBJECTS:RTInterception.${arch}>
185        CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
186        DEFS ${ASAN_COMMON_DEFINITIONS})
187      add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
188    endif()
189  endforeach()
190endif()
191
192add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
193add_dependencies(asan asan_blacklist)
194add_dependencies(compiler-rt asan)
195
196add_subdirectory(scripts)
197
198if(COMPILER_RT_INCLUDE_TESTS)
199  add_subdirectory(tests)
200endif()
201