CMakeLists.txt revision d2e32e323cca83b666ecd124ebfd1720232d0928
1# Build for the AddressSanitizer runtime support library.
2
3set(ASAN_SOURCES
4  asan_allocator2.cc
5  asan_fake_stack.cc
6  asan_globals.cc
7  asan_interceptors.cc
8  asan_linux.cc
9  asan_mac.cc
10  asan_malloc_linux.cc
11  asan_malloc_mac.cc
12  asan_malloc_win.cc
13  asan_new_delete.cc
14  asan_poisoning.cc
15  asan_posix.cc
16  asan_preinit.cc
17  asan_report.cc
18  asan_rtl.cc
19  asan_stack.cc
20  asan_stats.cc
21  asan_thread.cc
22  asan_win.cc)
23
24include_directories(..)
25
26if (NOT MSVC)
27  set(ASAN_CFLAGS
28    ${SANITIZER_COMMON_CFLAGS}
29    -fno-rtti)
30else()
31  set(ASAN_CFLAGS
32    ${SANITIZER_COMMON_CFLAGS}
33    /GR-)
34endif()
35
36set(ASAN_COMMON_DEFINITIONS
37  ASAN_HAS_EXCEPTIONS=1)
38
39if(ANDROID)
40  list(APPEND ASAN_COMMON_DEFINITIONS
41    ASAN_FLEXIBLE_MAPPING_AND_OFFSET=0
42    ASAN_NEEDS_SEGV=0
43    ASAN_LOW_MEMORY=1)
44elseif(MSVC)
45  list(APPEND ASAN_COMMON_DEFINITIONS
46    ASAN_FLEXIBLE_MAPPING_AND_OFFSET=0
47    ASAN_NEEDS_SEGV=0)
48else()
49  list(APPEND ASAN_COMMON_DEFINITIONS
50    ASAN_FLEXIBLE_MAPPING_AND_OFFSET=1
51    ASAN_NEEDS_SEGV=1)
52endif()
53
54# Architectures supported by ASan.
55filter_available_targets(ASAN_SUPPORTED_ARCH
56  x86_64 i386 powerpc64)
57
58# Compile ASan sources into an object library.
59if(APPLE)
60  foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
61    add_compiler_rt_darwin_object_library(RTAsan ${os}
62      ARCH ${ASAN_SUPPORTED_ARCH}
63      SOURCES ${ASAN_SOURCES}
64      CFLAGS ${ASAN_CFLAGS}
65      DEFS ${ASAN_COMMON_DEFINITIONS})
66  endforeach()
67elseif(ANDROID)
68  add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES})
69  set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
70  set_property(TARGET RTAsan.arm.android APPEND PROPERTY
71    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
72else()
73  foreach(arch ${ASAN_SUPPORTED_ARCH})
74    add_compiler_rt_object_library(RTAsan ${arch}
75      SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
76      DEFS ${ASAN_COMMON_DEFINITIONS})
77  endforeach()
78endif()
79
80# Build ASan runtimes shipped with Clang.
81set(ASAN_RUNTIME_LIBRARIES)
82if(APPLE)
83  foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
84    # Dynamic lookup is needed because shadow scale and offset are
85    # provided by the instrumented modules.
86    set(ASAN_RUNTIME_LDFLAGS
87        "-undefined dynamic_lookup")
88    add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
89      ARCH ${ASAN_SUPPORTED_ARCH}
90      SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
91              $<TARGET_OBJECTS:RTInterception.${os}>
92              $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
93              $<TARGET_OBJECTS:RTLSanCommon.${os}>
94      CFLAGS ${ASAN_CFLAGS}
95      DEFS ${ASAN_COMMON_DEFINITIONS}
96      LINKFLAGS ${ASAN_RUNTIME_LDFLAGS})
97    list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_${os}_dynamic)
98  endforeach()
99
100elseif(ANDROID)
101  add_library(clang_rt.asan-arm-android SHARED
102    $<TARGET_OBJECTS:RTAsan.arm.android>
103    $<TARGET_OBJECTS:RTInterception.arm.android>
104    $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
105  set_target_compile_flags(clang_rt.asan-arm-android
106    ${ASAN_CFLAGS})
107  set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
108    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
109  target_link_libraries(clang_rt.asan-arm-android dl)
110  list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-arm-android)
111else()
112  # Build separate libraries for each target.
113  foreach(arch ${ASAN_SUPPORTED_ARCH})
114    set(ASAN_RUNTIME_OBJECTS
115      $<TARGET_OBJECTS:RTAsan.${arch}>
116      $<TARGET_OBJECTS:RTInterception.${arch}>
117      $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
118      $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
119    if (NOT MSVC)
120      # We can't build Leak Sanitizer on Windows yet.
121      list(APPEND ASAN_RUNTIME_OBJECTS $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
122    endif()
123
124    add_compiler_rt_static_runtime(clang_rt.asan-${arch} ${arch}
125      SOURCES ${ASAN_RUNTIME_OBJECTS}
126      CFLAGS ${ASAN_CFLAGS}
127      DEFS ${ASAN_COMMON_DEFINITIONS})
128    list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch})
129    if (UNIX AND NOT ${arch} STREQUAL "i386")
130      add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
131      list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch}-symbols)
132    endif()
133
134    if (WIN32)
135      add_compiler_rt_static_runtime(clang_rt.asan_dll_thunk-${arch} ${arch}
136      SOURCES asan_dll_thunk.cc
137      CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
138      DEFS ${ASAN_COMMON_DEFINITIONS})
139      list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_dll_thunk-${arch})
140    endif()
141  endforeach()
142endif()
143
144add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
145
146if(LLVM_INCLUDE_TESTS)
147  add_subdirectory(tests)
148endif()
149
150add_subdirectory(lit_tests)
151