CMakeLists.txt revision 544bdfb538d082109194952ce66907b917eb8d51
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
24set(ASAN_DYLIB_SOURCES
25  ${ASAN_SOURCES})
26
27include_directories(..)
28
29if (NOT MSVC)
30  set(ASAN_CFLAGS
31    ${SANITIZER_COMMON_CFLAGS}
32    -fno-rtti)
33else()
34  set(ASAN_CFLAGS
35    ${SANITIZER_COMMON_CFLAGS}
36    /GR-)
37endif()
38
39set(ASAN_COMMON_DEFINITIONS
40  ASAN_HAS_EXCEPTIONS=1)
41
42if(ANDROID)
43  list(APPEND ASAN_COMMON_DEFINITIONS
44    ASAN_FLEXIBLE_MAPPING_AND_OFFSET=0
45    ASAN_NEEDS_SEGV=0
46    ASAN_LOW_MEMORY=1)
47elseif(MSVC)
48  list(APPEND ASAN_COMMON_DEFINITIONS
49    ASAN_FLEXIBLE_MAPPING_AND_OFFSET=0
50    ASAN_NEEDS_SEGV=0)
51else()
52  list(APPEND ASAN_COMMON_DEFINITIONS
53    ASAN_FLEXIBLE_MAPPING_AND_OFFSET=1
54    ASAN_NEEDS_SEGV=1)
55endif()
56
57# Architectures supported by ASan.
58filter_available_targets(ASAN_SUPPORTED_ARCH
59  x86_64 i386 powerpc64)
60
61set(ASAN_RUNTIME_LIBRARIES)
62if(APPLE)
63  # Build universal binary on APPLE.
64  add_compiler_rt_osx_dynamic_runtime(clang_rt.asan_osx_dynamic
65    ARCH ${ASAN_SUPPORTED_ARCH}
66    SOURCES ${ASAN_DYLIB_SOURCES}
67            $<TARGET_OBJECTS:RTInterception.osx>
68            $<TARGET_OBJECTS:RTSanitizerCommon.osx>
69            $<TARGET_OBJECTS:RTLSanCommon.osx>
70    CFLAGS ${ASAN_CFLAGS}
71    DEFS ${ASAN_COMMON_DEFINITIONS}
72    # Dynamic lookup is needed because shadow scale and offset are
73    # provided by the instrumented modules.
74    LINKFLAGS "-framework Foundation"
75              "-undefined dynamic_lookup")
76  list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_osx_dynamic)
77elseif(ANDROID)
78  add_library(clang_rt.asan-arm-android SHARED
79    ${ASAN_SOURCES}
80    $<TARGET_OBJECTS:RTInterception.arm.android>
81    $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>
82    )
83  set_target_compile_flags(clang_rt.asan-arm-android
84    ${ASAN_CFLAGS})
85  set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
86    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
87  target_link_libraries(clang_rt.asan-arm-android dl)
88  list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-arm-android)
89else()
90  # Otherwise, build separate libraries for each target.
91
92  foreach(arch ${ASAN_SUPPORTED_ARCH})
93    set(ASAN_SOURCE_LIBS
94      $<TARGET_OBJECTS:RTInterception.${arch}>
95      $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
96      $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
97    if (NOT MSVC)
98      # We can't build Leak Sanitizer on Windows yet.
99      list(APPEND ASAN_SOURCE_LIBS $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
100    endif()
101
102    add_compiler_rt_static_runtime(clang_rt.asan-${arch} ${arch}
103      SOURCES ${ASAN_SOURCES} ${ASAN_SOURCE_LIBS}
104      CFLAGS ${ASAN_CFLAGS}
105      DEFS ${ASAN_COMMON_DEFINITIONS})
106    list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch})
107    if (UNIX AND NOT ${arch} STREQUAL "i386")
108      add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
109      list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch}-symbols)
110    endif()
111
112    if (WIN32)
113      add_compiler_rt_static_runtime(clang_rt.asan_dll_thunk-${arch} ${arch}
114      SOURCES asan_dll_thunk.cc
115      CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
116      DEFS ${ASAN_COMMON_DEFINITIONS})
117      list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_dll_thunk-${arch})
118    endif()
119  endforeach()
120endif()
121
122add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
123
124if(LLVM_INCLUDE_TESTS)
125  add_subdirectory(tests)
126endif()
127
128add_subdirectory(lit_tests)
129