Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 5 | import("//build/buildflag_header.gni") |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 6 | import("//build/config/allocator.gni") |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 7 | import("//build/config/compiler/compiler.gni") |
| 8 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 9 | declare_args() { |
| 10 | # Provide a way to force disable debugallocation in Debug builds, |
| 11 | # e.g. for profiling (it's more rare to profile Debug builds, |
| 12 | # but people sometimes need to do that). |
| 13 | enable_debugallocation = is_debug |
| 14 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 15 | |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 16 | # The Windows-only allocator shim is only enabled for Release static builds, and |
| 17 | # is mutually exclusive with the generalized shim. |
| 18 | win_use_allocator_shim = is_win && !is_component_build && !is_debug && |
| 19 | !use_experimental_allocator_shim && !is_asan |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 20 | |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 21 | # This "allocator" meta-target will forward to the default allocator according |
| 22 | # to the build settings. |
| 23 | group("allocator") { |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 24 | public_deps = [] |
| 25 | deps = [] |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 26 | |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 27 | if (use_allocator == "tcmalloc") { |
| 28 | deps += [ ":tcmalloc" ] |
| 29 | } |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 30 | |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 31 | if (win_use_allocator_shim) { |
| 32 | public_deps += [ ":allocator_shim" ] |
| 33 | } |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | # This config defines ALLOCATOR_SHIM in the same conditions that the allocator |
| 37 | # shim will be used by the allocator target. |
| 38 | # |
| 39 | # TODO(brettw) this is only used in one place and is kind of mess, because it |
| 40 | # assumes that the library using it will eventually be linked with |
| 41 | # //base/allocator in the default way. Clean this up and delete this. |
| 42 | config("allocator_shim_define") { |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 43 | if (win_use_allocator_shim) { |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 44 | defines = [ "ALLOCATOR_SHIM" ] |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | config("tcmalloc_flags") { |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 49 | defines = [] |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 50 | if (enable_debugallocation) { |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 51 | defines += [ |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 52 | # Use debugallocation for Debug builds to catch problems early |
| 53 | # and cleanly, http://crbug.com/30715 . |
| 54 | "TCMALLOC_FOR_DEBUGALLOCATION", |
| 55 | ] |
| 56 | } |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 57 | if (use_experimental_allocator_shim) { |
| 58 | defines += [ "TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC" ] |
| 59 | } |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 60 | if (is_clang) { |
| 61 | cflags = [ |
| 62 | # tcmalloc initializes some fields in the wrong order. |
| 63 | "-Wno-reorder", |
| 64 | |
| 65 | # tcmalloc contains some unused local template specializations. |
| 66 | "-Wno-unused-function", |
| 67 | |
| 68 | # tcmalloc uses COMPILE_ASSERT without static_assert but with |
| 69 | # typedefs. |
| 70 | "-Wno-unused-local-typedefs", |
| 71 | |
| 72 | # for magic2_ in debugallocation.cc (only built in Debug builds) |
| 73 | # typedefs. |
| 74 | "-Wno-unused-private-field", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 75 | ] |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 76 | } else { |
| 77 | cflags = [] |
| 78 | } |
| 79 | |
| 80 | if (is_linux || is_android) { |
| 81 | # We enable all warnings by default, but upstream disables a few. |
| 82 | # Keep "-Wno-*" flags in sync with upstream by comparing against: |
| 83 | # http://code.google.com/p/google-perftools/source/browse/trunk/Makefile.am |
| 84 | cflags += [ |
| 85 | "-Wno-sign-compare", |
| 86 | "-Wno-unused-result", |
| 87 | ] |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 91 | # This config is only used on Windows static release builds for the |
| 92 | # allocator shim. |
| 93 | if (win_use_allocator_shim) { |
| 94 | source_set("allocator_shim") { |
| 95 | sources = [ |
| 96 | "allocator_shim_win.cc", |
| 97 | "allocator_shim_win.h", |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 98 | "winheap_stubs_win.cc", |
| 99 | "winheap_stubs_win.h", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 100 | ] |
Luis Hector Chavez | 94ffa55 | 2016-05-25 15:29:35 -0700 | [diff] [blame] | 101 | configs += [ ":allocator_shim_define" ] |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | if (use_allocator == "tcmalloc") { |
| 106 | # tcmalloc currently won't compile on Android. |
| 107 | source_set("tcmalloc") { |
| 108 | tcmalloc_dir = "//third_party/tcmalloc/chromium" |
| 109 | |
| 110 | # Don't check tcmalloc's includes. These files include various files like |
| 111 | # base/foo.h and they actually refer to tcmalloc's forked copy of base |
| 112 | # rather than the regular one, which confuses the header checker. |
| 113 | check_includes = false |
| 114 | |
| 115 | sources = [ |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 116 | # Generated for our configuration from tcmalloc's build |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 117 | # and checked in. |
| 118 | "$tcmalloc_dir/src/config.h", |
| 119 | "$tcmalloc_dir/src/config_android.h", |
| 120 | "$tcmalloc_dir/src/config_linux.h", |
| 121 | "$tcmalloc_dir/src/config_win.h", |
| 122 | |
| 123 | # tcmalloc native and forked files. |
| 124 | "$tcmalloc_dir/src/base/abort.cc", |
| 125 | "$tcmalloc_dir/src/base/abort.h", |
| 126 | "$tcmalloc_dir/src/base/arm_instruction_set_select.h", |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 127 | "$tcmalloc_dir/src/base/atomicops-internals-arm-generic.h", |
| 128 | "$tcmalloc_dir/src/base/atomicops-internals-arm-v6plus.h", |
| 129 | "$tcmalloc_dir/src/base/atomicops-internals-linuxppc.h", |
| 130 | "$tcmalloc_dir/src/base/atomicops-internals-macosx.h", |
| 131 | "$tcmalloc_dir/src/base/atomicops-internals-windows.h", |
| 132 | "$tcmalloc_dir/src/base/atomicops-internals-x86.cc", |
| 133 | "$tcmalloc_dir/src/base/atomicops-internals-x86.h", |
| 134 | "$tcmalloc_dir/src/base/atomicops.h", |
| 135 | "$tcmalloc_dir/src/base/commandlineflags.h", |
| 136 | "$tcmalloc_dir/src/base/cycleclock.h", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 137 | |
| 138 | # We don't list dynamic_annotations.c since its copy is already |
| 139 | # present in the dynamic_annotations target. |
| 140 | "$tcmalloc_dir/src/base/elf_mem_image.cc", |
| 141 | "$tcmalloc_dir/src/base/elf_mem_image.h", |
| 142 | "$tcmalloc_dir/src/base/linuxthreads.cc", |
| 143 | "$tcmalloc_dir/src/base/linuxthreads.h", |
| 144 | "$tcmalloc_dir/src/base/logging.cc", |
| 145 | "$tcmalloc_dir/src/base/logging.h", |
| 146 | "$tcmalloc_dir/src/base/low_level_alloc.cc", |
| 147 | "$tcmalloc_dir/src/base/low_level_alloc.h", |
| 148 | "$tcmalloc_dir/src/base/spinlock.cc", |
| 149 | "$tcmalloc_dir/src/base/spinlock.h", |
| 150 | "$tcmalloc_dir/src/base/spinlock_internal.cc", |
| 151 | "$tcmalloc_dir/src/base/spinlock_internal.h", |
| 152 | "$tcmalloc_dir/src/base/synchronization_profiling.h", |
| 153 | "$tcmalloc_dir/src/base/sysinfo.cc", |
| 154 | "$tcmalloc_dir/src/base/sysinfo.h", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 155 | "$tcmalloc_dir/src/base/vdso_support.cc", |
| 156 | "$tcmalloc_dir/src/base/vdso_support.h", |
| 157 | "$tcmalloc_dir/src/central_freelist.cc", |
| 158 | "$tcmalloc_dir/src/central_freelist.h", |
| 159 | "$tcmalloc_dir/src/common.cc", |
| 160 | "$tcmalloc_dir/src/common.h", |
| 161 | |
| 162 | # #included by debugallocation_shim.cc |
| 163 | #"$tcmalloc_dir/src/debugallocation.cc", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 164 | "$tcmalloc_dir/src/free_list.cc", |
| 165 | "$tcmalloc_dir/src/free_list.h", |
| 166 | "$tcmalloc_dir/src/heap-profile-table.cc", |
| 167 | "$tcmalloc_dir/src/heap-profile-table.h", |
| 168 | "$tcmalloc_dir/src/heap-profiler.cc", |
| 169 | "$tcmalloc_dir/src/internal_logging.cc", |
| 170 | "$tcmalloc_dir/src/internal_logging.h", |
| 171 | "$tcmalloc_dir/src/linked_list.h", |
| 172 | "$tcmalloc_dir/src/malloc_extension.cc", |
| 173 | "$tcmalloc_dir/src/malloc_hook-inl.h", |
| 174 | "$tcmalloc_dir/src/malloc_hook.cc", |
| 175 | "$tcmalloc_dir/src/maybe_threads.cc", |
| 176 | "$tcmalloc_dir/src/maybe_threads.h", |
| 177 | "$tcmalloc_dir/src/memory_region_map.cc", |
| 178 | "$tcmalloc_dir/src/memory_region_map.h", |
| 179 | "$tcmalloc_dir/src/page_heap.cc", |
| 180 | "$tcmalloc_dir/src/page_heap.h", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 181 | "$tcmalloc_dir/src/raw_printer.cc", |
| 182 | "$tcmalloc_dir/src/raw_printer.h", |
| 183 | "$tcmalloc_dir/src/sampler.cc", |
| 184 | "$tcmalloc_dir/src/sampler.h", |
| 185 | "$tcmalloc_dir/src/span.cc", |
| 186 | "$tcmalloc_dir/src/span.h", |
| 187 | "$tcmalloc_dir/src/stack_trace_table.cc", |
| 188 | "$tcmalloc_dir/src/stack_trace_table.h", |
| 189 | "$tcmalloc_dir/src/stacktrace.cc", |
| 190 | "$tcmalloc_dir/src/static_vars.cc", |
| 191 | "$tcmalloc_dir/src/static_vars.h", |
| 192 | "$tcmalloc_dir/src/symbolize.cc", |
| 193 | "$tcmalloc_dir/src/symbolize.h", |
| 194 | "$tcmalloc_dir/src/system-alloc.cc", |
| 195 | "$tcmalloc_dir/src/system-alloc.h", |
| 196 | |
| 197 | # #included by debugallocation_shim.cc |
| 198 | #"$tcmalloc_dir/src/tcmalloc.cc", |
| 199 | "$tcmalloc_dir/src/thread_cache.cc", |
| 200 | "$tcmalloc_dir/src/thread_cache.h", |
| 201 | "$tcmalloc_dir/src/windows/port.cc", |
| 202 | "$tcmalloc_dir/src/windows/port.h", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 203 | "debugallocation_shim.cc", |
| 204 | |
| 205 | # These are both #included by allocator_shim for maximal linking. |
| 206 | #"generic_allocators.cc", |
| 207 | #"win_allocator.cc", |
| 208 | ] |
| 209 | |
| 210 | # Disable the heap checker in tcmalloc. |
| 211 | defines = [ "NO_HEAP_CHECK" ] |
| 212 | |
| 213 | include_dirs = [ |
| 214 | ".", |
| 215 | "$tcmalloc_dir/src/base", |
| 216 | "$tcmalloc_dir/src", |
| 217 | ] |
| 218 | |
| 219 | configs -= [ "//build/config/compiler:chromium_code" ] |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 220 | configs += [ |
| 221 | "//build/config/compiler:no_chromium_code", |
| 222 | ":tcmalloc_flags", |
| 223 | ] |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 224 | |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 225 | # Thumb mode disabled due to bug in clang integrated assembler |
| 226 | # TODO(https://llvm.org/bugs/show_bug.cgi?id=31058) |
| 227 | configs -= [ "//build/config/compiler:compiler_arm_thumb" ] |
| 228 | configs += [ "//build/config/compiler:compiler_arm" ] |
| 229 | |
| 230 | # TODO(crbug.com/633719) Make tcmalloc work with AFDO if possible. |
| 231 | configs -= [ "//build/config/compiler:afdo" ] |
| 232 | |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 233 | deps = [] |
| 234 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 235 | if (enable_profiling) { |
| 236 | sources += [ |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 237 | "$tcmalloc_dir/src/base/thread_lister.c", |
| 238 | "$tcmalloc_dir/src/base/thread_lister.h", |
| 239 | "$tcmalloc_dir/src/profile-handler.cc", |
| 240 | "$tcmalloc_dir/src/profile-handler.h", |
| 241 | "$tcmalloc_dir/src/profiledata.cc", |
| 242 | "$tcmalloc_dir/src/profiledata.h", |
| 243 | "$tcmalloc_dir/src/profiler.cc", |
| 244 | ] |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 245 | defines += [ "ENABLE_PROFILING=1" ] |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (is_linux || is_android) { |
| 249 | sources -= [ |
| 250 | "$tcmalloc_dir/src/system-alloc.h", |
| 251 | "$tcmalloc_dir/src/windows/port.cc", |
| 252 | "$tcmalloc_dir/src/windows/port.h", |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 253 | ] |
| 254 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 255 | # Compiling tcmalloc with -fvisibility=default is only necessary when |
| 256 | # not using the allocator shim, which provides the correct visibility |
| 257 | # annotations for those symbols which need to be exported (see |
| 258 | # //base/allocator/allocator_shim_override_glibc_weak_symbols.h and |
| 259 | # //base/allocator/allocator_shim_internals.h for the definition of |
| 260 | # SHIM_ALWAYS_EXPORT). |
| 261 | if (!use_experimental_allocator_shim) { |
| 262 | configs -= [ "//build/config/gcc:symbol_visibility_hidden" ] |
| 263 | configs += [ "//build/config/gcc:symbol_visibility_default" ] |
| 264 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 265 | |
| 266 | ldflags = [ |
| 267 | # Don't let linker rip this symbol out, otherwise the heap&cpu |
| 268 | # profilers will not initialize properly on startup. |
| 269 | "-Wl,-uIsHeapProfilerRunning,-uProfilerStart", |
| 270 | |
| 271 | # Do the same for heap leak checker. |
| 272 | "-Wl,-u_Z21InitialMallocHook_NewPKvj,-u_Z22InitialMallocHook_MMapPKvS0_jiiix,-u_Z22InitialMallocHook_SbrkPKvi", |
| 273 | "-Wl,-u_Z21InitialMallocHook_NewPKvm,-u_Z22InitialMallocHook_MMapPKvS0_miiil,-u_Z22InitialMallocHook_SbrkPKvl", |
| 274 | "-Wl,-u_ZN15HeapLeakChecker12IgnoreObjectEPKv,-u_ZN15HeapLeakChecker14UnIgnoreObjectEPKv", |
| 275 | ] |
| 276 | } |
| 277 | |
| 278 | # Make sure the allocation library is optimized as much as possible when |
| 279 | # we"re in release mode. |
| 280 | if (!is_debug) { |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 281 | configs -= [ "//build/config/compiler:default_optimization" ] |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 282 | configs += [ "//build/config/compiler:optimize_max" ] |
| 283 | } |
| 284 | |
| 285 | deps += [ "//base/third_party/dynamic_annotations" ] |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 286 | } |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 287 | } # use_allocator == "tcmalloc" |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 288 | |
| 289 | buildflag_header("features") { |
| 290 | header = "features.h" |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 291 | flags = [ |
| 292 | "USE_EXPERIMENTAL_ALLOCATOR_SHIM=$use_experimental_allocator_shim", |
| 293 | "ENABLE_WIN_ALLOCATOR_SHIM_TESTS=($use_experimental_allocator_shim || $win_use_allocator_shim)", |
| 294 | ] |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame^] | 297 | # Used to shim malloc symbols on Android. see //base/allocator/README.md. |
| 298 | config("wrap_malloc_symbols") { |
| 299 | ldflags = [ |
| 300 | "-Wl,-wrap,calloc", |
| 301 | "-Wl,-wrap,free", |
| 302 | "-Wl,-wrap,malloc", |
| 303 | "-Wl,-wrap,memalign", |
| 304 | "-Wl,-wrap,posix_memalign", |
| 305 | "-Wl,-wrap,pvalloc", |
| 306 | "-Wl,-wrap,realloc", |
| 307 | "-Wl,-wrap,valloc", |
| 308 | ] |
Alex Vakulenko | 4577922 | 2016-03-17 10:36:19 -0700 | [diff] [blame] | 309 | } |