CMakeLists.txt revision 05fa3808f6ac96023cdf583a1a1b7220e5b451b8
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  add_compiler_rt_osx_object_library(RTAsan
61    ARCH ${ASAN_SUPPORTED_ARCH}
62    SOURCES ${ASAN_SOURCES}
63    CFLAGS ${ASAN_CFLAGS}
64    DEFS ${ASAN_COMMON_DEFINITIONS})
65elseif(ANDROID)
66  add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES})
67  set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
68  set_property(TARGET RTAsan.arm.android APPEND PROPERTY
69    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
70else()
71  foreach(arch ${ASAN_SUPPORTED_ARCH})
72    add_compiler_rt_object_library(RTAsan ${arch}
73      SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
74      DEFS ${ASAN_COMMON_DEFINITIONS})
75  endforeach()
76endif()
77
78# Build ASan runtimes shipped with Clang.
79set(ASAN_RUNTIME_LIBRARIES)
80if(APPLE)
81  add_compiler_rt_osx_dynamic_runtime(clang_rt.asan_osx_dynamic
82    ARCH ${ASAN_SUPPORTED_ARCH}
83    SOURCES $<TARGET_OBJECTS:RTAsan.osx>
84            $<TARGET_OBJECTS:RTInterception.osx>
85            $<TARGET_OBJECTS:RTSanitizerCommon.osx>
86            $<TARGET_OBJECTS:RTLSanCommon.osx>
87    CFLAGS ${ASAN_CFLAGS}
88    DEFS ${ASAN_COMMON_DEFINITIONS}
89    # Dynamic lookup is needed because shadow scale and offset are
90    # provided by the instrumented modules.
91    LINKFLAGS "-framework Foundation"
92              "-undefined dynamic_lookup")
93  list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_osx_dynamic)
94elseif(ANDROID)
95  add_library(clang_rt.asan-arm-android SHARED
96    $<TARGET_OBJECTS:RTAsan.arm.android>
97    $<TARGET_OBJECTS:RTInterception.arm.android>
98    $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
99  set_target_compile_flags(clang_rt.asan-arm-android
100    ${ASAN_CFLAGS})
101  set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
102    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
103  target_link_libraries(clang_rt.asan-arm-android dl)
104  list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-arm-android)
105else()
106  # Build separate libraries for each target.
107  foreach(arch ${ASAN_SUPPORTED_ARCH})
108    set(ASAN_RUNTIME_OBJECTS
109      $<TARGET_OBJECTS:RTAsan.${arch}>
110      $<TARGET_OBJECTS:RTInterception.${arch}>
111      $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
112      $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
113    if (NOT MSVC)
114      # We can't build Leak Sanitizer on Windows yet.
115      list(APPEND ASAN_RUNTIME_OBJECTS $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
116    endif()
117
118    add_compiler_rt_static_runtime(clang_rt.asan-${arch} ${arch}
119      SOURCES ${ASAN_RUNTIME_OBJECTS}
120      CFLAGS ${ASAN_CFLAGS}
121      DEFS ${ASAN_COMMON_DEFINITIONS})
122    list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch})
123    if (UNIX AND NOT ${arch} STREQUAL "i386")
124      add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
125      list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch}-symbols)
126    endif()
127
128    if (WIN32)
129      add_compiler_rt_static_runtime(clang_rt.asan_dll_thunk-${arch} ${arch}
130      SOURCES asan_dll_thunk.cc
131      CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
132      DEFS ${ASAN_COMMON_DEFINITIONS})
133      list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_dll_thunk-${arch})
134    endif()
135  endforeach()
136endif()
137
138add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
139
140if(LLVM_INCLUDE_TESTS)
141  add_subdirectory(tests)
142endif()
143
144add_subdirectory(lit_tests)
145