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/sanitizers/sanitizers.gni")
6import("//build/toolchain/toolchain.gni")
7
8import("//build/misc/overrides/build.gni")
9
10if (is_ohos) {
11  import("//build/config/ohos/abi.gni")
12}
13if (is_android) {
14  import("//build_plugins/config/aosp/abi.gni")
15}
16if (current_cpu == "arm" || current_cpu == "arm64") {
17  import("//build/config/arm.gni")
18}
19
20is_ohos_or_android = is_ohos || is_android
21
22declare_args() {
23  # How many symbols to include in the build. This affects the performance of
24  # the build since the symbols are large and dealing with them is slow.
25  #   2 means regular build with symbols.
26  #   1 means minimal symbols, usually enough for backtraces only. Symbols with
27  # internal linkage (static functions or those in anonymous namespaces) may not
28  # appear when using this level.
29  #   0 means no symbols.
30  #   -1 means auto-set according to debug/release and platform.
31  symbol_level = -1
32
33  # ohos-only: Strip the debug info of libraries within lib.unstripped to
34  # reduce size. As long as symbol_level > 0, this will still allow stacks to be
35  # symbolized.
36  strip_debug_info = false
37
38  # Compile in such a way as to enable profiling of the generated code. For
39  # example, don't omit the frame pointer and leave in symbols.
40  enable_profiling = false
41
42  # use_debug_fission: whether to use split DWARF debug info
43  # files. This can reduce link time significantly, but is incompatible
44  # with some utilities such as icecc and ccache. Requires gold and
45  # gcc >= 4.8 or clang.
46  # http://gcc.gnu.org/wiki/DebugFission
47  #
48  # This is a placeholder value indicating that the code below should set
49  # the default.  This is necessary to delay the evaluation of the default
50  # value expression until after its input values such as use_gold have
51  # been set, e.g. by a toolchain_args() block.
52  use_debug_fission = "default"
53
54  # Enables support for ThinLTO, which links 3x-10x faster than full LTO. See
55  # also http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html
56  if (use_libfuzzer) {
57    use_thin_lto = is_cfi || (is_ohos_or_android && is_official_build)
58  } else {
59    use_thin_lto = is_cfi || is_ohos_or_android
60  }
61
62  # Tell VS to create a PDB that references information in .obj files rather
63  # than copying it all. This should improve linker performance. mspdbcmf.exe
64  # can be used to convert a fastlink pdb to a normal one.
65  is_win_fastlink = false
66
67  # Whether or not we should turn on incremental WPO. Only affects the VS
68  # Windows build.
69  use_incremental_wpo = false
70
71  # Whether or not we should use position independent code.
72  use_pic = true
73
74  # Whether we're using a sample profile collected on an architecture different
75  # than the one we're compiling for.
76  #
77  # It's currently not possible to collect AFDO profiles on anything but
78  # x86{,_64}.
79  using_mismatched_sample_profile = current_cpu != "x64" && current_cpu != "x86"
80}
81
82assert(!is_cfi || use_thin_lto, "CFI requires ThinLTO")
83
84# If true, optimize for size. Does not affect windows builds.
85# Linux & Mac favor speed over size.
86optimize_for_size = is_ohos_or_android
87
88declare_args() {
89  # Whether we should consider the profile we're using to be accurate. Accurate
90  # profiles have the benefit of (potentially substantial) binary size
91  # reductions, by instructing the compiler to optimize cold and uncovered
92  # functions heavily for size. This often comes at the cost of performance.
93  sample_profile_is_accurate = optimize_for_size
94}
95
96# Determine whether to enable or disable frame pointers, based on the platform
97# and build arguments.
98if (is_mac || is_linux) {
99  enable_frame_pointers = true
100} else if (is_win) {
101  # 64-bit Windows ABI doesn't support frame pointers.
102  if (current_cpu == "x64") {
103    enable_frame_pointers = false
104  } else {
105    enable_frame_pointers = true
106  }
107} else if (is_chromeos) {
108  # ChromeOS generally prefers frame pointers, to support CWP.
109  # However, Clang does not currently generate usable frame pointers in ARM
110  # 32-bit builds (https://bugs.llvm.org/show_bug.cgi?id=18505) so disable them
111  # there to avoid the unnecessary overhead.
112  enable_frame_pointers = current_cpu != "arm"
113} else if (is_ohos_or_android) {
114  enable_frame_pointers =
115      enable_profiling ||
116      # Ensure that stacks from arm64 crash dumps are usable (crbug.com/391706).
117      current_cpu == "arm64" ||
118      # For x86 ohos, unwind tables are huge without frame pointers
119      # (crbug.com/762629). Enabling frame pointers grows the code size slightly
120      # but overall shrinks binaries considerably by avoiding huge unwind
121      # tables.
122      (current_cpu == "x86" && !exclude_unwind_tables && optimize_for_size) ||
123      using_sanitizer || current_cpu == "riscv64"
124} else {
125  # Explicitly ask for frame pointers, otherwise:
126  # * Stacks may be missing for sanitizer and profiling builds.
127  # * Debug tcmalloc can crash (crbug.com/636489).
128  enable_frame_pointers = using_sanitizer || enable_profiling || is_debug
129}
130
131# In general assume that if we have frame pointers then we can use them to
132# unwind the stack. However, this requires that they are enabled by default for
133# most translation units, that they are emitted correctly, and that the
134# compiler or platform provides a way to access them.
135can_unwind_with_frame_pointers = enable_frame_pointers
136if (current_cpu == "arm" && arm_use_thumb) {
137  # We cannot currently unwind ARM Thumb frame pointers correctly.
138  # See https://bugs.llvm.org/show_bug.cgi?id=18505
139  can_unwind_with_frame_pointers = false
140} else if (is_win) {
141  # Windows 32-bit does provide frame pointers, but the compiler does not
142  # provide intrinsics to access them, so we don't use them.
143  can_unwind_with_frame_pointers = false
144}
145
146assert(!can_unwind_with_frame_pointers || enable_frame_pointers)
147
148# Unwinding with CFI table is only possible on static library builds and
149# required only when frame pointers are not enabled.
150can_unwind_with_cfi_table = is_ohos_or_android && !is_component_build &&
151                            !enable_frame_pointers && current_cpu == "arm"
152
153declare_args() {
154  # Set to true to use lld, the LLVM linker. This flag may be used on Windows,
155  # Linux.
156  use_lld =
157      is_clang &&
158      (is_win || (use_thin_lto && target_os != "chromeos") ||
159       (is_linux && current_cpu == "x64" && target_os != "chromeos") ||
160       (is_ohos_or_android && (current_cpu != "arm" || arm_version >= 7) &&
161        current_cpu != "mipsel" && current_cpu != "mips64el"))
162}
163
164declare_args() {
165  # Whether to use the gold linker from binutils instead of lld or bfd.
166  use_gold =
167      !use_lld &&
168      !(is_linux && (current_cpu == "arm" || current_cpu == "mipsel")) &&
169      ((is_linux &&
170        (current_cpu == "x64" || current_cpu == "x86" || current_cpu == "arm" ||
171         current_cpu == "mipsel" || current_cpu == "mips64el")) ||
172       (is_ohos_or_android &&
173        (current_cpu == "x86" || current_cpu == "x64" || current_cpu == "arm" ||
174         current_cpu == "arm64" || current_cpu == "riscv64")))
175}
176
177# If it wasn't manually set, set to an appropriate default.
178assert(symbol_level >= -1 && symbol_level <= 2, "Invalid symbol_level")
179if (symbol_level == -1) {
180  if (is_ohos_or_android && use_order_profiling) {
181    # With instrumentation enabled, debug info puts libchrome.so over 4gb, which
182    # causes the linker to produce an invalid ELF. http://crbug.com/574476
183    symbol_level = 0
184  } else if ((is_ohos && !is_component_build &&
185              !(ohos_64bit_target_cpu && !build_app_secondary_abi)) ||
186             (is_android && !is_component_build &&
187              !(aosp_64bit_target_cpu && !build_app_secondary_abi))) {
188    # Reduce symbol level when it will cause invalid elf files to be created
189    # (due to file size). https://crbug.com/648948.
190    symbol_level = 1
191  } else if ((!is_nacl && !is_linux) || is_debug || is_official_build) {
192    # Linux builds slower by having symbols as part of the target binary,
193    # whereas Mac and Windows have them separate, so in Release Linux, default
194    # them off, but keep them on for Official builds and Chromecast builds.
195    symbol_level = 2
196  } else if (using_sanitizer) {
197    # Sanitizers need line table info for stack traces. They don't need type
198    # info or variable info, so we can leave that out to speed up the build.
199    # Sanitizers also require symbols for filename suppressions to work.
200    symbol_level = 1
201  } else {
202    symbol_level = 0
203  }
204}
205
206# Assert that the configuration isn't going to hit https://crbug.com/648948.
207# An exception is made when target_os == "chromeos" as we only use the ohos
208# toolchain there to build relatively small binaries.
209assert(ignore_elf32_limitations || !is_ohos_or_android ||
210           target_os == "chromeos" ||
211           (ohos_64bit_target_cpu && !build_app_secondary_abi) ||
212           is_component_build || symbol_level < 2,
213       "ohos 32-bit non-component builds cannot have symbol_level=2 " +
214           "due to 4GiB file size limit, see https://crbug.com/648948. " +
215           "If you really want to try this out, " +
216           "set ignore_elf32_limitations=true.")
217