CMakeLists.txt revision 49496747758bf44163768ce3e07e40c8f95ccba9
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 if (os STREQUAL "osx") 89 list(APPEND ASAN_RUNTIME_LDFLAGS "-framework Foundation") 90 endif() 91 add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os} 92 ARCH ${ASAN_SUPPORTED_ARCH} 93 SOURCES $<TARGET_OBJECTS:RTAsan.${os}> 94 $<TARGET_OBJECTS:RTInterception.${os}> 95 $<TARGET_OBJECTS:RTSanitizerCommon.${os}> 96 $<TARGET_OBJECTS:RTLSanCommon.${os}> 97 CFLAGS ${ASAN_CFLAGS} 98 DEFS ${ASAN_COMMON_DEFINITIONS} 99 LINKFLAGS ${ASAN_RUNTIME_LDFLAGS}) 100 list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_${os}_dynamic) 101 endforeach() 102 103elseif(ANDROID) 104 add_library(clang_rt.asan-arm-android SHARED 105 $<TARGET_OBJECTS:RTAsan.arm.android> 106 $<TARGET_OBJECTS:RTInterception.arm.android> 107 $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>) 108 set_target_compile_flags(clang_rt.asan-arm-android 109 ${ASAN_CFLAGS}) 110 set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY 111 COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS}) 112 target_link_libraries(clang_rt.asan-arm-android dl) 113 list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-arm-android) 114else() 115 # Build separate libraries for each target. 116 foreach(arch ${ASAN_SUPPORTED_ARCH}) 117 set(ASAN_RUNTIME_OBJECTS 118 $<TARGET_OBJECTS:RTAsan.${arch}> 119 $<TARGET_OBJECTS:RTInterception.${arch}> 120 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}> 121 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>) 122 if (NOT MSVC) 123 # We can't build Leak Sanitizer on Windows yet. 124 list(APPEND ASAN_RUNTIME_OBJECTS $<TARGET_OBJECTS:RTLSanCommon.${arch}>) 125 endif() 126 127 add_compiler_rt_static_runtime(clang_rt.asan-${arch} ${arch} 128 SOURCES ${ASAN_RUNTIME_OBJECTS} 129 CFLAGS ${ASAN_CFLAGS} 130 DEFS ${ASAN_COMMON_DEFINITIONS}) 131 list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch}) 132 if (UNIX AND NOT ${arch} STREQUAL "i386") 133 add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra) 134 list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan-${arch}-symbols) 135 endif() 136 137 if (WIN32) 138 add_compiler_rt_static_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} 139 SOURCES asan_dll_thunk.cc 140 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK 141 DEFS ${ASAN_COMMON_DEFINITIONS}) 142 list(APPEND ASAN_RUNTIME_LIBRARIES clang_rt.asan_dll_thunk-${arch}) 143 endif() 144 endforeach() 145endif() 146 147add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt) 148 149if(LLVM_INCLUDE_TESTS) 150 add_subdirectory(tests) 151endif() 152 153add_subdirectory(lit_tests) 154