1# Copyright 2015 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/config/chrome_build.gni")
6import("//build/config/chromecast_build.gni")
7import("//build/config/chromeos/args.gni")
8import("//build/config/compiler/pgo/pgo.gni")
9import("//build/config/sanitizers/sanitizers.gni")
10import("//build/toolchain/cc_wrapper.gni")
11import("//build/toolchain/goma.gni")
12import("//build/toolchain/toolchain.gni")
13import("//build_overrides/build.gni")
14
15if (is_android) {
16  import("//build/config/android/abi.gni")
17}
18if (current_cpu == "arm" || current_cpu == "arm64") {
19  import("//build/config/arm.gni")
20}
21
22if (is_apple) {
23  import("//build/config/mac/symbols.gni")
24}
25
26declare_args() {
27  # Default to warnings as errors for default workflow, where we catch
28  # warnings with known toolchains. Allow overriding this e.g. for Chromium
29  # builds on Linux that could use a different version of the compiler.
30  # With GCC, warnings in no-Chromium code are always not treated as errors.
31  treat_warnings_as_errors = true
32
33  # How many symbols to include in the build. This affects the performance of
34  # the build since the symbols are large and dealing with them is slow.
35  #   2 means regular build with symbols.
36  #   1 means minimal symbols, usually enough for backtraces only. Symbols with
37  # internal linkage (static functions or those in anonymous namespaces) may not
38  # appear when using this level.
39  #   0 means no symbols.
40  #   -1 means auto-set according to debug/release and platform.
41  symbol_level = -1
42
43  # Android-only: Strip the debug info of libraries within lib.unstripped to
44  # reduce size. As long as symbol_level > 0, this will still allow stacks to be
45  # symbolized.
46  strip_debug_info = false
47
48  # Compile in such a way as to enable profiling of the generated code. For
49  # example, don't omit the frame pointer and leave in symbols.
50  enable_profiling = false
51
52  # use_debug_fission: whether to use split DWARF debug info
53  # files. This can reduce link time significantly, but is incompatible
54  # with some utilities such as icecc and ccache. Requires gold and
55  # gcc >= 4.8 or clang.
56  # http://gcc.gnu.org/wiki/DebugFission
57  #
58  # This is a placeholder value indicating that the code below should set
59  # the default.  This is necessary to delay the evaluation of the default
60  # value expression until after its input values such as use_gold have
61  # been set, e.g. by a toolchain_args() block.
62  use_debug_fission = "default"
63
64  # Enables support for ThinLTO, which links 3x-10x faster than full LTO. See
65  # also http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html
66  # Use it by default on official-optimized android and Chrome OS builds, but
67  # not ARC or linux-chromeos since it's been seen to not play nicely with
68  # Chrome's clang. crbug.com/1033839
69  use_thin_lto = is_cfi || (is_official_build &&
70                            (target_os == "android" ||
71                             (target_os == "chromeos" && is_chromeos_device)))
72
73  # If true, use Goma for ThinLTO code generation where applicable.
74  use_goma_thin_lto = false
75
76  # Whether we're using a sample profile collected on an architecture different
77  # than the one we're compiling for.
78  #
79  # It's currently not possible to collect AFDO profiles on anything but
80  # x86{,_64}.
81  using_mismatched_sample_profile = current_cpu != "x64" && current_cpu != "x86"
82
83  # Whether an error should be raised on attempts to make debug builds with
84  # is_component_build=false. Very large debug symbols can have unwanted side
85  # effects so this is enforced by default for chromium.
86  forbid_non_component_debug_builds = build_with_chromium
87
88  # Exclude unwind tables by default for official builds as unwinding can be
89  # done from stack dumps produced by Crashpad at a later time "offline" in the
90  # crash server. Since this increases binary size, we don't recommend including
91  # them in shipping builds.
92  # For unofficial (e.g. development) builds and non-Chrome branded (e.g. Cronet
93  # which doesn't use Crashpad, crbug.com/479283) builds it's useful to be able
94  # to unwind at runtime.
95  exclude_unwind_tables = is_official_build
96
97  # Where to redirect clang crash diagnoses
98  clang_diagnostic_dir =
99      rebase_path("//tools/clang/crashreports", root_build_dir)
100
101  # Experimental option to mark binaries as compatible with Shadow
102  # Stack of Control-flow Enforcement Technology (CET). If Windows version
103  # and hardware supports the feature and it's enabled by OS then additional
104  # additional validation of return address will be performed as mitigation
105  # against Return-oriented programming (ROP).
106  # https://chromium.googlesource.com/chromium/src/+/master/docs/design/sandbox.md#cet-shadow-stack
107  enable_cet_shadow_stack = false
108}
109
110assert(!is_cfi || use_thin_lto, "CFI requires ThinLTO")
111
112# If true, optimize for size. Does not affect windows builds.
113# Linux & Mac favor speed over size.
114# TODO(brettw) it's weird that Mac and desktop Linux are different. We should
115# explore favoring size over speed in this case as well.
116optimize_for_size = is_android || is_chromecast || is_fuchsia || is_ios
117
118declare_args() {
119  # Whether we should consider the profile we're using to be accurate. Accurate
120  # profiles have the benefit of (potentially substantial) binary size
121  # reductions, by instructing the compiler to optimize cold and uncovered
122  # functions heavily for size. This often comes at the cost of performance.
123  sample_profile_is_accurate = optimize_for_size
124}
125
126# Determine whether to enable or disable frame pointers, based on the platform
127# and build arguments.
128if (is_chromeos) {
129  # ChromeOS generally prefers frame pointers, to support CWP.
130  # However, Clang does not currently generate usable frame pointers in ARM
131  # 32-bit builds (https://bugs.llvm.org/show_bug.cgi?id=18505) so disable them
132  # there to avoid the unnecessary overhead.
133  enable_frame_pointers = current_cpu != "arm"
134} else if (is_apple || is_linux || is_chromeos) {
135  enable_frame_pointers = true
136} else if (is_win) {
137  # 64-bit Windows ABI doesn't support frame pointers.
138  if (current_cpu == "x64") {
139    enable_frame_pointers = false
140  } else {
141    enable_frame_pointers = true
142  }
143} else if (is_android) {
144  enable_frame_pointers =
145      enable_profiling ||
146      # Ensure that stacks from arm64 crash dumps are usable (crbug.com/391706).
147      current_cpu == "arm64" ||
148      # For x86 Android, unwind tables are huge without frame pointers
149      # (crbug.com/762629). Enabling frame pointers grows the code size slightly
150      # but overall shrinks binaries considerably by avoiding huge unwind
151      # tables.
152      (current_cpu == "x86" && !exclude_unwind_tables && optimize_for_size) ||
153      using_sanitizer ||
154      # For caller-callee instrumentation version which needs frame pointers to
155      # get the caller address.
156      use_call_graph
157} else {
158  # Explicitly ask for frame pointers, otherwise:
159  # * Stacks may be missing for sanitizer and profiling builds.
160  # * Debug tcmalloc can crash (crbug.com/636489).
161  enable_frame_pointers = using_sanitizer || enable_profiling || is_debug
162}
163
164# In general assume that if we have frame pointers then we can use them to
165# unwind the stack. However, this requires that they are enabled by default for
166# most translation units, that they are emitted correctly, and that the
167# compiler or platform provides a way to access them.
168can_unwind_with_frame_pointers = enable_frame_pointers
169if (current_cpu == "arm" && arm_use_thumb) {
170  # We cannot currently unwind ARM Thumb frame pointers correctly.
171  # See https://bugs.llvm.org/show_bug.cgi?id=18505
172  can_unwind_with_frame_pointers = false
173} else if (is_win) {
174  # Windows 32-bit does provide frame pointers, but the compiler does not
175  # provide intrinsics to access them, so we don't use them.
176  can_unwind_with_frame_pointers = false
177}
178
179assert(!can_unwind_with_frame_pointers || enable_frame_pointers)
180
181# Unwinding with CFI table is only possible on static library builds and
182# requried only when frame pointers are not enabled.
183can_unwind_with_cfi_table = is_android && !is_component_build &&
184                            !enable_frame_pointers && current_cpu == "arm"
185
186# Whether or not cfi table should be enabled on arm.
187# TODO(crbug.com/1090409): Replace can_unwind_with_cfi_table with this once
188# sampling profiler is enabled on android.
189enable_arm_cfi_table = is_android && !is_component_build && current_cpu == "arm"
190
191declare_args() {
192  # Set to true to use lld, the LLVM linker.
193  use_lld = is_clang && !is_apple
194}
195
196declare_args() {
197  # Whether to use the gold linker from binutils instead of lld or bfd.
198  use_gold =
199      !is_bsd && !use_lld && !(is_chromecast && is_linux &&
200                    (current_cpu == "arm" || current_cpu == "mipsel")) &&
201      ((is_linux && (current_cpu == "x64" || current_cpu == "x86" ||
202                     current_cpu == "arm" || current_cpu == "arm64" ||
203                     current_cpu == "mipsel" || current_cpu == "mips64el")) ||
204       (is_android && (current_cpu == "x86" || current_cpu == "x64" ||
205                       current_cpu == "arm" || current_cpu == "arm64")))
206}
207
208# Use relative paths for debug info. This is important to make the build
209# results independent of the checkout and build directory names, which
210# in turn is important for goma compile hit rate.
211# Setting this to true may make it harder to debug binaries on Linux, see
212# https://chromium.googlesource.com/chromium/src/+/master/docs/linux/debugging.md#Source-level-debug-with-fdebug_compilation_dir
213# It's not clear if the crash server will correctly handle dSYMs with relative
214# paths, so we disable this feature for official benefit. The main benefit is
215# deterministic builds to reduce compile times, so this is less relevant for
216# official builders.
217strip_absolute_paths_from_debug_symbols_default =
218    is_android || is_fuchsia || is_nacl || (is_win && use_lld) || is_linux ||
219    is_chromeos || (is_apple && !enable_dsyms)
220
221# If the platform uses stripped absolute paths by default, then we don't expose
222# it as a configuration option. If this is causing problems, please file a bug.
223if (strip_absolute_paths_from_debug_symbols_default) {
224  strip_absolute_paths_from_debug_symbols = true
225} else {
226  declare_args() {
227    strip_absolute_paths_from_debug_symbols = false
228  }
229}
230
231# If it wasn't manually set, then default use_debug_fission to false.
232assert(
233    use_debug_fission == "default" || use_debug_fission || !use_debug_fission,
234    "Invalid use_debug_fission.")
235if (use_debug_fission == "default") {
236  use_debug_fission = is_debug && !is_android && !is_fuchsia && !is_apple &&
237                      !is_win && (use_gold || use_lld) && cc_wrapper == ""
238}
239
240# If it wasn't manually set, set to an appropriate default.
241assert(symbol_level >= -1 && symbol_level <= 2, "Invalid symbol_level")
242if (symbol_level == -1) {
243  if (is_android && !is_component_build && !use_debug_fission) {
244    # Reduce symbol level when it will cause invalid elf files to be created
245    # (due to file size). https://crbug.com/648948.
246    symbol_level = 1
247  } else if (is_chromeos_device) {
248    # Use lower symbol level in Simple Chrome build for faster link time.
249    # For Simple Chrome, this should take precedence over is_official_build,
250    # turned on by --internal.
251    if ((target_cpu == "x64" || target_cpu == "x86") && !is_debug) {
252      # For release x86/x64 build, specify symbol_level=0 for faster link time.
253      # x86/x64 shows backtraces with symbol_level=0 (arm requires
254      # symbol_level=1).
255      symbol_level = 0
256    } else {
257      symbol_level = 1
258    }
259  } else if (using_sanitizer) {
260    # Sanitizers need line table info for stack traces. They don't need type
261    # info or variable info, so we can leave that out to speed up the build.
262    # Sanitizers also require symbols for filename suppressions to work.
263    symbol_level = 1
264  } else if ((!is_nacl && !is_linux && !is_chromeos && !is_fuchsia &&
265              current_os != "aix") || is_debug || is_official_build ||
266             is_chromecast) {
267    # Linux builds slower by having symbols as part of the target binary,
268    # whereas Mac and Windows have them separate, so in Release Linux, default
269    # them off, but keep them on for Official builds and Chromecast builds.
270    symbol_level = 2
271  } else {
272    symbol_level = 0
273  }
274}
275
276# Split dwarf works only for symbol_level == 2.
277use_debug_fission = use_debug_fission && symbol_level == 2
278
279# Non-component debug builds with symbol_level = 2 are an undesirable (very slow
280# build times) and unsupported (some test binaries will fail with > 4 GB PDBs)
281# combination. This is only checked when current_toolchain == default_toolchain
282# because the is_component_build flag is set to false in various components of
283# the build (like nacl) and we don't want to assert on those.
284# iOS does not support component builds so add an exception for this platform.
285if (forbid_non_component_debug_builds) {
286  assert(symbol_level != 2 || current_toolchain != default_toolchain ||
287             is_component_build || !is_debug || is_ios,
288         "Can't do non-component debug builds at symbol_level=2")
289}
290
291# Assert that the configuration isn't going to hit https://crbug.com/648948.
292# An exception is made when target_os == "chromeos" as we only use the Android
293# toolchain there to build relatively small binaries.
294assert(
295    ignore_elf32_limitations || !is_android || target_os == "chromeos" ||
296        is_component_build || symbol_level < 2 || use_debug_fission,
297    "Android 32-bit non-component builds without DWARF Fission cannot " +
298        "have symbol_level=2 due to 4GiB file size limit, see " +
299        "https://crbug.com/648948. " + "If you really want to try this out, " +
300        "set ignore_elf32_limitations=true.")
301