blob: 8cdb06161f5c23afb34058f71a449024c5a93d54 [file] [log] [blame]
Daniel Eratb8cf9492015-07-06 13:18:13 -06001# 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 Vakulenko45779222016-03-17 10:36:19 -07005import("//build/buildflag_header.gni")
Daniel Eratb8cf9492015-07-06 13:18:13 -06006import("//build/config/allocator.gni")
Alex Vakulenko0d205d72016-01-15 13:02:14 -08007import("//build/config/compiler/compiler.gni")
8
Alex Vakulenko0d205d72016-01-15 13:02:14 -08009declare_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 Eratb8cf9492015-07-06 13:18:13 -060015
Jay Civelli3a83cdd2017-03-22 17:31:44 -070016# The Windows-only allocator shim is only enabled for Release static builds, and
17# is mutually exclusive with the generalized shim.
18win_use_allocator_shim = is_win && !is_component_build && !is_debug &&
19 !use_experimental_allocator_shim && !is_asan
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070020
Daniel Eratb8cf9492015-07-06 13:18:13 -060021# This "allocator" meta-target will forward to the default allocator according
22# to the build settings.
23group("allocator") {
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070024 public_deps = []
25 deps = []
Alex Vakulenko0d205d72016-01-15 13:02:14 -080026
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070027 if (use_allocator == "tcmalloc") {
28 deps += [ ":tcmalloc" ]
29 }
Alex Vakulenko45779222016-03-17 10:36:19 -070030
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070031 if (win_use_allocator_shim) {
32 public_deps += [ ":allocator_shim" ]
33 }
Alex Vakulenko0d205d72016-01-15 13:02:14 -080034}
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.
42config("allocator_shim_define") {
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070043 if (win_use_allocator_shim) {
Alex Vakulenko0d205d72016-01-15 13:02:14 -080044 defines = [ "ALLOCATOR_SHIM" ]
45 }
46}
47
48config("tcmalloc_flags") {
Alex Vakulenko45779222016-03-17 10:36:19 -070049 defines = []
Alex Vakulenko0d205d72016-01-15 13:02:14 -080050 if (enable_debugallocation) {
Alex Vakulenko45779222016-03-17 10:36:19 -070051 defines += [
Alex Vakulenko0d205d72016-01-15 13:02:14 -080052 # Use debugallocation for Debug builds to catch problems early
53 # and cleanly, http://crbug.com/30715 .
54 "TCMALLOC_FOR_DEBUGALLOCATION",
55 ]
56 }
Alex Vakulenko45779222016-03-17 10:36:19 -070057 if (use_experimental_allocator_shim) {
58 defines += [ "TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC" ]
59 }
Alex Vakulenko0d205d72016-01-15 13:02:14 -080060 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 Eratb8cf9492015-07-06 13:18:13 -060075 ]
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070076 } 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 Eratb8cf9492015-07-06 13:18:13 -060088 }
89}
90
Luis Hector Chavez94ffa552016-05-25 15:29:35 -070091# This config is only used on Windows static release builds for the
92# allocator shim.
93if (win_use_allocator_shim) {
94 source_set("allocator_shim") {
95 sources = [
96 "allocator_shim_win.cc",
97 "allocator_shim_win.h",
Jay Civelli3a83cdd2017-03-22 17:31:44 -070098 "winheap_stubs_win.cc",
99 "winheap_stubs_win.h",
Daniel Eratb8cf9492015-07-06 13:18:13 -0600100 ]
Luis Hector Chavez94ffa552016-05-25 15:29:35 -0700101 configs += [ ":allocator_shim_define" ]
Daniel Eratb8cf9492015-07-06 13:18:13 -0600102 }
103}
104
105if (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 Vakulenko0d205d72016-01-15 13:02:14 -0800116 # Generated for our configuration from tcmalloc's build
Daniel Eratb8cf9492015-07-06 13:18:13 -0600117 # 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 Vakulenko0d205d72016-01-15 13:02:14 -0800127 "$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 Eratb8cf9492015-07-06 13:18:13 -0600137
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 Eratb8cf9492015-07-06 13:18:13 -0600155 "$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 Eratb8cf9492015-07-06 13:18:13 -0600164 "$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 Eratb8cf9492015-07-06 13:18:13 -0600181 "$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 Eratb8cf9492015-07-06 13:18:13 -0600203 "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 Vakulenko0d205d72016-01-15 13:02:14 -0800220 configs += [
221 "//build/config/compiler:no_chromium_code",
222 ":tcmalloc_flags",
223 ]
Daniel Eratb8cf9492015-07-06 13:18:13 -0600224
Jay Civelli3a83cdd2017-03-22 17:31:44 -0700225 # 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 Eratb8cf9492015-07-06 13:18:13 -0600233 deps = []
234
Alex Vakulenko0d205d72016-01-15 13:02:14 -0800235 if (enable_profiling) {
236 sources += [
Daniel Eratb8cf9492015-07-06 13:18:13 -0600237 "$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 Vakulenko0d205d72016-01-15 13:02:14 -0800245 defines += [ "ENABLE_PROFILING=1" ]
Daniel Eratb8cf9492015-07-06 13:18:13 -0600246 }
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 Eratb8cf9492015-07-06 13:18:13 -0600253 ]
254
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -0700255 # 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 Eratb8cf9492015-07-06 13:18:13 -0600265
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 Vakulenko0d205d72016-01-15 13:02:14 -0800281 configs -= [ "//build/config/compiler:default_optimization" ]
Daniel Eratb8cf9492015-07-06 13:18:13 -0600282 configs += [ "//build/config/compiler:optimize_max" ]
283 }
284
285 deps += [ "//base/third_party/dynamic_annotations" ]
Daniel Eratb8cf9492015-07-06 13:18:13 -0600286 }
Alex Vakulenko0d205d72016-01-15 13:02:14 -0800287} # use_allocator == "tcmalloc"
Alex Vakulenko45779222016-03-17 10:36:19 -0700288
289buildflag_header("features") {
290 header = "features.h"
Jay Civelli3a83cdd2017-03-22 17:31:44 -0700291 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 Vakulenko45779222016-03-17 10:36:19 -0700295}
296
Jay Civelli3a83cdd2017-03-22 17:31:44 -0700297# Used to shim malloc symbols on Android. see //base/allocator/README.md.
298config("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 Vakulenko45779222016-03-17 10:36:19 -0700309}