summaryrefslogtreecommitdiff
path: root/base/allocator/BUILD.gn
blob: 8cdb06161f5c23afb34058f71a449024c5a93d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/buildflag_header.gni")
import("//build/config/allocator.gni")
import("//build/config/compiler/compiler.gni")

declare_args() {
  # Provide a way to force disable debugallocation in Debug builds,
  # e.g. for profiling (it's more rare to profile Debug builds,
  # but people sometimes need to do that).
  enable_debugallocation = is_debug
}

# The Windows-only allocator shim is only enabled for Release static builds, and
# is mutually exclusive with the generalized shim.
win_use_allocator_shim = is_win && !is_component_build && !is_debug &&
                         !use_experimental_allocator_shim && !is_asan

# This "allocator" meta-target will forward to the default allocator according
# to the build settings.
group("allocator") {
  public_deps = []
  deps = []

  if (use_allocator == "tcmalloc") {
    deps += [ ":tcmalloc" ]
  }

  if (win_use_allocator_shim) {
    public_deps += [ ":allocator_shim" ]
  }
}

# This config defines ALLOCATOR_SHIM in the same conditions that the allocator
# shim will be used by the allocator target.
#
# TODO(brettw) this is only used in one place and is kind of mess, because it
# assumes that the library using it will eventually be linked with
# //base/allocator in the default way. Clean this up and delete this.
config("allocator_shim_define") {
  if (win_use_allocator_shim) {
    defines = [ "ALLOCATOR_SHIM" ]
  }
}

config("tcmalloc_flags") {
  defines = []
  if (enable_debugallocation) {
    defines += [
      # Use debugallocation for Debug builds to catch problems early
      # and cleanly, http://crbug.com/30715 .
      "TCMALLOC_FOR_DEBUGALLOCATION",
    ]
  }
  if (use_experimental_allocator_shim) {
    defines += [ "TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC" ]
  }
  if (is_clang) {
    cflags = [
      # tcmalloc initializes some fields in the wrong order.
      "-Wno-reorder",

      # tcmalloc contains some unused local template specializations.
      "-Wno-unused-function",

      # tcmalloc uses COMPILE_ASSERT without static_assert but with
      # typedefs.
      "-Wno-unused-local-typedefs",

      # for magic2_ in debugallocation.cc (only built in Debug builds)
      # typedefs.
      "-Wno-unused-private-field",
    ]
  } else {
    cflags = []
  }

  if (is_linux || is_android) {
    # We enable all warnings by default, but upstream disables a few.
    # Keep "-Wno-*" flags in sync with upstream by comparing against:
    # http://code.google.com/p/google-perftools/source/browse/trunk/Makefile.am
    cflags += [
      "-Wno-sign-compare",
      "-Wno-unused-result",
    ]
  }
}

# This config is only used on Windows static release builds for the
# allocator shim.
if (win_use_allocator_shim) {
  source_set("allocator_shim") {
    sources = [
      "allocator_shim_win.cc",
      "allocator_shim_win.h",
      "winheap_stubs_win.cc",
      "winheap_stubs_win.h",
    ]
    configs += [ ":allocator_shim_define" ]
  }
}

if (use_allocator == "tcmalloc") {
  # tcmalloc currently won't compile on Android.
  source_set("tcmalloc") {
    tcmalloc_dir = "//third_party/tcmalloc/chromium"

    # Don't check tcmalloc's includes. These files include various files like
    # base/foo.h and they actually refer to tcmalloc's forked copy of base
    # rather than the regular one, which confuses the header checker.
    check_includes = false

    sources = [
      # Generated for our configuration from tcmalloc's build
      # and checked in.
      "$tcmalloc_dir/src/config.h",
      "$tcmalloc_dir/src/config_android.h",
      "$tcmalloc_dir/src/config_linux.h",
      "$tcmalloc_dir/src/config_win.h",

      # tcmalloc native and forked files.
      "$tcmalloc_dir/src/base/abort.cc",
      "$tcmalloc_dir/src/base/abort.h",
      "$tcmalloc_dir/src/base/arm_instruction_set_select.h",
      "$tcmalloc_dir/src/base/atomicops-internals-arm-generic.h",
      "$tcmalloc_dir/src/base/atomicops-internals-arm-v6plus.h",
      "$tcmalloc_dir/src/base/atomicops-internals-linuxppc.h",
      "$tcmalloc_dir/src/base/atomicops-internals-macosx.h",
      "$tcmalloc_dir/src/base/atomicops-internals-windows.h",
      "$tcmalloc_dir/src/base/atomicops-internals-x86.cc",
      "$tcmalloc_dir/src/base/atomicops-internals-x86.h",
      "$tcmalloc_dir/src/base/atomicops.h",
      "$tcmalloc_dir/src/base/commandlineflags.h",
      "$tcmalloc_dir/src/base/cycleclock.h",

      # We don't list dynamic_annotations.c since its copy is already
      # present in the dynamic_annotations target.
      "$tcmalloc_dir/src/base/elf_mem_image.cc",
      "$tcmalloc_dir/src/base/elf_mem_image.h",
      "$tcmalloc_dir/src/base/linuxthreads.cc",
      "$tcmalloc_dir/src/base/linuxthreads.h",
      "$tcmalloc_dir/src/base/logging.cc",
      "$tcmalloc_dir/src/base/logging.h",
      "$tcmalloc_dir/src/base/low_level_alloc.cc",
      "$tcmalloc_dir/src/base/low_level_alloc.h",
      "$tcmalloc_dir/src/base/spinlock.cc",
      "$tcmalloc_dir/src/base/spinlock.h",
      "$tcmalloc_dir/src/base/spinlock_internal.cc",
      "$tcmalloc_dir/src/base/spinlock_internal.h",
      "$tcmalloc_dir/src/base/synchronization_profiling.h",
      "$tcmalloc_dir/src/base/sysinfo.cc",
      "$tcmalloc_dir/src/base/sysinfo.h",
      "$tcmalloc_dir/src/base/vdso_support.cc",
      "$tcmalloc_dir/src/base/vdso_support.h",
      "$tcmalloc_dir/src/central_freelist.cc",
      "$tcmalloc_dir/src/central_freelist.h",
      "$tcmalloc_dir/src/common.cc",
      "$tcmalloc_dir/src/common.h",

      # #included by debugallocation_shim.cc
      #"$tcmalloc_dir/src/debugallocation.cc",
      "$tcmalloc_dir/src/free_list.cc",
      "$tcmalloc_dir/src/free_list.h",
      "$tcmalloc_dir/src/heap-profile-table.cc",
      "$tcmalloc_dir/src/heap-profile-table.h",
      "$tcmalloc_dir/src/heap-profiler.cc",
      "$tcmalloc_dir/src/internal_logging.cc",
      "$tcmalloc_dir/src/internal_logging.h",
      "$tcmalloc_dir/src/linked_list.h",
      "$tcmalloc_dir/src/malloc_extension.cc",
      "$tcmalloc_dir/src/malloc_hook-inl.h",
      "$tcmalloc_dir/src/malloc_hook.cc",
      "$tcmalloc_dir/src/maybe_threads.cc",
      "$tcmalloc_dir/src/maybe_threads.h",
      "$tcmalloc_dir/src/memory_region_map.cc",
      "$tcmalloc_dir/src/memory_region_map.h",
      "$tcmalloc_dir/src/page_heap.cc",
      "$tcmalloc_dir/src/page_heap.h",
      "$tcmalloc_dir/src/raw_printer.cc",
      "$tcmalloc_dir/src/raw_printer.h",
      "$tcmalloc_dir/src/sampler.cc",
      "$tcmalloc_dir/src/sampler.h",
      "$tcmalloc_dir/src/span.cc",
      "$tcmalloc_dir/src/span.h",
      "$tcmalloc_dir/src/stack_trace_table.cc",
      "$tcmalloc_dir/src/stack_trace_table.h",
      "$tcmalloc_dir/src/stacktrace.cc",
      "$tcmalloc_dir/src/static_vars.cc",
      "$tcmalloc_dir/src/static_vars.h",
      "$tcmalloc_dir/src/symbolize.cc",
      "$tcmalloc_dir/src/symbolize.h",
      "$tcmalloc_dir/src/system-alloc.cc",
      "$tcmalloc_dir/src/system-alloc.h",

      # #included by debugallocation_shim.cc
      #"$tcmalloc_dir/src/tcmalloc.cc",
      "$tcmalloc_dir/src/thread_cache.cc",
      "$tcmalloc_dir/src/thread_cache.h",
      "$tcmalloc_dir/src/windows/port.cc",
      "$tcmalloc_dir/src/windows/port.h",
      "debugallocation_shim.cc",

      # These are both #included by allocator_shim for maximal linking.
      #"generic_allocators.cc",
      #"win_allocator.cc",
    ]

    # Disable the heap checker in tcmalloc.
    defines = [ "NO_HEAP_CHECK" ]

    include_dirs = [
      ".",
      "$tcmalloc_dir/src/base",
      "$tcmalloc_dir/src",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      "//build/config/compiler:no_chromium_code",
      ":tcmalloc_flags",
    ]

    # Thumb mode disabled due to bug in clang integrated assembler
    # TODO(https://llvm.org/bugs/show_bug.cgi?id=31058)
    configs -= [ "//build/config/compiler:compiler_arm_thumb" ]
    configs += [ "//build/config/compiler:compiler_arm" ]

    # TODO(crbug.com/633719) Make tcmalloc work with AFDO if possible.
    configs -= [ "//build/config/compiler:afdo" ]

    deps = []

    if (enable_profiling) {
      sources += [
        "$tcmalloc_dir/src/base/thread_lister.c",
        "$tcmalloc_dir/src/base/thread_lister.h",
        "$tcmalloc_dir/src/profile-handler.cc",
        "$tcmalloc_dir/src/profile-handler.h",
        "$tcmalloc_dir/src/profiledata.cc",
        "$tcmalloc_dir/src/profiledata.h",
        "$tcmalloc_dir/src/profiler.cc",
      ]
      defines += [ "ENABLE_PROFILING=1" ]
    }

    if (is_linux || is_android) {
      sources -= [
        "$tcmalloc_dir/src/system-alloc.h",
        "$tcmalloc_dir/src/windows/port.cc",
        "$tcmalloc_dir/src/windows/port.h",
      ]

      # Compiling tcmalloc with -fvisibility=default is only necessary when
      # not using the allocator shim, which provides the correct visibility
      # annotations for those symbols which need to be exported (see
      # //base/allocator/allocator_shim_override_glibc_weak_symbols.h and
      # //base/allocator/allocator_shim_internals.h for the definition of
      # SHIM_ALWAYS_EXPORT).
      if (!use_experimental_allocator_shim) {
        configs -= [ "//build/config/gcc:symbol_visibility_hidden" ]
        configs += [ "//build/config/gcc:symbol_visibility_default" ]
      }

      ldflags = [
        # Don't let linker rip this symbol out, otherwise the heap&cpu
        # profilers will not initialize properly on startup.
        "-Wl,-uIsHeapProfilerRunning,-uProfilerStart",

        # Do the same for heap leak checker.
        "-Wl,-u_Z21InitialMallocHook_NewPKvj,-u_Z22InitialMallocHook_MMapPKvS0_jiiix,-u_Z22InitialMallocHook_SbrkPKvi",
        "-Wl,-u_Z21InitialMallocHook_NewPKvm,-u_Z22InitialMallocHook_MMapPKvS0_miiil,-u_Z22InitialMallocHook_SbrkPKvl",
        "-Wl,-u_ZN15HeapLeakChecker12IgnoreObjectEPKv,-u_ZN15HeapLeakChecker14UnIgnoreObjectEPKv",
      ]
    }

    # Make sure the allocation library is optimized as much as possible when
    # we"re in release mode.
    if (!is_debug) {
      configs -= [ "//build/config/compiler:default_optimization" ]
      configs += [ "//build/config/compiler:optimize_max" ]
    }

    deps += [ "//base/third_party/dynamic_annotations" ]
  }
}  # use_allocator == "tcmalloc"

buildflag_header("features") {
  header = "features.h"
  flags = [
    "USE_EXPERIMENTAL_ALLOCATOR_SHIM=$use_experimental_allocator_shim",
    "ENABLE_WIN_ALLOCATOR_SHIM_TESTS=($use_experimental_allocator_shim || $win_use_allocator_shim)",
  ]
}

# Used to shim malloc symbols on Android. see //base/allocator/README.md.
config("wrap_malloc_symbols") {
  ldflags = [
    "-Wl,-wrap,calloc",
    "-Wl,-wrap,free",
    "-Wl,-wrap,malloc",
    "-Wl,-wrap,memalign",
    "-Wl,-wrap,posix_memalign",
    "-Wl,-wrap,pvalloc",
    "-Wl,-wrap,realloc",
    "-Wl,-wrap,valloc",
  ]
}