1add_compiler_rt_component(scudo_standalone)
2
3include_directories(../.. include)
4
5set(SCUDO_CFLAGS)
6
7list(APPEND SCUDO_CFLAGS
8  -Werror=conversion
9  -Wall
10  -Wextra
11  -pedantic
12  -g
13  -nostdinc++)
14
15# Remove -stdlib= which is unused when passing -nostdinc++.
16string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
17
18append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SCUDO_CFLAGS)
19
20append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SCUDO_CFLAGS)
21
22append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic SCUDO_CFLAGS)
23
24# FIXME: find cleaner way to agree with GWPAsan flags
25append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SCUDO_CFLAGS)
26
27if(COMPILER_RT_DEBUG)
28  list(APPEND SCUDO_CFLAGS -O0 -DSCUDO_DEBUG=1)
29else()
30  list(APPEND SCUDO_CFLAGS -O3)
31endif()
32
33set(SCUDO_LINK_FLAGS)
34
35list(APPEND SCUDO_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro)
36
37list(APPEND SCUDO_LINK_FLAGS -ffunction-sections -fdata-sections -Wl,--gc-sections)
38
39# We don't use the C++ standard library, so avoid including it by mistake.
40append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SCUDO_LINK_FLAGS)
41append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SCUDO_LINK_FLAGS)
42
43if(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH)
44  list(APPEND SCUDO_CFLAGS "--sysroot=${COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH}")
45endif()
46
47if(ANDROID)
48  list(APPEND SCUDO_CFLAGS -fno-emulated-tls)
49
50# Put the shared library in the global group. For more details, see
51# android-changes-for-ndk-developers.md#changes-to-library-search-order
52  append_list_if(COMPILER_RT_HAS_Z_GLOBAL -Wl,-z,global SCUDO_LINK_FLAGS)
53endif()
54
55set(SCUDO_HEADERS
56  allocator_config.h
57  atomic_helpers.h
58  bytemap.h
59  checksum.h
60  chunk.h
61  combined.h
62  common.h
63  flags_parser.h
64  flags.h
65  fuchsia.h
66  internal_defs.h
67  linux.h
68  list.h
69  local_cache.h
70  memtag.h
71  mutex.h
72  options.h
73  platform.h
74  primary32.h
75  primary64.h
76  quarantine.h
77  release.h
78  report.h
79  rss_limit_checker.h
80  secondary.h
81  size_class_map.h
82  stack_depot.h
83  stats.h
84  string_utils.h
85  tsd_exclusive.h
86  tsd_shared.h
87  tsd.h
88  vector.h
89  wrappers_c_checks.h
90  wrappers_c.h
91
92  include/scudo/interface.h
93  )
94
95set(SCUDO_SOURCES
96  checksum.cpp
97  common.cpp
98  crc32_hw.cpp
99  flags_parser.cpp
100  flags.cpp
101  fuchsia.cpp
102  linux.cpp
103  release.cpp
104  report.cpp
105  rss_limit_checker.cpp
106  string_utils.cpp
107  )
108
109# Enable the necessary instruction set for scudo_crc32.cpp, if available.
110# Newer compiler versions use -mcrc32 rather than -msse4.2.
111if (COMPILER_RT_HAS_MCRC32_FLAG)
112  set_source_files_properties(crc32_hw.cpp PROPERTIES COMPILE_FLAGS -mcrc32)
113elseif (COMPILER_RT_HAS_MSSE4_2_FLAG)
114  set_source_files_properties(crc32_hw.cpp PROPERTIES COMPILE_FLAGS -msse4.2)
115endif()
116
117# Enable the AArch64 CRC32 feature for crc32_hw.cpp, if available.
118# Note that it is enabled by default starting with armv8.1-a.
119if (COMPILER_RT_HAS_MCRC_FLAG)
120  set_source_files_properties(crc32_hw.cpp PROPERTIES COMPILE_FLAGS -mcrc)
121endif()
122
123set(SCUDO_SOURCES_C_WRAPPERS
124  wrappers_c.cpp
125  )
126
127set(SCUDO_SOURCES_CXX_WRAPPERS
128  wrappers_cpp.cpp
129  )
130
131set(SCUDO_OBJECT_LIBS)
132set(SCUDO_LINK_LIBS)
133
134if (COMPILER_RT_HAS_GWP_ASAN)
135  if(COMPILER_RT_USE_LLVM_UNWINDER)
136    list(APPEND SCUDO_LINK_LIBS ${COMPILER_RT_UNWINDER_LINK_LIBS} dl)
137  elseif (COMPILER_RT_HAS_GCC_S_LIB)
138    list(APPEND SCUDO_LINK_LIBS gcc_s)
139  elseif (COMPILER_RT_HAS_GCC_LIB)
140    list(APPEND SCUDO_LINK_LIBS gcc)
141  elseif (NOT COMPILER_RT_USE_BUILTINS_LIBRARY)
142    message(FATAL_ERROR "No suitable unwinder library")
143  endif()
144
145  add_dependencies(scudo_standalone gwp_asan)
146  list(APPEND SCUDO_OBJECT_LIBS
147       RTGwpAsan RTGwpAsanBacktraceLibc RTGwpAsanSegvHandler
148       RTGwpAsanOptionsParser)
149
150  append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fno-omit-frame-pointer
151                                                         -mno-omit-leaf-frame-pointer
152                 SCUDO_CFLAGS)
153  list(APPEND SCUDO_CFLAGS -DGWP_ASAN_HOOKS)
154
155endif()
156
157if(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC)
158  include_directories(${COMPILER_RT_BINARY_DIR}/../libc/include/)
159
160  set(SCUDO_DEPS libc-headers)
161
162  list(APPEND SCUDO_CFLAGS "-ffreestanding")
163endif()
164
165append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SCUDO_LINK_FLAGS)
166
167append_list_if(FUCHSIA zircon SCUDO_LINK_LIBS)
168
169if(COMPILER_RT_DEFAULT_TARGET_ARCH MATCHES "mips|mips64|mipsel|mips64el")
170  list(APPEND SCUDO_LINK_LIBS atomic)
171endif()
172
173if(COMPILER_RT_HAS_SCUDO_STANDALONE)
174  add_compiler_rt_object_libraries(RTScudoStandalone
175    ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
176    SOURCES ${SCUDO_SOURCES}
177    ADDITIONAL_HEADERS ${SCUDO_HEADERS}
178    CFLAGS ${SCUDO_CFLAGS}
179    DEPS ${SCUDO_DEPS})
180  add_compiler_rt_object_libraries(RTScudoStandaloneCWrappers
181    ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
182    SOURCES ${SCUDO_SOURCES_C_WRAPPERS}
183    ADDITIONAL_HEADERS ${SCUDO_HEADERS}
184    CFLAGS ${SCUDO_CFLAGS}
185    DEPS ${SCUDO_DEPS})
186  add_compiler_rt_object_libraries(RTScudoStandaloneCxxWrappers
187    ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
188    SOURCES ${SCUDO_SOURCES_CXX_WRAPPERS}
189    ADDITIONAL_HEADERS ${SCUDO_HEADERS}
190    CFLAGS ${SCUDO_CFLAGS}
191    DEPS ${SCUDO_DEPS})
192
193  add_compiler_rt_runtime(clang_rt.scudo_standalone
194    STATIC
195    ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
196    SOURCES ${SCUDO_SOURCES} ${SCUDO_SOURCES_C_WRAPPERS}
197    ADDITIONAL_HEADERS ${SCUDO_HEADERS}
198    CFLAGS ${SCUDO_CFLAGS}
199    DEPS ${SCUDO_DEPS}
200    OBJECT_LIBS ${SCUDO_OBJECT_LIBS}
201    PARENT_TARGET scudo_standalone)
202  add_compiler_rt_runtime(clang_rt.scudo_standalone_cxx
203    STATIC
204    ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
205    SOURCES ${SCUDO_SOURCES_CXX_WRAPPERS}
206    ADDITIONAL_HEADERS ${SCUDO_HEADERS}
207    CFLAGS ${SCUDO_CFLAGS}
208    DEPS ${SCUDO_DEPS}
209    PARENT_TARGET scudo_standalone)
210
211  if(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED)
212    add_compiler_rt_runtime(clang_rt.scudo_standalone
213      SHARED
214      ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
215      SOURCES ${SCUDO_SOURCES} ${SCUDO_SOURCES_C_WRAPPERS} ${SCUDO_SOURCES_CXX_WRAPPERS}
216      ADDITIONAL_HEADERS ${SCUDO_HEADERS}
217      CFLAGS ${SCUDO_CFLAGS}
218      DEPS ${SCUDO_DEPS}
219      OBJECT_LIBS ${SCUDO_OBJECT_LIBS}
220      LINK_FLAGS ${SCUDO_LINK_FLAGS}
221      LINK_LIBS ${SCUDO_LINK_LIBS}
222      PARENT_TARGET scudo_standalone)
223  endif()
224
225  add_subdirectory(benchmarks)
226  if(COMPILER_RT_INCLUDE_TESTS)
227    add_subdirectory(tests)
228  endif()
229endif()
230