xref: /llvm-project/compiler-rt/lib/memprof/CMakeLists.txt (revision 3ecba1ad1f6d2e961948ddfdb165f22621d0788f)
1# Build for the Memory Profiler runtime support library.
2
3set(MEMPROF_SOURCES
4  memprof_allocator.cpp
5  memprof_descriptions.cpp
6  memprof_flags.cpp
7  memprof_interceptors.cpp
8  memprof_interceptors_memintrinsics.cpp
9  memprof_linux.cpp
10  memprof_malloc_linux.cpp
11  memprof_mibmap.cpp
12  memprof_posix.cpp
13  memprof_rawprofile.cpp
14  memprof_rtl.cpp
15  memprof_shadow_setup.cpp
16  memprof_stack.cpp
17  memprof_stats.cpp
18  memprof_thread.cpp
19  )
20
21set(MEMPROF_CXX_SOURCES
22  memprof_new_delete.cpp
23  )
24
25set(MEMPROF_PREINIT_SOURCES
26  memprof_preinit.cpp
27  )
28
29SET(MEMPROF_HEADERS
30  memprof_allocator.h
31  memprof_descriptions.h
32  memprof_flags.h
33  memprof_flags.inc
34  memprof_init_version.h
35  memprof_interceptors.h
36  memprof_interceptors_memintrinsics.h
37  memprof_interface_internal.h
38  memprof_internal.h
39  memprof_mapping.h
40  memprof_mibmap.h
41  memprof_rawprofile.h
42  memprof_stack.h
43  memprof_stats.h
44  memprof_thread.h
45  )
46
47include_directories(..)
48include_directories(../../include)
49
50set(MEMPROF_CFLAGS ${SANITIZER_COMMON_CFLAGS})
51set(MEMPROF_COMMON_DEFINITIONS "")
52
53# Too many existing bugs, needs cleanup.
54append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format MEMPROF_CFLAGS)
55
56append_rtti_flag(OFF MEMPROF_CFLAGS)
57
58set(MEMPROF_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
59
60set(MEMPROF_DYNAMIC_DEFINITIONS
61  ${MEMPROF_COMMON_DEFINITIONS} MEMPROF_DYNAMIC=1)
62
63set(MEMPROF_DYNAMIC_CFLAGS ${MEMPROF_CFLAGS})
64append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
65  -ftls-model=initial-exec MEMPROF_DYNAMIC_CFLAGS)
66
67set(MEMPROF_DYNAMIC_LIBS
68  ${COMPILER_RT_UNWINDER_LINK_LIBS}
69  ${SANITIZER_CXX_ABI_LIBRARIES}
70  ${SANITIZER_COMMON_LINK_LIBS})
71
72append_list_if(COMPILER_RT_HAS_LIBDL dl MEMPROF_DYNAMIC_LIBS)
73append_list_if(COMPILER_RT_HAS_LIBRT rt MEMPROF_DYNAMIC_LIBS)
74append_list_if(COMPILER_RT_HAS_LIBM m MEMPROF_DYNAMIC_LIBS)
75append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread MEMPROF_DYNAMIC_LIBS)
76append_list_if(COMPILER_RT_HAS_LIBLOG log MEMPROF_DYNAMIC_LIBS)
77
78# Compile MemProf sources into an object library.
79
80add_compiler_rt_object_libraries(RTMemprof_dynamic
81  OS ${SANITIZER_COMMON_SUPPORTED_OS}
82  ARCHS ${MEMPROF_SUPPORTED_ARCH}
83  SOURCES ${MEMPROF_SOURCES} ${MEMPROF_CXX_SOURCES}
84  ADDITIONAL_HEADERS ${MEMPROF_HEADERS}
85  CFLAGS ${MEMPROF_DYNAMIC_CFLAGS}
86  DEFS ${MEMPROF_DYNAMIC_DEFINITIONS}
87  DEPS ${MEMPROF_DEPS})
88
89add_compiler_rt_object_libraries(RTMemprof
90  ARCHS ${MEMPROF_SUPPORTED_ARCH}
91  SOURCES ${MEMPROF_SOURCES}
92  ADDITIONAL_HEADERS ${MEMPROF_HEADERS}
93  CFLAGS ${MEMPROF_CFLAGS}
94  DEFS ${MEMPROF_COMMON_DEFINITIONS}
95  DEPS ${MEMPROF_DEPS})
96add_compiler_rt_object_libraries(RTMemprof_cxx
97  ARCHS ${MEMPROF_SUPPORTED_ARCH}
98  SOURCES ${MEMPROF_CXX_SOURCES}
99  ADDITIONAL_HEADERS ${MEMPROF_HEADERS}
100  CFLAGS ${MEMPROF_CFLAGS}
101  DEFS ${MEMPROF_COMMON_DEFINITIONS}
102  DEPS ${MEMPROF_DEPS})
103add_compiler_rt_object_libraries(RTMemprof_preinit
104  ARCHS ${MEMPROF_SUPPORTED_ARCH}
105  SOURCES ${MEMPROF_PREINIT_SOURCES}
106  ADDITIONAL_HEADERS ${MEMPROF_HEADERS}
107  CFLAGS ${MEMPROF_CFLAGS}
108  DEFS ${MEMPROF_COMMON_DEFINITIONS}
109  DEPS ${MEMPROF_DEPS})
110
111file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
112add_compiler_rt_object_libraries(RTMemprof_dynamic_version_script_dummy
113  ARCHS ${MEMPROF_SUPPORTED_ARCH}
114  SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
115  CFLAGS ${MEMPROF_DYNAMIC_CFLAGS}
116  DEFS ${MEMPROF_DYNAMIC_DEFINITIONS}
117  DEPS ${MEMPROF_DEPS})
118
119# Build MemProf runtimes shipped with Clang.
120add_compiler_rt_component(memprof)
121
122# Build separate libraries for each target.
123
124set(MEMPROF_COMMON_RUNTIME_OBJECT_LIBS
125  RTInterception
126  RTSanitizerCommon
127  RTSanitizerCommonLibc
128  RTSanitizerCommonCoverage
129  RTSanitizerCommonSymbolizer
130  # FIXME: hangs.
131  # RTSanitizerCommonSymbolizerInternal
132)
133
134add_compiler_rt_runtime(clang_rt.memprof
135  STATIC
136  ARCHS ${MEMPROF_SUPPORTED_ARCH}
137  OBJECT_LIBS RTMemprof_preinit
138              RTMemprof
139              ${MEMPROF_COMMON_RUNTIME_OBJECT_LIBS}
140  CFLAGS ${MEMPROF_CFLAGS}
141  DEFS ${MEMPROF_COMMON_DEFINITIONS}
142  PARENT_TARGET memprof)
143
144add_compiler_rt_runtime(clang_rt.memprof_cxx
145  STATIC
146  ARCHS ${MEMPROF_SUPPORTED_ARCH}
147  OBJECT_LIBS RTMemprof_cxx
148  CFLAGS ${MEMPROF_CFLAGS}
149  DEFS ${MEMPROF_COMMON_DEFINITIONS}
150  PARENT_TARGET memprof)
151
152add_compiler_rt_runtime(clang_rt.memprof-preinit
153  STATIC
154  ARCHS ${MEMPROF_SUPPORTED_ARCH}
155  OBJECT_LIBS RTMemprof_preinit
156  CFLAGS ${MEMPROF_CFLAGS}
157  DEFS ${MEMPROF_COMMON_DEFINITIONS}
158  PARENT_TARGET memprof)
159
160foreach(arch ${MEMPROF_SUPPORTED_ARCH})
161  if (UNIX)
162    add_sanitizer_rt_version_list(clang_rt.memprof-dynamic-${arch}
163                                  LIBS clang_rt.memprof-${arch} clang_rt.memprof_cxx-${arch}
164                                  EXTRA memprof.syms.extra)
165    set(VERSION_SCRIPT_FLAG
166         -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.memprof-dynamic-${arch}.vers)
167    set_property(SOURCE
168      ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
169      APPEND PROPERTY
170      OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.memprof-dynamic-${arch}.vers)
171  else()
172    set(VERSION_SCRIPT_FLAG)
173  endif()
174
175  set(MEMPROF_DYNAMIC_WEAK_INTERCEPTION)
176
177  add_compiler_rt_runtime(clang_rt.memprof
178    SHARED
179    ARCHS ${arch}
180    OBJECT_LIBS ${MEMPROF_COMMON_RUNTIME_OBJECT_LIBS}
181            RTMemprof_dynamic
182            # The only purpose of RTMemprof_dynamic_version_script_dummy is to
183            # carry a dependency of the shared runtime on the version script.
184            # Replacing it with a straightforward
185            # add_dependencies(clang_rt.memprof-dynamic-${arch} clang_rt.memprof-dynamic-${arch}-version-list)
186            # generates an order-only dependency in ninja.
187            RTMemprof_dynamic_version_script_dummy
188            ${MEMPROF_DYNAMIC_WEAK_INTERCEPTION}
189    CFLAGS ${MEMPROF_DYNAMIC_CFLAGS}
190    LINK_FLAGS ${MEMPROF_DYNAMIC_LINK_FLAGS}
191              ${VERSION_SCRIPT_FLAG}
192    LINK_LIBS ${MEMPROF_DYNAMIC_LIBS}
193    DEFS ${MEMPROF_DYNAMIC_DEFINITIONS}
194    PARENT_TARGET memprof)
195
196  if (SANITIZER_USE_SYMBOLS)
197    add_sanitizer_rt_symbols(clang_rt.memprof_cxx
198      ARCHS ${arch})
199    add_dependencies(memprof clang_rt.memprof_cxx-${arch}-symbols)
200    add_sanitizer_rt_symbols(clang_rt.memprof
201      ARCHS ${arch}
202      EXTRA memprof.syms.extra)
203    add_dependencies(memprof clang_rt.memprof-${arch}-symbols)
204  endif()
205endforeach()
206
207
208if(COMPILER_RT_INCLUDE_TESTS)
209  add_subdirectory(tests)
210endif()
211