[CMake] Respect CMAKE_CXX_FLAGS in custom clang_compile commands
[blocksruntime.git] / lib / asan / CMakeLists.txt
blob5bfb7c1791a08a25d4d83ac51263f386e7a587c7
1 # Build for the AddressSanitizer runtime support library.
3 if(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.
7 set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
8 endif()
10 set(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_new_delete.cc
22   asan_poisoning.cc
23   asan_posix.cc
24   asan_preinit.cc
25   asan_report.cc
26   asan_rtl.cc
27   asan_stack.cc
28   asan_stats.cc
29   asan_thread.cc
30   asan_win.cc)
32 include_directories(..)
34 if(ANDROID)
35   include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
36 endif()
38 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
39 append_no_rtti_flag(ASAN_CFLAGS)
41 set(ASAN_COMMON_DEFINITIONS
42   ASAN_HAS_EXCEPTIONS=1)
44 if(ANDROID)
45   list(APPEND ASAN_COMMON_DEFINITIONS
46     ASAN_LOW_MEMORY=1)
47 endif()
49 if (NOT MSVC)
50   set(ASAN_ASM_SOURCES asan_asm_instrumentation.S)
51   set_source_files_properties(${ASAN_ASM_SOURCES} PROPERTIES LANGUAGE C)
52   list(APPEND ASAN_SOURCES ${ASAN_ASM_SOURCES})
53 endif()
55 # Compile ASan sources into an object library.
56 if(APPLE)
57   foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
58     add_compiler_rt_darwin_object_library(RTAsan ${os}
59       ARCH ${ASAN_SUPPORTED_ARCH}
60       SOURCES ${ASAN_SOURCES}
61       CFLAGS ${ASAN_CFLAGS}
62       DEFS ${ASAN_COMMON_DEFINITIONS})
63   endforeach()
64 elseif(ANDROID)
65   add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES})
66   set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
67   set_property(TARGET RTAsan.arm.android APPEND PROPERTY
68     COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
69 else()
70   foreach(arch ${ASAN_SUPPORTED_ARCH})
71     add_compiler_rt_object_library(RTAsan ${arch}
72       SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
73       DEFS ${ASAN_COMMON_DEFINITIONS})
74   endforeach()
75 endif()
77 # Build ASan runtimes shipped with Clang.
78 add_custom_target(asan)
79 if(APPLE)
80   foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
81     add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
82       ARCH ${ASAN_SUPPORTED_ARCH}
83       SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
84               $<TARGET_OBJECTS:RTInterception.${os}>
85               $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
86               $<TARGET_OBJECTS:RTLSanCommon.${os}>
87       CFLAGS ${ASAN_CFLAGS}
88       DEFS ${ASAN_COMMON_DEFINITIONS})
89     add_dependencies(asan clang_rt.asan_${os}_dynamic)
90   endforeach()
92 elseif(ANDROID)
93   add_library(clang_rt.asan-arm-android SHARED
94     $<TARGET_OBJECTS:RTAsan.arm.android>
95     $<TARGET_OBJECTS:RTInterception.arm.android>
96     $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
97   set_target_compile_flags(clang_rt.asan-arm-android
98     ${ASAN_CFLAGS})
99   set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
100     COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
101   target_link_libraries(clang_rt.asan-arm-android dl log)
102   add_dependencies(asan clang_rt.asan-arm-android)
103 else()
104   # Build separate libraries for each target.
105   foreach(arch ${ASAN_SUPPORTED_ARCH})
106     set(ASAN_RUNTIME_OBJECTS
107       $<TARGET_OBJECTS:RTAsan.${arch}>
108       $<TARGET_OBJECTS:RTInterception.${arch}>
109       $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
110       $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
111     if (NOT WIN32)
112       # We can't build Leak Sanitizer on Windows yet.
113       list(APPEND ASAN_RUNTIME_OBJECTS $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
114     endif()
116     add_compiler_rt_static_runtime(clang_rt.asan-${arch} ${arch}
117       SOURCES ${ASAN_RUNTIME_OBJECTS}
118       CFLAGS ${ASAN_CFLAGS}
119       DEFS ${ASAN_COMMON_DEFINITIONS})
120     add_dependencies(asan clang_rt.asan-${arch})
121     if (UNIX AND NOT ${arch} STREQUAL "i386")
122       add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
123       add_dependencies(asan clang_rt.asan-${arch}-symbols)
124     endif()
126     if (WIN32)
127       add_compiler_rt_static_runtime(clang_rt.asan_dll_thunk-${arch} ${arch}
128       SOURCES asan_dll_thunk.cc
129       CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
130       DEFS ${ASAN_COMMON_DEFINITIONS})
131       add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
132     endif()
133   endforeach()
134 endif()
136 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
137 add_dependencies(asan asan_blacklist)
138 add_dependencies(compiler-rt asan)
140 add_subdirectory(scripts)
142 if(COMPILER_RT_INCLUDE_TESTS)
143   add_subdirectory(tests)
144 endif()