[ASan/Win] Don't apply dllexport to __asan_init in the DLL thunk
[blocksruntime.git] / lib / asan / CMakeLists.txt
blob69c0cd8c2636f5622d53ebe416025dd239be6126
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_poisoning.cc
22   asan_posix.cc
23   asan_report.cc
24   asan_rtl.cc
25   asan_stack.cc
26   asan_stats.cc
27   asan_thread.cc
28   asan_win.cc)
30 set(ASAN_CXX_SOURCES
31   asan_new_delete.cc)
33 set(ASAN_PREINIT_SOURCES
34   asan_preinit.cc)
36 include_directories(..)
38 if(ANDROID)
39   include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
40 endif()
42 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
43 append_no_rtti_flag(ASAN_CFLAGS)
45 set(ASAN_COMMON_DEFINITIONS
46   ASAN_HAS_EXCEPTIONS=1)
48 if(ANDROID)
49   list(APPEND ASAN_COMMON_DEFINITIONS
50     ASAN_LOW_MEMORY=1)
51 endif()
53 set(ASAN_DYNAMIC_DEFINITIONS
54   ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
56 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
57 append_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
58   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
60 set(ASAN_DYNAMIC_LIBS stdc++ m c)
61 append_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
62 append_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
64 # Compile ASan sources into an object library.
65 if(APPLE)
66   foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
67     add_compiler_rt_darwin_object_library(RTAsan ${os}
68       ARCH ${ASAN_SUPPORTED_ARCH}
69       SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
70       CFLAGS ${ASAN_CFLAGS}
71       DEFS ${ASAN_COMMON_DEFINITIONS})
72   endforeach()
73 elseif(ANDROID)
74   add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES} ${ASAN_CXX_SOURCES})
75   set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
76   set_property(TARGET RTAsan.arm.android APPEND PROPERTY
77     COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
78 else()
79   foreach(arch ${ASAN_SUPPORTED_ARCH})
80     add_compiler_rt_object_library(RTAsan ${arch}
81       SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
82       DEFS ${ASAN_COMMON_DEFINITIONS})
83     add_compiler_rt_object_library(RTAsan_cxx ${arch}
84       SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
85       DEFS ${ASAN_COMMON_DEFINITIONS})
86     add_compiler_rt_object_library(RTAsan_preinit ${arch}
87       SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
88       DEFS ${ASAN_COMMON_DEFINITIONS})
89     if (COMPILER_RT_BUILD_SHARED_ASAN)
90       add_compiler_rt_object_library(RTAsan_dynamic ${arch}
91         SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
92         CFLAGS ${ASAN_DYNAMIC_CFLAGS}
93         DEFS ${ASAN_DYNAMIC_DEFINITIONS})
94     endif()
95   endforeach()
96 endif()
98 # Build ASan runtimes shipped with Clang.
99 add_custom_target(asan)
100 if(APPLE)
101   foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
102     add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
103       ARCH ${ASAN_SUPPORTED_ARCH}
104       SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
105               $<TARGET_OBJECTS:RTInterception.${os}>
106               $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
107               $<TARGET_OBJECTS:RTLSanCommon.${os}>
108       CFLAGS ${ASAN_CFLAGS}
109       DEFS ${ASAN_COMMON_DEFINITIONS})
110     add_dependencies(asan clang_rt.asan_${os}_dynamic)
111   endforeach()
113 elseif(ANDROID)
114   add_library(clang_rt.asan-arm-android SHARED
115     $<TARGET_OBJECTS:RTAsan.arm.android>
116     $<TARGET_OBJECTS:RTInterception.arm.android>
117     $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
118   set_target_compile_flags(clang_rt.asan-arm-android
119     ${ASAN_CFLAGS})
120   set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
121     COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
122   target_link_libraries(clang_rt.asan-arm-android dl log)
123   add_dependencies(asan clang_rt.asan-arm-android)
124   install(TARGETS clang_rt.asan-arm-android
125           ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
126           LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
127 else()
128   # Build separate libraries for each target.
129   foreach(arch ${ASAN_SUPPORTED_ARCH})
130     set(ASAN_COMMON_RUNTIME_OBJECTS
131       $<TARGET_OBJECTS:RTInterception.${arch}>
132       $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
133       $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
134     if (NOT WIN32)
135       # We can't build Leak Sanitizer on Windows yet.
136       list(APPEND ASAN_COMMON_RUNTIME_OBJECTS
137            $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
138     endif()
140     add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
141       SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
142               $<TARGET_OBJECTS:RTAsan.${arch}>
143               ${ASAN_COMMON_RUNTIME_OBJECTS}
144       CFLAGS ${ASAN_CFLAGS}
145       DEFS ${ASAN_COMMON_DEFINITIONS})
146     add_dependencies(asan clang_rt.asan-${arch})
148     add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
149       SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
150       CFLAGS ${ASAN_CFLAGS}
151       DEFS ${ASAN_COMMON_DEFINITIONS})
152     add_dependencies(asan clang_rt.asan_cxx-${arch})
154     if (COMPILER_RT_BUILD_SHARED_ASAN)
155       add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
156         SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
157         CFLAGS ${ASAN_CFLAGS}
158         DEFS ${ASAN_COMMON_DEFINITIONS})
159       add_dependencies(asan clang_rt.asan-preinit-${arch})
161       add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
162         OUTPUT_NAME clang_rt.asan-${arch}
163         SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
164                 ${ASAN_COMMON_RUNTIME_OBJECTS}
165         CFLAGS ${ASAN_DYNAMIC_CFLAGS}
166         DEFS ${ASAN_DYNAMIC_DEFINITIONS})
167       target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
168       add_dependencies(asan clang_rt.asan-dynamic-${arch})
169     endif()
171     if (UNIX AND NOT ${arch} STREQUAL "i386")
172       add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
173       add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
174       add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
175       add_dependencies(asan clang_rt.asan-${arch}-symbols)
176     endif()
178     if (WIN32)
179       add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
180         SOURCES asan_dll_thunk.cc
181                 $<TARGET_OBJECTS:RTInterception.${arch}>
182         CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK -DSANITIZER_DLL_THUNK
183         DEFS ${ASAN_COMMON_DEFINITIONS})
184       add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
185     endif()
186   endforeach()
187 endif()
189 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
190 add_dependencies(asan asan_blacklist)
191 add_dependencies(compiler-rt asan)
193 add_subdirectory(scripts)
195 if(COMPILER_RT_INCLUDE_TESTS)
196   add_subdirectory(tests)
197 endif()