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