xref: /aosp_15_r20/external/compiler-rt/lib/tsan/CMakeLists.txt (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot# Build for the ThreadSanitizer runtime support library.
2*7c3d14c8STreehugger Robot
3*7c3d14c8STreehugger Robotinclude_directories(..)
4*7c3d14c8STreehugger Robot
5*7c3d14c8STreehugger Robotset(TSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
6*7c3d14c8STreehugger Robot# SANITIZER_COMMON_CFLAGS contains -fPIC, but it's performance-critical for
7*7c3d14c8STreehugger Robot# TSan runtime to be built with -fPIE to reduce the number of register spills.
8*7c3d14c8STreehugger Robotappend_list_if(COMPILER_RT_HAS_FPIE_FLAG -fPIE TSAN_CFLAGS)
9*7c3d14c8STreehugger Robotappend_rtti_flag(OFF TSAN_CFLAGS)
10*7c3d14c8STreehugger Robot
11*7c3d14c8STreehugger Robotif(COMPILER_RT_TSAN_DEBUG_OUTPUT)
12*7c3d14c8STreehugger Robot  # Add extra debug information to TSan runtime. This configuration is rarely
13*7c3d14c8STreehugger Robot  # used, but we need to support it so that debug output will not bitrot.
14*7c3d14c8STreehugger Robot  list(APPEND TSAN_CFLAGS -DTSAN_COLLECT_STATS=1
15*7c3d14c8STreehugger Robot                          -DTSAN_DEBUG_OUTPUT=2)
16*7c3d14c8STreehugger Robotendif()
17*7c3d14c8STreehugger Robot
18*7c3d14c8STreehugger Robotset(TSAN_RTL_CFLAGS ${TSAN_CFLAGS})
19*7c3d14c8STreehugger Robotappend_list_if(COMPILER_RT_HAS_MSSE3_FLAG -msse3 TSAN_RTL_CFLAGS)
20*7c3d14c8STreehugger Robotappend_list_if(SANITIZER_LIMIT_FRAME_SIZE -Wframe-larger-than=512
21*7c3d14c8STreehugger Robot               TSAN_RTL_CFLAGS)
22*7c3d14c8STreehugger Robotappend_list_if(COMPILER_RT_HAS_WGLOBAL_CONSTRUCTORS_FLAG -Wglobal-constructors
23*7c3d14c8STreehugger Robot               TSAN_RTL_CFLAGS)
24*7c3d14c8STreehugger Robot
25*7c3d14c8STreehugger Robotset(TSAN_SOURCES
26*7c3d14c8STreehugger Robot  rtl/tsan_clock.cc
27*7c3d14c8STreehugger Robot  rtl/tsan_debugging.cc
28*7c3d14c8STreehugger Robot  rtl/tsan_fd.cc
29*7c3d14c8STreehugger Robot  rtl/tsan_flags.cc
30*7c3d14c8STreehugger Robot  rtl/tsan_ignoreset.cc
31*7c3d14c8STreehugger Robot  rtl/tsan_interceptors.cc
32*7c3d14c8STreehugger Robot  rtl/tsan_interface.cc
33*7c3d14c8STreehugger Robot  rtl/tsan_interface_ann.cc
34*7c3d14c8STreehugger Robot  rtl/tsan_interface_atomic.cc
35*7c3d14c8STreehugger Robot  rtl/tsan_interface_java.cc
36*7c3d14c8STreehugger Robot  rtl/tsan_malloc_mac.cc
37*7c3d14c8STreehugger Robot  rtl/tsan_md5.cc
38*7c3d14c8STreehugger Robot  rtl/tsan_mman.cc
39*7c3d14c8STreehugger Robot  rtl/tsan_mutex.cc
40*7c3d14c8STreehugger Robot  rtl/tsan_mutexset.cc
41*7c3d14c8STreehugger Robot  rtl/tsan_preinit.cc
42*7c3d14c8STreehugger Robot  rtl/tsan_report.cc
43*7c3d14c8STreehugger Robot  rtl/tsan_rtl.cc
44*7c3d14c8STreehugger Robot  rtl/tsan_rtl_mutex.cc
45*7c3d14c8STreehugger Robot  rtl/tsan_rtl_proc.cc
46*7c3d14c8STreehugger Robot  rtl/tsan_rtl_report.cc
47*7c3d14c8STreehugger Robot  rtl/tsan_rtl_thread.cc
48*7c3d14c8STreehugger Robot  rtl/tsan_stack_trace.cc
49*7c3d14c8STreehugger Robot  rtl/tsan_stat.cc
50*7c3d14c8STreehugger Robot  rtl/tsan_suppressions.cc
51*7c3d14c8STreehugger Robot  rtl/tsan_symbolize.cc
52*7c3d14c8STreehugger Robot  rtl/tsan_sync.cc)
53*7c3d14c8STreehugger Robot
54*7c3d14c8STreehugger Robotset(TSAN_CXX_SOURCES
55*7c3d14c8STreehugger Robot  rtl/tsan_new_delete.cc)
56*7c3d14c8STreehugger Robot
57*7c3d14c8STreehugger Robotif(APPLE)
58*7c3d14c8STreehugger Robot  list(APPEND TSAN_SOURCES
59*7c3d14c8STreehugger Robot    rtl/tsan_interceptors_mac.cc
60*7c3d14c8STreehugger Robot    rtl/tsan_libdispatch_mac.cc
61*7c3d14c8STreehugger Robot    rtl/tsan_platform_mac.cc
62*7c3d14c8STreehugger Robot    rtl/tsan_platform_posix.cc)
63*7c3d14c8STreehugger Robotelseif(UNIX)
64*7c3d14c8STreehugger Robot  # Assume Linux
65*7c3d14c8STreehugger Robot  list(APPEND TSAN_SOURCES
66*7c3d14c8STreehugger Robot    rtl/tsan_platform_linux.cc
67*7c3d14c8STreehugger Robot    rtl/tsan_platform_posix.cc)
68*7c3d14c8STreehugger Robotendif()
69*7c3d14c8STreehugger Robot
70*7c3d14c8STreehugger Robotset(TSAN_HEADERS
71*7c3d14c8STreehugger Robot  rtl/tsan_clock.h
72*7c3d14c8STreehugger Robot  rtl/tsan_defs.h
73*7c3d14c8STreehugger Robot  rtl/tsan_dense_alloc.h
74*7c3d14c8STreehugger Robot  rtl/tsan_fd.h
75*7c3d14c8STreehugger Robot  rtl/tsan_flags.h
76*7c3d14c8STreehugger Robot  rtl/tsan_flags.inc
77*7c3d14c8STreehugger Robot  rtl/tsan_ignoreset.h
78*7c3d14c8STreehugger Robot  rtl/tsan_interceptors.h
79*7c3d14c8STreehugger Robot  rtl/tsan_interface_ann.h
80*7c3d14c8STreehugger Robot  rtl/tsan_interface.h
81*7c3d14c8STreehugger Robot  rtl/tsan_interface_inl.h
82*7c3d14c8STreehugger Robot  rtl/tsan_interface_java.h
83*7c3d14c8STreehugger Robot  rtl/tsan_mman.h
84*7c3d14c8STreehugger Robot  rtl/tsan_mutex.h
85*7c3d14c8STreehugger Robot  rtl/tsan_mutexset.h
86*7c3d14c8STreehugger Robot  rtl/tsan_platform.h
87*7c3d14c8STreehugger Robot  rtl/tsan_report.h
88*7c3d14c8STreehugger Robot  rtl/tsan_rtl.h
89*7c3d14c8STreehugger Robot  rtl/tsan_stack_trace.h
90*7c3d14c8STreehugger Robot  rtl/tsan_stat.h
91*7c3d14c8STreehugger Robot  rtl/tsan_suppressions.h
92*7c3d14c8STreehugger Robot  rtl/tsan_symbolize.h
93*7c3d14c8STreehugger Robot  rtl/tsan_sync.h
94*7c3d14c8STreehugger Robot  rtl/tsan_trace.h
95*7c3d14c8STreehugger Robot  rtl/tsan_update_shadow_word_inl.h
96*7c3d14c8STreehugger Robot  rtl/tsan_vector.h)
97*7c3d14c8STreehugger Robot
98*7c3d14c8STreehugger Robotset(TSAN_RUNTIME_LIBRARIES)
99*7c3d14c8STreehugger Robotadd_custom_target(tsan)
100*7c3d14c8STreehugger Robotset_target_properties(tsan PROPERTIES FOLDER "Compiler-RT Misc")
101*7c3d14c8STreehugger Robot
102*7c3d14c8STreehugger Robotif(APPLE)
103*7c3d14c8STreehugger Robot  set(TSAN_ASM_SOURCES rtl/tsan_rtl_amd64.S)
104*7c3d14c8STreehugger Robot  # Xcode will try to compile this file as C ('clang -x c'), and that will fail.
105*7c3d14c8STreehugger Robot  if (${CMAKE_GENERATOR} STREQUAL "Xcode")
106*7c3d14c8STreehugger Robot    enable_language(ASM)
107*7c3d14c8STreehugger Robot  else()
108*7c3d14c8STreehugger Robot    # Pass ASM file directly to the C++ compiler.
109*7c3d14c8STreehugger Robot    set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES LANGUAGE C)
110*7c3d14c8STreehugger Robot  endif()
111*7c3d14c8STreehugger Robot  add_compiler_rt_runtime(clang_rt.tsan
112*7c3d14c8STreehugger Robot    SHARED
113*7c3d14c8STreehugger Robot    OS ${TSAN_SUPPORTED_OS}
114*7c3d14c8STreehugger Robot    ARCHS ${TSAN_SUPPORTED_ARCH}
115*7c3d14c8STreehugger Robot    SOURCES ${TSAN_SOURCES} ${TSAN_CXX_SOURCES} ${TSAN_ASM_SOURCES}
116*7c3d14c8STreehugger Robot    OBJECT_LIBS RTInterception
117*7c3d14c8STreehugger Robot                RTSanitizerCommon
118*7c3d14c8STreehugger Robot                RTSanitizerCommonLibc
119*7c3d14c8STreehugger Robot                RTUbsan
120*7c3d14c8STreehugger Robot    CFLAGS ${TSAN_RTL_CFLAGS}
121*7c3d14c8STreehugger Robot    PARENT_TARGET tsan)
122*7c3d14c8STreehugger Robot  add_compiler_rt_object_libraries(RTTsan_dynamic
123*7c3d14c8STreehugger Robot    OS ${TSAN_SUPPORTED_OS}
124*7c3d14c8STreehugger Robot    ARCHS ${TSAN_SUPPORTED_ARCH}
125*7c3d14c8STreehugger Robot    SOURCES ${TSAN_SOURCES} ${TSAN_CXX_SOURCES} ${TSAN_ASM_SOURCES}
126*7c3d14c8STreehugger Robot    CFLAGS ${TSAN_RTL_CFLAGS})
127*7c3d14c8STreehugger Robot
128*7c3d14c8STreehugger Robot  # Build and check Go runtime.
129*7c3d14c8STreehugger Robot  set(BUILDGO_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/go/buildgo.sh)
130*7c3d14c8STreehugger Robot  add_custom_target(GotsanRuntimeCheck
131*7c3d14c8STreehugger Robot    COMMAND env "CC=${CMAKE_C_COMPILER} ${OSX_SYSROOT_FLAG}"
132*7c3d14c8STreehugger Robot            IN_TMPDIR=1 SILENT=1 ${BUILDGO_SCRIPT}
133*7c3d14c8STreehugger Robot    DEPENDS tsan ${BUILDGO_SCRIPT}
134*7c3d14c8STreehugger Robot    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/go
135*7c3d14c8STreehugger Robot    COMMENT "Checking TSan Go runtime..."
136*7c3d14c8STreehugger Robot    VERBATIM)
137*7c3d14c8STreehugger Robotelse()
138*7c3d14c8STreehugger Robot  foreach(arch ${TSAN_SUPPORTED_ARCH})
139*7c3d14c8STreehugger Robot    if(arch STREQUAL "x86_64")
140*7c3d14c8STreehugger Robot      set(TSAN_ASM_SOURCES rtl/tsan_rtl_amd64.S)
141*7c3d14c8STreehugger Robot      # Pass ASM file directly to the C++ compiler.
142*7c3d14c8STreehugger Robot      set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES
143*7c3d14c8STreehugger Robot        LANGUAGE C)
144*7c3d14c8STreehugger Robot      # Sanity check for Go runtime.
145*7c3d14c8STreehugger Robot      set(BUILDGO_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/go/buildgo.sh)
146*7c3d14c8STreehugger Robot      add_custom_target(GotsanRuntimeCheck
147*7c3d14c8STreehugger Robot        COMMAND env "CC=${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
148*7c3d14c8STreehugger Robot                IN_TMPDIR=1 SILENT=1 ${BUILDGO_SCRIPT}
149*7c3d14c8STreehugger Robot        DEPENDS clang_rt.tsan-${arch} ${BUILDGO_SCRIPT}
150*7c3d14c8STreehugger Robot        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/go
151*7c3d14c8STreehugger Robot        COMMENT "Checking TSan Go runtime..."
152*7c3d14c8STreehugger Robot        VERBATIM)
153*7c3d14c8STreehugger Robot    elseif(arch STREQUAL "aarch64")
154*7c3d14c8STreehugger Robot      set(TSAN_ASM_SOURCES rtl/tsan_rtl_aarch64.S)
155*7c3d14c8STreehugger Robot      # Pass ASM file directly to the C++ compiler.
156*7c3d14c8STreehugger Robot      set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES
157*7c3d14c8STreehugger Robot        LANGUAGE C)
158*7c3d14c8STreehugger Robot   elseif(arch MATCHES "powerpc64|powerpc64le")
159*7c3d14c8STreehugger Robot     set(TSAN_ASM_SOURCES rtl/tsan_rtl_ppc64.S)
160*7c3d14c8STreehugger Robot     # Pass ASM file directly to the C++ compiler.
161*7c3d14c8STreehugger Robot     set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES
162*7c3d14c8STreehugger Robot       LANGUAGE C)
163*7c3d14c8STreehugger Robot    else()
164*7c3d14c8STreehugger Robot      set(TSAN_ASM_SOURCES)
165*7c3d14c8STreehugger Robot    endif()
166*7c3d14c8STreehugger Robot    add_compiler_rt_runtime(clang_rt.tsan
167*7c3d14c8STreehugger Robot      STATIC
168*7c3d14c8STreehugger Robot      ARCHS ${arch}
169*7c3d14c8STreehugger Robot      SOURCES ${TSAN_SOURCES} ${TSAN_ASM_SOURCES}
170*7c3d14c8STreehugger Robot              $<TARGET_OBJECTS:RTInterception.${arch}>
171*7c3d14c8STreehugger Robot              $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
172*7c3d14c8STreehugger Robot              $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
173*7c3d14c8STreehugger Robot              $<TARGET_OBJECTS:RTUbsan.${arch}>
174*7c3d14c8STreehugger Robot      CFLAGS ${TSAN_RTL_CFLAGS})
175*7c3d14c8STreehugger Robot    add_compiler_rt_runtime(clang_rt.tsan_cxx
176*7c3d14c8STreehugger Robot      STATIC
177*7c3d14c8STreehugger Robot      ARCHS ${arch}
178*7c3d14c8STreehugger Robot      SOURCES ${TSAN_CXX_SOURCES}
179*7c3d14c8STreehugger Robot              $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
180*7c3d14c8STreehugger Robot      CFLAGS ${TSAN_RTL_CFLAGS})
181*7c3d14c8STreehugger Robot    list(APPEND TSAN_RUNTIME_LIBRARIES clang_rt.tsan-${arch}
182*7c3d14c8STreehugger Robot                                       clang_rt.tsan_cxx-${arch})
183*7c3d14c8STreehugger Robot    add_sanitizer_rt_symbols(clang_rt.tsan
184*7c3d14c8STreehugger Robot      ARCHS ${arch}
185*7c3d14c8STreehugger Robot      EXTRA rtl/tsan.syms.extra)
186*7c3d14c8STreehugger Robot    add_sanitizer_rt_symbols(clang_rt.tsan_cxx
187*7c3d14c8STreehugger Robot      ARCHS ${arch}
188*7c3d14c8STreehugger Robot      EXTRA rtl/tsan.syms.extra)
189*7c3d14c8STreehugger Robot    add_dependencies(tsan clang_rt.tsan-${arch}
190*7c3d14c8STreehugger Robot                          clang_rt.tsan_cxx-${arch}
191*7c3d14c8STreehugger Robot                          clang_rt.tsan-${arch}-symbols
192*7c3d14c8STreehugger Robot                          clang_rt.tsan_cxx-${arch}-symbols)
193*7c3d14c8STreehugger Robot  endforeach()
194*7c3d14c8STreehugger Robotendif()
195*7c3d14c8STreehugger Robot
196*7c3d14c8STreehugger Robotadd_dependencies(compiler-rt tsan)
197*7c3d14c8STreehugger Robot
198*7c3d14c8STreehugger Robot# Make sure that non-platform-specific files don't include any system headers.
199*7c3d14c8STreehugger Robot# FreeBSD does not install a number of Clang-provided headers for the compiler
200*7c3d14c8STreehugger Robot# in the base system due to incompatibilities between FreeBSD's and Clang's
201*7c3d14c8STreehugger Robot# versions. As a workaround do not use --sysroot=. on FreeBSD until this is
202*7c3d14c8STreehugger Robot# addressed.
203*7c3d14c8STreehugger Robotif(COMPILER_RT_HAS_SYSROOT_FLAG AND NOT CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
204*7c3d14c8STreehugger Robot  file(GLOB _tsan_generic_sources rtl/tsan*)
205*7c3d14c8STreehugger Robot  file(GLOB _tsan_platform_sources rtl/tsan*posix* rtl/tsan*mac*
206*7c3d14c8STreehugger Robot                                   rtl/tsan*linux*)
207*7c3d14c8STreehugger Robot  list(REMOVE_ITEM _tsan_generic_sources ${_tsan_platform_sources})
208*7c3d14c8STreehugger Robot  set_source_files_properties(${_tsan_generic_sources}
209*7c3d14c8STreehugger Robot    PROPERTIES COMPILE_FLAGS "--sysroot=.")
210*7c3d14c8STreehugger Robotendif()
211*7c3d14c8STreehugger Robot
212*7c3d14c8STreehugger Robot# Build libcxx instrumented with TSan.
213*7c3d14c8STreehugger Robotif(COMPILER_RT_HAS_LIBCXX_SOURCES AND
214*7c3d14c8STreehugger Robot   COMPILER_RT_TEST_COMPILER_ID STREQUAL "Clang")
215*7c3d14c8STreehugger Robot  set(libcxx_tsan_deps)
216*7c3d14c8STreehugger Robot  foreach(arch ${TSAN_SUPPORTED_ARCH})
217*7c3d14c8STreehugger Robot    get_target_flags_for_arch(${arch} TARGET_CFLAGS)
218*7c3d14c8STreehugger Robot    set(LIBCXX_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libcxx_tsan_${arch})
219*7c3d14c8STreehugger Robot    add_custom_libcxx(libcxx_tsan_${arch} ${LIBCXX_PREFIX}
220*7c3d14c8STreehugger Robot      DEPS ${TSAN_RUNTIME_LIBRARIES}
221*7c3d14c8STreehugger Robot      CFLAGS ${TARGET_CFLAGS} -fsanitize=thread)
222*7c3d14c8STreehugger Robot    list(APPEND libcxx_tsan_deps libcxx_tsan_${arch})
223*7c3d14c8STreehugger Robot  endforeach()
224*7c3d14c8STreehugger Robot
225*7c3d14c8STreehugger Robot  add_custom_target(libcxx_tsan DEPENDS ${libcxx_tsan_deps})
226*7c3d14c8STreehugger Robotendif()
227*7c3d14c8STreehugger Robot
228*7c3d14c8STreehugger Robotif(COMPILER_RT_INCLUDE_TESTS)
229*7c3d14c8STreehugger Robot  add_subdirectory(tests)
230*7c3d14c8STreehugger Robotendif()
231