xref: /aosp_15_r20/external/angle/build/config/compiler/BUILD.gn (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2013 The Chromium Authors
2*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
3*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
4*8975f5c5SAndroid Build Coastguard Worker
5*8975f5c5SAndroid Build Coastguard Workerimport("//build/buildflag_header.gni")
6*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/android/config.gni")
7*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/c++/c++.gni")
8*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/chrome_build.gni")
9*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/chromeos/args.gni")
10*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/chromeos/ui_mode.gni")
11*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/clang/clang.gni")
12*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/compiler/compiler.gni")
13*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/coverage/coverage.gni")
14*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/dcheck_always_on.gni")
15*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/gclient_args.gni")
16*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/host_byteorder.gni")
17*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/pch.gni")
18*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/rust.gni")
19*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/ui.gni")
20*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/unwind.gni")
21*8975f5c5SAndroid Build Coastguard Workerimport("//build/toolchain/cros/cros_config.gni")
22*8975f5c5SAndroid Build Coastguard Workerimport("//build/toolchain/rbe.gni")
23*8975f5c5SAndroid Build Coastguard Workerimport("//build/toolchain/toolchain.gni")
24*8975f5c5SAndroid Build Coastguard Workerimport("//build_overrides/build.gni")
25*8975f5c5SAndroid Build Coastguard Worker
26*8975f5c5SAndroid Build Coastguard Workerif (current_cpu == "arm" || current_cpu == "arm64") {
27*8975f5c5SAndroid Build Coastguard Worker  import("//build/config/arm.gni")
28*8975f5c5SAndroid Build Coastguard Worker}
29*8975f5c5SAndroid Build Coastguard Workerif (current_cpu == "mipsel" || current_cpu == "mips64el" ||
30*8975f5c5SAndroid Build Coastguard Worker    current_cpu == "mips" || current_cpu == "mips64") {
31*8975f5c5SAndroid Build Coastguard Worker  import("//build/config/mips.gni")
32*8975f5c5SAndroid Build Coastguard Worker}
33*8975f5c5SAndroid Build Coastguard Workerif (is_mac) {
34*8975f5c5SAndroid Build Coastguard Worker  import("//build/config/apple/symbols.gni")
35*8975f5c5SAndroid Build Coastguard Worker}
36*8975f5c5SAndroid Build Coastguard Workerif (is_ios) {
37*8975f5c5SAndroid Build Coastguard Worker  import("//build/config/ios/ios_sdk.gni")
38*8975f5c5SAndroid Build Coastguard Worker}
39*8975f5c5SAndroid Build Coastguard Workerif (is_nacl) {
40*8975f5c5SAndroid Build Coastguard Worker  # To keep NaCl variables out of builds that don't include NaCl, all
41*8975f5c5SAndroid Build Coastguard Worker  # variables defined in nacl/config.gni referenced here should be protected by
42*8975f5c5SAndroid Build Coastguard Worker  # is_nacl conditions.
43*8975f5c5SAndroid Build Coastguard Worker  import("//build/config/nacl/config.gni")
44*8975f5c5SAndroid Build Coastguard Worker}
45*8975f5c5SAndroid Build Coastguard Worker
46*8975f5c5SAndroid Build Coastguard Workerlld_path = ""
47*8975f5c5SAndroid Build Coastguard Workerif (!is_clang) {
48*8975f5c5SAndroid Build Coastguard Worker  declare_args() {
49*8975f5c5SAndroid Build Coastguard Worker    # This allows overriding the location of lld.
50*8975f5c5SAndroid Build Coastguard Worker    lld_path = rebase_path("$clang_base_path/bin", root_build_dir)
51*8975f5c5SAndroid Build Coastguard Worker  }
52*8975f5c5SAndroid Build Coastguard Worker} else {
53*8975f5c5SAndroid Build Coastguard Worker  # clang looks for lld next to it, no need for -B.
54*8975f5c5SAndroid Build Coastguard Worker  lld_path = ""
55*8975f5c5SAndroid Build Coastguard Worker}
56*8975f5c5SAndroid Build Coastguard Worker
57*8975f5c5SAndroid Build Coastguard Workerdeclare_args() {
58*8975f5c5SAndroid Build Coastguard Worker  # Normally, Android builds are lightly optimized, even for debug builds, to
59*8975f5c5SAndroid Build Coastguard Worker  # keep binary size down. Setting this flag to true disables such optimization
60*8975f5c5SAndroid Build Coastguard Worker  android_full_debug = false
61*8975f5c5SAndroid Build Coastguard Worker
62*8975f5c5SAndroid Build Coastguard Worker  # Compile in such a way as to make it possible for the profiler to unwind full
63*8975f5c5SAndroid Build Coastguard Worker  # stack frames. Setting this flag has a large effect on the performance of the
64*8975f5c5SAndroid Build Coastguard Worker  # generated code than just setting profiling, but gives the profiler more
65*8975f5c5SAndroid Build Coastguard Worker  # information to analyze.
66*8975f5c5SAndroid Build Coastguard Worker  # Requires profiling to be set to true.
67*8975f5c5SAndroid Build Coastguard Worker  enable_full_stack_frames_for_profiling = false
68*8975f5c5SAndroid Build Coastguard Worker
69*8975f5c5SAndroid Build Coastguard Worker  # Enable fatal linker warnings. Building Chromium with certain versions
70*8975f5c5SAndroid Build Coastguard Worker  # of binutils can cause linker warning.
71*8975f5c5SAndroid Build Coastguard Worker  fatal_linker_warnings = true
72*8975f5c5SAndroid Build Coastguard Worker
73*8975f5c5SAndroid Build Coastguard Worker  # Build with C++ RTTI enabled. Chromium builds without RTTI by default,
74*8975f5c5SAndroid Build Coastguard Worker  # but some sanitizers are known to require it, like CFI diagnostics
75*8975f5c5SAndroid Build Coastguard Worker  # and UBsan variants.
76*8975f5c5SAndroid Build Coastguard Worker  use_rtti = use_cfi_diag || is_ubsan_vptr || is_ubsan_security
77*8975f5c5SAndroid Build Coastguard Worker
78*8975f5c5SAndroid Build Coastguard Worker  # AFDO (Automatic Feedback Directed Optimizer) is a form of profile-guided
79*8975f5c5SAndroid Build Coastguard Worker  # optimization that GCC supports. It used by ChromeOS in their official
80*8975f5c5SAndroid Build Coastguard Worker  # builds. To use it, set auto_profile_path to the path to a file containing
81*8975f5c5SAndroid Build Coastguard Worker  # the needed gcov profiling data.
82*8975f5c5SAndroid Build Coastguard Worker  auto_profile_path = ""
83*8975f5c5SAndroid Build Coastguard Worker
84*8975f5c5SAndroid Build Coastguard Worker  # Optimize for coverage guided fuzzing (balance between speed and number of
85*8975f5c5SAndroid Build Coastguard Worker  # branches)
86*8975f5c5SAndroid Build Coastguard Worker  optimize_for_fuzzing = false
87*8975f5c5SAndroid Build Coastguard Worker
88*8975f5c5SAndroid Build Coastguard Worker  # Path to an AFDO profile to use while building with clang, if any. Empty
89*8975f5c5SAndroid Build Coastguard Worker  # implies none.
90*8975f5c5SAndroid Build Coastguard Worker  clang_sample_profile_path = ""
91*8975f5c5SAndroid Build Coastguard Worker
92*8975f5c5SAndroid Build Coastguard Worker  # Some configurations have default sample profiles. If this is true and
93*8975f5c5SAndroid Build Coastguard Worker  # clang_sample_profile_path is empty, we'll fall back to the default.
94*8975f5c5SAndroid Build Coastguard Worker  #
95*8975f5c5SAndroid Build Coastguard Worker  # We currently only have default profiles for Chromium in-tree, so we disable
96*8975f5c5SAndroid Build Coastguard Worker  # this by default for all downstream projects, since these profiles are likely
97*8975f5c5SAndroid Build Coastguard Worker  # nonsensical for said projects.
98*8975f5c5SAndroid Build Coastguard Worker  clang_use_default_sample_profile =
99*8975f5c5SAndroid Build Coastguard Worker      chrome_pgo_phase == 0 && build_with_chromium && is_official_build &&
100*8975f5c5SAndroid Build Coastguard Worker      ((is_android && !is_high_end_android) || chromeos_is_browser_only)
101*8975f5c5SAndroid Build Coastguard Worker
102*8975f5c5SAndroid Build Coastguard Worker  # This configuration is used to select a default profile in Chrome OS based on
103*8975f5c5SAndroid Build Coastguard Worker  # the microarchitectures we are using. This is only used if
104*8975f5c5SAndroid Build Coastguard Worker  # clang_use_default_sample_profile is true and clang_sample_profile_path is
105*8975f5c5SAndroid Build Coastguard Worker  # empty.
106*8975f5c5SAndroid Build Coastguard Worker  chromeos_afdo_platform = "atom"
107*8975f5c5SAndroid Build Coastguard Worker
108*8975f5c5SAndroid Build Coastguard Worker  # Emit debug information for profiling wile building with clang.
109*8975f5c5SAndroid Build Coastguard Worker  # Only enable this for ChromeOS official builds for AFDO.
110*8975f5c5SAndroid Build Coastguard Worker  clang_emit_debug_info_for_profiling = is_chromeos_device && is_official_build
111*8975f5c5SAndroid Build Coastguard Worker
112*8975f5c5SAndroid Build Coastguard Worker  # Turn this on to have the compiler output extra timing information.
113*8975f5c5SAndroid Build Coastguard Worker  compiler_timing = false
114*8975f5c5SAndroid Build Coastguard Worker
115*8975f5c5SAndroid Build Coastguard Worker  # Turn this on to use ghash feature of lld for faster debug link on Windows.
116*8975f5c5SAndroid Build Coastguard Worker  # http://blog.llvm.org/2018/01/improving-link-time-on-windows-with.html
117*8975f5c5SAndroid Build Coastguard Worker  use_ghash = true
118*8975f5c5SAndroid Build Coastguard Worker
119*8975f5c5SAndroid Build Coastguard Worker  # Whether to enable ThinLTO optimizations. Turning ThinLTO optimizations on
120*8975f5c5SAndroid Build Coastguard Worker  # can substantially increase link time and binary size, but they generally
121*8975f5c5SAndroid Build Coastguard Worker  # also make binaries a fair bit faster.
122*8975f5c5SAndroid Build Coastguard Worker  #
123*8975f5c5SAndroid Build Coastguard Worker  # TODO(gbiv): We disable optimizations by default on most platforms because
124*8975f5c5SAndroid Build Coastguard Worker  # the space overhead is too great. We should use some mixture of profiles and
125*8975f5c5SAndroid Build Coastguard Worker  # optimization settings to better tune the size increase.
126*8975f5c5SAndroid Build Coastguard Worker  thin_lto_enable_optimizations =
127*8975f5c5SAndroid Build Coastguard Worker      (is_chromeos || is_android || is_win || is_linux || is_mac ||
128*8975f5c5SAndroid Build Coastguard Worker       (is_ios && use_lld)) && is_official_build
129*8975f5c5SAndroid Build Coastguard Worker
130*8975f5c5SAndroid Build Coastguard Worker  # Whether to enable thin lto incremental builds.
131*8975f5c5SAndroid Build Coastguard Worker  # See: https://clang.llvm.org/docs/ThinLTO.html#incremental
132*8975f5c5SAndroid Build Coastguard Worker  # The cache can lead to non-determinism: https://crbug.com/1486045
133*8975f5c5SAndroid Build Coastguard Worker  thin_lto_enable_cache = true
134*8975f5c5SAndroid Build Coastguard Worker
135*8975f5c5SAndroid Build Coastguard Worker  # Initialize all local variables with a pattern. This flag will fill
136*8975f5c5SAndroid Build Coastguard Worker  # uninitialized floating-point types (and 32-bit pointers) with 0xFF and the
137*8975f5c5SAndroid Build Coastguard Worker  # rest with 0xAA. This makes behavior of uninitialized memory bugs consistent,
138*8975f5c5SAndroid Build Coastguard Worker  # recognizable in the debugger, and crashes on memory accesses through
139*8975f5c5SAndroid Build Coastguard Worker  # uninitialized pointers.
140*8975f5c5SAndroid Build Coastguard Worker  #
141*8975f5c5SAndroid Build Coastguard Worker  # Flag discussion: https://crbug.com/977230
142*8975f5c5SAndroid Build Coastguard Worker  #
143*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/40721698): This regresses binary size by ~1MB on Android and
144*8975f5c5SAndroid Build Coastguard Worker  # needs to be evaluated before enabling it there as well.
145*8975f5c5SAndroid Build Coastguard Worker  init_stack_vars = !(is_android && is_official_build)
146*8975f5c5SAndroid Build Coastguard Worker
147*8975f5c5SAndroid Build Coastguard Worker  # Zero init has favorable performance/size tradeoffs for Chrome OS
148*8975f5c5SAndroid Build Coastguard Worker  # but was not evaluated for other platforms.
149*8975f5c5SAndroid Build Coastguard Worker  init_stack_vars_zero = is_chromeos
150*8975f5c5SAndroid Build Coastguard Worker
151*8975f5c5SAndroid Build Coastguard Worker  # This argument is to control whether enabling text section splitting in the
152*8975f5c5SAndroid Build Coastguard Worker  # final binary. When enabled, the separated text sections with prefix
153*8975f5c5SAndroid Build Coastguard Worker  # '.text.hot', '.text.unlikely', '.text.startup' and '.text.exit' will not be
154*8975f5c5SAndroid Build Coastguard Worker  # merged to '.text' section. This allows us to identify the hot code section
155*8975f5c5SAndroid Build Coastguard Worker  # ('.text.hot') in the binary, which allows our data collection pipelines to
156*8975f5c5SAndroid Build Coastguard Worker  # more easily identify code that we assume to be hot/cold that doesn't turn
157*8975f5c5SAndroid Build Coastguard Worker  # out to be such in the field.
158*8975f5c5SAndroid Build Coastguard Worker  use_text_section_splitting = is_chromeos
159*8975f5c5SAndroid Build Coastguard Worker
160*8975f5c5SAndroid Build Coastguard Worker  # Enable DWARF v5.
161*8975f5c5SAndroid Build Coastguard Worker  use_dwarf5 = false
162*8975f5c5SAndroid Build Coastguard Worker
163*8975f5c5SAndroid Build Coastguard Worker  # Override this to put full paths to PDBs in Windows PE files. This helps
164*8975f5c5SAndroid Build Coastguard Worker  # windbg and Windows Performance Analyzer with finding the PDBs in some local-
165*8975f5c5SAndroid Build Coastguard Worker  # build scenarios. This is never needed for bots or official builds. Because
166*8975f5c5SAndroid Build Coastguard Worker  # this puts the output directory in the DLLs/EXEs it breaks build determinism.
167*8975f5c5SAndroid Build Coastguard Worker  # Bugs have been reported to the windbg/WPA teams and this workaround will be
168*8975f5c5SAndroid Build Coastguard Worker  # removed when they are fixed.
169*8975f5c5SAndroid Build Coastguard Worker  use_full_pdb_paths = false
170*8975f5c5SAndroid Build Coastguard Worker
171*8975f5c5SAndroid Build Coastguard Worker  # Enable -H, which prints the include tree during compilation.
172*8975f5c5SAndroid Build Coastguard Worker  # For use by tools/clang/scripts/analyze_includes.py
173*8975f5c5SAndroid Build Coastguard Worker  show_includes = false
174*8975f5c5SAndroid Build Coastguard Worker
175*8975f5c5SAndroid Build Coastguard Worker  # Enable Profi algorithm. Profi can infer block and edge counts.
176*8975f5c5SAndroid Build Coastguard Worker  # https://clang.llvm.org/docs/UsersManual.html#using-sampling-profilers
177*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/1375958i:) Possibly enable this for Android too.
178*8975f5c5SAndroid Build Coastguard Worker  use_profi = is_chromeos
179*8975f5c5SAndroid Build Coastguard Worker
180*8975f5c5SAndroid Build Coastguard Worker  # If true, linker crashes will be rerun with `--reproduce` which causes
181*8975f5c5SAndroid Build Coastguard Worker  # a reproducer file to be saved.
182*8975f5c5SAndroid Build Coastguard Worker  save_reproducers_on_lld_crash = false
183*8975f5c5SAndroid Build Coastguard Worker
184*8975f5c5SAndroid Build Coastguard Worker  # Enable ShadowCallStack for compiled binaries. SCS stores a pointer to a
185*8975f5c5SAndroid Build Coastguard Worker  # shadow call stack in register x18. Hence, x18 must not be used by the OS
186*8975f5c5SAndroid Build Coastguard Worker  # or libraries. We assume that to be the case for high end Android
187*8975f5c5SAndroid Build Coastguard Worker  # configurations. For more details see
188*8975f5c5SAndroid Build Coastguard Worker  # https://clang.llvm.org/docs/ShadowCallStack.html
189*8975f5c5SAndroid Build Coastguard Worker  enable_shadow_call_stack = false
190*8975f5c5SAndroid Build Coastguard Worker
191*8975f5c5SAndroid Build Coastguard Worker  # Use DWARF simple template names, with the following exceptions:
192*8975f5c5SAndroid Build Coastguard Worker  #
193*8975f5c5SAndroid Build Coastguard Worker  # * Windows is not supported as it doesn't use DWARF.
194*8975f5c5SAndroid Build Coastguard Worker  # * Apple platforms (e.g. MacOS, iPhone, iPad) aren't supported because xcode
195*8975f5c5SAndroid Build Coastguard Worker  #   lldb doesn't have the needed changes yet.
196*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/40244196): Remove if the upstream default ever changes.
197*8975f5c5SAndroid Build Coastguard Worker  #
198*8975f5c5SAndroid Build Coastguard Worker  # This greatly reduces the size of debug builds, at the cost of
199*8975f5c5SAndroid Build Coastguard Worker  # debugging information which is required by some specialized
200*8975f5c5SAndroid Build Coastguard Worker  # debugging tools.
201*8975f5c5SAndroid Build Coastguard Worker  simple_template_names = is_clang && !is_nacl && !is_win && !is_apple
202*8975f5c5SAndroid Build Coastguard Worker}
203*8975f5c5SAndroid Build Coastguard Worker
204*8975f5c5SAndroid Build Coastguard Workerdeclare_args() {
205*8975f5c5SAndroid Build Coastguard Worker  # Set to true to use icf, Identical Code Folding.
206*8975f5c5SAndroid Build Coastguard Worker  use_icf = (is_posix || is_fuchsia) && !is_debug && !using_sanitizer &&
207*8975f5c5SAndroid Build Coastguard Worker            !use_clang_coverage && current_os != "zos" &&
208*8975f5c5SAndroid Build Coastguard Worker            !(is_android && use_order_profiling) && use_lld
209*8975f5c5SAndroid Build Coastguard Worker}
210*8975f5c5SAndroid Build Coastguard Worker
211*8975f5c5SAndroid Build Coastguard Workerif (is_android) {
212*8975f5c5SAndroid Build Coastguard Worker  # Set the path to use orderfile for linking Chrome
213*8975f5c5SAndroid Build Coastguard Worker  # Note that this is for using only one orderfile for linking
214*8975f5c5SAndroid Build Coastguard Worker  # the Chrome binary/library.
215*8975f5c5SAndroid Build Coastguard Worker  declare_args() {
216*8975f5c5SAndroid Build Coastguard Worker    chrome_orderfile_path = ""
217*8975f5c5SAndroid Build Coastguard Worker
218*8975f5c5SAndroid Build Coastguard Worker    # The orderfile is trained on PGO builds (for arm64) and AFDO builds (for
219*8975f5c5SAndroid Build Coastguard Worker    # arm32), so apply them only in these cases.
220*8975f5c5SAndroid Build Coastguard Worker    if (defined(default_chrome_orderfile)) {
221*8975f5c5SAndroid Build Coastguard Worker      if (((current_cpu == "arm64" || current_cpu == "x64") &&
222*8975f5c5SAndroid Build Coastguard Worker           chrome_pgo_phase == 2) ||
223*8975f5c5SAndroid Build Coastguard Worker          ((current_cpu == "arm" || current_cpu == "x86") &&
224*8975f5c5SAndroid Build Coastguard Worker           clang_use_default_sample_profile)) {
225*8975f5c5SAndroid Build Coastguard Worker        chrome_orderfile_path = default_chrome_orderfile
226*8975f5c5SAndroid Build Coastguard Worker      }
227*8975f5c5SAndroid Build Coastguard Worker    }
228*8975f5c5SAndroid Build Coastguard Worker  }
229*8975f5c5SAndroid Build Coastguard Worker}
230*8975f5c5SAndroid Build Coastguard Worker
231*8975f5c5SAndroid Build Coastguard Workerdeclare_args() {
232*8975f5c5SAndroid Build Coastguard Worker  # Turn off the --call-graph-profile-sort flag for lld by default. Enable
233*8975f5c5SAndroid Build Coastguard Worker  # selectively for targets where it's beneficial.
234*8975f5c5SAndroid Build Coastguard Worker  enable_call_graph_profile_sort =
235*8975f5c5SAndroid Build Coastguard Worker      chrome_pgo_phase == 2 ||
236*8975f5c5SAndroid Build Coastguard Worker      (is_chromeos &&
237*8975f5c5SAndroid Build Coastguard Worker       (clang_use_default_sample_profile || clang_sample_profile_path != ""))
238*8975f5c5SAndroid Build Coastguard Worker}
239*8975f5c5SAndroid Build Coastguard Worker
240*8975f5c5SAndroid Build Coastguard Workerassert(!(llvm_force_head_revision && use_remoteexec && host_os != "linux"),
241*8975f5c5SAndroid Build Coastguard Worker       "rbe with locally built clang only works on linux")
242*8975f5c5SAndroid Build Coastguard Worker
243*8975f5c5SAndroid Build Coastguard Worker# default_include_dirs ---------------------------------------------------------
244*8975f5c5SAndroid Build Coastguard Worker#
245*8975f5c5SAndroid Build Coastguard Worker# This is a separate config so that third_party code (which would not use the
246*8975f5c5SAndroid Build Coastguard Worker# source root and might have conflicting versions of some headers) can remove
247*8975f5c5SAndroid Build Coastguard Worker# this and specify their own include paths.
248*8975f5c5SAndroid Build Coastguard Workerconfig("default_include_dirs") {
249*8975f5c5SAndroid Build Coastguard Worker  include_dirs = [
250*8975f5c5SAndroid Build Coastguard Worker    "//",
251*8975f5c5SAndroid Build Coastguard Worker    root_gen_dir,
252*8975f5c5SAndroid Build Coastguard Worker  ]
253*8975f5c5SAndroid Build Coastguard Worker}
254*8975f5c5SAndroid Build Coastguard Worker
255*8975f5c5SAndroid Build Coastguard Worker# Compiler instrumentation can introduce dependencies in DSOs to symbols in
256*8975f5c5SAndroid Build Coastguard Worker# the executable they are loaded into, so they are unresolved at link-time.
257*8975f5c5SAndroid Build Coastguard Workerconfig("no_unresolved_symbols") {
258*8975f5c5SAndroid Build Coastguard Worker  if (!using_sanitizer &&
259*8975f5c5SAndroid Build Coastguard Worker      (is_linux || is_chromeos || is_android || is_fuchsia)) {
260*8975f5c5SAndroid Build Coastguard Worker    ldflags = [
261*8975f5c5SAndroid Build Coastguard Worker      "-Wl,-z,defs",
262*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--as-needed",
263*8975f5c5SAndroid Build Coastguard Worker    ]
264*8975f5c5SAndroid Build Coastguard Worker  }
265*8975f5c5SAndroid Build Coastguard Worker}
266*8975f5c5SAndroid Build Coastguard Worker
267*8975f5c5SAndroid Build Coastguard Worker# compiler ---------------------------------------------------------------------
268*8975f5c5SAndroid Build Coastguard Worker#
269*8975f5c5SAndroid Build Coastguard Worker# Base compiler configuration.
270*8975f5c5SAndroid Build Coastguard Worker#
271*8975f5c5SAndroid Build Coastguard Worker# See also "runtime_library" below for related stuff and a discussion about
272*8975f5c5SAndroid Build Coastguard Worker# where stuff should go. Put warning related stuff in the "warnings" config.
273*8975f5c5SAndroid Build Coastguard Worker
274*8975f5c5SAndroid Build Coastguard Workerconfig("compiler") {
275*8975f5c5SAndroid Build Coastguard Worker  asmflags = []
276*8975f5c5SAndroid Build Coastguard Worker  cflags = []
277*8975f5c5SAndroid Build Coastguard Worker  cflags_c = []
278*8975f5c5SAndroid Build Coastguard Worker  cflags_cc = []
279*8975f5c5SAndroid Build Coastguard Worker  cflags_objc = []
280*8975f5c5SAndroid Build Coastguard Worker  cflags_objcc = []
281*8975f5c5SAndroid Build Coastguard Worker  rustflags = []
282*8975f5c5SAndroid Build Coastguard Worker  ldflags = []
283*8975f5c5SAndroid Build Coastguard Worker  defines = []
284*8975f5c5SAndroid Build Coastguard Worker  configs = []
285*8975f5c5SAndroid Build Coastguard Worker
286*8975f5c5SAndroid Build Coastguard Worker  # System-specific flags. If your compiler flags apply to one of the
287*8975f5c5SAndroid Build Coastguard Worker  # categories here, add it to the associated file to keep this shared config
288*8975f5c5SAndroid Build Coastguard Worker  # smaller.
289*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
290*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/win:compiler" ]
291*8975f5c5SAndroid Build Coastguard Worker  } else if (is_android) {
292*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/android:compiler" ]
293*8975f5c5SAndroid Build Coastguard Worker  } else if (is_linux || is_chromeos) {
294*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/linux:compiler" ]
295*8975f5c5SAndroid Build Coastguard Worker  } else if (is_nacl) {
296*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/nacl:compiler" ]
297*8975f5c5SAndroid Build Coastguard Worker  } else if (is_mac) {
298*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/mac:compiler" ]
299*8975f5c5SAndroid Build Coastguard Worker  } else if (is_ios) {
300*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/ios:compiler" ]
301*8975f5c5SAndroid Build Coastguard Worker  } else if (is_fuchsia) {
302*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/fuchsia:compiler" ]
303*8975f5c5SAndroid Build Coastguard Worker  } else if (current_os == "aix") {
304*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/aix:compiler" ]
305*8975f5c5SAndroid Build Coastguard Worker  } else if (current_os == "zos") {
306*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/zos:compiler" ]
307*8975f5c5SAndroid Build Coastguard Worker  }
308*8975f5c5SAndroid Build Coastguard Worker
309*8975f5c5SAndroid Build Coastguard Worker  configs += [
310*8975f5c5SAndroid Build Coastguard Worker    # See the definitions below.
311*8975f5c5SAndroid Build Coastguard Worker    ":clang_revision",
312*8975f5c5SAndroid Build Coastguard Worker    ":rustc_revision",
313*8975f5c5SAndroid Build Coastguard Worker    ":compiler_cpu_abi",
314*8975f5c5SAndroid Build Coastguard Worker    ":compiler_codegen",
315*8975f5c5SAndroid Build Coastguard Worker    ":compiler_deterministic",
316*8975f5c5SAndroid Build Coastguard Worker  ]
317*8975f5c5SAndroid Build Coastguard Worker
318*8975f5c5SAndroid Build Coastguard Worker  # Here we enable -fno-delete-null-pointer-checks, which makes various nullptr
319*8975f5c5SAndroid Build Coastguard Worker  # operations (e.g. dereferencing) into defined behavior. This avoids deletion
320*8975f5c5SAndroid Build Coastguard Worker  # of some security-critical code: see https://crbug.com/1139129.
321*8975f5c5SAndroid Build Coastguard Worker  # The older NaCl toolchain does not support the flag. And, we still want UBSan
322*8975f5c5SAndroid Build Coastguard Worker  # to catch undefined behavior related to nullptrs, so do not add this flag if
323*8975f5c5SAndroid Build Coastguard Worker  # UBSan is enabled. GCC seems to have some bugs compiling constexpr code when
324*8975f5c5SAndroid Build Coastguard Worker  # this is defined, so only enable it if using_clang.
325*8975f5c5SAndroid Build Coastguard Worker  # See: https://gcc.gnu.org/PR97913
326*8975f5c5SAndroid Build Coastguard Worker  # TODO(mpdenton): remove is_clang once GCC bug is fixed.
327*8975f5c5SAndroid Build Coastguard Worker  if ((!is_nacl || is_nacl_saigo) && !is_ubsan && is_clang) {
328*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fno-delete-null-pointer-checks" ]
329*8975f5c5SAndroid Build Coastguard Worker  }
330*8975f5c5SAndroid Build Coastguard Worker
331*8975f5c5SAndroid Build Coastguard Worker  # Don't emit the GCC version ident directives, they just end up in the
332*8975f5c5SAndroid Build Coastguard Worker  # .comment section or debug info taking up binary size, and makes comparing
333*8975f5c5SAndroid Build Coastguard Worker  # .o files built with different compiler versions harder.
334*8975f5c5SAndroid Build Coastguard Worker  if (!is_win || is_clang) {
335*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fno-ident" ]
336*8975f5c5SAndroid Build Coastguard Worker  }
337*8975f5c5SAndroid Build Coastguard Worker
338*8975f5c5SAndroid Build Coastguard Worker  # In general, Windows is totally different, but all the other builds share
339*8975f5c5SAndroid Build Coastguard Worker  # some common compiler and linker configuration.
340*8975f5c5SAndroid Build Coastguard Worker  if (!is_win) {
341*8975f5c5SAndroid Build Coastguard Worker    # Common POSIX compiler flags setup.
342*8975f5c5SAndroid Build Coastguard Worker    # --------------------------------
343*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fno-strict-aliasing" ]  # See http://crbug.com/32204
344*8975f5c5SAndroid Build Coastguard Worker
345*8975f5c5SAndroid Build Coastguard Worker    # Stack protection. ShadowCallStack and Stack protector address the same
346*8975f5c5SAndroid Build Coastguard Worker    # problems. Therefore, we only enable one or the other. Clang advertises SCS as
347*8975f5c5SAndroid Build Coastguard Worker    # a stronger alternative to StackProtector, so we give SCS precedence over SP.
348*8975f5c5SAndroid Build Coastguard Worker    if (enable_shadow_call_stack) {
349*8975f5c5SAndroid Build Coastguard Worker      # On Aarch64, SCS requires the x18 register to be unused because it will hold
350*8975f5c5SAndroid Build Coastguard Worker      # a pointer to the shadow stack. For Android we know that Clang doesn't use
351*8975f5c5SAndroid Build Coastguard Worker      # x18 by default. On other OSs adding "-ffixed-x18" might be required.
352*8975f5c5SAndroid Build Coastguard Worker      assert(is_android)
353*8975f5c5SAndroid Build Coastguard Worker
354*8975f5c5SAndroid Build Coastguard Worker      scs_parameters = [
355*8975f5c5SAndroid Build Coastguard Worker        "-fsanitize=shadow-call-stack",
356*8975f5c5SAndroid Build Coastguard Worker        "-fno-stack-protector",
357*8975f5c5SAndroid Build Coastguard Worker      ]
358*8975f5c5SAndroid Build Coastguard Worker      cflags += scs_parameters
359*8975f5c5SAndroid Build Coastguard Worker      ldflags += scs_parameters
360*8975f5c5SAndroid Build Coastguard Worker    } else {
361*8975f5c5SAndroid Build Coastguard Worker      if (is_apple) {
362*8975f5c5SAndroid Build Coastguard Worker        # The strong variant of the stack protector significantly increases
363*8975f5c5SAndroid Build Coastguard Worker        # binary size, so only enable it in debug mode.
364*8975f5c5SAndroid Build Coastguard Worker        if (is_debug) {
365*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-fstack-protector-strong" ]
366*8975f5c5SAndroid Build Coastguard Worker        } else {
367*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-fstack-protector" ]
368*8975f5c5SAndroid Build Coastguard Worker        }
369*8975f5c5SAndroid Build Coastguard Worker      } else if (is_chromeos) {
370*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-fstack-protector-strong" ]
371*8975f5c5SAndroid Build Coastguard Worker      } else if ((is_posix && !is_nacl) || is_fuchsia) {
372*8975f5c5SAndroid Build Coastguard Worker        if (current_os != "aix") {
373*8975f5c5SAndroid Build Coastguard Worker          # Not available on aix.
374*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-fstack-protector" ]
375*8975f5c5SAndroid Build Coastguard Worker        }
376*8975f5c5SAndroid Build Coastguard Worker      }
377*8975f5c5SAndroid Build Coastguard Worker    }
378*8975f5c5SAndroid Build Coastguard Worker
379*8975f5c5SAndroid Build Coastguard Worker    if (use_lld) {
380*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-fuse-ld=lld" ]
381*8975f5c5SAndroid Build Coastguard Worker      if (lld_path != "") {
382*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-B$lld_path" ]
383*8975f5c5SAndroid Build Coastguard Worker      }
384*8975f5c5SAndroid Build Coastguard Worker    }
385*8975f5c5SAndroid Build Coastguard Worker
386*8975f5c5SAndroid Build Coastguard Worker    # Linker warnings.
387*8975f5c5SAndroid Build Coastguard Worker    if (fatal_linker_warnings && !is_apple && current_os != "aix" &&
388*8975f5c5SAndroid Build Coastguard Worker        current_os != "zos") {
389*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--fatal-warnings" ]
390*8975f5c5SAndroid Build Coastguard Worker    }
391*8975f5c5SAndroid Build Coastguard Worker    if (fatal_linker_warnings && is_apple) {
392*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,-fatal_warnings" ]
393*8975f5c5SAndroid Build Coastguard Worker    }
394*8975f5c5SAndroid Build Coastguard Worker  }
395*8975f5c5SAndroid Build Coastguard Worker
396*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && is_debug) {
397*8975f5c5SAndroid Build Coastguard Worker    # Allow comparing the address of references and 'this' against 0
398*8975f5c5SAndroid Build Coastguard Worker    # in debug builds. Technically, these can never be null in
399*8975f5c5SAndroid Build Coastguard Worker    # well-defined C/C++ and Clang can optimize such checks away in
400*8975f5c5SAndroid Build Coastguard Worker    # release builds, but they may be used in asserts in debug builds.
401*8975f5c5SAndroid Build Coastguard Worker    cflags_cc += [
402*8975f5c5SAndroid Build Coastguard Worker      "-Wno-undefined-bool-conversion",
403*8975f5c5SAndroid Build Coastguard Worker      "-Wno-tautological-undefined-compare",
404*8975f5c5SAndroid Build Coastguard Worker    ]
405*8975f5c5SAndroid Build Coastguard Worker  }
406*8975f5c5SAndroid Build Coastguard Worker
407*8975f5c5SAndroid Build Coastguard Worker  # Non-Apple Posix and Fuchsia compiler flags setup.
408*8975f5c5SAndroid Build Coastguard Worker  # -----------------------------------
409*8975f5c5SAndroid Build Coastguard Worker  if ((is_posix && !is_apple) || is_fuchsia) {
410*8975f5c5SAndroid Build Coastguard Worker    if (enable_profiling) {
411*8975f5c5SAndroid Build Coastguard Worker      if (!is_debug) {
412*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-g" ]
413*8975f5c5SAndroid Build Coastguard Worker
414*8975f5c5SAndroid Build Coastguard Worker        if (enable_full_stack_frames_for_profiling) {
415*8975f5c5SAndroid Build Coastguard Worker          cflags += [
416*8975f5c5SAndroid Build Coastguard Worker            "-fno-inline",
417*8975f5c5SAndroid Build Coastguard Worker            "-fno-optimize-sibling-calls",
418*8975f5c5SAndroid Build Coastguard Worker          ]
419*8975f5c5SAndroid Build Coastguard Worker        }
420*8975f5c5SAndroid Build Coastguard Worker      }
421*8975f5c5SAndroid Build Coastguard Worker    }
422*8975f5c5SAndroid Build Coastguard Worker
423*8975f5c5SAndroid Build Coastguard Worker    # Explicitly pass --build-id to ld. Compilers used to always pass this
424*8975f5c5SAndroid Build Coastguard Worker    # implicitly but don't any more (in particular clang when built without
425*8975f5c5SAndroid Build Coastguard Worker    # ENABLE_LINKER_BUILD_ID=ON).
426*8975f5c5SAndroid Build Coastguard Worker    if (is_official_build) {
427*8975f5c5SAndroid Build Coastguard Worker      # The sha1 build id has lower risk of collision but is more expensive to
428*8975f5c5SAndroid Build Coastguard Worker      # compute, so only use it in the official build to avoid slowing down
429*8975f5c5SAndroid Build Coastguard Worker      # links.
430*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--build-id=sha1" ]
431*8975f5c5SAndroid Build Coastguard Worker    } else if (current_os != "aix" && current_os != "zos") {
432*8975f5c5SAndroid Build Coastguard Worker      if (use_lld && !is_nacl) {
433*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,--build-id=fast" ]
434*8975f5c5SAndroid Build Coastguard Worker      } else {
435*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,--build-id" ]
436*8975f5c5SAndroid Build Coastguard Worker      }
437*8975f5c5SAndroid Build Coastguard Worker    }
438*8975f5c5SAndroid Build Coastguard Worker
439*8975f5c5SAndroid Build Coastguard Worker    if (!is_android) {
440*8975f5c5SAndroid Build Coastguard Worker      defines += [
441*8975f5c5SAndroid Build Coastguard Worker        # _FILE_OFFSET_BITS=64 should not be set on Android in order to maintain
442*8975f5c5SAndroid Build Coastguard Worker        # the behavior of the Android NDK from earlier versions.
443*8975f5c5SAndroid Build Coastguard Worker        # See https://android-developers.googleblog.com/2017/09/introducing-android-native-development.html
444*8975f5c5SAndroid Build Coastguard Worker        "_FILE_OFFSET_BITS=64",
445*8975f5c5SAndroid Build Coastguard Worker        "_LARGEFILE_SOURCE",
446*8975f5c5SAndroid Build Coastguard Worker        "_LARGEFILE64_SOURCE",
447*8975f5c5SAndroid Build Coastguard Worker      ]
448*8975f5c5SAndroid Build Coastguard Worker    }
449*8975f5c5SAndroid Build Coastguard Worker
450*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl) {
451*8975f5c5SAndroid Build Coastguard Worker      if (exclude_unwind_tables) {
452*8975f5c5SAndroid Build Coastguard Worker        cflags += [
453*8975f5c5SAndroid Build Coastguard Worker          "-fno-unwind-tables",
454*8975f5c5SAndroid Build Coastguard Worker          "-fno-asynchronous-unwind-tables",
455*8975f5c5SAndroid Build Coastguard Worker        ]
456*8975f5c5SAndroid Build Coastguard Worker        rustflags += [ "-Cforce-unwind-tables=no" ]
457*8975f5c5SAndroid Build Coastguard Worker        defines += [ "NO_UNWIND_TABLES" ]
458*8975f5c5SAndroid Build Coastguard Worker      } else {
459*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-funwind-tables" ]
460*8975f5c5SAndroid Build Coastguard Worker        rustflags += [ "-Cforce-unwind-tables=yes" ]
461*8975f5c5SAndroid Build Coastguard Worker      }
462*8975f5c5SAndroid Build Coastguard Worker    }
463*8975f5c5SAndroid Build Coastguard Worker  }
464*8975f5c5SAndroid Build Coastguard Worker
465*8975f5c5SAndroid Build Coastguard Worker  # Apple compiler flags setup.
466*8975f5c5SAndroid Build Coastguard Worker  # ---------------------------------
467*8975f5c5SAndroid Build Coastguard Worker  if (is_apple) {
468*8975f5c5SAndroid Build Coastguard Worker    # On Intel, clang emits both Apple's "compact unwind" information and
469*8975f5c5SAndroid Build Coastguard Worker    # DWARF eh_frame unwind information by default, for compatibility reasons.
470*8975f5c5SAndroid Build Coastguard Worker    # This flag limits emission of eh_frame information to functions
471*8975f5c5SAndroid Build Coastguard Worker    # whose unwind information can't be expressed in the compact unwind format
472*8975f5c5SAndroid Build Coastguard Worker    # (which in practice means almost everything gets only compact unwind
473*8975f5c5SAndroid Build Coastguard Worker    # entries). This reduces object file size a bit and makes linking a bit
474*8975f5c5SAndroid Build Coastguard Worker    # faster.
475*8975f5c5SAndroid Build Coastguard Worker    # On arm64, this is already the default behavior.
476*8975f5c5SAndroid Build Coastguard Worker    if (current_cpu == "x64") {
477*8975f5c5SAndroid Build Coastguard Worker      asmflags += [ "-femit-dwarf-unwind=no-compact-unwind" ]
478*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-femit-dwarf-unwind=no-compact-unwind" ]
479*8975f5c5SAndroid Build Coastguard Worker    }
480*8975f5c5SAndroid Build Coastguard Worker
481*8975f5c5SAndroid Build Coastguard Worker    # dsymutil is not available in the system, on bots, for rustc to call. Our
482*8975f5c5SAndroid Build Coastguard Worker    # linker_driver.py script runs dsymutil itself, which is set to be the
483*8975f5c5SAndroid Build Coastguard Worker    # linker for Rust targets as well.
484*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "-Csplit-debuginfo=unpacked" ]
485*8975f5c5SAndroid Build Coastguard Worker  }
486*8975f5c5SAndroid Build Coastguard Worker
487*8975f5c5SAndroid Build Coastguard Worker  # Linux/Android/Fuchsia common flags setup.
488*8975f5c5SAndroid Build Coastguard Worker  # ---------------------------------
489*8975f5c5SAndroid Build Coastguard Worker  if (is_linux || is_chromeos || is_android || is_fuchsia) {
490*8975f5c5SAndroid Build Coastguard Worker    asmflags += [ "-fPIC" ]
491*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fPIC" ]
492*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-fPIC" ]
493*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "-Crelocation-model=pic" ]
494*8975f5c5SAndroid Build Coastguard Worker
495*8975f5c5SAndroid Build Coastguard Worker    if (!is_clang) {
496*8975f5c5SAndroid Build Coastguard Worker      # Use pipes for communicating between sub-processes. Faster.
497*8975f5c5SAndroid Build Coastguard Worker      # (This flag doesn't do anything with Clang.)
498*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-pipe" ]
499*8975f5c5SAndroid Build Coastguard Worker    }
500*8975f5c5SAndroid Build Coastguard Worker
501*8975f5c5SAndroid Build Coastguard Worker    ldflags += [
502*8975f5c5SAndroid Build Coastguard Worker      "-Wl,-z,noexecstack",
503*8975f5c5SAndroid Build Coastguard Worker      "-Wl,-z,relro",
504*8975f5c5SAndroid Build Coastguard Worker    ]
505*8975f5c5SAndroid Build Coastguard Worker
506*8975f5c5SAndroid Build Coastguard Worker    if (!is_component_build) {
507*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,-z,now" ]
508*8975f5c5SAndroid Build Coastguard Worker    }
509*8975f5c5SAndroid Build Coastguard Worker  }
510*8975f5c5SAndroid Build Coastguard Worker
511*8975f5c5SAndroid Build Coastguard Worker  # Linux-specific compiler flags setup.
512*8975f5c5SAndroid Build Coastguard Worker  # ------------------------------------
513*8975f5c5SAndroid Build Coastguard Worker  if (use_icf && (!is_apple || use_lld)) {
514*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,--icf=all" ]
515*8975f5c5SAndroid Build Coastguard Worker  }
516*8975f5c5SAndroid Build Coastguard Worker
517*8975f5c5SAndroid Build Coastguard Worker  if (is_linux || is_chromeos) {
518*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-pthread" ]
519*8975f5c5SAndroid Build Coastguard Worker    # Do not use the -pthread ldflag here since it becomes a no-op
520*8975f5c5SAndroid Build Coastguard Worker    # when using -nodefaultlibs, which would cause an unused argument
521*8975f5c5SAndroid Build Coastguard Worker    # error.  "-lpthread" is added in //build/config:default_libs.
522*8975f5c5SAndroid Build Coastguard Worker  }
523*8975f5c5SAndroid Build Coastguard Worker
524*8975f5c5SAndroid Build Coastguard Worker  # ChromeOS-specific compiler flags setup.
525*8975f5c5SAndroid Build Coastguard Worker  # ---------------------------------------
526*8975f5c5SAndroid Build Coastguard Worker  if (is_chromeos) {
527*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-Werror=poison-system-directories" ]
528*8975f5c5SAndroid Build Coastguard Worker  }
529*8975f5c5SAndroid Build Coastguard Worker
530*8975f5c5SAndroid Build Coastguard Worker  # Clang-specific compiler flags setup.
531*8975f5c5SAndroid Build Coastguard Worker  # ------------------------------------
532*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
533*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fcolor-diagnostics" ]
534*8975f5c5SAndroid Build Coastguard Worker
535*8975f5c5SAndroid Build Coastguard Worker    # Enable -fmerge-all-constants. This used to be the default in clang
536*8975f5c5SAndroid Build Coastguard Worker    # for over a decade. It makes clang non-conforming, but is fairly safe
537*8975f5c5SAndroid Build Coastguard Worker    # in practice and saves some binary size. We might want to consider
538*8975f5c5SAndroid Build Coastguard Worker    # disabling this (https://bugs.llvm.org/show_bug.cgi?id=18538#c13),
539*8975f5c5SAndroid Build Coastguard Worker    # but for now it looks like our build might rely on it
540*8975f5c5SAndroid Build Coastguard Worker    # (https://crbug.com/829795).
541*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fmerge-all-constants" ]
542*8975f5c5SAndroid Build Coastguard Worker
543*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/345541122): investigate the fuchsia binary size increase.
544*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
545*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "/Zc:sizedDealloc-" ]
546*8975f5c5SAndroid Build Coastguard Worker    } else {
547*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-fno-sized-deallocation" ]
548*8975f5c5SAndroid Build Coastguard Worker    }
549*8975f5c5SAndroid Build Coastguard Worker  }
550*8975f5c5SAndroid Build Coastguard Worker
551*8975f5c5SAndroid Build Coastguard Worker  if (use_lld) {
552*8975f5c5SAndroid Build Coastguard Worker    # TODO(thakis): Make the driver pass --color-diagnostics to the linker
553*8975f5c5SAndroid Build Coastguard Worker    # if -fcolor-diagnostics is passed to it, and pass -fcolor-diagnostics
554*8975f5c5SAndroid Build Coastguard Worker    # in ldflags instead.
555*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
556*8975f5c5SAndroid Build Coastguard Worker      # On Windows, we call the linker directly, instead of calling it through
557*8975f5c5SAndroid Build Coastguard Worker      # the driver.
558*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "--color-diagnostics" ]
559*8975f5c5SAndroid Build Coastguard Worker    } else {
560*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--color-diagnostics" ]
561*8975f5c5SAndroid Build Coastguard Worker    }
562*8975f5c5SAndroid Build Coastguard Worker  }
563*8975f5c5SAndroid Build Coastguard Worker
564*8975f5c5SAndroid Build Coastguard Worker  # Enable text section splitting only on linux when using lld for now. Other
565*8975f5c5SAndroid Build Coastguard Worker  # platforms can be added later if needed.
566*8975f5c5SAndroid Build Coastguard Worker  if ((is_linux || is_chromeos) && use_lld && use_text_section_splitting) {
567*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,-z,keep-text-section-prefix" ]
568*8975f5c5SAndroid Build Coastguard Worker  }
569*8975f5c5SAndroid Build Coastguard Worker
570*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && !is_nacl) {
571*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fcrash-diagnostics-dir=" + clang_diagnostic_dir ]
572*8975f5c5SAndroid Build Coastguard Worker    if (save_reproducers_on_lld_crash && use_lld) {
573*8975f5c5SAndroid Build Coastguard Worker      ldflags += [
574*8975f5c5SAndroid Build Coastguard Worker        "-fcrash-diagnostics=all",
575*8975f5c5SAndroid Build Coastguard Worker        "-fcrash-diagnostics-dir=" + clang_diagnostic_dir,
576*8975f5c5SAndroid Build Coastguard Worker      ]
577*8975f5c5SAndroid Build Coastguard Worker    }
578*8975f5c5SAndroid Build Coastguard Worker
579*8975f5c5SAndroid Build Coastguard Worker    # TODO(hans): Remove this once Clang generates better optimized debug info
580*8975f5c5SAndroid Build Coastguard Worker    # by default. https://crbug.com/765793
581*8975f5c5SAndroid Build Coastguard Worker    cflags += [
582*8975f5c5SAndroid Build Coastguard Worker      "-mllvm",
583*8975f5c5SAndroid Build Coastguard Worker      "-instcombine-lower-dbg-declare=0",
584*8975f5c5SAndroid Build Coastguard Worker    ]
585*8975f5c5SAndroid Build Coastguard Worker    if (!is_debug && use_thin_lto && is_a_target_toolchain) {
586*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
587*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mllvm:-instcombine-lower-dbg-declare=0" ]
588*8975f5c5SAndroid Build Coastguard Worker      } else {
589*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,-mllvm,-instcombine-lower-dbg-declare=0" ]
590*8975f5c5SAndroid Build Coastguard Worker      }
591*8975f5c5SAndroid Build Coastguard Worker    }
592*8975f5c5SAndroid Build Coastguard Worker
593*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/40283598): This causes binary size growth and potentially
594*8975f5c5SAndroid Build Coastguard Worker    # other problems.
595*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/40284925): This isn't supported by Cronet's mainline llvm version.
596*8975f5c5SAndroid Build Coastguard Worker    if (default_toolchain != "//build/toolchain/cros:target" &&
597*8975f5c5SAndroid Build Coastguard Worker        !llvm_android_mainline) {
598*8975f5c5SAndroid Build Coastguard Worker      cflags += [
599*8975f5c5SAndroid Build Coastguard Worker        "-mllvm",
600*8975f5c5SAndroid Build Coastguard Worker        "-split-threshold-for-reg-with-hint=0",
601*8975f5c5SAndroid Build Coastguard Worker      ]
602*8975f5c5SAndroid Build Coastguard Worker      if (use_thin_lto && is_a_target_toolchain) {
603*8975f5c5SAndroid Build Coastguard Worker        if (is_win) {
604*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-mllvm:-split-threshold-for-reg-with-hint=0" ]
605*8975f5c5SAndroid Build Coastguard Worker        } else {
606*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-Wl,-mllvm,-split-threshold-for-reg-with-hint=0" ]
607*8975f5c5SAndroid Build Coastguard Worker        }
608*8975f5c5SAndroid Build Coastguard Worker      }
609*8975f5c5SAndroid Build Coastguard Worker    }
610*8975f5c5SAndroid Build Coastguard Worker
611*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/40192287): Investigate why/if this should be needed.
612*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
613*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "/clang:-ffp-contract=off" ]
614*8975f5c5SAndroid Build Coastguard Worker    } else {
615*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-ffp-contract=off" ]
616*8975f5c5SAndroid Build Coastguard Worker    }
617*8975f5c5SAndroid Build Coastguard Worker
618*8975f5c5SAndroid Build Coastguard Worker    # Enable ELF CREL (see crbug.com/357878242) for all platforms that use ELF
619*8975f5c5SAndroid Build Coastguard Worker    # (excluding toolchains that use an older version of LLVM).
620*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/376278218): This causes segfault on Linux ARM builds.
621*8975f5c5SAndroid Build Coastguard Worker    if (is_linux && !llvm_android_mainline && current_cpu != "arm" &&
622*8975f5c5SAndroid Build Coastguard Worker        default_toolchain != "//build/toolchain/cros:target") {
623*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wa,--crel,--allow-experimental-crel" ]
624*8975f5c5SAndroid Build Coastguard Worker    }
625*8975f5c5SAndroid Build Coastguard Worker  }
626*8975f5c5SAndroid Build Coastguard Worker
627*8975f5c5SAndroid Build Coastguard Worker  # C11/C++11 compiler flags setup.
628*8975f5c5SAndroid Build Coastguard Worker  # ---------------------------
629*8975f5c5SAndroid Build Coastguard Worker  if (is_linux || is_chromeos || is_android || (is_nacl && is_clang) ||
630*8975f5c5SAndroid Build Coastguard Worker      current_os == "aix") {
631*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
632*8975f5c5SAndroid Build Coastguard Worker      standard_prefix = "c"
633*8975f5c5SAndroid Build Coastguard Worker
634*8975f5c5SAndroid Build Coastguard Worker      # Since we build with -std=c* and not -std=gnu*, _GNU_SOURCE will not be
635*8975f5c5SAndroid Build Coastguard Worker      # defined by the compiler.  However, lots of code relies on the
636*8975f5c5SAndroid Build Coastguard Worker      # non-standard features that _GNU_SOURCE enables, so define it manually.
637*8975f5c5SAndroid Build Coastguard Worker      defines += [ "_GNU_SOURCE" ]
638*8975f5c5SAndroid Build Coastguard Worker
639*8975f5c5SAndroid Build Coastguard Worker      if (is_nacl) {
640*8975f5c5SAndroid Build Coastguard Worker        # Undefine __STRICT_ANSI__ to get non-standard features which would
641*8975f5c5SAndroid Build Coastguard Worker        # otherwise not be enabled by NaCl's sysroots.
642*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-U__STRICT_ANSI__" ]
643*8975f5c5SAndroid Build Coastguard Worker      }
644*8975f5c5SAndroid Build Coastguard Worker    } else {
645*8975f5c5SAndroid Build Coastguard Worker      # Gcc does not support ##__VA_ARGS__ when in standards-conforming mode,
646*8975f5c5SAndroid Build Coastguard Worker      # but we use this feature in several places in Chromium.
647*8975f5c5SAndroid Build Coastguard Worker      # TODO(thomasanderson): Replace usages of ##__VA_ARGS__ with the
648*8975f5c5SAndroid Build Coastguard Worker      # standard-compliant __VA_OPT__ added by C++20, and switch the gcc build
649*8975f5c5SAndroid Build Coastguard Worker      # to -std=c*.
650*8975f5c5SAndroid Build Coastguard Worker      standard_prefix = "gnu"
651*8975f5c5SAndroid Build Coastguard Worker    }
652*8975f5c5SAndroid Build Coastguard Worker
653*8975f5c5SAndroid Build Coastguard Worker    cflags_c += [ "-std=${standard_prefix}11" ]
654*8975f5c5SAndroid Build Coastguard Worker    if (is_nacl && !is_nacl_saigo) {
655*8975f5c5SAndroid Build Coastguard Worker      # This is for the pnacl_newlib toolchain. It's only used to build
656*8975f5c5SAndroid Build Coastguard Worker      # a few independent ppapi test files that don't pull in any other
657*8975f5c5SAndroid Build Coastguard Worker      # dependencies.
658*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "-std=${standard_prefix}++14" ]
659*8975f5c5SAndroid Build Coastguard Worker      if (is_clang) {
660*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [ "-fno-trigraphs" ]
661*8975f5c5SAndroid Build Coastguard Worker      }
662*8975f5c5SAndroid Build Coastguard Worker    } else if (is_clang) {
663*8975f5c5SAndroid Build Coastguard Worker      if (defined(use_cxx17) && use_cxx17) {
664*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [ "-std=${standard_prefix}++17" ]
665*8975f5c5SAndroid Build Coastguard Worker      } else {
666*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [ "-std=${standard_prefix}++20" ]
667*8975f5c5SAndroid Build Coastguard Worker      }
668*8975f5c5SAndroid Build Coastguard Worker    } else {
669*8975f5c5SAndroid Build Coastguard Worker      # The gcc bots are currently using GCC 9, which is not new enough to
670*8975f5c5SAndroid Build Coastguard Worker      # support "c++20"/"gnu++20".
671*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "-std=${standard_prefix}++2a" ]
672*8975f5c5SAndroid Build Coastguard Worker    }
673*8975f5c5SAndroid Build Coastguard Worker  } else if (is_win) {
674*8975f5c5SAndroid Build Coastguard Worker    cflags_c += [ "/std:c11" ]
675*8975f5c5SAndroid Build Coastguard Worker    if (defined(use_cxx17) && use_cxx17) {
676*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "/std:c++17" ]
677*8975f5c5SAndroid Build Coastguard Worker    } else {
678*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "/std:c++20" ]
679*8975f5c5SAndroid Build Coastguard Worker    }
680*8975f5c5SAndroid Build Coastguard Worker    if (!is_clang) {
681*8975f5c5SAndroid Build Coastguard Worker      # Required for the __cplusplus macro definition to match the C++ version
682*8975f5c5SAndroid Build Coastguard Worker      # on MSVC. clang-cl defines it by default and doesn't need this flag.
683*8975f5c5SAndroid Build Coastguard Worker      # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus
684*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "/Zc:__cplusplus" ]
685*8975f5c5SAndroid Build Coastguard Worker    }
686*8975f5c5SAndroid Build Coastguard Worker  } else if (!is_nacl) {
687*8975f5c5SAndroid Build Coastguard Worker    # TODO(mcgrathr) - the NaCl GCC toolchain doesn't support either
688*8975f5c5SAndroid Build Coastguard Worker    # gnu11/gnu++11 or c11/c++11; we technically don't need this toolchain any
689*8975f5c5SAndroid Build Coastguard Worker    # more, but there are still a few buildbots using it, so until those are
690*8975f5c5SAndroid Build Coastguard Worker    # turned off we need the !is_nacl clause and the (is_nacl && is_clang)
691*8975f5c5SAndroid Build Coastguard Worker    # clause, above.
692*8975f5c5SAndroid Build Coastguard Worker    cflags_c += [ "-std=c11" ]
693*8975f5c5SAndroid Build Coastguard Worker
694*8975f5c5SAndroid Build Coastguard Worker    if (defined(use_cxx17) && use_cxx17) {
695*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "-std=c++17" ]
696*8975f5c5SAndroid Build Coastguard Worker    } else {
697*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [ "-std=c++20" ]
698*8975f5c5SAndroid Build Coastguard Worker    }
699*8975f5c5SAndroid Build Coastguard Worker  }
700*8975f5c5SAndroid Build Coastguard Worker
701*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
702*8975f5c5SAndroid Build Coastguard Worker    # C++17 removes trigraph support, but clang still warns that it ignores
703*8975f5c5SAndroid Build Coastguard Worker    # them when seeing them.  Don't.
704*8975f5c5SAndroid Build Coastguard Worker    cflags_cc += [ "-Wno-trigraphs" ]
705*8975f5c5SAndroid Build Coastguard Worker  }
706*8975f5c5SAndroid Build Coastguard Worker
707*8975f5c5SAndroid Build Coastguard Worker  if (use_relative_vtables_abi) {
708*8975f5c5SAndroid Build Coastguard Worker    cflags_cc += [ "-fexperimental-relative-c++-abi-vtables" ]
709*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-fexperimental-relative-c++-abi-vtables" ]
710*8975f5c5SAndroid Build Coastguard Worker  }
711*8975f5c5SAndroid Build Coastguard Worker
712*8975f5c5SAndroid Build Coastguard Worker  # Add flags for link-time optimization. These flags enable
713*8975f5c5SAndroid Build Coastguard Worker  # optimizations/transformations that require whole-program visibility at link
714*8975f5c5SAndroid Build Coastguard Worker  # time, so they need to be applied to all translation units, and we may end up
715*8975f5c5SAndroid Build Coastguard Worker  # with miscompiles if only part of the program is compiled with LTO flags. For
716*8975f5c5SAndroid Build Coastguard Worker  # that reason, we cannot allow targets to enable or disable these flags, for
717*8975f5c5SAndroid Build Coastguard Worker  # example by disabling the optimize configuration.
718*8975f5c5SAndroid Build Coastguard Worker  # TODO(pcc): Make this conditional on is_official_build rather than on gn
719*8975f5c5SAndroid Build Coastguard Worker  # flags for specific features.
720*8975f5c5SAndroid Build Coastguard Worker  #
721*8975f5c5SAndroid Build Coastguard Worker  # High-end Android: While Full LTO provides a small performance improvement
722*8975f5c5SAndroid Build Coastguard Worker  # (according to Speedometer), it also results in an unacceptable increase in
723*8975f5c5SAndroid Build Coastguard Worker  # build time. Thin LTO appears to provide the best build time-optimization
724*8975f5c5SAndroid Build Coastguard Worker  # tradeoff. As of April 2024, Full LTO:
725*8975f5c5SAndroid Build Coastguard Worker  #   - Increases build time by ~1:30 hours, to ~2:40 hours (from ~1:10 hours
726*8975f5c5SAndroid Build Coastguard Worker  #     with Thin LTO) on Chromium builders.
727*8975f5c5SAndroid Build Coastguard Worker  #   - Increases Speedometer 2.1 score by 1.1% [0].
728*8975f5c5SAndroid Build Coastguard Worker  #   - Increases Speedometer 3.0 score by 1.2% [1].
729*8975f5c5SAndroid Build Coastguard Worker  # ... over Thin LTO.
730*8975f5c5SAndroid Build Coastguard Worker  #
731*8975f5c5SAndroid Build Coastguard Worker  # [0]: https://pinpoint-dot-chromeperf.appspot.com/job/15efb0313e0000
732*8975f5c5SAndroid Build Coastguard Worker  # [1]: https://pinpoint-dot-chromeperf.appspot.com/job/157f0b42be0000
733*8975f5c5SAndroid Build Coastguard Worker  if (!is_debug && use_thin_lto && is_a_target_toolchain) {
734*8975f5c5SAndroid Build Coastguard Worker    assert(use_lld, "LTO is only supported with lld")
735*8975f5c5SAndroid Build Coastguard Worker
736*8975f5c5SAndroid Build Coastguard Worker    cflags += [
737*8975f5c5SAndroid Build Coastguard Worker      "-flto=thin",
738*8975f5c5SAndroid Build Coastguard Worker      "-fsplit-lto-unit",
739*8975f5c5SAndroid Build Coastguard Worker    ]
740*8975f5c5SAndroid Build Coastguard Worker
741*8975f5c5SAndroid Build Coastguard Worker    if (thin_lto_enable_cache) {
742*8975f5c5SAndroid Build Coastguard Worker      # Limit the size of the ThinLTO cache to the lesser of 10% of
743*8975f5c5SAndroid Build Coastguard Worker      # available disk space, 40GB and 100000 files.
744*8975f5c5SAndroid Build Coastguard Worker      cache_policy =
745*8975f5c5SAndroid Build Coastguard Worker          "cache_size=10%:cache_size_bytes=40g:cache_size_files=100000"
746*8975f5c5SAndroid Build Coastguard Worker      cache_dir = rebase_path("$root_out_dir/thinlto-cache", root_build_dir)
747*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
748*8975f5c5SAndroid Build Coastguard Worker        ldflags += [
749*8975f5c5SAndroid Build Coastguard Worker          "/lldltocache:$cache_dir",
750*8975f5c5SAndroid Build Coastguard Worker          "/lldltocachepolicy:$cache_policy",
751*8975f5c5SAndroid Build Coastguard Worker        ]
752*8975f5c5SAndroid Build Coastguard Worker      } else {
753*8975f5c5SAndroid Build Coastguard Worker        if (is_apple) {
754*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-Wl,-cache_path_lto,$cache_dir" ]
755*8975f5c5SAndroid Build Coastguard Worker        } else {
756*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-Wl,--thinlto-cache-dir=$cache_dir" ]
757*8975f5c5SAndroid Build Coastguard Worker        }
758*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,--thinlto-cache-policy=$cache_policy" ]
759*8975f5c5SAndroid Build Coastguard Worker      }
760*8975f5c5SAndroid Build Coastguard Worker    }
761*8975f5c5SAndroid Build Coastguard Worker
762*8975f5c5SAndroid Build Coastguard Worker    # An import limit of 30 has better performance (per speedometer) and lower
763*8975f5c5SAndroid Build Coastguard Worker    # binary size than the default setting of 100.
764*8975f5c5SAndroid Build Coastguard Worker    # TODO(gbiv): We ideally shouldn't need to specify this; ThinLTO
765*8975f5c5SAndroid Build Coastguard Worker    # should be able to better manage binary size increases on its own.
766*8975f5c5SAndroid Build Coastguard Worker    #
767*8975f5c5SAndroid Build Coastguard Worker    # For high-end Android, 30 seems to be the right trade-off between performance
768*8975f5c5SAndroid Build Coastguard Worker    # and binary size, at least based on Speedometer. For example, increasing
769*8975f5c5SAndroid Build Coastguard Worker    # `import_instr_limit`to 50 improves Speedometer 2.1 score by 0.7% [0], while
770*8975f5c5SAndroid Build Coastguard Worker    # Speedometer 3.0 score remains roughly the same (-0.1%) [1]. The binary size
771*8975f5c5SAndroid Build Coastguard Worker    # increases by about 2MB [2] (or 1.1%: the arm64 native code size in M124 is
772*8975f5c5SAndroid Build Coastguard Worker    # 178MB).
773*8975f5c5SAndroid Build Coastguard Worker    # [0]: https://pinpoint-dot-chromeperf.appspot.com/job/16984a18be0000
774*8975f5c5SAndroid Build Coastguard Worker    # [1]: https://pinpoint-dot-chromeperf.appspot.com/job/11984a18be0000
775*8975f5c5SAndroid Build Coastguard Worker    # [2]: https://ci.chromium.org/ui/p/chromium/builders/try/android-binary-size/1848442
776*8975f5c5SAndroid Build Coastguard Worker    import_instr_limit = 30
777*8975f5c5SAndroid Build Coastguard Worker
778*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
779*8975f5c5SAndroid Build Coastguard Worker      ldflags += [
780*8975f5c5SAndroid Build Coastguard Worker        "/opt:lldltojobs=all",
781*8975f5c5SAndroid Build Coastguard Worker        "-mllvm:-import-instr-limit=$import_instr_limit",
782*8975f5c5SAndroid Build Coastguard Worker        "-mllvm:-disable-auto-upgrade-debug-info",
783*8975f5c5SAndroid Build Coastguard Worker      ]
784*8975f5c5SAndroid Build Coastguard Worker    } else {
785*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-flto=thin" ]
786*8975f5c5SAndroid Build Coastguard Worker
787*8975f5c5SAndroid Build Coastguard Worker      # Enabling ThinLTO on Chrome OS too, in an effort to reduce the memory
788*8975f5c5SAndroid Build Coastguard Worker      # usage in crbug.com/1038040. Note this will increase build time in
789*8975f5c5SAndroid Build Coastguard Worker      # Chrome OS.
790*8975f5c5SAndroid Build Coastguard Worker
791*8975f5c5SAndroid Build Coastguard Worker      # In ThinLTO builds, we run at most one link process at a time,
792*8975f5c5SAndroid Build Coastguard Worker      # and let it use all cores.
793*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): Check if '=0' (that is, number of cores, instead
794*8975f5c5SAndroid Build Coastguard Worker      # of "all" which means number of hardware threads) is faster.
795*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--thinlto-jobs=all" ]
796*8975f5c5SAndroid Build Coastguard Worker
797*8975f5c5SAndroid Build Coastguard Worker      if (is_chromeos) {
798*8975f5c5SAndroid Build Coastguard Worker        # ARM was originally set lower than x86 to keep the size
799*8975f5c5SAndroid Build Coastguard Worker        # bloat of ThinLTO to <10%, but that's potentially no longer true.
800*8975f5c5SAndroid Build Coastguard Worker        # FIXME(inglorion): maybe tune these?
801*8975f5c5SAndroid Build Coastguard Worker        # TODO(b/271459198): Revert limit on amd64 to 30 when fixed.
802*8975f5c5SAndroid Build Coastguard Worker        import_instr_limit = 20
803*8975f5c5SAndroid Build Coastguard Worker      } else if (is_android && optimize_for_size) {
804*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/40219076): Investigate if we can get the > 6% perf win
805*8975f5c5SAndroid Build Coastguard Worker        # of import_instr_limit 30 with a binary size hit smaller than ~2 MiB.
806*8975f5c5SAndroid Build Coastguard Worker        import_instr_limit = 5
807*8975f5c5SAndroid Build Coastguard Worker      }
808*8975f5c5SAndroid Build Coastguard Worker
809*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,-mllvm,-import-instr-limit=$import_instr_limit" ]
810*8975f5c5SAndroid Build Coastguard Worker
811*8975f5c5SAndroid Build Coastguard Worker      if (is_apple) {
812*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wcrl,object_path_lto" ]
813*8975f5c5SAndroid Build Coastguard Worker      }
814*8975f5c5SAndroid Build Coastguard Worker
815*8975f5c5SAndroid Build Coastguard Worker      # We only use one version of LLVM within a build so there's no need to
816*8975f5c5SAndroid Build Coastguard Worker      # upgrade debug info, which can be expensive since it runs the verifier.
817*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,-mllvm,-disable-auto-upgrade-debug-info" ]
818*8975f5c5SAndroid Build Coastguard Worker    }
819*8975f5c5SAndroid Build Coastguard Worker
820*8975f5c5SAndroid Build Coastguard Worker    if (!optimize_for_size) {
821*8975f5c5SAndroid Build Coastguard Worker      # Ideally the compiler would handle this automatically with PGO (see
822*8975f5c5SAndroid Build Coastguard Worker      # comments at https://crrev.com/c/5440500).
823*8975f5c5SAndroid Build Coastguard Worker      cflags += [
824*8975f5c5SAndroid Build Coastguard Worker        "-mllvm",
825*8975f5c5SAndroid Build Coastguard Worker        "-inlinehint-threshold=360",
826*8975f5c5SAndroid Build Coastguard Worker      ]
827*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
828*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mllvm:-inlinehint-threshold=360" ]
829*8975f5c5SAndroid Build Coastguard Worker      } else {
830*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,-mllvm,-inlinehint-threshold=360" ]
831*8975f5c5SAndroid Build Coastguard Worker      }
832*8975f5c5SAndroid Build Coastguard Worker    }
833*8975f5c5SAndroid Build Coastguard Worker
834*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/40182783): investigate why this isn't effective on
835*8975f5c5SAndroid Build Coastguard Worker    # arm32.
836*8975f5c5SAndroid Build Coastguard Worker    if (!is_android || current_cpu == "arm64") {
837*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-fwhole-program-vtables" ]
838*8975f5c5SAndroid Build Coastguard Worker
839*8975f5c5SAndroid Build Coastguard Worker      if (toolchain_supports_rust_thin_lto) {
840*8975f5c5SAndroid Build Coastguard Worker        # whole-program-vtables implies -fsplit-lto-unit, and Rust needs to match
841*8975f5c5SAndroid Build Coastguard Worker        # behaviour. Rust needs to know the linker will be doing LTO in this case
842*8975f5c5SAndroid Build Coastguard Worker        # or it rejects the Zsplit-lto-unit flag.
843*8975f5c5SAndroid Build Coastguard Worker        rustflags += [
844*8975f5c5SAndroid Build Coastguard Worker          "-Zsplit-lto-unit",
845*8975f5c5SAndroid Build Coastguard Worker          "-Clinker-plugin-lto=yes",
846*8975f5c5SAndroid Build Coastguard Worker        ]
847*8975f5c5SAndroid Build Coastguard Worker      } else {
848*8975f5c5SAndroid Build Coastguard Worker        # Don't include bitcode if it won't be used.
849*8975f5c5SAndroid Build Coastguard Worker        rustflags += [ "-Cembed-bitcode=no" ]
850*8975f5c5SAndroid Build Coastguard Worker      }
851*8975f5c5SAndroid Build Coastguard Worker
852*8975f5c5SAndroid Build Coastguard Worker      if (!is_win) {
853*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-fwhole-program-vtables" ]
854*8975f5c5SAndroid Build Coastguard Worker      }
855*8975f5c5SAndroid Build Coastguard Worker    }
856*8975f5c5SAndroid Build Coastguard Worker
857*8975f5c5SAndroid Build Coastguard Worker    # This flag causes LTO to create an .ARM.attributes section with the correct
858*8975f5c5SAndroid Build Coastguard Worker    # architecture. This is necessary because LLD will refuse to link a program
859*8975f5c5SAndroid Build Coastguard Worker    # unless the architecture revision in .ARM.attributes is sufficiently new.
860*8975f5c5SAndroid Build Coastguard Worker    # TODO(pcc): The contents of .ARM.attributes should be based on the
861*8975f5c5SAndroid Build Coastguard Worker    # -march flag passed at compile time (see llvm.org/pr36291).
862*8975f5c5SAndroid Build Coastguard Worker    if (current_cpu == "arm") {
863*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-march=$arm_arch" ]
864*8975f5c5SAndroid Build Coastguard Worker    }
865*8975f5c5SAndroid Build Coastguard Worker  }
866*8975f5c5SAndroid Build Coastguard Worker
867*8975f5c5SAndroid Build Coastguard Worker  if (compiler_timing) {
868*8975f5c5SAndroid Build Coastguard Worker    if (is_clang && !is_nacl) {
869*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-ftime-trace" ]
870*8975f5c5SAndroid Build Coastguard Worker      if (use_lld && is_mac) {
871*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,--time-trace" ]
872*8975f5c5SAndroid Build Coastguard Worker      }
873*8975f5c5SAndroid Build Coastguard Worker    } else if (is_win) {
874*8975f5c5SAndroid Build Coastguard Worker      cflags += [
875*8975f5c5SAndroid Build Coastguard Worker        # "Documented" here:
876*8975f5c5SAndroid Build Coastguard Worker        # http://aras-p.info/blog/2017/10/23/Best-unknown-MSVC-flag-d2cgsummary/
877*8975f5c5SAndroid Build Coastguard Worker        "/d2cgsummary",
878*8975f5c5SAndroid Build Coastguard Worker      ]
879*8975f5c5SAndroid Build Coastguard Worker    }
880*8975f5c5SAndroid Build Coastguard Worker  }
881*8975f5c5SAndroid Build Coastguard Worker
882*8975f5c5SAndroid Build Coastguard Worker  # Pass flag to LLD so Android builds can allow debuggerd to properly symbolize
883*8975f5c5SAndroid Build Coastguard Worker  # stack crashes (http://crbug.com/919499).
884*8975f5c5SAndroid Build Coastguard Worker  if (use_lld && is_android) {
885*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,--no-rosegment" ]
886*8975f5c5SAndroid Build Coastguard Worker  }
887*8975f5c5SAndroid Build Coastguard Worker
888*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/40242425): Cleanup undefined symbol errors caught by
889*8975f5c5SAndroid Build Coastguard Worker  # --no-undefined-version.
890*8975f5c5SAndroid Build Coastguard Worker  if (use_lld && !is_win && !is_mac && !is_ios) {
891*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,--undefined-version" ]
892*8975f5c5SAndroid Build Coastguard Worker  }
893*8975f5c5SAndroid Build Coastguard Worker
894*8975f5c5SAndroid Build Coastguard Worker  if (use_lld && is_apple) {
895*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,--strict-auto-link" ]
896*8975f5c5SAndroid Build Coastguard Worker  }
897*8975f5c5SAndroid Build Coastguard Worker
898*8975f5c5SAndroid Build Coastguard Worker  # LLD does call-graph-sorted binary layout by default when profile data is
899*8975f5c5SAndroid Build Coastguard Worker  # present. On Android this increases binary size due to more thinks for long
900*8975f5c5SAndroid Build Coastguard Worker  # jumps. Turn it off by default and enable selectively for targets where it's
901*8975f5c5SAndroid Build Coastguard Worker  # beneficial.
902*8975f5c5SAndroid Build Coastguard Worker  if (use_lld && !enable_call_graph_profile_sort) {
903*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
904*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "/call-graph-profile-sort:no" ]
905*8975f5c5SAndroid Build Coastguard Worker    } else {
906*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--no-call-graph-profile-sort" ]
907*8975f5c5SAndroid Build Coastguard Worker    }
908*8975f5c5SAndroid Build Coastguard Worker  }
909*8975f5c5SAndroid Build Coastguard Worker
910*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && !is_nacl && show_includes) {
911*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
912*8975f5c5SAndroid Build Coastguard Worker      cflags += [
913*8975f5c5SAndroid Build Coastguard Worker        "/clang:-H",
914*8975f5c5SAndroid Build Coastguard Worker        "/clang:-fshow-skipped-includes",
915*8975f5c5SAndroid Build Coastguard Worker      ]
916*8975f5c5SAndroid Build Coastguard Worker    } else {
917*8975f5c5SAndroid Build Coastguard Worker      cflags += [
918*8975f5c5SAndroid Build Coastguard Worker        "-H",
919*8975f5c5SAndroid Build Coastguard Worker        "-fshow-skipped-includes",
920*8975f5c5SAndroid Build Coastguard Worker      ]
921*8975f5c5SAndroid Build Coastguard Worker    }
922*8975f5c5SAndroid Build Coastguard Worker  }
923*8975f5c5SAndroid Build Coastguard Worker
924*8975f5c5SAndroid Build Coastguard Worker  # This flag enforces that member pointer base types are complete. It helps
925*8975f5c5SAndroid Build Coastguard Worker  # prevent us from running into problems in the Microsoft C++ ABI (see
926*8975f5c5SAndroid Build Coastguard Worker  # https://crbug.com/847724).
927*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && !is_nacl && target_os != "chromeos" &&
928*8975f5c5SAndroid Build Coastguard Worker      (is_win || use_custom_libcxx)) {
929*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-fcomplete-member-pointers" ]
930*8975f5c5SAndroid Build Coastguard Worker  }
931*8975f5c5SAndroid Build Coastguard Worker
932*8975f5c5SAndroid Build Coastguard Worker  # Use DWARF simple template names.
933*8975f5c5SAndroid Build Coastguard Worker  if (simple_template_names) {
934*8975f5c5SAndroid Build Coastguard Worker    cflags_cc += [ "-gsimple-template-names" ]
935*8975f5c5SAndroid Build Coastguard Worker  }
936*8975f5c5SAndroid Build Coastguard Worker
937*8975f5c5SAndroid Build Coastguard Worker  # MLGO specific flags. These flags enable an ML-based inliner trained on
938*8975f5c5SAndroid Build Coastguard Worker  # Chrome on Android (arm32) with ThinLTO enabled, optimizing for size.
939*8975f5c5SAndroid Build Coastguard Worker  # The "release" ML model is embedded into clang as part of its build.
940*8975f5c5SAndroid Build Coastguard Worker  # Currently, the ML inliner is only enabled when targeting Android due to:
941*8975f5c5SAndroid Build Coastguard Worker  # a) Android is where size matters the most.
942*8975f5c5SAndroid Build Coastguard Worker  # b) MLGO presently has the limitation of only being able to embed one model
943*8975f5c5SAndroid Build Coastguard Worker  #    at a time; It is unclear if the embedded model is beneficial for
944*8975f5c5SAndroid Build Coastguard Worker  #    non-Android targets.
945*8975f5c5SAndroid Build Coastguard Worker  # MLGO is only officially supported on linux.
946*8975f5c5SAndroid Build Coastguard Worker  if (use_ml_inliner && is_a_target_toolchain) {
947*8975f5c5SAndroid Build Coastguard Worker    assert(
948*8975f5c5SAndroid Build Coastguard Worker        is_android && host_os == "linux",
949*8975f5c5SAndroid Build Coastguard Worker        "MLGO is currently only supported for targeting Android on a linux host")
950*8975f5c5SAndroid Build Coastguard Worker    if (use_thin_lto) {
951*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,-mllvm,-enable-ml-inliner=release" ]
952*8975f5c5SAndroid Build Coastguard Worker      if (is_high_end_android) {
953*8975f5c5SAndroid Build Coastguard Worker        # Besides using the arm64 - trained model, instruct the inline advisor
954*8975f5c5SAndroid Build Coastguard Worker        # to not use the ML policy (which will aim to reduce size) for any
955*8975f5c5SAndroid Build Coastguard Worker        # callsites where the caller is not cold. This avoids performance
956*8975f5c5SAndroid Build Coastguard Worker        # regressions while still bringing size benefits.
957*8975f5c5SAndroid Build Coastguard Worker        # Profile quality is essential here.
958*8975f5c5SAndroid Build Coastguard Worker        ldflags += [
959*8975f5c5SAndroid Build Coastguard Worker          "-Wl,-mllvm,-ml-inliner-model-selector=arm64-mixed",
960*8975f5c5SAndroid Build Coastguard Worker          "-Wl,-mllvm,-ml-inliner-skip-policy=if-caller-not-cold",
961*8975f5c5SAndroid Build Coastguard Worker        ]
962*8975f5c5SAndroid Build Coastguard Worker      } else {
963*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-Wl,-mllvm,-ml-inliner-model-selector=arm32-size" ]
964*8975f5c5SAndroid Build Coastguard Worker      }
965*8975f5c5SAndroid Build Coastguard Worker    }
966*8975f5c5SAndroid Build Coastguard Worker  }
967*8975f5c5SAndroid Build Coastguard Worker
968*8975f5c5SAndroid Build Coastguard Worker  if (clang_embed_bitcode) {
969*8975f5c5SAndroid Build Coastguard Worker    assert(!use_thin_lto,
970*8975f5c5SAndroid Build Coastguard Worker           "clang_embed_bitcode is only supported in non-ThinLTO builds")
971*8975f5c5SAndroid Build Coastguard Worker    cflags += [
972*8975f5c5SAndroid Build Coastguard Worker      "-Xclang",
973*8975f5c5SAndroid Build Coastguard Worker      "-fembed-bitcode=all",
974*8975f5c5SAndroid Build Coastguard Worker    ]
975*8975f5c5SAndroid Build Coastguard Worker  }
976*8975f5c5SAndroid Build Coastguard Worker
977*8975f5c5SAndroid Build Coastguard Worker  if (lld_emit_indexes_and_imports) {
978*8975f5c5SAndroid Build Coastguard Worker    assert(use_thin_lto,
979*8975f5c5SAndroid Build Coastguard Worker           "lld_emit_indexes_and_imports is only supported with ThinLTO builds")
980*8975f5c5SAndroid Build Coastguard Worker    ldflags += [
981*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--save-temps=import",
982*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--thinlto-emit-index-files",
983*8975f5c5SAndroid Build Coastguard Worker    ]
984*8975f5c5SAndroid Build Coastguard Worker  }
985*8975f5c5SAndroid Build Coastguard Worker
986*8975f5c5SAndroid Build Coastguard Worker  # Pass the same C/C++ flags to the objective C/C++ compiler.
987*8975f5c5SAndroid Build Coastguard Worker  cflags_objc += cflags_c
988*8975f5c5SAndroid Build Coastguard Worker  cflags_objcc += cflags_cc
989*8975f5c5SAndroid Build Coastguard Worker
990*8975f5c5SAndroid Build Coastguard Worker  # Assign any flags set for the C compiler to asmflags so that they are sent
991*8975f5c5SAndroid Build Coastguard Worker  # to the assembler. The Windows assembler takes different types of flags
992*8975f5c5SAndroid Build Coastguard Worker  # so only do so for posix platforms.
993*8975f5c5SAndroid Build Coastguard Worker  if (is_posix || is_fuchsia) {
994*8975f5c5SAndroid Build Coastguard Worker    asmflags += cflags
995*8975f5c5SAndroid Build Coastguard Worker    asmflags += cflags_c
996*8975f5c5SAndroid Build Coastguard Worker  }
997*8975f5c5SAndroid Build Coastguard Worker
998*8975f5c5SAndroid Build Coastguard Worker  if (is_chromeos_device && !is_nacl) {
999*8975f5c5SAndroid Build Coastguard Worker    # On ChromeOS devices, we want to ensure we're using Chrome's allocator
1000*8975f5c5SAndroid Build Coastguard Worker    # symbols for all C++ new/delete operator overloads. PartitionAlloc
1001*8975f5c5SAndroid Build Coastguard Worker    # and other local allocators should always take precedence over system or
1002*8975f5c5SAndroid Build Coastguard Worker    # preloaded allocators. These are the mangled symbol names.
1003*8975f5c5SAndroid Build Coastguard Worker    # See b/280115910 for details.
1004*8975f5c5SAndroid Build Coastguard Worker    ldflags += [
1005*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdaPv,-u,_ZdaPv",
1006*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdaPvRKSt9nothrow_t,-u,_ZdaPvRKSt9nothrow_t",
1007*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdlPv,-u,_ZdlPv",
1008*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdlPvm,-u,_ZdlPvm",
1009*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdlPvRKSt9nothrow_t,-u,_ZdlPvRKSt9nothrow_t",
1010*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_Znam,-u,_Znam",
1011*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZnamRKSt9nothrow_t,-u,_ZnamRKSt9nothrow_t",
1012*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_Znwm,-u,_Znwm",
1013*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZnwmRKSt9nothrow_t,-u,_ZnwmRKSt9nothrow_t",
1014*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdaPvmSt11align_val_t,-u,_ZdaPvmSt11align_val_t",
1015*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdaPvSt11align_val_t,-u,_ZdaPvSt11align_val_t",
1016*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdaPvSt11align_val_tRKSt9nothrow_t,-u,_ZdaPvSt11align_val_tRKSt9nothrow_t",
1017*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdlPvmSt11align_val_t,-u,_ZdlPvmSt11align_val_t",
1018*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdlPvSt11align_val_t,-u,_ZdlPvSt11align_val_t",
1019*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZdlPvSt11align_val_tRKSt9nothrow_t,-u,_ZdlPvSt11align_val_tRKSt9nothrow_t",
1020*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZnamSt11align_val_t,-u,_ZnamSt11align_val_t",
1021*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZnamSt11align_val_tRKSt9nothrow_t,-u,_ZnamSt11align_val_tRKSt9nothrow_t",
1022*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZnwmSt11align_val_t,-u,_ZnwmSt11align_val_t",
1023*8975f5c5SAndroid Build Coastguard Worker      "-Wl,--export-dynamic-symbol=_ZnwmSt11align_val_tRKSt9nothrow_t,-u,_ZnwmSt11align_val_tRKSt9nothrow_t",
1024*8975f5c5SAndroid Build Coastguard Worker    ]
1025*8975f5c5SAndroid Build Coastguard Worker  }
1026*8975f5c5SAndroid Build Coastguard Worker
1027*8975f5c5SAndroid Build Coastguard Worker  # Rust compiler flags setup.
1028*8975f5c5SAndroid Build Coastguard Worker  # ---------------------------
1029*8975f5c5SAndroid Build Coastguard Worker  rustflags += [
1030*8975f5c5SAndroid Build Coastguard Worker    # Overflow checks are optional in Rust, but even if switched
1031*8975f5c5SAndroid Build Coastguard Worker    # off they do not cause undefined behavior (the overflowing
1032*8975f5c5SAndroid Build Coastguard Worker    # behavior is defined). Because containers are bounds-checked
1033*8975f5c5SAndroid Build Coastguard Worker    # in safe Rust, they also can't provoke buffer overflows.
1034*8975f5c5SAndroid Build Coastguard Worker    # As such these checks may be less important in Rust than C++.
1035*8975f5c5SAndroid Build Coastguard Worker    # But in (simplistic) testing they have negligible performance
1036*8975f5c5SAndroid Build Coastguard Worker    # overhead, and this helps to provide consistent behavior
1037*8975f5c5SAndroid Build Coastguard Worker    # between different configurations, so we'll keep them on until
1038*8975f5c5SAndroid Build Coastguard Worker    # we discover a reason to turn them off.
1039*8975f5c5SAndroid Build Coastguard Worker    "-Coverflow-checks=on",
1040*8975f5c5SAndroid Build Coastguard Worker
1041*8975f5c5SAndroid Build Coastguard Worker    # By default Rust passes `-nodefaultlibs` to the linker, however this
1042*8975f5c5SAndroid Build Coastguard Worker    # conflicts with our `--unwind=none` flag for Android dylibs, as the latter
1043*8975f5c5SAndroid Build Coastguard Worker    # is then unused and produces a warning/error. So this removes the
1044*8975f5c5SAndroid Build Coastguard Worker    # `-nodefaultlibs` from the linker invocation from Rust, which would be used
1045*8975f5c5SAndroid Build Coastguard Worker    # to compile dylibs on Android, such as for constructing unit test APKs.
1046*8975f5c5SAndroid Build Coastguard Worker    "-Cdefault-linker-libraries",
1047*8975f5c5SAndroid Build Coastguard Worker
1048*8975f5c5SAndroid Build Coastguard Worker    # To make Rust .d files compatible with ninja
1049*8975f5c5SAndroid Build Coastguard Worker    "-Zdep-info-omit-d-target",
1050*8975f5c5SAndroid Build Coastguard Worker
1051*8975f5c5SAndroid Build Coastguard Worker    # If a macro panics during compilation, show which macro and where it is
1052*8975f5c5SAndroid Build Coastguard Worker    # defined.
1053*8975f5c5SAndroid Build Coastguard Worker    "-Zmacro-backtrace",
1054*8975f5c5SAndroid Build Coastguard Worker
1055*8975f5c5SAndroid Build Coastguard Worker    # For deterministic builds, keep the local machine's current working
1056*8975f5c5SAndroid Build Coastguard Worker    # directory from appearing in build outputs.
1057*8975f5c5SAndroid Build Coastguard Worker    "-Zremap-cwd-prefix=.",
1058*8975f5c5SAndroid Build Coastguard Worker
1059*8975f5c5SAndroid Build Coastguard Worker    # We use clang-rt sanitizer runtimes.
1060*8975f5c5SAndroid Build Coastguard Worker    "-Zexternal-clangrt",
1061*8975f5c5SAndroid Build Coastguard Worker  ]
1062*8975f5c5SAndroid Build Coastguard Worker
1063*8975f5c5SAndroid Build Coastguard Worker  if (!is_win || force_rustc_color_output) {
1064*8975f5c5SAndroid Build Coastguard Worker    # Colorize error output. The analogous flag is passed for clang. This must
1065*8975f5c5SAndroid Build Coastguard Worker    # be platform-gated since rustc will unconditionally output ANSI escape
1066*8975f5c5SAndroid Build Coastguard Worker    # sequences, ignoring the platform, when stderr is not a terminal.
1067*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "--color=always" ]
1068*8975f5c5SAndroid Build Coastguard Worker  }
1069*8975f5c5SAndroid Build Coastguard Worker  if (rust_abi_target != "") {
1070*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "--target=$rust_abi_target" ]
1071*8975f5c5SAndroid Build Coastguard Worker  }
1072*8975f5c5SAndroid Build Coastguard Worker  if (!use_thin_lto || !toolchain_supports_rust_thin_lto) {
1073*8975f5c5SAndroid Build Coastguard Worker    # Don't include bitcode if it won't be used.
1074*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "-Cembed-bitcode=no" ]
1075*8975f5c5SAndroid Build Coastguard Worker
1076*8975f5c5SAndroid Build Coastguard Worker    # Disable "automatic" ThinLTO between codegen units. The weak symbol
1077*8975f5c5SAndroid Build Coastguard Worker    # resolution across units can have surprising effects on linking, see
1078*8975f5c5SAndroid Build Coastguard Worker    # crbug.com/324126269 and github.com/rust-lang/rust/issues/120842.
1079*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "-Clto=no" ]
1080*8975f5c5SAndroid Build Coastguard Worker  }
1081*8975f5c5SAndroid Build Coastguard Worker  if (is_official_build) {
1082*8975f5c5SAndroid Build Coastguard Worker    rustflags += [ "-Ccodegen-units=1" ]
1083*8975f5c5SAndroid Build Coastguard Worker  }
1084*8975f5c5SAndroid Build Coastguard Worker  if (!rust_prebuilt_stdlib) {
1085*8975f5c5SAndroid Build Coastguard Worker    # When building against the Chromium Rust stdlib (which we compile) always
1086*8975f5c5SAndroid Build Coastguard Worker    # abort instead of unwinding when panic occurs. In official builds, panics
1087*8975f5c5SAndroid Build Coastguard Worker    # abort immediately (this is configured in the stdlib) to keep binary size
1088*8975f5c5SAndroid Build Coastguard Worker    # down. So we unconditionally match behaviour in unofficial too.
1089*8975f5c5SAndroid Build Coastguard Worker    rustflags += [
1090*8975f5c5SAndroid Build Coastguard Worker      "-Cpanic=abort",
1091*8975f5c5SAndroid Build Coastguard Worker      "-Zpanic_abort_tests",
1092*8975f5c5SAndroid Build Coastguard Worker    ]
1093*8975f5c5SAndroid Build Coastguard Worker  }
1094*8975f5c5SAndroid Build Coastguard Worker
1095*8975f5c5SAndroid Build Coastguard Worker  # 64-bit Android sometimes defines __ARM_NEON but not __ARM_NEON__.
1096*8975f5c5SAndroid Build Coastguard Worker  # 32-bit Android builds and macOS, however, define __ARM_NEON__,
1097*8975f5c5SAndroid Build Coastguard Worker  # and code typically checks for this.
1098*8975f5c5SAndroid Build Coastguard Worker  #
1099*8975f5c5SAndroid Build Coastguard Worker  # Reduce confusion by making the __ARM_NEON__ #define always available,
1100*8975f5c5SAndroid Build Coastguard Worker  # as NEON is a mandatory part of ARMv8 anyway.
1101*8975f5c5SAndroid Build Coastguard Worker  if (current_cpu == "arm64") {
1102*8975f5c5SAndroid Build Coastguard Worker    defines += [ "__ARM_NEON__=1" ]
1103*8975f5c5SAndroid Build Coastguard Worker  }
1104*8975f5c5SAndroid Build Coastguard Worker}
1105*8975f5c5SAndroid Build Coastguard Worker
1106*8975f5c5SAndroid Build Coastguard Worker# Don't allow unstable features to be enabled by `#![feature()]` without
1107*8975f5c5SAndroid Build Coastguard Worker# additional command line flags.
1108*8975f5c5SAndroid Build Coastguard Workerconfig("disallow_unstable_features") {
1109*8975f5c5SAndroid Build Coastguard Worker  rustflags = [ "-Zallow-features=" ]
1110*8975f5c5SAndroid Build Coastguard Worker}
1111*8975f5c5SAndroid Build Coastguard Worker
1112*8975f5c5SAndroid Build Coastguard Workerconfig("libcxx_hardening") {
1113*8975f5c5SAndroid Build Coastguard Worker  # Normally, this would be defined in the `runtime_library` config but NaCl
1114*8975f5c5SAndroid Build Coastguard Worker  # saigo libc++ does not use the custom hermetic libc++. Unfortunately, there
1115*8975f5c5SAndroid Build Coastguard Worker  # isn't really a better config to add this define for the define to
1116*8975f5c5SAndroid Build Coastguard Worker  # consistently apply in both Chromium and non-Chromium code *and* non-NaCl and
1117*8975f5c5SAndroid Build Coastguard Worker  # NaCl code.
1118*8975f5c5SAndroid Build Coastguard Worker  #
1119*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/40511454): Move this back to the `runtime_library` config
1120*8975f5c5SAndroid Build Coastguard Worker  # when NaCl is removed.
1121*8975f5c5SAndroid Build Coastguard Worker  if (use_safe_libcxx) {
1122*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/40275904): Switch saigo to hardened mode once it's rolled
1123*8975f5c5SAndroid Build Coastguard Worker    # in.
1124*8975f5c5SAndroid Build Coastguard Worker    if (is_nacl_saigo) {
1125*8975f5c5SAndroid Build Coastguard Worker      defines = [ "_LIBCPP_ENABLE_ASSERTIONS=1" ]
1126*8975f5c5SAndroid Build Coastguard Worker    } else {
1127*8975f5c5SAndroid Build Coastguard Worker      defines = [ "_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE" ]
1128*8975f5c5SAndroid Build Coastguard Worker    }
1129*8975f5c5SAndroid Build Coastguard Worker  } else {
1130*8975f5c5SAndroid Build Coastguard Worker    defines = [ "_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE" ]
1131*8975f5c5SAndroid Build Coastguard Worker  }
1132*8975f5c5SAndroid Build Coastguard Worker
1133*8975f5c5SAndroid Build Coastguard Worker  # Enable libstdc++ hardening lightweight assertions. Those have a low
1134*8975f5c5SAndroid Build Coastguard Worker  # performance penalty but are considered a bare minimum for security.
1135*8975f5c5SAndroid Build Coastguard Worker  if (use_safe_libstdcxx) {
1136*8975f5c5SAndroid Build Coastguard Worker    defines += [ "_GLIBCXX_ASSERTIONS=1" ]
1137*8975f5c5SAndroid Build Coastguard Worker  }
1138*8975f5c5SAndroid Build Coastguard Worker}
1139*8975f5c5SAndroid Build Coastguard Worker
1140*8975f5c5SAndroid Build Coastguard Worker# The BUILDCONFIG file sets this config on targets by default, which means when
1141*8975f5c5SAndroid Build Coastguard Worker# building with ThinLTO, no optimization is performed in the link step.
1142*8975f5c5SAndroid Build Coastguard Workerconfig("thinlto_optimize_default") {
1143*8975f5c5SAndroid Build Coastguard Worker  if (!is_debug && use_thin_lto && is_a_target_toolchain) {
1144*8975f5c5SAndroid Build Coastguard Worker    lto_opt_level = 0
1145*8975f5c5SAndroid Build Coastguard Worker
1146*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
1147*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/opt:lldlto=" + lto_opt_level ]
1148*8975f5c5SAndroid Build Coastguard Worker    } else {
1149*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "-Wl,--lto-O" + lto_opt_level ]
1150*8975f5c5SAndroid Build Coastguard Worker    }
1151*8975f5c5SAndroid Build Coastguard Worker
1152*8975f5c5SAndroid Build Coastguard Worker    if (toolchain_supports_rust_thin_lto) {
1153*8975f5c5SAndroid Build Coastguard Worker      # We always point Rust to a linker that performs LTO, so we don't want Rust
1154*8975f5c5SAndroid Build Coastguard Worker      # to preemptively do so during compilation too or they conflict. But we do
1155*8975f5c5SAndroid Build Coastguard Worker      # want Rust to generate LTO metadata in order for the linker to do its job.
1156*8975f5c5SAndroid Build Coastguard Worker      rustflags = [ "-Clinker-plugin-lto=yes" ]
1157*8975f5c5SAndroid Build Coastguard Worker    } else {
1158*8975f5c5SAndroid Build Coastguard Worker      # Don't include bitcode if it won't be used.
1159*8975f5c5SAndroid Build Coastguard Worker      rustflags = [ "-Cembed-bitcode=no" ]
1160*8975f5c5SAndroid Build Coastguard Worker    }
1161*8975f5c5SAndroid Build Coastguard Worker  }
1162*8975f5c5SAndroid Build Coastguard Worker}
1163*8975f5c5SAndroid Build Coastguard Worker
1164*8975f5c5SAndroid Build Coastguard Worker# Use this to enable optimization in the ThinLTO link step for select targets
1165*8975f5c5SAndroid Build Coastguard Worker# when thin_lto_enable_optimizations is set by doing:
1166*8975f5c5SAndroid Build Coastguard Worker#
1167*8975f5c5SAndroid Build Coastguard Worker#   configs -= [ "//build/config/compiler:thinlto_optimize_default" ]
1168*8975f5c5SAndroid Build Coastguard Worker#   configs += [ "//build/config/compiler:thinlto_optimize_max" ]
1169*8975f5c5SAndroid Build Coastguard Worker#
1170*8975f5c5SAndroid Build Coastguard Worker# Since it makes linking significantly slower and more resource intensive, only
1171*8975f5c5SAndroid Build Coastguard Worker# use it on important targets such as the main browser executable or dll.
1172*8975f5c5SAndroid Build Coastguard Workerconfig("thinlto_optimize_max") {
1173*8975f5c5SAndroid Build Coastguard Worker  if (!is_debug && use_thin_lto && is_a_target_toolchain) {
1174*8975f5c5SAndroid Build Coastguard Worker    if (thin_lto_enable_optimizations) {
1175*8975f5c5SAndroid Build Coastguard Worker      lto_opt_level = 2
1176*8975f5c5SAndroid Build Coastguard Worker    } else {
1177*8975f5c5SAndroid Build Coastguard Worker      lto_opt_level = 0
1178*8975f5c5SAndroid Build Coastguard Worker    }
1179*8975f5c5SAndroid Build Coastguard Worker
1180*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
1181*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/opt:lldlto=" + lto_opt_level ]
1182*8975f5c5SAndroid Build Coastguard Worker    } else {
1183*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "-Wl,--lto-O" + lto_opt_level ]
1184*8975f5c5SAndroid Build Coastguard Worker    }
1185*8975f5c5SAndroid Build Coastguard Worker
1186*8975f5c5SAndroid Build Coastguard Worker    if (toolchain_supports_rust_thin_lto) {
1187*8975f5c5SAndroid Build Coastguard Worker      # We always point Rust to a linker that performs LTO, so we don't want Rust
1188*8975f5c5SAndroid Build Coastguard Worker      # to preemptively do so during compilation too or they conflict. But we do
1189*8975f5c5SAndroid Build Coastguard Worker      # want Rust to generate LTO metadata in order for the linker to do its job.
1190*8975f5c5SAndroid Build Coastguard Worker      rustflags = [ "-Clinker-plugin-lto=yes" ]
1191*8975f5c5SAndroid Build Coastguard Worker    } else {
1192*8975f5c5SAndroid Build Coastguard Worker      # Don't include bitcode if it won't be used.
1193*8975f5c5SAndroid Build Coastguard Worker      rustflags = [ "-Cembed-bitcode=no" ]
1194*8975f5c5SAndroid Build Coastguard Worker    }
1195*8975f5c5SAndroid Build Coastguard Worker  }
1196*8975f5c5SAndroid Build Coastguard Worker}
1197*8975f5c5SAndroid Build Coastguard Worker
1198*8975f5c5SAndroid Build Coastguard Worker# This provides the basic options to select the target CPU and ABI.
1199*8975f5c5SAndroid Build Coastguard Worker# It is factored out of "compiler" so that special cases can use this
1200*8975f5c5SAndroid Build Coastguard Worker# without using everything that "compiler" brings in.  Options that
1201*8975f5c5SAndroid Build Coastguard Worker# tweak code generation for a particular CPU do not belong here!
1202*8975f5c5SAndroid Build Coastguard Worker# See "compiler_codegen", below.
1203*8975f5c5SAndroid Build Coastguard Workerconfig("compiler_cpu_abi") {
1204*8975f5c5SAndroid Build Coastguard Worker  cflags = []
1205*8975f5c5SAndroid Build Coastguard Worker  ldflags = []
1206*8975f5c5SAndroid Build Coastguard Worker  defines = []
1207*8975f5c5SAndroid Build Coastguard Worker
1208*8975f5c5SAndroid Build Coastguard Worker  configs = []
1209*8975f5c5SAndroid Build Coastguard Worker  if (is_chromeos) {
1210*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/chromeos:compiler_cpu_abi" ]
1211*8975f5c5SAndroid Build Coastguard Worker  }
1212*8975f5c5SAndroid Build Coastguard Worker
1213*8975f5c5SAndroid Build Coastguard Worker  if ((is_posix && !is_apple) || is_fuchsia) {
1214*8975f5c5SAndroid Build Coastguard Worker    # CPU architecture. We may or may not be doing a cross compile now, so for
1215*8975f5c5SAndroid Build Coastguard Worker    # simplicity we always explicitly set the architecture.
1216*8975f5c5SAndroid Build Coastguard Worker    if (current_cpu == "x64") {
1217*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1218*8975f5c5SAndroid Build Coastguard Worker        "-m64",
1219*8975f5c5SAndroid Build Coastguard Worker        "-msse3",
1220*8975f5c5SAndroid Build Coastguard Worker      ]
1221*8975f5c5SAndroid Build Coastguard Worker
1222*8975f5c5SAndroid Build Coastguard Worker      # Minimum SIMD support for devices running lacros.
1223*8975f5c5SAndroid Build Coastguard Worker      # See https://crbug.com/1475858
1224*8975f5c5SAndroid Build Coastguard Worker      if (is_chromeos_lacros) {
1225*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1226*8975f5c5SAndroid Build Coastguard Worker          "-mssse3",
1227*8975f5c5SAndroid Build Coastguard Worker          "-msse4",
1228*8975f5c5SAndroid Build Coastguard Worker          "-msse4.1",
1229*8975f5c5SAndroid Build Coastguard Worker          "-msse4.2",
1230*8975f5c5SAndroid Build Coastguard Worker        ]
1231*8975f5c5SAndroid Build Coastguard Worker      }
1232*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-m64" ]
1233*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "x86") {
1234*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-m32" ]
1235*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-m32" ]
1236*8975f5c5SAndroid Build Coastguard Worker      if (!is_nacl) {
1237*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1238*8975f5c5SAndroid Build Coastguard Worker          "-mfpmath=sse",
1239*8975f5c5SAndroid Build Coastguard Worker          "-msse3",
1240*8975f5c5SAndroid Build Coastguard Worker        ]
1241*8975f5c5SAndroid Build Coastguard Worker      }
1242*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "arm") {
1243*8975f5c5SAndroid Build Coastguard Worker      if (is_clang && !is_android && !is_nacl && !is_chromeos_device) {
1244*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "--target=arm-linux-gnueabihf" ]
1245*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "--target=arm-linux-gnueabihf" ]
1246*8975f5c5SAndroid Build Coastguard Worker      }
1247*8975f5c5SAndroid Build Coastguard Worker      if (!is_nacl) {
1248*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1249*8975f5c5SAndroid Build Coastguard Worker          "-march=$arm_arch",
1250*8975f5c5SAndroid Build Coastguard Worker          "-mfloat-abi=$arm_float_abi",
1251*8975f5c5SAndroid Build Coastguard Worker        ]
1252*8975f5c5SAndroid Build Coastguard Worker      }
1253*8975f5c5SAndroid Build Coastguard Worker      if (arm_tune != "") {
1254*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-mtune=$arm_tune" ]
1255*8975f5c5SAndroid Build Coastguard Worker      }
1256*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "arm64") {
1257*8975f5c5SAndroid Build Coastguard Worker      if (is_clang && !is_android && !is_nacl && !is_fuchsia &&
1258*8975f5c5SAndroid Build Coastguard Worker          !is_chromeos_device) {
1259*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "--target=aarch64-linux-gnu" ]
1260*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "--target=aarch64-linux-gnu" ]
1261*8975f5c5SAndroid Build Coastguard Worker      }
1262*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "mipsel" && !is_nacl) {
1263*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--hash-style=sysv" ]
1264*8975f5c5SAndroid Build Coastguard Worker      if (custom_toolchain == "") {
1265*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1266*8975f5c5SAndroid Build Coastguard Worker          if (is_android) {
1267*8975f5c5SAndroid Build Coastguard Worker            cflags += [ "--target=mipsel-linux-android" ]
1268*8975f5c5SAndroid Build Coastguard Worker            ldflags += [ "--target=mipsel-linux-android" ]
1269*8975f5c5SAndroid Build Coastguard Worker          } else {
1270*8975f5c5SAndroid Build Coastguard Worker            cflags += [ "--target=mipsel-linux-gnu" ]
1271*8975f5c5SAndroid Build Coastguard Worker            ldflags += [ "--target=mipsel-linux-gnu" ]
1272*8975f5c5SAndroid Build Coastguard Worker          }
1273*8975f5c5SAndroid Build Coastguard Worker        } else {
1274*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-EL" ]
1275*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-EL" ]
1276*8975f5c5SAndroid Build Coastguard Worker        }
1277*8975f5c5SAndroid Build Coastguard Worker      }
1278*8975f5c5SAndroid Build Coastguard Worker
1279*8975f5c5SAndroid Build Coastguard Worker      if (mips_arch_variant == "r6") {
1280*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-mno-odd-spreg" ]
1281*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mips32r6" ]
1282*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1283*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1284*8975f5c5SAndroid Build Coastguard Worker            "-march=mipsel",
1285*8975f5c5SAndroid Build Coastguard Worker            "-mcpu=mips32r6",
1286*8975f5c5SAndroid Build Coastguard Worker          ]
1287*8975f5c5SAndroid Build Coastguard Worker        } else {
1288*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1289*8975f5c5SAndroid Build Coastguard Worker            "-mips32r6",
1290*8975f5c5SAndroid Build Coastguard Worker            "-Wa,-mips32r6",
1291*8975f5c5SAndroid Build Coastguard Worker          ]
1292*8975f5c5SAndroid Build Coastguard Worker          if (is_android) {
1293*8975f5c5SAndroid Build Coastguard Worker            ldflags += [ "-Wl,-melf32ltsmip" ]
1294*8975f5c5SAndroid Build Coastguard Worker          }
1295*8975f5c5SAndroid Build Coastguard Worker        }
1296*8975f5c5SAndroid Build Coastguard Worker        if (mips_use_msa == true) {
1297*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1298*8975f5c5SAndroid Build Coastguard Worker            "-mmsa",
1299*8975f5c5SAndroid Build Coastguard Worker            "-mfp64",
1300*8975f5c5SAndroid Build Coastguard Worker          ]
1301*8975f5c5SAndroid Build Coastguard Worker        }
1302*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "r2") {
1303*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mips32r2" ]
1304*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1305*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1306*8975f5c5SAndroid Build Coastguard Worker            "-march=mipsel",
1307*8975f5c5SAndroid Build Coastguard Worker            "-mcpu=mips32r2",
1308*8975f5c5SAndroid Build Coastguard Worker          ]
1309*8975f5c5SAndroid Build Coastguard Worker        } else {
1310*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1311*8975f5c5SAndroid Build Coastguard Worker            "-mips32r2",
1312*8975f5c5SAndroid Build Coastguard Worker            "-Wa,-mips32r2",
1313*8975f5c5SAndroid Build Coastguard Worker          ]
1314*8975f5c5SAndroid Build Coastguard Worker          if (mips_float_abi == "hard" && mips_fpu_mode != "") {
1315*8975f5c5SAndroid Build Coastguard Worker            cflags += [ "-m$mips_fpu_mode" ]
1316*8975f5c5SAndroid Build Coastguard Worker          }
1317*8975f5c5SAndroid Build Coastguard Worker        }
1318*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "r1") {
1319*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mips32" ]
1320*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1321*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1322*8975f5c5SAndroid Build Coastguard Worker            "-march=mipsel",
1323*8975f5c5SAndroid Build Coastguard Worker            "-mcpu=mips32",
1324*8975f5c5SAndroid Build Coastguard Worker          ]
1325*8975f5c5SAndroid Build Coastguard Worker        } else {
1326*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1327*8975f5c5SAndroid Build Coastguard Worker            "-mips32",
1328*8975f5c5SAndroid Build Coastguard Worker            "-Wa,-mips32",
1329*8975f5c5SAndroid Build Coastguard Worker          ]
1330*8975f5c5SAndroid Build Coastguard Worker        }
1331*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "loongson3") {
1332*8975f5c5SAndroid Build Coastguard Worker        defines += [ "_MIPS_ARCH_LOONGSON" ]
1333*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1334*8975f5c5SAndroid Build Coastguard Worker          "-march=loongson3a",
1335*8975f5c5SAndroid Build Coastguard Worker          "-mno-branch-likely",
1336*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-march=loongson3a",
1337*8975f5c5SAndroid Build Coastguard Worker        ]
1338*8975f5c5SAndroid Build Coastguard Worker      }
1339*8975f5c5SAndroid Build Coastguard Worker
1340*8975f5c5SAndroid Build Coastguard Worker      if (mips_dsp_rev == 1) {
1341*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-mdsp" ]
1342*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_dsp_rev == 2) {
1343*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-mdspr2" ]
1344*8975f5c5SAndroid Build Coastguard Worker      }
1345*8975f5c5SAndroid Build Coastguard Worker
1346*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-m${mips_float_abi}-float" ]
1347*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "mips" && !is_nacl) {
1348*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--hash-style=sysv" ]
1349*8975f5c5SAndroid Build Coastguard Worker      if (custom_toolchain == "") {
1350*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1351*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "--target=mips-linux-gnu" ]
1352*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "--target=mips-linux-gnu" ]
1353*8975f5c5SAndroid Build Coastguard Worker        } else {
1354*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-EB" ]
1355*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-EB" ]
1356*8975f5c5SAndroid Build Coastguard Worker        }
1357*8975f5c5SAndroid Build Coastguard Worker      }
1358*8975f5c5SAndroid Build Coastguard Worker
1359*8975f5c5SAndroid Build Coastguard Worker      if (mips_arch_variant == "r6") {
1360*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1361*8975f5c5SAndroid Build Coastguard Worker          "-mips32r6",
1362*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-mips32r6",
1363*8975f5c5SAndroid Build Coastguard Worker        ]
1364*8975f5c5SAndroid Build Coastguard Worker        if (mips_use_msa == true) {
1365*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1366*8975f5c5SAndroid Build Coastguard Worker            "-mmsa",
1367*8975f5c5SAndroid Build Coastguard Worker            "-mfp64",
1368*8975f5c5SAndroid Build Coastguard Worker          ]
1369*8975f5c5SAndroid Build Coastguard Worker        }
1370*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "r2") {
1371*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1372*8975f5c5SAndroid Build Coastguard Worker          "-mips32r2",
1373*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-mips32r2",
1374*8975f5c5SAndroid Build Coastguard Worker        ]
1375*8975f5c5SAndroid Build Coastguard Worker        if (mips_float_abi == "hard" && mips_fpu_mode != "") {
1376*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-m$mips_fpu_mode" ]
1377*8975f5c5SAndroid Build Coastguard Worker        }
1378*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "r1") {
1379*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1380*8975f5c5SAndroid Build Coastguard Worker          "-mips32",
1381*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-mips32",
1382*8975f5c5SAndroid Build Coastguard Worker        ]
1383*8975f5c5SAndroid Build Coastguard Worker      }
1384*8975f5c5SAndroid Build Coastguard Worker
1385*8975f5c5SAndroid Build Coastguard Worker      if (mips_dsp_rev == 1) {
1386*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-mdsp" ]
1387*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_dsp_rev == 2) {
1388*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-mdspr2" ]
1389*8975f5c5SAndroid Build Coastguard Worker      }
1390*8975f5c5SAndroid Build Coastguard Worker
1391*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-m${mips_float_abi}-float" ]
1392*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "mips64el") {
1393*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-D__SANE_USERSPACE_TYPES__" ]
1394*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--hash-style=sysv" ]
1395*8975f5c5SAndroid Build Coastguard Worker      if (custom_toolchain == "") {
1396*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1397*8975f5c5SAndroid Build Coastguard Worker          if (is_android) {
1398*8975f5c5SAndroid Build Coastguard Worker            cflags += [ "--target=mips64el-linux-android" ]
1399*8975f5c5SAndroid Build Coastguard Worker            ldflags += [ "--target=mips64el-linux-android" ]
1400*8975f5c5SAndroid Build Coastguard Worker          } else {
1401*8975f5c5SAndroid Build Coastguard Worker            cflags += [ "--target=mips64el-linux-gnuabi64" ]
1402*8975f5c5SAndroid Build Coastguard Worker            ldflags += [ "--target=mips64el-linux-gnuabi64" ]
1403*8975f5c5SAndroid Build Coastguard Worker          }
1404*8975f5c5SAndroid Build Coastguard Worker        } else {
1405*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1406*8975f5c5SAndroid Build Coastguard Worker            "-EL",
1407*8975f5c5SAndroid Build Coastguard Worker            "-mabi=64",
1408*8975f5c5SAndroid Build Coastguard Worker          ]
1409*8975f5c5SAndroid Build Coastguard Worker          ldflags += [
1410*8975f5c5SAndroid Build Coastguard Worker            "-EL",
1411*8975f5c5SAndroid Build Coastguard Worker            "-mabi=64",
1412*8975f5c5SAndroid Build Coastguard Worker          ]
1413*8975f5c5SAndroid Build Coastguard Worker        }
1414*8975f5c5SAndroid Build Coastguard Worker      }
1415*8975f5c5SAndroid Build Coastguard Worker
1416*8975f5c5SAndroid Build Coastguard Worker      if (mips_arch_variant == "r6") {
1417*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1418*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1419*8975f5c5SAndroid Build Coastguard Worker            "-march=mips64el",
1420*8975f5c5SAndroid Build Coastguard Worker            "-mcpu=mips64r6",
1421*8975f5c5SAndroid Build Coastguard Worker          ]
1422*8975f5c5SAndroid Build Coastguard Worker        } else {
1423*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1424*8975f5c5SAndroid Build Coastguard Worker            "-mips64r6",
1425*8975f5c5SAndroid Build Coastguard Worker            "-Wa,-mips64r6",
1426*8975f5c5SAndroid Build Coastguard Worker          ]
1427*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "-mips64r6" ]
1428*8975f5c5SAndroid Build Coastguard Worker        }
1429*8975f5c5SAndroid Build Coastguard Worker        if (mips_use_msa == true) {
1430*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1431*8975f5c5SAndroid Build Coastguard Worker            "-mmsa",
1432*8975f5c5SAndroid Build Coastguard Worker            "-mfp64",
1433*8975f5c5SAndroid Build Coastguard Worker          ]
1434*8975f5c5SAndroid Build Coastguard Worker        }
1435*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "r2") {
1436*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mips64r2" ]
1437*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1438*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1439*8975f5c5SAndroid Build Coastguard Worker            "-march=mips64el",
1440*8975f5c5SAndroid Build Coastguard Worker            "-mcpu=mips64r2",
1441*8975f5c5SAndroid Build Coastguard Worker          ]
1442*8975f5c5SAndroid Build Coastguard Worker        } else {
1443*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1444*8975f5c5SAndroid Build Coastguard Worker            "-mips64r2",
1445*8975f5c5SAndroid Build Coastguard Worker            "-Wa,-mips64r2",
1446*8975f5c5SAndroid Build Coastguard Worker          ]
1447*8975f5c5SAndroid Build Coastguard Worker        }
1448*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "loongson3") {
1449*8975f5c5SAndroid Build Coastguard Worker        defines += [ "_MIPS_ARCH_LOONGSON" ]
1450*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1451*8975f5c5SAndroid Build Coastguard Worker          "-march=loongson3a",
1452*8975f5c5SAndroid Build Coastguard Worker          "-mno-branch-likely",
1453*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-march=loongson3a",
1454*8975f5c5SAndroid Build Coastguard Worker        ]
1455*8975f5c5SAndroid Build Coastguard Worker      }
1456*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "mips64") {
1457*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--hash-style=sysv" ]
1458*8975f5c5SAndroid Build Coastguard Worker      if (custom_toolchain == "") {
1459*8975f5c5SAndroid Build Coastguard Worker        if (is_clang) {
1460*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "--target=mips64-linux-gnuabi64" ]
1461*8975f5c5SAndroid Build Coastguard Worker          ldflags += [ "--target=mips64-linux-gnuabi64" ]
1462*8975f5c5SAndroid Build Coastguard Worker        } else {
1463*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1464*8975f5c5SAndroid Build Coastguard Worker            "-EB",
1465*8975f5c5SAndroid Build Coastguard Worker            "-mabi=64",
1466*8975f5c5SAndroid Build Coastguard Worker          ]
1467*8975f5c5SAndroid Build Coastguard Worker          ldflags += [
1468*8975f5c5SAndroid Build Coastguard Worker            "-EB",
1469*8975f5c5SAndroid Build Coastguard Worker            "-mabi=64",
1470*8975f5c5SAndroid Build Coastguard Worker          ]
1471*8975f5c5SAndroid Build Coastguard Worker        }
1472*8975f5c5SAndroid Build Coastguard Worker      }
1473*8975f5c5SAndroid Build Coastguard Worker
1474*8975f5c5SAndroid Build Coastguard Worker      if (mips_arch_variant == "r6") {
1475*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1476*8975f5c5SAndroid Build Coastguard Worker          "-mips64r6",
1477*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-mips64r6",
1478*8975f5c5SAndroid Build Coastguard Worker        ]
1479*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mips64r6" ]
1480*8975f5c5SAndroid Build Coastguard Worker
1481*8975f5c5SAndroid Build Coastguard Worker        if (mips_use_msa == true) {
1482*8975f5c5SAndroid Build Coastguard Worker          cflags += [
1483*8975f5c5SAndroid Build Coastguard Worker            "-mmsa",
1484*8975f5c5SAndroid Build Coastguard Worker            "-mfp64",
1485*8975f5c5SAndroid Build Coastguard Worker          ]
1486*8975f5c5SAndroid Build Coastguard Worker        }
1487*8975f5c5SAndroid Build Coastguard Worker      } else if (mips_arch_variant == "r2") {
1488*8975f5c5SAndroid Build Coastguard Worker        cflags += [
1489*8975f5c5SAndroid Build Coastguard Worker          "-mips64r2",
1490*8975f5c5SAndroid Build Coastguard Worker          "-Wa,-mips64r2",
1491*8975f5c5SAndroid Build Coastguard Worker        ]
1492*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-mips64r2" ]
1493*8975f5c5SAndroid Build Coastguard Worker      }
1494*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "ppc64") {
1495*8975f5c5SAndroid Build Coastguard Worker      if (current_os == "aix") {
1496*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-maix64" ]
1497*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-maix64" ]
1498*8975f5c5SAndroid Build Coastguard Worker      } else {
1499*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-m64" ]
1500*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "-m64" ]
1501*8975f5c5SAndroid Build Coastguard Worker      }
1502*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "riscv64") {
1503*8975f5c5SAndroid Build Coastguard Worker      if (is_clang && !is_android) {
1504*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "--target=riscv64-linux-gnu" ]
1505*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "--target=riscv64-linux-gnu" ]
1506*8975f5c5SAndroid Build Coastguard Worker      }
1507*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-mabi=lp64d" ]
1508*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "loong64") {
1509*8975f5c5SAndroid Build Coastguard Worker      if (is_clang) {
1510*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "--target=loongarch64-linux-gnu" ]
1511*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "--target=loongarch64-linux-gnu" ]
1512*8975f5c5SAndroid Build Coastguard Worker      }
1513*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1514*8975f5c5SAndroid Build Coastguard Worker        "-mabi=lp64d",
1515*8975f5c5SAndroid Build Coastguard Worker        "-mcmodel=medium",
1516*8975f5c5SAndroid Build Coastguard Worker      ]
1517*8975f5c5SAndroid Build Coastguard Worker    } else if (current_cpu == "s390x") {
1518*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-m64" ]
1519*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-m64" ]
1520*8975f5c5SAndroid Build Coastguard Worker    }
1521*8975f5c5SAndroid Build Coastguard Worker  }
1522*8975f5c5SAndroid Build Coastguard Worker
1523*8975f5c5SAndroid Build Coastguard Worker  asmflags = cflags
1524*8975f5c5SAndroid Build Coastguard Worker}
1525*8975f5c5SAndroid Build Coastguard Worker
1526*8975f5c5SAndroid Build Coastguard Worker# This provides options to tweak code generation that are necessary
1527*8975f5c5SAndroid Build Coastguard Worker# for particular Chromium code or for working around particular
1528*8975f5c5SAndroid Build Coastguard Worker# compiler bugs (or the combination of the two).
1529*8975f5c5SAndroid Build Coastguard Workerconfig("compiler_codegen") {
1530*8975f5c5SAndroid Build Coastguard Worker  configs = []
1531*8975f5c5SAndroid Build Coastguard Worker  cflags = []
1532*8975f5c5SAndroid Build Coastguard Worker  ldflags = []
1533*8975f5c5SAndroid Build Coastguard Worker
1534*8975f5c5SAndroid Build Coastguard Worker  if (is_nacl) {
1535*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/nacl:compiler_codegen" ]
1536*8975f5c5SAndroid Build Coastguard Worker  }
1537*8975f5c5SAndroid Build Coastguard Worker
1538*8975f5c5SAndroid Build Coastguard Worker  if (current_cpu == "arm64" && !is_win && is_clang) {
1539*8975f5c5SAndroid Build Coastguard Worker    # Disable outlining everywhere on arm64 except Win. For more information see
1540*8975f5c5SAndroid Build Coastguard Worker    # crbug.com/931297 for Android and crbug.com/1410297 for iOS.
1541*8975f5c5SAndroid Build Coastguard Worker    # TODO(crbug.com/40890229): Enable this on Windows if possible.
1542*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-mno-outline" ]
1543*8975f5c5SAndroid Build Coastguard Worker
1544*8975f5c5SAndroid Build Coastguard Worker    # This can be removed once https://bugs.llvm.org/show_bug.cgi?id=40348
1545*8975f5c5SAndroid Build Coastguard Worker    # has been resolved, and -mno-outline is obeyed by the linker during
1546*8975f5c5SAndroid Build Coastguard Worker    # ThinLTO.
1547*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,-mllvm,-enable-machine-outliner=never" ]
1548*8975f5c5SAndroid Build Coastguard Worker  }
1549*8975f5c5SAndroid Build Coastguard Worker
1550*8975f5c5SAndroid Build Coastguard Worker  asmflags = cflags
1551*8975f5c5SAndroid Build Coastguard Worker}
1552*8975f5c5SAndroid Build Coastguard Worker
1553*8975f5c5SAndroid Build Coastguard Worker# This provides options that make the build deterministic, so that the same
1554*8975f5c5SAndroid Build Coastguard Worker# revision produces the same output, independent of the name of the build
1555*8975f5c5SAndroid Build Coastguard Worker# directory and of the computer the build is done on.
1556*8975f5c5SAndroid Build Coastguard Worker# The relative path from build dir to source dir makes it into the build
1557*8975f5c5SAndroid Build Coastguard Worker# outputs, so it's recommended that you use a build dir two levels deep
1558*8975f5c5SAndroid Build Coastguard Worker# (e.g. "out/Release") so that you get the same "../.." path as all the bots
1559*8975f5c5SAndroid Build Coastguard Worker# in your build outputs.
1560*8975f5c5SAndroid Build Coastguard Workerconfig("compiler_deterministic") {
1561*8975f5c5SAndroid Build Coastguard Worker  cflags = []
1562*8975f5c5SAndroid Build Coastguard Worker  ldflags = []
1563*8975f5c5SAndroid Build Coastguard Worker  swiftflags = []
1564*8975f5c5SAndroid Build Coastguard Worker
1565*8975f5c5SAndroid Build Coastguard Worker  # Eliminate build metadata (__DATE__, __TIME__ and __TIMESTAMP__) for
1566*8975f5c5SAndroid Build Coastguard Worker  # deterministic build.  See https://crbug.com/314403
1567*8975f5c5SAndroid Build Coastguard Worker  if (!is_official_build) {
1568*8975f5c5SAndroid Build Coastguard Worker    if (is_win && !is_clang) {
1569*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1570*8975f5c5SAndroid Build Coastguard Worker        "/wd4117",  # Trying to define or undefine a predefined macro.
1571*8975f5c5SAndroid Build Coastguard Worker        "/D__DATE__=",
1572*8975f5c5SAndroid Build Coastguard Worker        "/D__TIME__=",
1573*8975f5c5SAndroid Build Coastguard Worker        "/D__TIMESTAMP__=",
1574*8975f5c5SAndroid Build Coastguard Worker      ]
1575*8975f5c5SAndroid Build Coastguard Worker    } else {
1576*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1577*8975f5c5SAndroid Build Coastguard Worker        "-Wno-builtin-macro-redefined",
1578*8975f5c5SAndroid Build Coastguard Worker        "-D__DATE__=",
1579*8975f5c5SAndroid Build Coastguard Worker        "-D__TIME__=",
1580*8975f5c5SAndroid Build Coastguard Worker        "-D__TIMESTAMP__=",
1581*8975f5c5SAndroid Build Coastguard Worker      ]
1582*8975f5c5SAndroid Build Coastguard Worker    }
1583*8975f5c5SAndroid Build Coastguard Worker  }
1584*8975f5c5SAndroid Build Coastguard Worker
1585*8975f5c5SAndroid Build Coastguard Worker  # Makes builds independent of absolute file path.
1586*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && strip_absolute_paths_from_debug_symbols) {
1587*8975f5c5SAndroid Build Coastguard Worker    # If debug option is given, clang includes $cwd in debug info by default.
1588*8975f5c5SAndroid Build Coastguard Worker    # For such build, this flag generates reproducible obj files even we use
1589*8975f5c5SAndroid Build Coastguard Worker    # different build directory like "out/feature_a" and "out/feature_b" if
1590*8975f5c5SAndroid Build Coastguard Worker    # we build same files with same compile flag.
1591*8975f5c5SAndroid Build Coastguard Worker    # Other paths are already given in relative, no need to normalize them.
1592*8975f5c5SAndroid Build Coastguard Worker    if (is_nacl) {
1593*8975f5c5SAndroid Build Coastguard Worker      # TODO(https://crbug.com/1231236): Use -ffile-compilation-dir= here.
1594*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1595*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
1596*8975f5c5SAndroid Build Coastguard Worker        "-fdebug-compilation-dir",
1597*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
1598*8975f5c5SAndroid Build Coastguard Worker        ".",
1599*8975f5c5SAndroid Build Coastguard Worker      ]
1600*8975f5c5SAndroid Build Coastguard Worker    } else {
1601*8975f5c5SAndroid Build Coastguard Worker      # -ffile-compilation-dir is an alias for both -fdebug-compilation-dir=
1602*8975f5c5SAndroid Build Coastguard Worker      # and -fcoverage-compilation-dir=.
1603*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-ffile-compilation-dir=." ]
1604*8975f5c5SAndroid Build Coastguard Worker
1605*8975f5c5SAndroid Build Coastguard Worker      # Convert absolute paths to relative paths. Expands to, for example:
1606*8975f5c5SAndroid Build Coastguard Worker      #   -file-prefix-map /path/to/chromium/src=../..
1607*8975f5c5SAndroid Build Coastguard Worker      swiftflags += [
1608*8975f5c5SAndroid Build Coastguard Worker        "-file-prefix-map",
1609*8975f5c5SAndroid Build Coastguard Worker        rebase_path("//.", "") + "=" + rebase_path("//.", root_build_dir),
1610*8975f5c5SAndroid Build Coastguard Worker      ]
1611*8975f5c5SAndroid Build Coastguard Worker    }
1612*8975f5c5SAndroid Build Coastguard Worker    if (!is_win) {
1613*8975f5c5SAndroid Build Coastguard Worker      # We don't use clang -cc1as on Windows (yet? https://crbug.com/762167)
1614*8975f5c5SAndroid Build Coastguard Worker      asmflags = [ "-Wa,-fdebug-compilation-dir,." ]
1615*8975f5c5SAndroid Build Coastguard Worker    }
1616*8975f5c5SAndroid Build Coastguard Worker
1617*8975f5c5SAndroid Build Coastguard Worker    if (is_win && use_lld) {
1618*8975f5c5SAndroid Build Coastguard Worker      if (symbol_level == 2 || (is_clang && using_sanitizer)) {
1619*8975f5c5SAndroid Build Coastguard Worker        # Absolutize source file paths for PDB. Pass the real build directory
1620*8975f5c5SAndroid Build Coastguard Worker        # if the pdb contains source-level debug information and if linker
1621*8975f5c5SAndroid Build Coastguard Worker        # reproducibility is not critical.
1622*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "/PDBSourcePath:" + rebase_path(root_build_dir) ]
1623*8975f5c5SAndroid Build Coastguard Worker      } else {
1624*8975f5c5SAndroid Build Coastguard Worker        # Use a fake fixed base directory for paths in the pdb to make the pdb
1625*8975f5c5SAndroid Build Coastguard Worker        # output fully deterministic and independent of the build directory.
1626*8975f5c5SAndroid Build Coastguard Worker        ldflags += [ "/PDBSourcePath:o:\fake\prefix" ]
1627*8975f5c5SAndroid Build Coastguard Worker      }
1628*8975f5c5SAndroid Build Coastguard Worker    }
1629*8975f5c5SAndroid Build Coastguard Worker  }
1630*8975f5c5SAndroid Build Coastguard Worker
1631*8975f5c5SAndroid Build Coastguard Worker  # Tells the compiler not to use absolute paths when passing the default
1632*8975f5c5SAndroid Build Coastguard Worker  # paths to the tools it invokes. We don't want this because we don't
1633*8975f5c5SAndroid Build Coastguard Worker  # really need it and it can mess up the RBE cache entries.
1634*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && (!is_nacl || is_nacl_saigo)) {
1635*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-no-canonical-prefixes" ]
1636*8975f5c5SAndroid Build Coastguard Worker
1637*8975f5c5SAndroid Build Coastguard Worker    # Same for links: Let the compiler driver invoke the linker
1638*8975f5c5SAndroid Build Coastguard Worker    # with a relative path and pass relative paths to built-in
1639*8975f5c5SAndroid Build Coastguard Worker    # libraries. Not needed on Windows because we call the linker
1640*8975f5c5SAndroid Build Coastguard Worker    # directly there, not through the compiler driver.
1641*8975f5c5SAndroid Build Coastguard Worker    # We don't link on RBE, so this change is just for cleaner
1642*8975f5c5SAndroid Build Coastguard Worker    # internal linker invocations, for people who work on the build.
1643*8975f5c5SAndroid Build Coastguard Worker    if (!is_win) {
1644*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-no-canonical-prefixes" ]
1645*8975f5c5SAndroid Build Coastguard Worker    }
1646*8975f5c5SAndroid Build Coastguard Worker  }
1647*8975f5c5SAndroid Build Coastguard Worker}
1648*8975f5c5SAndroid Build Coastguard Worker
1649*8975f5c5SAndroid Build Coastguard Workerconfig("clang_revision") {
1650*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && clang_base_path == default_clang_base_path &&
1651*8975f5c5SAndroid Build Coastguard Worker      current_os != "zos") {
1652*8975f5c5SAndroid Build Coastguard Worker    update_args = [
1653*8975f5c5SAndroid Build Coastguard Worker      "--print-revision",
1654*8975f5c5SAndroid Build Coastguard Worker      "--verify-version=$clang_version",
1655*8975f5c5SAndroid Build Coastguard Worker    ]
1656*8975f5c5SAndroid Build Coastguard Worker    if (llvm_force_head_revision) {
1657*8975f5c5SAndroid Build Coastguard Worker      update_args += [ "--llvm-force-head-revision" ]
1658*8975f5c5SAndroid Build Coastguard Worker    }
1659*8975f5c5SAndroid Build Coastguard Worker    clang_revision = exec_script("//tools/clang/scripts/update.py",
1660*8975f5c5SAndroid Build Coastguard Worker                                 update_args,
1661*8975f5c5SAndroid Build Coastguard Worker                                 "trim string")
1662*8975f5c5SAndroid Build Coastguard Worker
1663*8975f5c5SAndroid Build Coastguard Worker    # This is here so that all files get recompiled after a clang roll and
1664*8975f5c5SAndroid Build Coastguard Worker    # when turning clang on or off. (defines are passed via the command line,
1665*8975f5c5SAndroid Build Coastguard Worker    # and build system rebuild things when their commandline changes). Nothing
1666*8975f5c5SAndroid Build Coastguard Worker    # should ever read this define.
1667*8975f5c5SAndroid Build Coastguard Worker    defines = [ "CR_CLANG_REVISION=\"$clang_revision\"" ]
1668*8975f5c5SAndroid Build Coastguard Worker  }
1669*8975f5c5SAndroid Build Coastguard Worker}
1670*8975f5c5SAndroid Build Coastguard Worker
1671*8975f5c5SAndroid Build Coastguard Workerconfig("rustc_revision") {
1672*8975f5c5SAndroid Build Coastguard Worker  if (rustc_revision != "") {
1673*8975f5c5SAndroid Build Coastguard Worker    # Similar to the above config, this is here so that all files get recompiled
1674*8975f5c5SAndroid Build Coastguard Worker    # after a rustc roll. Nothing should ever read this cfg. This will not be
1675*8975f5c5SAndroid Build Coastguard Worker    # set if a custom toolchain is used.
1676*8975f5c5SAndroid Build Coastguard Worker    rustflags = [
1677*8975f5c5SAndroid Build Coastguard Worker      "--cfg",
1678*8975f5c5SAndroid Build Coastguard Worker      "cr_rustc_revision=\"$rustc_revision\"",
1679*8975f5c5SAndroid Build Coastguard Worker    ]
1680*8975f5c5SAndroid Build Coastguard Worker  }
1681*8975f5c5SAndroid Build Coastguard Worker}
1682*8975f5c5SAndroid Build Coastguard Worker
1683*8975f5c5SAndroid Build Coastguard Workerconfig("compiler_arm_fpu") {
1684*8975f5c5SAndroid Build Coastguard Worker  if (current_cpu == "arm" && !is_ios && !is_nacl) {
1685*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-mfpu=$arm_fpu" ]
1686*8975f5c5SAndroid Build Coastguard Worker    if (!arm_use_thumb) {
1687*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-marm" ]
1688*8975f5c5SAndroid Build Coastguard Worker    }
1689*8975f5c5SAndroid Build Coastguard Worker    asmflags = cflags
1690*8975f5c5SAndroid Build Coastguard Worker  }
1691*8975f5c5SAndroid Build Coastguard Worker}
1692*8975f5c5SAndroid Build Coastguard Worker
1693*8975f5c5SAndroid Build Coastguard Workerconfig("compiler_arm_thumb") {
1694*8975f5c5SAndroid Build Coastguard Worker  if (current_cpu == "arm" && arm_use_thumb && is_posix &&
1695*8975f5c5SAndroid Build Coastguard Worker      !(is_apple || is_nacl)) {
1696*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-mthumb" ]
1697*8975f5c5SAndroid Build Coastguard Worker  }
1698*8975f5c5SAndroid Build Coastguard Worker}
1699*8975f5c5SAndroid Build Coastguard Worker
1700*8975f5c5SAndroid Build Coastguard Workerconfig("compiler_arm") {
1701*8975f5c5SAndroid Build Coastguard Worker  if (current_cpu == "arm" && is_chromeos) {
1702*8975f5c5SAndroid Build Coastguard Worker    # arm is normally the default mode for clang, but on chromeos a wrapper
1703*8975f5c5SAndroid Build Coastguard Worker    # is used to pass -mthumb, and therefor change the default.
1704*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-marm" ]
1705*8975f5c5SAndroid Build Coastguard Worker  }
1706*8975f5c5SAndroid Build Coastguard Worker}
1707*8975f5c5SAndroid Build Coastguard Worker
1708*8975f5c5SAndroid Build Coastguard Workerconfig("libcxx_module") {
1709*8975f5c5SAndroid Build Coastguard Worker  if (use_libcxx_modules) {
1710*8975f5c5SAndroid Build Coastguard Worker    modulemap = rebase_path("//third_party/libc++/src/include/module.modulemap",
1711*8975f5c5SAndroid Build Coastguard Worker                            root_build_dir)
1712*8975f5c5SAndroid Build Coastguard Worker    cflags_cc = [
1713*8975f5c5SAndroid Build Coastguard Worker      "-fmodules",
1714*8975f5c5SAndroid Build Coastguard Worker      "-fmodule-map-file=" + modulemap,
1715*8975f5c5SAndroid Build Coastguard Worker      "-fno-implicit-module-maps",
1716*8975f5c5SAndroid Build Coastguard Worker      "-fbuiltin-module-map",
1717*8975f5c5SAndroid Build Coastguard Worker      "-fmodules-cache-path=" +
1718*8975f5c5SAndroid Build Coastguard Worker          rebase_path("$libcxx_module_prefix/module_cache", root_build_dir),
1719*8975f5c5SAndroid Build Coastguard Worker
1720*8975f5c5SAndroid Build Coastguard Worker      "-Xclang",
1721*8975f5c5SAndroid Build Coastguard Worker      "-fmodules-local-submodule-visibility",  # required for builtins
1722*8975f5c5SAndroid Build Coastguard Worker
1723*8975f5c5SAndroid Build Coastguard Worker      # TODO(crbug.com/1456385): Figure out if this can be removed.
1724*8975f5c5SAndroid Build Coastguard Worker      "-Wno-modules-ambiguous-internal-linkage",
1725*8975f5c5SAndroid Build Coastguard Worker
1726*8975f5c5SAndroid Build Coastguard Worker      # TODO(crbug.com/40440396): Clean up.
1727*8975f5c5SAndroid Build Coastguard Worker      "-Wno-modules-import-nested-redundant",
1728*8975f5c5SAndroid Build Coastguard Worker
1729*8975f5c5SAndroid Build Coastguard Worker      # Needed to allow using builtin modules when the headers are
1730*8975f5c5SAndroid Build Coastguard Worker      # unnecessarily using extern "C".
1731*8975f5c5SAndroid Build Coastguard Worker      "-Wno-module-import-in-extern-c",
1732*8975f5c5SAndroid Build Coastguard Worker    ]
1733*8975f5c5SAndroid Build Coastguard Worker  }
1734*8975f5c5SAndroid Build Coastguard Worker}
1735*8975f5c5SAndroid Build Coastguard Worker
1736*8975f5c5SAndroid Build Coastguard Worker# runtime_library -------------------------------------------------------------
1737*8975f5c5SAndroid Build Coastguard Worker#
1738*8975f5c5SAndroid Build Coastguard Worker# Sets the runtime library and associated options.
1739*8975f5c5SAndroid Build Coastguard Worker#
1740*8975f5c5SAndroid Build Coastguard Worker# How do you determine what should go in here vs. "compiler" above? Consider if
1741*8975f5c5SAndroid Build Coastguard Worker# a target might choose to use a different runtime library (ignore for a moment
1742*8975f5c5SAndroid Build Coastguard Worker# if this is possible or reasonable on your system). If such a target would want
1743*8975f5c5SAndroid Build Coastguard Worker# to change or remove your option, put it in the runtime_library config. If a
1744*8975f5c5SAndroid Build Coastguard Worker# target wants the option regardless, put it in the compiler config.
1745*8975f5c5SAndroid Build Coastguard Worker
1746*8975f5c5SAndroid Build Coastguard Workerconfig("runtime_library") {
1747*8975f5c5SAndroid Build Coastguard Worker  configs = []
1748*8975f5c5SAndroid Build Coastguard Worker
1749*8975f5c5SAndroid Build Coastguard Worker  # The order of this config is important: it must appear before
1750*8975f5c5SAndroid Build Coastguard Worker  # android:runtime_library.  This is to ensure libc++ appears before
1751*8975f5c5SAndroid Build Coastguard Worker  # libandroid_support in the -isystem include order.  Otherwise, there will be
1752*8975f5c5SAndroid Build Coastguard Worker  # build errors related to symbols declared in math.h.
1753*8975f5c5SAndroid Build Coastguard Worker  if (use_custom_libcxx) {
1754*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/c++:runtime_library" ]
1755*8975f5c5SAndroid Build Coastguard Worker  }
1756*8975f5c5SAndroid Build Coastguard Worker
1757*8975f5c5SAndroid Build Coastguard Worker  # Rust and C++ both provide intrinsics for LLVM to call for math operations. We
1758*8975f5c5SAndroid Build Coastguard Worker  # want to use the C++ intrinsics, not the ones in the Rust compiler_builtins
1759*8975f5c5SAndroid Build Coastguard Worker  # library. The Rust symbols are marked as weak, so that they can be replaced by
1760*8975f5c5SAndroid Build Coastguard Worker  # the C++ symbols. This config ensures the C++ symbols exist and are strong in
1761*8975f5c5SAndroid Build Coastguard Worker  # order to cause that replacement to occur by explicitly linking in clang's
1762*8975f5c5SAndroid Build Coastguard Worker  # compiler-rt library.
1763*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && !is_nacl && !is_cronet_build) {
1764*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/clang:compiler_builtins" ]
1765*8975f5c5SAndroid Build Coastguard Worker  }
1766*8975f5c5SAndroid Build Coastguard Worker
1767*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/40570904): Come up with a better name for is POSIX + Fuchsia
1768*8975f5c5SAndroid Build Coastguard Worker  # configuration.
1769*8975f5c5SAndroid Build Coastguard Worker  if (is_posix || is_fuchsia) {
1770*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/posix:runtime_library" ]
1771*8975f5c5SAndroid Build Coastguard Worker
1772*8975f5c5SAndroid Build Coastguard Worker    if (use_custom_libunwind) {
1773*8975f5c5SAndroid Build Coastguard Worker      # Instead of using an unwind lib from the toolchain,
1774*8975f5c5SAndroid Build Coastguard Worker      # buildtools/third_party/libunwind will be built and used directly.
1775*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "--unwindlib=none" ]
1776*8975f5c5SAndroid Build Coastguard Worker    }
1777*8975f5c5SAndroid Build Coastguard Worker  }
1778*8975f5c5SAndroid Build Coastguard Worker
1779*8975f5c5SAndroid Build Coastguard Worker  # System-specific flags. If your compiler flags apply to one of the
1780*8975f5c5SAndroid Build Coastguard Worker  # categories here, add it to the associated file to keep this shared config
1781*8975f5c5SAndroid Build Coastguard Worker  # smaller.
1782*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
1783*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/win:runtime_library" ]
1784*8975f5c5SAndroid Build Coastguard Worker  } else if (is_linux || is_chromeos) {
1785*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/linux:runtime_library" ]
1786*8975f5c5SAndroid Build Coastguard Worker    if (is_chromeos) {
1787*8975f5c5SAndroid Build Coastguard Worker      configs += [ "//build/config/chromeos:runtime_library" ]
1788*8975f5c5SAndroid Build Coastguard Worker    }
1789*8975f5c5SAndroid Build Coastguard Worker  } else if (is_ios) {
1790*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/ios:runtime_library" ]
1791*8975f5c5SAndroid Build Coastguard Worker  } else if (is_mac) {
1792*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/mac:runtime_library" ]
1793*8975f5c5SAndroid Build Coastguard Worker  } else if (is_android) {
1794*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config/android:runtime_library" ]
1795*8975f5c5SAndroid Build Coastguard Worker  }
1796*8975f5c5SAndroid Build Coastguard Worker
1797*8975f5c5SAndroid Build Coastguard Worker  if (is_component_build) {
1798*8975f5c5SAndroid Build Coastguard Worker    defines = [ "COMPONENT_BUILD" ]
1799*8975f5c5SAndroid Build Coastguard Worker  }
1800*8975f5c5SAndroid Build Coastguard Worker}
1801*8975f5c5SAndroid Build Coastguard Worker
1802*8975f5c5SAndroid Build Coastguard Worker# treat_warnings_as_errors ----------------------------------------------------
1803*8975f5c5SAndroid Build Coastguard Worker#
1804*8975f5c5SAndroid Build Coastguard Worker# Adding this config causes the compiler to treat warnings as fatal errors.
1805*8975f5c5SAndroid Build Coastguard Worker# This is used as a subconfig of both chromium_code and no_chromium_code, and
1806*8975f5c5SAndroid Build Coastguard Worker# is broken out separately so nocompile tests can force-enable this setting
1807*8975f5c5SAndroid Build Coastguard Worker# independently of the default warning flags.
1808*8975f5c5SAndroid Build Coastguard Workerconfig("treat_warnings_as_errors") {
1809*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
1810*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "/WX" ]
1811*8975f5c5SAndroid Build Coastguard Worker  } else {
1812*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-Werror" ]
1813*8975f5c5SAndroid Build Coastguard Worker
1814*8975f5c5SAndroid Build Coastguard Worker    # The compiler driver can sometimes (rarely) emit warnings before calling
1815*8975f5c5SAndroid Build Coastguard Worker    # the actual linker.  Make sure these warnings are treated as errors as
1816*8975f5c5SAndroid Build Coastguard Worker    # well.
1817*8975f5c5SAndroid Build Coastguard Worker    ldflags = [ "-Werror" ]
1818*8975f5c5SAndroid Build Coastguard Worker  }
1819*8975f5c5SAndroid Build Coastguard Worker
1820*8975f5c5SAndroid Build Coastguard Worker  # Turn rustc warnings into the "deny" lint level, which produce compiler
1821*8975f5c5SAndroid Build Coastguard Worker  # errors. The equivalent of -Werror for clang/gcc.
1822*8975f5c5SAndroid Build Coastguard Worker  #
1823*8975f5c5SAndroid Build Coastguard Worker  # Note we apply the actual lint flags in config("compiler"). All warnings
1824*8975f5c5SAndroid Build Coastguard Worker  # are suppressed in third-party crates.
1825*8975f5c5SAndroid Build Coastguard Worker  rustflags = [ "-Dwarnings" ]
1826*8975f5c5SAndroid Build Coastguard Worker
1827*8975f5c5SAndroid Build Coastguard Worker  # TODO(https://crbug.com/326247202): Fix unused imports and remove this flag.
1828*8975f5c5SAndroid Build Coastguard Worker  rustflags += [ "-Aunused-imports" ]
1829*8975f5c5SAndroid Build Coastguard Worker}
1830*8975f5c5SAndroid Build Coastguard Worker
1831*8975f5c5SAndroid Build Coastguard Worker# default_warnings ------------------------------------------------------------
1832*8975f5c5SAndroid Build Coastguard Worker#
1833*8975f5c5SAndroid Build Coastguard Worker# Collects all warning flags that are used by default.  This is used as a
1834*8975f5c5SAndroid Build Coastguard Worker# subconfig of both chromium_code and no_chromium_code.  This way these
1835*8975f5c5SAndroid Build Coastguard Worker# flags are guaranteed to appear on the compile command line after -Wall.
1836*8975f5c5SAndroid Build Coastguard Workerconfig("default_warnings") {
1837*8975f5c5SAndroid Build Coastguard Worker  cflags = []
1838*8975f5c5SAndroid Build Coastguard Worker  cflags_c = []
1839*8975f5c5SAndroid Build Coastguard Worker  cflags_cc = []
1840*8975f5c5SAndroid Build Coastguard Worker  ldflags = []
1841*8975f5c5SAndroid Build Coastguard Worker  configs = []
1842*8975f5c5SAndroid Build Coastguard Worker
1843*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
1844*8975f5c5SAndroid Build Coastguard Worker    if (fatal_linker_warnings) {
1845*8975f5c5SAndroid Build Coastguard Worker      arflags = [ "/WX" ]
1846*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/WX" ]
1847*8975f5c5SAndroid Build Coastguard Worker    }
1848*8975f5c5SAndroid Build Coastguard Worker    defines = [
1849*8975f5c5SAndroid Build Coastguard Worker      # Without this, Windows headers warn that functions like wcsnicmp
1850*8975f5c5SAndroid Build Coastguard Worker      # should be spelled _wcsnicmp. But all other platforms keep spelling
1851*8975f5c5SAndroid Build Coastguard Worker      # it wcsnicmp, making this warning unhelpful. We don't want it.
1852*8975f5c5SAndroid Build Coastguard Worker      "_CRT_NONSTDC_NO_WARNINGS",
1853*8975f5c5SAndroid Build Coastguard Worker
1854*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): winsock wants us to use getaddrinfo instead of
1855*8975f5c5SAndroid Build Coastguard Worker      # gethostbyname. Fires mostly in non-Chromium code. We probably
1856*8975f5c5SAndroid Build Coastguard Worker      # want to remove this define eventually.
1857*8975f5c5SAndroid Build Coastguard Worker      "_WINSOCK_DEPRECATED_NO_WARNINGS",
1858*8975f5c5SAndroid Build Coastguard Worker    ]
1859*8975f5c5SAndroid Build Coastguard Worker    if (!is_clang) {
1860*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): Remove this once
1861*8975f5c5SAndroid Build Coastguard Worker      # https://swiftshader-review.googlesource.com/c/SwiftShader/+/57968 has
1862*8975f5c5SAndroid Build Coastguard Worker      # rolled into angle.
1863*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "/wd4244" ]
1864*8975f5c5SAndroid Build Coastguard Worker    }
1865*8975f5c5SAndroid Build Coastguard Worker  } else {
1866*8975f5c5SAndroid Build Coastguard Worker    if ((is_apple || is_android) && !is_nacl) {
1867*8975f5c5SAndroid Build Coastguard Worker      # Warns if a method is used whose availability is newer than the
1868*8975f5c5SAndroid Build Coastguard Worker      # deployment target.
1869*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wunguarded-availability" ]
1870*8975f5c5SAndroid Build Coastguard Worker    }
1871*8975f5c5SAndroid Build Coastguard Worker
1872*8975f5c5SAndroid Build Coastguard Worker    if (is_ios) {
1873*8975f5c5SAndroid Build Coastguard Worker      # When compiling Objective-C, warns if a selector named via @selector has
1874*8975f5c5SAndroid Build Coastguard Worker      # not been defined in any visible interface.
1875*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wundeclared-selector" ]
1876*8975f5c5SAndroid Build Coastguard Worker
1877*8975f5c5SAndroid Build Coastguard Worker      # Blink builds use a higher deployment target than non-Blink builds, so
1878*8975f5c5SAndroid Build Coastguard Worker      # suppress deprecation warnings in these builds.
1879*8975f5c5SAndroid Build Coastguard Worker      if (use_blink) {
1880*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-Wno-deprecated-declarations" ]
1881*8975f5c5SAndroid Build Coastguard Worker      }
1882*8975f5c5SAndroid Build Coastguard Worker    }
1883*8975f5c5SAndroid Build Coastguard Worker
1884*8975f5c5SAndroid Build Coastguard Worker    # Suppress warnings about ABI changes on ARM (Clang doesn't give this
1885*8975f5c5SAndroid Build Coastguard Worker    # warning).
1886*8975f5c5SAndroid Build Coastguard Worker    if (current_cpu == "arm" && !is_clang) {
1887*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-psabi" ]
1888*8975f5c5SAndroid Build Coastguard Worker    }
1889*8975f5c5SAndroid Build Coastguard Worker
1890*8975f5c5SAndroid Build Coastguard Worker    if (!is_clang) {
1891*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [
1892*8975f5c5SAndroid Build Coastguard Worker        # See comment for -Wno-c++11-narrowing.
1893*8975f5c5SAndroid Build Coastguard Worker        "-Wno-narrowing",
1894*8975f5c5SAndroid Build Coastguard Worker      ]
1895*8975f5c5SAndroid Build Coastguard Worker
1896*8975f5c5SAndroid Build Coastguard Worker      # -Wno-class-memaccess warns about hash table and vector in blink.
1897*8975f5c5SAndroid Build Coastguard Worker      # But the violation is intentional.
1898*8975f5c5SAndroid Build Coastguard Worker      if (!is_nacl) {
1899*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [ "-Wno-class-memaccess" ]
1900*8975f5c5SAndroid Build Coastguard Worker      }
1901*8975f5c5SAndroid Build Coastguard Worker
1902*8975f5c5SAndroid Build Coastguard Worker      # -Wunused-local-typedefs is broken in gcc,
1903*8975f5c5SAndroid Build Coastguard Worker      # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872
1904*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-unused-local-typedefs" ]
1905*8975f5c5SAndroid Build Coastguard Worker
1906*8975f5c5SAndroid Build Coastguard Worker      # Don't warn about "maybe" uninitialized. Clang doesn't include this
1907*8975f5c5SAndroid Build Coastguard Worker      # in -Wall but gcc does, and it gives false positives.
1908*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-maybe-uninitialized" ]
1909*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-deprecated-declarations" ]
1910*8975f5c5SAndroid Build Coastguard Worker
1911*8975f5c5SAndroid Build Coastguard Worker      # -Wcomment gives too many false positives in the case a
1912*8975f5c5SAndroid Build Coastguard Worker      # backslash ended comment line is followed by a new line of
1913*8975f5c5SAndroid Build Coastguard Worker      # comments
1914*8975f5c5SAndroid Build Coastguard Worker      # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638
1915*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-comments" ]
1916*8975f5c5SAndroid Build Coastguard Worker
1917*8975f5c5SAndroid Build Coastguard Worker      # -Wpacked-not-aligned complains all generated mojom-shared-internal.h
1918*8975f5c5SAndroid Build Coastguard Worker      # files.
1919*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-packed-not-aligned" ]
1920*8975f5c5SAndroid Build Coastguard Worker    }
1921*8975f5c5SAndroid Build Coastguard Worker  }
1922*8975f5c5SAndroid Build Coastguard Worker
1923*8975f5c5SAndroid Build Coastguard Worker  # Common Clang and GCC warning setup.
1924*8975f5c5SAndroid Build Coastguard Worker  if (!is_win || is_clang) {
1925*8975f5c5SAndroid Build Coastguard Worker    cflags += [
1926*8975f5c5SAndroid Build Coastguard Worker      # Disables.
1927*8975f5c5SAndroid Build Coastguard Worker      "-Wno-missing-field-initializers",  # "struct foo f = {0};"
1928*8975f5c5SAndroid Build Coastguard Worker      "-Wno-unused-parameter",  # Unused function parameters.
1929*8975f5c5SAndroid Build Coastguard Worker    ]
1930*8975f5c5SAndroid Build Coastguard Worker
1931*8975f5c5SAndroid Build Coastguard Worker    cflags_cc += [
1932*8975f5c5SAndroid Build Coastguard Worker      # Disables for C++ only
1933*8975f5c5SAndroid Build Coastguard Worker      "-Wno-invalid-offsetof",  # offsetof on non-standard-layout type
1934*8975f5c5SAndroid Build Coastguard Worker                                # (crbug.com/40285259)
1935*8975f5c5SAndroid Build Coastguard Worker    ]
1936*8975f5c5SAndroid Build Coastguard Worker
1937*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl || is_nacl_saigo) {
1938*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1939*8975f5c5SAndroid Build Coastguard Worker        # An ABI compat warning we don't care about, https://crbug.com/1102157
1940*8975f5c5SAndroid Build Coastguard Worker        # TODO(thakis): Push this to the (few) targets that need it,
1941*8975f5c5SAndroid Build Coastguard Worker        # instead of having a global flag.
1942*8975f5c5SAndroid Build Coastguard Worker        "-Wno-psabi",
1943*8975f5c5SAndroid Build Coastguard Worker      ]
1944*8975f5c5SAndroid Build Coastguard Worker    }
1945*8975f5c5SAndroid Build Coastguard Worker  }
1946*8975f5c5SAndroid Build Coastguard Worker
1947*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
1948*8975f5c5SAndroid Build Coastguard Worker    cflags += [
1949*8975f5c5SAndroid Build Coastguard Worker      "-Wloop-analysis",
1950*8975f5c5SAndroid Build Coastguard Worker
1951*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): This used to be implied by -Wno-unused-function,
1952*8975f5c5SAndroid Build Coastguard Worker      # which we no longer use. Check if it makes sense to remove
1953*8975f5c5SAndroid Build Coastguard Worker      # this as well. http://crbug.com/316352
1954*8975f5c5SAndroid Build Coastguard Worker      "-Wno-unneeded-internal-declaration",
1955*8975f5c5SAndroid Build Coastguard Worker    ]
1956*8975f5c5SAndroid Build Coastguard Worker
1957*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl || is_nacl_saigo) {
1958*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
1959*8975f5c5SAndroid Build Coastguard Worker        # TODO(thakis): https://crbug.com/617318
1960*8975f5c5SAndroid Build Coastguard Worker        # Currently RBE can not handle case sensitiveness for windows well.
1961*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-Wno-nonportable-include-path" ]
1962*8975f5c5SAndroid Build Coastguard Worker      }
1963*8975f5c5SAndroid Build Coastguard Worker
1964*8975f5c5SAndroid Build Coastguard Worker      if (is_fuchsia) {
1965*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [
1966*8975f5c5SAndroid Build Coastguard Worker          # TODO(crbug.com/42050603): fix and reenable
1967*8975f5c5SAndroid Build Coastguard Worker          "-Wno-missing-field-initializers",
1968*8975f5c5SAndroid Build Coastguard Worker        ]
1969*8975f5c5SAndroid Build Coastguard Worker      }
1970*8975f5c5SAndroid Build Coastguard Worker
1971*8975f5c5SAndroid Build Coastguard Worker      cflags += [
1972*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/330524456): -Wcast-function-type is under -Wextra now.
1973*8975f5c5SAndroid Build Coastguard Worker        "-Wno-cast-function-type",
1974*8975f5c5SAndroid Build Coastguard Worker
1975*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/40284799): Fix and re-enable.
1976*8975f5c5SAndroid Build Coastguard Worker        "-Wno-thread-safety-reference-return",
1977*8975f5c5SAndroid Build Coastguard Worker      ]
1978*8975f5c5SAndroid Build Coastguard Worker
1979*8975f5c5SAndroid Build Coastguard Worker      cflags_cc += [
1980*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/328490295): Fix and re-enable for C flags.
1981*8975f5c5SAndroid Build Coastguard Worker        "-Wenum-compare-conditional",
1982*8975f5c5SAndroid Build Coastguard Worker      ]
1983*8975f5c5SAndroid Build Coastguard Worker
1984*8975f5c5SAndroid Build Coastguard Worker      if (!is_nacl) {
1985*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [
1986*8975f5c5SAndroid Build Coastguard Worker          # TODO(crbug.com/41486292): Fix and re-enable.
1987*8975f5c5SAndroid Build Coastguard Worker          "-Wno-c++11-narrowing-const-reference",
1988*8975f5c5SAndroid Build Coastguard Worker        ]
1989*8975f5c5SAndroid Build Coastguard Worker
1990*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/344680447): Fix and re-enable.
1991*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [ "-Wno-missing-template-arg-list-after-template-kw" ]
1992*8975f5c5SAndroid Build Coastguard Worker
1993*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/356172342): Fix and re-enable.
1994*8975f5c5SAndroid Build Coastguard Worker        cflags_cc += [ "-Wno-dangling-assignment-gsl" ]
1995*8975f5c5SAndroid Build Coastguard Worker
1996*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/376641662): Fix and re-enable.
1997*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-Wno-nontrivial-memaccess" ]
1998*8975f5c5SAndroid Build Coastguard Worker      }
1999*8975f5c5SAndroid Build Coastguard Worker    }
2000*8975f5c5SAndroid Build Coastguard Worker
2001*8975f5c5SAndroid Build Coastguard Worker    # Some builders, such as Cronet, use a different version of Clang than
2002*8975f5c5SAndroid Build Coastguard Worker    # Chromium. This can cause minor errors when compiling Chromium changes. We
2003*8975f5c5SAndroid Build Coastguard Worker    # want to avoid these errors.
2004*8975f5c5SAndroid Build Coastguard Worker    if (llvm_android_mainline) {
2005*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2006*8975f5c5SAndroid Build Coastguard Worker        "-Wno-error=unknown-warning-option",
2007*8975f5c5SAndroid Build Coastguard Worker        "-Wno-error=unused-command-line-argument",
2008*8975f5c5SAndroid Build Coastguard Worker        "-Wno-error=unknown-pragmas",
2009*8975f5c5SAndroid Build Coastguard Worker      ]
2010*8975f5c5SAndroid Build Coastguard Worker    }
2011*8975f5c5SAndroid Build Coastguard Worker  }
2012*8975f5c5SAndroid Build Coastguard Worker
2013*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/354162568): Clean up and enable.
2014*8975f5c5SAndroid Build Coastguard Worker  if (is_apple && use_lld) {
2015*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,--no-warn-duplicate-rpath" ]
2016*8975f5c5SAndroid Build Coastguard Worker  }
2017*8975f5c5SAndroid Build Coastguard Worker
2018*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/355446806): Clean up and enable.
2019*8975f5c5SAndroid Build Coastguard Worker  if (is_apple) {
2020*8975f5c5SAndroid Build Coastguard Worker    ldflags += [ "-Wl,-no_warn_duplicate_libraries" ]
2021*8975f5c5SAndroid Build Coastguard Worker  }
2022*8975f5c5SAndroid Build Coastguard Worker
2023*8975f5c5SAndroid Build Coastguard Worker  # Rust warnings
2024*8975f5c5SAndroid Build Coastguard Worker
2025*8975f5c5SAndroid Build Coastguard Worker  # Require `unsafe` blocks even in `unsafe` fns. This is intended to become
2026*8975f5c5SAndroid Build Coastguard Worker  # an error by default eventually; see
2027*8975f5c5SAndroid Build Coastguard Worker  # https://github.com/rust-lang/rust/issues/71668
2028*8975f5c5SAndroid Build Coastguard Worker  rustflags = [ "-Dunsafe_op_in_unsafe_fn" ]
2029*8975f5c5SAndroid Build Coastguard Worker}
2030*8975f5c5SAndroid Build Coastguard Worker
2031*8975f5c5SAndroid Build Coastguard Worker# prevent_unsafe_narrowing ----------------------------------------------------
2032*8975f5c5SAndroid Build Coastguard Worker#
2033*8975f5c5SAndroid Build Coastguard Worker# Warnings that prevent narrowing or comparisons of integer types that are
2034*8975f5c5SAndroid Build Coastguard Worker# likely to cause out-of-bound read/writes or Undefined Behaviour. In
2035*8975f5c5SAndroid Build Coastguard Worker# particular, size_t is used for memory sizes, allocation, indexing, and
2036*8975f5c5SAndroid Build Coastguard Worker# offsets. Using other integer types along with size_t produces risk of
2037*8975f5c5SAndroid Build Coastguard Worker# memory-safety bugs and thus security exploits.
2038*8975f5c5SAndroid Build Coastguard Worker#
2039*8975f5c5SAndroid Build Coastguard Worker# In order to prevent these bugs, allocation sizes were historically limited to
2040*8975f5c5SAndroid Build Coastguard Worker# sizes that can be represented within 31 bits of information, allowing `int` to
2041*8975f5c5SAndroid Build Coastguard Worker# be safely misused instead of `size_t` (https://crbug.com/169327). In order to
2042*8975f5c5SAndroid Build Coastguard Worker# support increasing the allocation limit we require strictly adherence to
2043*8975f5c5SAndroid Build Coastguard Worker# using the correct types, avoiding lossy conversions, and preventing overflow.
2044*8975f5c5SAndroid Build Coastguard Worker# To do so, enable this config and fix errors by converting types to be
2045*8975f5c5SAndroid Build Coastguard Worker# `size_t`, which is both large enough and unsigned, when dealing with memory
2046*8975f5c5SAndroid Build Coastguard Worker# sizes, allocations, indices, or offsets.In cases where type conversion is not
2047*8975f5c5SAndroid Build Coastguard Worker# possible or is superfluous, use base::strict_cast<> or base::checked_cast<>
2048*8975f5c5SAndroid Build Coastguard Worker# to convert to size_t as needed.
2049*8975f5c5SAndroid Build Coastguard Worker# See also: https://docs.google.com/document/d/1CTbQ-5cQjnjU8aCOtLiA7G6P0i5C6HpSDNlSNq6nl5E
2050*8975f5c5SAndroid Build Coastguard Worker#
2051*8975f5c5SAndroid Build Coastguard Worker# To enable in a GN target, use:
2052*8975f5c5SAndroid Build Coastguard Worker#   configs += [ "//build/config/compiler:prevent_unsafe_narrowing" ]
2053*8975f5c5SAndroid Build Coastguard Worker
2054*8975f5c5SAndroid Build Coastguard Workerconfig("prevent_unsafe_narrowing") {
2055*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2056*8975f5c5SAndroid Build Coastguard Worker    cflags = [
2057*8975f5c5SAndroid Build Coastguard Worker      "-Wshorten-64-to-32",
2058*8975f5c5SAndroid Build Coastguard Worker      "-Wimplicit-int-conversion",
2059*8975f5c5SAndroid Build Coastguard Worker      "-Wsign-compare",
2060*8975f5c5SAndroid Build Coastguard Worker      "-Wsign-conversion",
2061*8975f5c5SAndroid Build Coastguard Worker    ]
2062*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl) {
2063*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2064*8975f5c5SAndroid Build Coastguard Worker        # Avoid bugs of the form `if (size_t i = size; i >= 0; --i)` while
2065*8975f5c5SAndroid Build Coastguard Worker        # fixing types to be sign-correct.
2066*8975f5c5SAndroid Build Coastguard Worker        "-Wtautological-unsigned-zero-compare",
2067*8975f5c5SAndroid Build Coastguard Worker      ]
2068*8975f5c5SAndroid Build Coastguard Worker    }
2069*8975f5c5SAndroid Build Coastguard Worker  }
2070*8975f5c5SAndroid Build Coastguard Worker}
2071*8975f5c5SAndroid Build Coastguard Worker
2072*8975f5c5SAndroid Build Coastguard Worker# chromium_code ---------------------------------------------------------------
2073*8975f5c5SAndroid Build Coastguard Worker#
2074*8975f5c5SAndroid Build Coastguard Worker# Toggles between higher and lower warnings for code that is (or isn't)
2075*8975f5c5SAndroid Build Coastguard Worker# part of Chromium.
2076*8975f5c5SAndroid Build Coastguard Worker
2077*8975f5c5SAndroid Build Coastguard Workerconfig("chromium_code") {
2078*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2079*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
2080*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "/W4" ]  # Warning level 4.
2081*8975f5c5SAndroid Build Coastguard Worker
2082*8975f5c5SAndroid Build Coastguard Worker      # Opt in to additional [[nodiscard]] on standard library methods.
2083*8975f5c5SAndroid Build Coastguard Worker      defines = [ "_HAS_NODISCARD" ]
2084*8975f5c5SAndroid Build Coastguard Worker    }
2085*8975f5c5SAndroid Build Coastguard Worker  } else {
2086*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-Wall" ]
2087*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
2088*8975f5c5SAndroid Build Coastguard Worker      # Enable extra warnings for chromium_code when we control the compiler.
2089*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wextra" ]
2090*8975f5c5SAndroid Build Coastguard Worker    }
2091*8975f5c5SAndroid Build Coastguard Worker
2092*8975f5c5SAndroid Build Coastguard Worker    # In Chromium code, we define __STDC_foo_MACROS in order to get the
2093*8975f5c5SAndroid Build Coastguard Worker    # C99 macros on Mac and Linux.
2094*8975f5c5SAndroid Build Coastguard Worker    defines = [
2095*8975f5c5SAndroid Build Coastguard Worker      "__STDC_CONSTANT_MACROS",
2096*8975f5c5SAndroid Build Coastguard Worker      "__STDC_FORMAT_MACROS",
2097*8975f5c5SAndroid Build Coastguard Worker    ]
2098*8975f5c5SAndroid Build Coastguard Worker
2099*8975f5c5SAndroid Build Coastguard Worker    if (!is_debug && !using_sanitizer && current_cpu != "s390x" &&
2100*8975f5c5SAndroid Build Coastguard Worker        current_cpu != "s390" && current_cpu != "ppc64" &&
2101*8975f5c5SAndroid Build Coastguard Worker        current_cpu != "mips" && current_cpu != "mips64" &&
2102*8975f5c5SAndroid Build Coastguard Worker        current_cpu != "riscv64" && current_cpu != "loong64") {
2103*8975f5c5SAndroid Build Coastguard Worker      # Non-chromium code is not guaranteed to compile cleanly with
2104*8975f5c5SAndroid Build Coastguard Worker      # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
2105*8975f5c5SAndroid Build Coastguard Worker      # disabled, so only do that for Release build.
2106*8975f5c5SAndroid Build Coastguard Worker      fortify_level = "2"
2107*8975f5c5SAndroid Build Coastguard Worker
2108*8975f5c5SAndroid Build Coastguard Worker      # ChromeOS's toolchain supports a high-quality _FORTIFY_SOURCE=3
2109*8975f5c5SAndroid Build Coastguard Worker      # implementation with a few custom glibc patches. Use that if it's
2110*8975f5c5SAndroid Build Coastguard Worker      # available.
2111*8975f5c5SAndroid Build Coastguard Worker      if (is_chromeos_device && !lacros_use_chromium_toolchain) {
2112*8975f5c5SAndroid Build Coastguard Worker        fortify_level = "3"
2113*8975f5c5SAndroid Build Coastguard Worker      }
2114*8975f5c5SAndroid Build Coastguard Worker      defines += [ "_FORTIFY_SOURCE=" + fortify_level ]
2115*8975f5c5SAndroid Build Coastguard Worker    }
2116*8975f5c5SAndroid Build Coastguard Worker
2117*8975f5c5SAndroid Build Coastguard Worker    if (is_apple) {
2118*8975f5c5SAndroid Build Coastguard Worker      cflags_objc = [ "-Wimplicit-retain-self" ]
2119*8975f5c5SAndroid Build Coastguard Worker      cflags_objcc = [ "-Wimplicit-retain-self" ]
2120*8975f5c5SAndroid Build Coastguard Worker    }
2121*8975f5c5SAndroid Build Coastguard Worker
2122*8975f5c5SAndroid Build Coastguard Worker    if (is_mac) {
2123*8975f5c5SAndroid Build Coastguard Worker      cflags_objc += [ "-Wobjc-missing-property-synthesis" ]
2124*8975f5c5SAndroid Build Coastguard Worker      cflags_objcc += [ "-Wobjc-missing-property-synthesis" ]
2125*8975f5c5SAndroid Build Coastguard Worker    }
2126*8975f5c5SAndroid Build Coastguard Worker
2127*8975f5c5SAndroid Build Coastguard Worker    if (is_ios) {
2128*8975f5c5SAndroid Build Coastguard Worker      cflags_objc += [ "-Widiomatic-parentheses" ]
2129*8975f5c5SAndroid Build Coastguard Worker      cflags_objcc += [ "-Widiomatic-parentheses" ]
2130*8975f5c5SAndroid Build Coastguard Worker    }
2131*8975f5c5SAndroid Build Coastguard Worker  }
2132*8975f5c5SAndroid Build Coastguard Worker
2133*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2134*8975f5c5SAndroid Build Coastguard Worker    cflags += [
2135*8975f5c5SAndroid Build Coastguard Worker      # Warn on missing break statements at the end of switch cases.
2136*8975f5c5SAndroid Build Coastguard Worker      # For intentional fallthrough, use [[fallthrough]].
2137*8975f5c5SAndroid Build Coastguard Worker      "-Wimplicit-fallthrough",
2138*8975f5c5SAndroid Build Coastguard Worker
2139*8975f5c5SAndroid Build Coastguard Worker      # Warn on unnecessary extra semicolons outside of function definitions.
2140*8975f5c5SAndroid Build Coastguard Worker      "-Wextra-semi",
2141*8975f5c5SAndroid Build Coastguard Worker
2142*8975f5c5SAndroid Build Coastguard Worker      # Warn on unreachable code, including unreachable breaks and returns.
2143*8975f5c5SAndroid Build Coastguard Worker      # See https://crbug.com/346399#c148 for suppression strategies.
2144*8975f5c5SAndroid Build Coastguard Worker      "-Wunreachable-code-aggressive",
2145*8975f5c5SAndroid Build Coastguard Worker    ]
2146*8975f5c5SAndroid Build Coastguard Worker
2147*8975f5c5SAndroid Build Coastguard Worker    # Thread safety analysis is broken under nacl: https://crbug.com/982423.
2148*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl || is_nacl_saigo) {
2149*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2150*8975f5c5SAndroid Build Coastguard Worker        # Thread safety analysis. See base/thread_annotations.h and
2151*8975f5c5SAndroid Build Coastguard Worker        # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
2152*8975f5c5SAndroid Build Coastguard Worker        "-Wthread-safety",
2153*8975f5c5SAndroid Build Coastguard Worker
2154*8975f5c5SAndroid Build Coastguard Worker        # Warn on GNU extensions.
2155*8975f5c5SAndroid Build Coastguard Worker        "-Wgnu",
2156*8975f5c5SAndroid Build Coastguard Worker
2157*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/357081796): Try to enable these.
2158*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-anonymous-struct",
2159*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-conditional-omitted-operand",
2160*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-include-next",
2161*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-label-as-value",
2162*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-redeclared-enum",
2163*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-statement-expression",
2164*8975f5c5SAndroid Build Coastguard Worker        "-Wno-gnu-zero-variadic-macro-arguments",
2165*8975f5c5SAndroid Build Coastguard Worker        "-Wno-zero-length-array",
2166*8975f5c5SAndroid Build Coastguard Worker      ]
2167*8975f5c5SAndroid Build Coastguard Worker
2168*8975f5c5SAndroid Build Coastguard Worker      if (is_chromeos) {
2169*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-Wno-gnu-offsetof-extensions" ]
2170*8975f5c5SAndroid Build Coastguard Worker      }
2171*8975f5c5SAndroid Build Coastguard Worker      if (is_ios) {
2172*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-Wno-gnu-case-range" ]
2173*8975f5c5SAndroid Build Coastguard Worker      }
2174*8975f5c5SAndroid Build Coastguard Worker    }
2175*8975f5c5SAndroid Build Coastguard Worker  }
2176*8975f5c5SAndroid Build Coastguard Worker
2177*8975f5c5SAndroid Build Coastguard Worker  configs = [
2178*8975f5c5SAndroid Build Coastguard Worker    ":default_warnings",
2179*8975f5c5SAndroid Build Coastguard Worker    ":noshadowing",
2180*8975f5c5SAndroid Build Coastguard Worker  ]
2181*8975f5c5SAndroid Build Coastguard Worker  if (treat_warnings_as_errors) {
2182*8975f5c5SAndroid Build Coastguard Worker    configs += [ ":treat_warnings_as_errors" ]
2183*8975f5c5SAndroid Build Coastguard Worker  }
2184*8975f5c5SAndroid Build Coastguard Worker}
2185*8975f5c5SAndroid Build Coastguard Worker
2186*8975f5c5SAndroid Build Coastguard Workerconfig("no_chromium_code") {
2187*8975f5c5SAndroid Build Coastguard Worker  cflags = []
2188*8975f5c5SAndroid Build Coastguard Worker  cflags_cc = []
2189*8975f5c5SAndroid Build Coastguard Worker  defines = []
2190*8975f5c5SAndroid Build Coastguard Worker
2191*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2192*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
2193*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "/W3" ]  # Warning level 3.
2194*8975f5c5SAndroid Build Coastguard Worker    }
2195*8975f5c5SAndroid Build Coastguard Worker    cflags += [
2196*8975f5c5SAndroid Build Coastguard Worker      "/wd4800",  # Disable warning when forcing value to bool.
2197*8975f5c5SAndroid Build Coastguard Worker      "/wd4267",  # TODO(jschuh): size_t to int.
2198*8975f5c5SAndroid Build Coastguard Worker    ]
2199*8975f5c5SAndroid Build Coastguard Worker  } else {
2200*8975f5c5SAndroid Build Coastguard Worker    if (is_clang && !is_nacl) {
2201*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): Remove !is_nacl once
2202*8975f5c5SAndroid Build Coastguard Worker      # https://codereview.webrtc.org/1552863002/ made its way into chromium.
2203*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wall" ]
2204*8975f5c5SAndroid Build Coastguard Worker    }
2205*8975f5c5SAndroid Build Coastguard Worker  }
2206*8975f5c5SAndroid Build Coastguard Worker
2207*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2208*8975f5c5SAndroid Build Coastguard Worker    cflags += [
2209*8975f5c5SAndroid Build Coastguard Worker      # Lots of third-party libraries have unused variables. Instead of
2210*8975f5c5SAndroid Build Coastguard Worker      # suppressing them individually, we just blanket suppress them here.
2211*8975f5c5SAndroid Build Coastguard Worker      "-Wno-unused-variable",
2212*8975f5c5SAndroid Build Coastguard Worker
2213*8975f5c5SAndroid Build Coastguard Worker      # Similarly, we're not going to fix all the C++11 narrowing issues in
2214*8975f5c5SAndroid Build Coastguard Worker      # third-party libraries.
2215*8975f5c5SAndroid Build Coastguard Worker      "-Wno-c++11-narrowing",
2216*8975f5c5SAndroid Build Coastguard Worker    ]
2217*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl) {
2218*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2219*8975f5c5SAndroid Build Coastguard Worker        # Disabled for similar reasons as -Wunused-variable.
2220*8975f5c5SAndroid Build Coastguard Worker        "-Wno-unused-but-set-variable",
2221*8975f5c5SAndroid Build Coastguard Worker
2222*8975f5c5SAndroid Build Coastguard Worker        # TODO(crbug.com/40762742): Clean up and enable.
2223*8975f5c5SAndroid Build Coastguard Worker        "-Wno-misleading-indentation",
2224*8975f5c5SAndroid Build Coastguard Worker      ]
2225*8975f5c5SAndroid Build Coastguard Worker    }
2226*8975f5c5SAndroid Build Coastguard Worker  }
2227*8975f5c5SAndroid Build Coastguard Worker
2228*8975f5c5SAndroid Build Coastguard Worker  # Suppress all warnings in third party, as Cargo does:
2229*8975f5c5SAndroid Build Coastguard Worker  # https://doc.rust-lang.org/rustc/lints/levels.html#capping-lints
2230*8975f5c5SAndroid Build Coastguard Worker  rustflags = [ "--cap-lints=allow" ]
2231*8975f5c5SAndroid Build Coastguard Worker
2232*8975f5c5SAndroid Build Coastguard Worker  configs = [ ":default_warnings" ]
2233*8975f5c5SAndroid Build Coastguard Worker
2234*8975f5c5SAndroid Build Coastguard Worker  # GCC may emit unsuppressible warnings so only apply this config when
2235*8975f5c5SAndroid Build Coastguard Worker  # building with clang. crbug.com/589724
2236*8975f5c5SAndroid Build Coastguard Worker  if (treat_warnings_as_errors && is_clang) {
2237*8975f5c5SAndroid Build Coastguard Worker    configs += [ ":treat_warnings_as_errors" ]
2238*8975f5c5SAndroid Build Coastguard Worker  }
2239*8975f5c5SAndroid Build Coastguard Worker}
2240*8975f5c5SAndroid Build Coastguard Worker
2241*8975f5c5SAndroid Build Coastguard Worker# noshadowing -----------------------------------------------------------------
2242*8975f5c5SAndroid Build Coastguard Worker#
2243*8975f5c5SAndroid Build Coastguard Worker# Allows turning -Wshadow on.
2244*8975f5c5SAndroid Build Coastguard Worker
2245*8975f5c5SAndroid Build Coastguard Workerconfig("noshadowing") {
2246*8975f5c5SAndroid Build Coastguard Worker  # This flag has to be disabled for nacl because the nacl compiler is too
2247*8975f5c5SAndroid Build Coastguard Worker  # strict about shadowing.
2248*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && (!is_nacl || is_nacl_saigo)) {
2249*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-Wshadow" ]
2250*8975f5c5SAndroid Build Coastguard Worker  }
2251*8975f5c5SAndroid Build Coastguard Worker}
2252*8975f5c5SAndroid Build Coastguard Worker
2253*8975f5c5SAndroid Build Coastguard Worker# rtti ------------------------------------------------------------------------
2254*8975f5c5SAndroid Build Coastguard Worker#
2255*8975f5c5SAndroid Build Coastguard Worker# Allows turning Run-Time Type Identification on or off.
2256*8975f5c5SAndroid Build Coastguard Worker
2257*8975f5c5SAndroid Build Coastguard Workerconfig("rtti") {
2258*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2259*8975f5c5SAndroid Build Coastguard Worker    cflags_cc = [ "/GR" ]
2260*8975f5c5SAndroid Build Coastguard Worker  } else {
2261*8975f5c5SAndroid Build Coastguard Worker    cflags_cc = [ "-frtti" ]
2262*8975f5c5SAndroid Build Coastguard Worker  }
2263*8975f5c5SAndroid Build Coastguard Worker}
2264*8975f5c5SAndroid Build Coastguard Worker
2265*8975f5c5SAndroid Build Coastguard Workerconfig("no_rtti") {
2266*8975f5c5SAndroid Build Coastguard Worker  # Some sanitizer configs may require RTTI to be left enabled globally
2267*8975f5c5SAndroid Build Coastguard Worker  if (!use_rtti) {
2268*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
2269*8975f5c5SAndroid Build Coastguard Worker      cflags_cc = [ "/GR-" ]
2270*8975f5c5SAndroid Build Coastguard Worker    } else {
2271*8975f5c5SAndroid Build Coastguard Worker      cflags_cc = [ "-fno-rtti" ]
2272*8975f5c5SAndroid Build Coastguard Worker      cflags_objcc = cflags_cc
2273*8975f5c5SAndroid Build Coastguard Worker    }
2274*8975f5c5SAndroid Build Coastguard Worker  }
2275*8975f5c5SAndroid Build Coastguard Worker}
2276*8975f5c5SAndroid Build Coastguard Worker
2277*8975f5c5SAndroid Build Coastguard Worker# export_dynamic ---------------------------------------------------------------
2278*8975f5c5SAndroid Build Coastguard Worker#
2279*8975f5c5SAndroid Build Coastguard Worker# Ensures all exported symbols are added to the dynamic symbol table.  This is
2280*8975f5c5SAndroid Build Coastguard Worker# necessary to expose Chrome's custom operator new() and operator delete() (and
2281*8975f5c5SAndroid Build Coastguard Worker# other memory-related symbols) to libraries.  Otherwise, they might
2282*8975f5c5SAndroid Build Coastguard Worker# (de)allocate memory on a different heap, which would spell trouble if pointers
2283*8975f5c5SAndroid Build Coastguard Worker# to heap-allocated memory are passed over shared library boundaries.
2284*8975f5c5SAndroid Build Coastguard Workerconfig("export_dynamic") {
2285*8975f5c5SAndroid Build Coastguard Worker  # TODO(crbug.com/40118868): Revisit after target_os flip is completed.
2286*8975f5c5SAndroid Build Coastguard Worker  if (is_linux || is_chromeos_lacros || export_libcxxabi_from_executables) {
2287*8975f5c5SAndroid Build Coastguard Worker    ldflags = [ "-rdynamic" ]
2288*8975f5c5SAndroid Build Coastguard Worker  }
2289*8975f5c5SAndroid Build Coastguard Worker}
2290*8975f5c5SAndroid Build Coastguard Worker
2291*8975f5c5SAndroid Build Coastguard Worker# thin_archive -----------------------------------------------------------------
2292*8975f5c5SAndroid Build Coastguard Worker#
2293*8975f5c5SAndroid Build Coastguard Worker# Enables thin archives on posix, and on windows when the lld linker is used.
2294*8975f5c5SAndroid Build Coastguard Worker# Regular archives directly include the object files used to generate it.
2295*8975f5c5SAndroid Build Coastguard Worker# Thin archives merely reference the object files.
2296*8975f5c5SAndroid Build Coastguard Worker# This makes building them faster since it requires less disk IO, but is
2297*8975f5c5SAndroid Build Coastguard Worker# inappropriate if you wish to redistribute your static library.
2298*8975f5c5SAndroid Build Coastguard Worker# This config is added to the global config, so thin archives should already be
2299*8975f5c5SAndroid Build Coastguard Worker# enabled.  If you want to make a distributable static library, you need to do 2
2300*8975f5c5SAndroid Build Coastguard Worker# things:
2301*8975f5c5SAndroid Build Coastguard Worker# 1. Set complete_static_lib so that all dependencies of the library make it
2302*8975f5c5SAndroid Build Coastguard Worker#    into the library. See `gn help complete_static_lib` for details.
2303*8975f5c5SAndroid Build Coastguard Worker# 2. Remove the thin_archive config, so that the .a file actually contains all
2304*8975f5c5SAndroid Build Coastguard Worker#    .o files, instead of just references to .o files in the build directoy
2305*8975f5c5SAndroid Build Coastguard Workerconfig("thin_archive") {
2306*8975f5c5SAndroid Build Coastguard Worker  if ((is_apple && use_lld) || (is_linux && !is_clang)) {
2307*8975f5c5SAndroid Build Coastguard Worker    # The macOS and iOS linker ld64.ldd doesn't support thin archive without
2308*8975f5c5SAndroid Build Coastguard Worker    # symbol table, gcc on linux also throws the error `archive has no index`.
2309*8975f5c5SAndroid Build Coastguard Worker    arflags = [
2310*8975f5c5SAndroid Build Coastguard Worker      "-T",
2311*8975f5c5SAndroid Build Coastguard Worker      "-s",
2312*8975f5c5SAndroid Build Coastguard Worker    ]
2313*8975f5c5SAndroid Build Coastguard Worker  } else if ((is_posix && !is_nacl && (!is_apple || use_lld)) || is_fuchsia) {
2314*8975f5c5SAndroid Build Coastguard Worker    # The macOS and iOS default linker ld64 does not support reading thin
2315*8975f5c5SAndroid Build Coastguard Worker    # archives.
2316*8975f5c5SAndroid Build Coastguard Worker    arflags = [
2317*8975f5c5SAndroid Build Coastguard Worker      "-T",
2318*8975f5c5SAndroid Build Coastguard Worker      "-S",
2319*8975f5c5SAndroid Build Coastguard Worker    ]
2320*8975f5c5SAndroid Build Coastguard Worker  } else if (is_win && use_lld) {
2321*8975f5c5SAndroid Build Coastguard Worker    arflags = [ "/llvmlibthin" ]
2322*8975f5c5SAndroid Build Coastguard Worker  }
2323*8975f5c5SAndroid Build Coastguard Worker}
2324*8975f5c5SAndroid Build Coastguard Worker
2325*8975f5c5SAndroid Build Coastguard Worker# exceptions -------------------------------------------------------------------
2326*8975f5c5SAndroid Build Coastguard Worker#
2327*8975f5c5SAndroid Build Coastguard Worker# Allows turning Exceptions on or off.
2328*8975f5c5SAndroid Build Coastguard Worker# Note: exceptions are disallowed in Google code.
2329*8975f5c5SAndroid Build Coastguard Worker
2330*8975f5c5SAndroid Build Coastguard Workerconfig("exceptions") {
2331*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2332*8975f5c5SAndroid Build Coastguard Worker    # Enables exceptions in the STL.
2333*8975f5c5SAndroid Build Coastguard Worker    if (!use_custom_libcxx) {
2334*8975f5c5SAndroid Build Coastguard Worker      defines = [ "_HAS_EXCEPTIONS=1" ]
2335*8975f5c5SAndroid Build Coastguard Worker    }
2336*8975f5c5SAndroid Build Coastguard Worker    cflags_cc = [ "/EHsc" ]
2337*8975f5c5SAndroid Build Coastguard Worker  } else {
2338*8975f5c5SAndroid Build Coastguard Worker    cflags_cc = [ "-fexceptions" ]
2339*8975f5c5SAndroid Build Coastguard Worker    cflags_objcc = cflags_cc
2340*8975f5c5SAndroid Build Coastguard Worker  }
2341*8975f5c5SAndroid Build Coastguard Worker}
2342*8975f5c5SAndroid Build Coastguard Worker
2343*8975f5c5SAndroid Build Coastguard Workerconfig("no_exceptions") {
2344*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2345*8975f5c5SAndroid Build Coastguard Worker    # Disables exceptions in the STL.
2346*8975f5c5SAndroid Build Coastguard Worker    # libc++ uses the __has_feature macro to control whether to use exceptions,
2347*8975f5c5SAndroid Build Coastguard Worker    # so defining this macro is unnecessary. Defining _HAS_EXCEPTIONS to 0 also
2348*8975f5c5SAndroid Build Coastguard Worker    # breaks libc++ because it depends on MSVC headers that only provide certain
2349*8975f5c5SAndroid Build Coastguard Worker    # declarations if _HAS_EXCEPTIONS is 1. Those MSVC headers do not use
2350*8975f5c5SAndroid Build Coastguard Worker    # exceptions, despite being conditional on _HAS_EXCEPTIONS.
2351*8975f5c5SAndroid Build Coastguard Worker    if (!use_custom_libcxx) {
2352*8975f5c5SAndroid Build Coastguard Worker      defines = [ "_HAS_EXCEPTIONS=0" ]
2353*8975f5c5SAndroid Build Coastguard Worker    }
2354*8975f5c5SAndroid Build Coastguard Worker  } else {
2355*8975f5c5SAndroid Build Coastguard Worker    cflags_cc = [ "-fno-exceptions" ]
2356*8975f5c5SAndroid Build Coastguard Worker    cflags_objcc = cflags_cc
2357*8975f5c5SAndroid Build Coastguard Worker  }
2358*8975f5c5SAndroid Build Coastguard Worker}
2359*8975f5c5SAndroid Build Coastguard Worker
2360*8975f5c5SAndroid Build Coastguard Worker# Warnings ---------------------------------------------------------------------
2361*8975f5c5SAndroid Build Coastguard Worker
2362*8975f5c5SAndroid Build Coastguard Worker# Generate a warning for code that might emit a static initializer.
2363*8975f5c5SAndroid Build Coastguard Worker# See: //docs/static_initializers.md
2364*8975f5c5SAndroid Build Coastguard Worker# See: https://groups.google.com/a/chromium.org/d/topic/chromium-dev/B9Q5KTD7iCo/discussion
2365*8975f5c5SAndroid Build Coastguard Workerconfig("wglobal_constructors") {
2366*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2367*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-Wglobal-constructors" ]
2368*8975f5c5SAndroid Build Coastguard Worker  }
2369*8975f5c5SAndroid Build Coastguard Worker}
2370*8975f5c5SAndroid Build Coastguard Worker
2371*8975f5c5SAndroid Build Coastguard Worker# This will generate warnings when using Clang if code generates exit-time
2372*8975f5c5SAndroid Build Coastguard Worker# destructors, which will slow down closing the program.
2373*8975f5c5SAndroid Build Coastguard Worker# TODO(thakis): Make this a blocklist instead, http://crbug.com/101600
2374*8975f5c5SAndroid Build Coastguard Workerconfig("wexit_time_destructors") {
2375*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2376*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-Wexit-time-destructors" ]
2377*8975f5c5SAndroid Build Coastguard Worker  }
2378*8975f5c5SAndroid Build Coastguard Worker}
2379*8975f5c5SAndroid Build Coastguard Worker
2380*8975f5c5SAndroid Build Coastguard Worker# Some code presumes that pointers to structures/objects are compatible
2381*8975f5c5SAndroid Build Coastguard Worker# regardless of whether what they point to is already known to be valid.
2382*8975f5c5SAndroid Build Coastguard Worker# gcc 4.9 and earlier had no way of suppressing this warning without
2383*8975f5c5SAndroid Build Coastguard Worker# suppressing the rest of them.  Here we centralize the identification of
2384*8975f5c5SAndroid Build Coastguard Worker# the gcc 4.9 toolchains.
2385*8975f5c5SAndroid Build Coastguard Workerconfig("no_incompatible_pointer_warnings") {
2386*8975f5c5SAndroid Build Coastguard Worker  cflags = []
2387*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2388*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-Wno-incompatible-pointer-types" ]
2389*8975f5c5SAndroid Build Coastguard Worker  } else if (current_cpu == "mipsel" || current_cpu == "mips64el") {
2390*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-w" ]
2391*8975f5c5SAndroid Build Coastguard Worker  } else if (is_chromeos_ash && current_cpu == "arm") {
2392*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-w" ]
2393*8975f5c5SAndroid Build Coastguard Worker  }
2394*8975f5c5SAndroid Build Coastguard Worker}
2395*8975f5c5SAndroid Build Coastguard Worker
2396*8975f5c5SAndroid Build Coastguard Worker# Optimization -----------------------------------------------------------------
2397*8975f5c5SAndroid Build Coastguard Worker#
2398*8975f5c5SAndroid Build Coastguard Worker# The BUILDCONFIG file sets the "default_optimization" config on targets by
2399*8975f5c5SAndroid Build Coastguard Worker# default. It will be equivalent to either "optimize" (release) or
2400*8975f5c5SAndroid Build Coastguard Worker# "no_optimize" (debug) optimization configs.
2401*8975f5c5SAndroid Build Coastguard Worker#
2402*8975f5c5SAndroid Build Coastguard Worker# You can override the optimization level on a per-target basis by removing the
2403*8975f5c5SAndroid Build Coastguard Worker# default config and then adding the named one you want:
2404*8975f5c5SAndroid Build Coastguard Worker#
2405*8975f5c5SAndroid Build Coastguard Worker#   configs -= [ "//build/config/compiler:default_optimization" ]
2406*8975f5c5SAndroid Build Coastguard Worker#   configs += [ "//build/config/compiler:optimize_max" ]
2407*8975f5c5SAndroid Build Coastguard Worker
2408*8975f5c5SAndroid Build Coastguard Worker# Shared settings for both "optimize" and "optimize_max" configs.
2409*8975f5c5SAndroid Build Coastguard Worker# IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags.
2410*8975f5c5SAndroid Build Coastguard Workerif (is_win) {
2411*8975f5c5SAndroid Build Coastguard Worker  common_optimize_on_cflags = [
2412*8975f5c5SAndroid Build Coastguard Worker    "/Oy-",  # Disable omitting frame pointers, must be after /O2.
2413*8975f5c5SAndroid Build Coastguard Worker    "/Zc:inline",  # Remove unreferenced COMDAT (faster links).
2414*8975f5c5SAndroid Build Coastguard Worker  ]
2415*8975f5c5SAndroid Build Coastguard Worker  if (!is_asan) {
2416*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_cflags += [
2417*8975f5c5SAndroid Build Coastguard Worker      # Put data in separate COMDATs. This allows the linker
2418*8975f5c5SAndroid Build Coastguard Worker      # to put bit-identical constants at the same address even if
2419*8975f5c5SAndroid Build Coastguard Worker      # they're unrelated constants, which saves binary size.
2420*8975f5c5SAndroid Build Coastguard Worker      # This optimization can't be used when ASan is enabled because
2421*8975f5c5SAndroid Build Coastguard Worker      # it is not compatible with the ASan ODR checker.
2422*8975f5c5SAndroid Build Coastguard Worker      "/Gw",
2423*8975f5c5SAndroid Build Coastguard Worker    ]
2424*8975f5c5SAndroid Build Coastguard Worker  }
2425*8975f5c5SAndroid Build Coastguard Worker  common_optimize_on_ldflags = []
2426*8975f5c5SAndroid Build Coastguard Worker
2427*8975f5c5SAndroid Build Coastguard Worker  # /OPT:ICF is not desirable in Debug builds, since code-folding can result in
2428*8975f5c5SAndroid Build Coastguard Worker  # misleading symbols in stack traces.
2429*8975f5c5SAndroid Build Coastguard Worker  if (!is_debug && !is_component_build) {
2430*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_ldflags += [ "/OPT:ICF" ]  # Redundant COMDAT folding.
2431*8975f5c5SAndroid Build Coastguard Worker  }
2432*8975f5c5SAndroid Build Coastguard Worker
2433*8975f5c5SAndroid Build Coastguard Worker  if (is_official_build) {
2434*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_ldflags += [ "/OPT:REF" ]  # Remove unreferenced data.
2435*8975f5c5SAndroid Build Coastguard Worker  }
2436*8975f5c5SAndroid Build Coastguard Worker
2437*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2438*8975f5c5SAndroid Build Coastguard Worker    # See below.
2439*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_cflags += [ "/clang:-fno-math-errno" ]
2440*8975f5c5SAndroid Build Coastguard Worker  }
2441*8975f5c5SAndroid Build Coastguard Worker} else {
2442*8975f5c5SAndroid Build Coastguard Worker  common_optimize_on_cflags = []
2443*8975f5c5SAndroid Build Coastguard Worker  common_optimize_on_ldflags = []
2444*8975f5c5SAndroid Build Coastguard Worker
2445*8975f5c5SAndroid Build Coastguard Worker  if (is_android) {
2446*8975f5c5SAndroid Build Coastguard Worker    # TODO(jdduke) Re-enable on mips after resolving linking
2447*8975f5c5SAndroid Build Coastguard Worker    # issues with libc++ (crbug.com/456380).
2448*8975f5c5SAndroid Build Coastguard Worker    if (current_cpu != "mipsel" && current_cpu != "mips64el") {
2449*8975f5c5SAndroid Build Coastguard Worker      common_optimize_on_ldflags += [
2450*8975f5c5SAndroid Build Coastguard Worker        # Warn in case of text relocations.
2451*8975f5c5SAndroid Build Coastguard Worker        "-Wl,--warn-shared-textrel",
2452*8975f5c5SAndroid Build Coastguard Worker      ]
2453*8975f5c5SAndroid Build Coastguard Worker    }
2454*8975f5c5SAndroid Build Coastguard Worker  }
2455*8975f5c5SAndroid Build Coastguard Worker
2456*8975f5c5SAndroid Build Coastguard Worker  if (is_apple) {
2457*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
2458*8975f5c5SAndroid Build Coastguard Worker
2459*8975f5c5SAndroid Build Coastguard Worker    if (is_official_build) {
2460*8975f5c5SAndroid Build Coastguard Worker      common_optimize_on_ldflags += [
2461*8975f5c5SAndroid Build Coastguard Worker        "-Wl,-no_data_in_code_info",
2462*8975f5c5SAndroid Build Coastguard Worker        "-Wl,-no_function_starts",
2463*8975f5c5SAndroid Build Coastguard Worker      ]
2464*8975f5c5SAndroid Build Coastguard Worker    }
2465*8975f5c5SAndroid Build Coastguard Worker  } else if (current_os != "aix" && current_os != "zos") {
2466*8975f5c5SAndroid Build Coastguard Worker    # Non-Mac Posix flags.
2467*8975f5c5SAndroid Build Coastguard Worker    # Aix does not support these.
2468*8975f5c5SAndroid Build Coastguard Worker
2469*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_cflags += [
2470*8975f5c5SAndroid Build Coastguard Worker      # Put data and code in their own sections, so that unused symbols
2471*8975f5c5SAndroid Build Coastguard Worker      # can be removed at link time with --gc-sections.
2472*8975f5c5SAndroid Build Coastguard Worker      "-fdata-sections",
2473*8975f5c5SAndroid Build Coastguard Worker      "-ffunction-sections",
2474*8975f5c5SAndroid Build Coastguard Worker    ]
2475*8975f5c5SAndroid Build Coastguard Worker    if ((!is_nacl || is_nacl_saigo) && is_clang) {
2476*8975f5c5SAndroid Build Coastguard Worker      # We don't care about unique section names, this makes object files a bit
2477*8975f5c5SAndroid Build Coastguard Worker      # smaller.
2478*8975f5c5SAndroid Build Coastguard Worker      common_optimize_on_cflags += [ "-fno-unique-section-names" ]
2479*8975f5c5SAndroid Build Coastguard Worker    }
2480*8975f5c5SAndroid Build Coastguard Worker
2481*8975f5c5SAndroid Build Coastguard Worker    if (is_official_build) {
2482*8975f5c5SAndroid Build Coastguard Worker      common_optimize_on_ldflags += [
2483*8975f5c5SAndroid Build Coastguard Worker        # Specifically tell the linker to perform optimizations.
2484*8975f5c5SAndroid Build Coastguard Worker        # See http://lwn.net/Articles/192624/.
2485*8975f5c5SAndroid Build Coastguard Worker        # -O2 enables string tail merge optimization in lld.
2486*8975f5c5SAndroid Build Coastguard Worker        "-Wl,-O2",
2487*8975f5c5SAndroid Build Coastguard Worker      ]
2488*8975f5c5SAndroid Build Coastguard Worker    }
2489*8975f5c5SAndroid Build Coastguard Worker
2490*8975f5c5SAndroid Build Coastguard Worker    common_optimize_on_ldflags += [ "-Wl,--gc-sections" ]
2491*8975f5c5SAndroid Build Coastguard Worker  }
2492*8975f5c5SAndroid Build Coastguard Worker
2493*8975f5c5SAndroid Build Coastguard Worker  # We cannot rely on errno being set after math functions,
2494*8975f5c5SAndroid Build Coastguard Worker  # especially since glibc does not set it. Thus, use -fno-math-errno
2495*8975f5c5SAndroid Build Coastguard Worker  # so that the compiler knows it can inline math functions.
2496*8975f5c5SAndroid Build Coastguard Worker  # Note that this is different from -ffast-math (even though -ffast-math
2497*8975f5c5SAndroid Build Coastguard Worker  # implies -fno-math-errno), which also allows a number of unsafe
2498*8975f5c5SAndroid Build Coastguard Worker  # optimizations.
2499*8975f5c5SAndroid Build Coastguard Worker  common_optimize_on_cflags += [ "-fno-math-errno" ]
2500*8975f5c5SAndroid Build Coastguard Worker}
2501*8975f5c5SAndroid Build Coastguard Worker
2502*8975f5c5SAndroid Build Coastguard Workerconfig("default_stack_frames") {
2503*8975f5c5SAndroid Build Coastguard Worker  if (!is_win) {
2504*8975f5c5SAndroid Build Coastguard Worker    if (enable_frame_pointers) {
2505*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-fno-omit-frame-pointer" ]
2506*8975f5c5SAndroid Build Coastguard Worker
2507*8975f5c5SAndroid Build Coastguard Worker      # Omit frame pointers for leaf functions on x86, otherwise building libyuv
2508*8975f5c5SAndroid Build Coastguard Worker      # gives clang's register allocator issues, see llvm.org/PR15798 /
2509*8975f5c5SAndroid Build Coastguard Worker      # crbug.com/233709
2510*8975f5c5SAndroid Build Coastguard Worker      if (is_clang && current_cpu == "x86" && !is_apple) {
2511*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-momit-leaf-frame-pointer" ]
2512*8975f5c5SAndroid Build Coastguard Worker      }
2513*8975f5c5SAndroid Build Coastguard Worker    } else {
2514*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-fomit-frame-pointer" ]
2515*8975f5c5SAndroid Build Coastguard Worker    }
2516*8975f5c5SAndroid Build Coastguard Worker  }
2517*8975f5c5SAndroid Build Coastguard Worker  # On Windows, the flag to enable framepointers "/Oy-" must always come after
2518*8975f5c5SAndroid Build Coastguard Worker  # the optimization flag [e.g. "/O2"]. The optimization flag is set by one of
2519*8975f5c5SAndroid Build Coastguard Worker  # the "optimize" configs, see rest of this file. The ordering that cflags are
2520*8975f5c5SAndroid Build Coastguard Worker  # applied is well-defined by the GN spec, and there is no way to ensure that
2521*8975f5c5SAndroid Build Coastguard Worker  # cflags set by "default_stack_frames" is applied after those set by an
2522*8975f5c5SAndroid Build Coastguard Worker  # "optimize" config. Similarly, there is no way to propagate state from this
2523*8975f5c5SAndroid Build Coastguard Worker  # config into the "optimize" config. We always apply the "/Oy-" config in the
2524*8975f5c5SAndroid Build Coastguard Worker  # definition for common_optimize_on_cflags definition, even though this may
2525*8975f5c5SAndroid Build Coastguard Worker  # not be correct.
2526*8975f5c5SAndroid Build Coastguard Worker}
2527*8975f5c5SAndroid Build Coastguard Worker
2528*8975f5c5SAndroid Build Coastguard Worker# Default "optimization on" config.
2529*8975f5c5SAndroid Build Coastguard Worker#
2530*8975f5c5SAndroid Build Coastguard Worker# High-end Android: As of April 2024, `-O2` appears to be a good default,
2531*8975f5c5SAndroid Build Coastguard Worker# particularly since a selection of "hot" targets are already using `-O3`.
2532*8975f5c5SAndroid Build Coastguard Worker# Enabling `-O3` for all targets does not change performance much (according
2533*8975f5c5SAndroid Build Coastguard Worker# to Speedometer), but regresses binary size. Using `-O3` as the default:
2534*8975f5c5SAndroid Build Coastguard Worker#   - Decreases Speedometer 2.1 score by 0.2% [0].
2535*8975f5c5SAndroid Build Coastguard Worker#   - Increases Speedometer 3.0 score by 0.1% [1].
2536*8975f5c5SAndroid Build Coastguard Worker#   - Increases binary size by 1.47MB [2] (or 0.8%: the arm64 native code size
2537*8975f5c5SAndroid Build Coastguard Worker#     in M124 is 178MB).
2538*8975f5c5SAndroid Build Coastguard Worker# ... over `-O2`.
2539*8975f5c5SAndroid Build Coastguard Worker#
2540*8975f5c5SAndroid Build Coastguard Worker# [0]: https://pinpoint-dot-chromeperf.appspot.com/job/147634a8be0000
2541*8975f5c5SAndroid Build Coastguard Worker# [1]: https://pinpoint-dot-chromeperf.appspot.com/job/132bc772be0000
2542*8975f5c5SAndroid Build Coastguard Worker# [2]: https://crrev.com/c/5447532
2543*8975f5c5SAndroid Build Coastguard Workerconfig("optimize") {
2544*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2545*8975f5c5SAndroid Build Coastguard Worker    # clang-cl's /O2 corresponds to clang's -O3, and really want -O2 for
2546*8975f5c5SAndroid Build Coastguard Worker    # consistency with the other platforms.
2547*8975f5c5SAndroid Build Coastguard Worker    cflags = [
2548*8975f5c5SAndroid Build Coastguard Worker               "/O2",
2549*8975f5c5SAndroid Build Coastguard Worker               "/clang:-O2",
2550*8975f5c5SAndroid Build Coastguard Worker             ] + common_optimize_on_cflags
2551*8975f5c5SAndroid Build Coastguard Worker
2552*8975f5c5SAndroid Build Coastguard Worker    # The `-O3` for clang turns on extra optimizations compared to the standard
2553*8975f5c5SAndroid Build Coastguard Worker    # `-O2`. But for rust, `-Copt-level=3` is the default and is thus reliable
2554*8975f5c5SAndroid Build Coastguard Worker    # to use.
2555*8975f5c5SAndroid Build Coastguard Worker    rustflags = [ "-Copt-level=3" ]
2556*8975f5c5SAndroid Build Coastguard Worker  } else if (optimize_for_size || is_chromeos) {
2557*8975f5c5SAndroid Build Coastguard Worker    # Favor size over speed.
2558*8975f5c5SAndroid Build Coastguard Worker    # -Os in clang is more of a size-conscious -O2 than "size at any cost"
2559*8975f5c5SAndroid Build Coastguard Worker    # (AKA -Oz).
2560*8975f5c5SAndroid Build Coastguard Worker
2561*8975f5c5SAndroid Build Coastguard Worker    if (is_fuchsia) {
2562*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-Oz" ] + common_optimize_on_cflags
2563*8975f5c5SAndroid Build Coastguard Worker    } else {
2564*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-Os" ] + common_optimize_on_cflags
2565*8975f5c5SAndroid Build Coastguard Worker    }
2566*8975f5c5SAndroid Build Coastguard Worker
2567*8975f5c5SAndroid Build Coastguard Worker    if (is_clang && use_ml_inliner && is_a_target_toolchain && !is_chromeos &&
2568*8975f5c5SAndroid Build Coastguard Worker        !is_high_end_android) {
2569*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2570*8975f5c5SAndroid Build Coastguard Worker        "-mllvm",
2571*8975f5c5SAndroid Build Coastguard Worker        "-enable-ml-inliner=release",
2572*8975f5c5SAndroid Build Coastguard Worker        "-mllvm",
2573*8975f5c5SAndroid Build Coastguard Worker        "-ml-inliner-model-selector=arm32-size",
2574*8975f5c5SAndroid Build Coastguard Worker      ]
2575*8975f5c5SAndroid Build Coastguard Worker    }
2576*8975f5c5SAndroid Build Coastguard Worker
2577*8975f5c5SAndroid Build Coastguard Worker    # Similar to clang, we optimize with `-Copt-level=s` to keep loop
2578*8975f5c5SAndroid Build Coastguard Worker    # vectorization while otherwise optimizing for size.
2579*8975f5c5SAndroid Build Coastguard Worker    rustflags = [ "-Copt-level=s" ]
2580*8975f5c5SAndroid Build Coastguard Worker  } else {
2581*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-O2" ] + common_optimize_on_cflags
2582*8975f5c5SAndroid Build Coastguard Worker
2583*8975f5c5SAndroid Build Coastguard Worker    # The `-O3` for clang turns on extra optimizations compared to the standard
2584*8975f5c5SAndroid Build Coastguard Worker    # `-O2`. But for rust, `-Copt-level=3` is the default and is thus reliable
2585*8975f5c5SAndroid Build Coastguard Worker    # to use.
2586*8975f5c5SAndroid Build Coastguard Worker    rustflags = [ "-Copt-level=3" ]
2587*8975f5c5SAndroid Build Coastguard Worker  }
2588*8975f5c5SAndroid Build Coastguard Worker  ldflags = common_optimize_on_ldflags
2589*8975f5c5SAndroid Build Coastguard Worker}
2590*8975f5c5SAndroid Build Coastguard Worker
2591*8975f5c5SAndroid Build Coastguard Worker# Turn off optimizations.
2592*8975f5c5SAndroid Build Coastguard Workerconfig("no_optimize") {
2593*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2594*8975f5c5SAndroid Build Coastguard Worker    cflags = [
2595*8975f5c5SAndroid Build Coastguard Worker      "/Od",  # Disable optimization.
2596*8975f5c5SAndroid Build Coastguard Worker      "/Ob0",  # Disable all inlining (on by default).
2597*8975f5c5SAndroid Build Coastguard Worker      "/GF",  # Enable string pooling (off by default).
2598*8975f5c5SAndroid Build Coastguard Worker    ]
2599*8975f5c5SAndroid Build Coastguard Worker
2600*8975f5c5SAndroid Build Coastguard Worker    if (target_cpu == "arm64") {
2601*8975f5c5SAndroid Build Coastguard Worker      # Disable omitting frame pointers for no_optimize build because stack
2602*8975f5c5SAndroid Build Coastguard Worker      # traces on Windows ARM64 rely on it.
2603*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "/Oy-" ]
2604*8975f5c5SAndroid Build Coastguard Worker    }
2605*8975f5c5SAndroid Build Coastguard Worker  } else if (is_android && !android_full_debug) {
2606*8975f5c5SAndroid Build Coastguard Worker    # On Android we kind of optimize some things that don't affect debugging
2607*8975f5c5SAndroid Build Coastguard Worker    # much even when optimization is disabled to get the binary size down.
2608*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
2609*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-Oz" ] + common_optimize_on_cflags
2610*8975f5c5SAndroid Build Coastguard Worker    } else {
2611*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-Os" ] + common_optimize_on_cflags
2612*8975f5c5SAndroid Build Coastguard Worker    }
2613*8975f5c5SAndroid Build Coastguard Worker
2614*8975f5c5SAndroid Build Coastguard Worker    if (!is_component_build) {
2615*8975f5c5SAndroid Build Coastguard Worker      # Required for library partitions. Without this all symbols just end up
2616*8975f5c5SAndroid Build Coastguard Worker      # in the base partition.
2617*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "-Wl,--gc-sections" ]
2618*8975f5c5SAndroid Build Coastguard Worker    }
2619*8975f5c5SAndroid Build Coastguard Worker  } else if (is_fuchsia) {
2620*8975f5c5SAndroid Build Coastguard Worker    # On Fuchsia, we optimize for size here to reduce the size of debug build
2621*8975f5c5SAndroid Build Coastguard Worker    # packages so they can be run in a KVM. See crbug.com/910243 for details.
2622*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-Og" ]
2623*8975f5c5SAndroid Build Coastguard Worker  } else {
2624*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-O0" ]
2625*8975f5c5SAndroid Build Coastguard Worker    ldflags = []
2626*8975f5c5SAndroid Build Coastguard Worker  }
2627*8975f5c5SAndroid Build Coastguard Worker}
2628*8975f5c5SAndroid Build Coastguard Worker
2629*8975f5c5SAndroid Build Coastguard Worker# Turns up the optimization level. Used to explicitly enable -O2 instead of
2630*8975f5c5SAndroid Build Coastguard Worker# -Os for select targets on platforms that use optimize_for_size. No-op
2631*8975f5c5SAndroid Build Coastguard Worker# elsewhere.
2632*8975f5c5SAndroid Build Coastguard Workerconfig("optimize_max") {
2633*8975f5c5SAndroid Build Coastguard Worker  if (is_nacl && is_nacl_irt) {
2634*8975f5c5SAndroid Build Coastguard Worker    # The NaCl IRT is a special case and always wants its own config.
2635*8975f5c5SAndroid Build Coastguard Worker    # Various components do:
2636*8975f5c5SAndroid Build Coastguard Worker    #   if (!is_debug) {
2637*8975f5c5SAndroid Build Coastguard Worker    #     configs -= [ "//build/config/compiler:default_optimization" ]
2638*8975f5c5SAndroid Build Coastguard Worker    #     configs += [ "//build/config/compiler:optimize_max" ]
2639*8975f5c5SAndroid Build Coastguard Worker    #   }
2640*8975f5c5SAndroid Build Coastguard Worker    # So this config has to have the selection logic just like
2641*8975f5c5SAndroid Build Coastguard Worker    # "default_optimization", below.
2642*8975f5c5SAndroid Build Coastguard Worker    configs = [ "//build/config/nacl:irt_optimize" ]
2643*8975f5c5SAndroid Build Coastguard Worker  } else {
2644*8975f5c5SAndroid Build Coastguard Worker    ldflags = common_optimize_on_ldflags
2645*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
2646*8975f5c5SAndroid Build Coastguard Worker      # Favor speed over size, /O2 must be before the common flags.
2647*8975f5c5SAndroid Build Coastguard Worker      # /O2 implies /Ot, /Oi, and /GF.
2648*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "/O2" ] + common_optimize_on_cflags
2649*8975f5c5SAndroid Build Coastguard Worker    } else if (optimize_for_fuzzing) {
2650*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-O1" ] + common_optimize_on_cflags
2651*8975f5c5SAndroid Build Coastguard Worker    } else {
2652*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-O2" ] + common_optimize_on_cflags
2653*8975f5c5SAndroid Build Coastguard Worker    }
2654*8975f5c5SAndroid Build Coastguard Worker    rustflags = [ "-Copt-level=3" ]
2655*8975f5c5SAndroid Build Coastguard Worker  }
2656*8975f5c5SAndroid Build Coastguard Worker}
2657*8975f5c5SAndroid Build Coastguard Worker
2658*8975f5c5SAndroid Build Coastguard Worker# This config can be used to override the default settings for per-component
2659*8975f5c5SAndroid Build Coastguard Worker# and whole-program optimization, optimizing the particular target for speed
2660*8975f5c5SAndroid Build Coastguard Worker# instead of code size. This config is exactly the same as "optimize_max"
2661*8975f5c5SAndroid Build Coastguard Worker# except that we use -O3 instead of -O2 on non-IRT platforms.
2662*8975f5c5SAndroid Build Coastguard Worker#
2663*8975f5c5SAndroid Build Coastguard Worker# TODO(crbug.com/41259697) - rework how all of these configs are related
2664*8975f5c5SAndroid Build Coastguard Worker# so that we don't need this disclaimer.
2665*8975f5c5SAndroid Build Coastguard Workerconfig("optimize_speed") {
2666*8975f5c5SAndroid Build Coastguard Worker  if (is_nacl && is_nacl_irt) {
2667*8975f5c5SAndroid Build Coastguard Worker    # The NaCl IRT is a special case and always wants its own config.
2668*8975f5c5SAndroid Build Coastguard Worker    # Various components do:
2669*8975f5c5SAndroid Build Coastguard Worker    #   if (!is_debug) {
2670*8975f5c5SAndroid Build Coastguard Worker    #     configs -= [ "//build/config/compiler:default_optimization" ]
2671*8975f5c5SAndroid Build Coastguard Worker    #     configs += [ "//build/config/compiler:optimize_max" ]
2672*8975f5c5SAndroid Build Coastguard Worker    #   }
2673*8975f5c5SAndroid Build Coastguard Worker    # So this config has to have the selection logic just like
2674*8975f5c5SAndroid Build Coastguard Worker    # "default_optimization", below.
2675*8975f5c5SAndroid Build Coastguard Worker    configs = [ "//build/config/nacl:irt_optimize" ]
2676*8975f5c5SAndroid Build Coastguard Worker  } else {
2677*8975f5c5SAndroid Build Coastguard Worker    ldflags = common_optimize_on_ldflags
2678*8975f5c5SAndroid Build Coastguard Worker    if (is_win) {
2679*8975f5c5SAndroid Build Coastguard Worker      # Favor speed over size, /O2 must be before the common flags.
2680*8975f5c5SAndroid Build Coastguard Worker      # /O2 implies /Ot, /Oi, and /GF.
2681*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "/O2" ] + common_optimize_on_cflags
2682*8975f5c5SAndroid Build Coastguard Worker      if (is_clang) {
2683*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "/clang:-O3" ]
2684*8975f5c5SAndroid Build Coastguard Worker      }
2685*8975f5c5SAndroid Build Coastguard Worker    } else if (optimize_for_fuzzing) {
2686*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-O1" ] + common_optimize_on_cflags
2687*8975f5c5SAndroid Build Coastguard Worker    } else {
2688*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "-O3" ] + common_optimize_on_cflags
2689*8975f5c5SAndroid Build Coastguard Worker    }
2690*8975f5c5SAndroid Build Coastguard Worker    rustflags = [ "-Copt-level=3" ]
2691*8975f5c5SAndroid Build Coastguard Worker  }
2692*8975f5c5SAndroid Build Coastguard Worker}
2693*8975f5c5SAndroid Build Coastguard Worker
2694*8975f5c5SAndroid Build Coastguard Workerconfig("optimize_fuzzing") {
2695*8975f5c5SAndroid Build Coastguard Worker  cflags = [ "-O1" ] + common_optimize_on_cflags
2696*8975f5c5SAndroid Build Coastguard Worker  rustflags = [ "-Copt-level=1" ]
2697*8975f5c5SAndroid Build Coastguard Worker  ldflags = common_optimize_on_ldflags
2698*8975f5c5SAndroid Build Coastguard Worker  visibility = [ ":default_optimization" ]
2699*8975f5c5SAndroid Build Coastguard Worker}
2700*8975f5c5SAndroid Build Coastguard Worker
2701*8975f5c5SAndroid Build Coastguard Worker# The default optimization applied to all targets. This will be equivalent to
2702*8975f5c5SAndroid Build Coastguard Worker# either "optimize" or "no_optimize", depending on the build flags.
2703*8975f5c5SAndroid Build Coastguard Workerconfig("default_optimization") {
2704*8975f5c5SAndroid Build Coastguard Worker  if (is_nacl && is_nacl_irt) {
2705*8975f5c5SAndroid Build Coastguard Worker    # The NaCl IRT is a special case and always wants its own config.
2706*8975f5c5SAndroid Build Coastguard Worker    # It gets optimized the same way regardless of the type of build.
2707*8975f5c5SAndroid Build Coastguard Worker    configs = [ "//build/config/nacl:irt_optimize" ]
2708*8975f5c5SAndroid Build Coastguard Worker  } else if (is_debug) {
2709*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":no_optimize" ]
2710*8975f5c5SAndroid Build Coastguard Worker  } else if (optimize_for_fuzzing) {
2711*8975f5c5SAndroid Build Coastguard Worker    assert(!is_win, "Fuzzing optimize level not supported on Windows")
2712*8975f5c5SAndroid Build Coastguard Worker
2713*8975f5c5SAndroid Build Coastguard Worker    # Coverage build is quite slow. Using "optimize_for_fuzzing" makes it even
2714*8975f5c5SAndroid Build Coastguard Worker    # slower as it uses "-O1" instead of "-O3". Prevent that from happening.
2715*8975f5c5SAndroid Build Coastguard Worker    assert(!use_clang_coverage,
2716*8975f5c5SAndroid Build Coastguard Worker           "optimize_for_fuzzing=true should not be used with " +
2717*8975f5c5SAndroid Build Coastguard Worker               "use_clang_coverage=true.")
2718*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":optimize_fuzzing" ]
2719*8975f5c5SAndroid Build Coastguard Worker  } else {
2720*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":optimize" ]
2721*8975f5c5SAndroid Build Coastguard Worker  }
2722*8975f5c5SAndroid Build Coastguard Worker}
2723*8975f5c5SAndroid Build Coastguard Worker
2724*8975f5c5SAndroid Build Coastguard Worker_clang_sample_profile = ""
2725*8975f5c5SAndroid Build Coastguard Workerif (is_clang && is_a_target_toolchain) {
2726*8975f5c5SAndroid Build Coastguard Worker  if (clang_sample_profile_path != "") {
2727*8975f5c5SAndroid Build Coastguard Worker    _clang_sample_profile = clang_sample_profile_path
2728*8975f5c5SAndroid Build Coastguard Worker  } else if (clang_use_default_sample_profile) {
2729*8975f5c5SAndroid Build Coastguard Worker    assert(build_with_chromium,
2730*8975f5c5SAndroid Build Coastguard Worker           "Our default profiles currently only apply to Chromium")
2731*8975f5c5SAndroid Build Coastguard Worker    assert(is_android || is_chromeos || is_castos,
2732*8975f5c5SAndroid Build Coastguard Worker           "The current platform has no default profile")
2733*8975f5c5SAndroid Build Coastguard Worker    if (is_android || is_castos) {
2734*8975f5c5SAndroid Build Coastguard Worker      _clang_sample_profile = "//chrome/android/profiles/afdo.prof"
2735*8975f5c5SAndroid Build Coastguard Worker    } else {
2736*8975f5c5SAndroid Build Coastguard Worker      assert(chromeos_afdo_platform == "atom" ||
2737*8975f5c5SAndroid Build Coastguard Worker                 chromeos_afdo_platform == "bigcore" ||
2738*8975f5c5SAndroid Build Coastguard Worker                 chromeos_afdo_platform == "arm",
2739*8975f5c5SAndroid Build Coastguard Worker             "Only 'atom', 'bigcore', and 'arm' are valid ChromeOS profiles.")
2740*8975f5c5SAndroid Build Coastguard Worker      _clang_sample_profile =
2741*8975f5c5SAndroid Build Coastguard Worker          "//chromeos/profiles/${chromeos_afdo_platform}.afdo.prof"
2742*8975f5c5SAndroid Build Coastguard Worker    }
2743*8975f5c5SAndroid Build Coastguard Worker  }
2744*8975f5c5SAndroid Build Coastguard Worker}
2745*8975f5c5SAndroid Build Coastguard Worker
2746*8975f5c5SAndroid Build Coastguard Worker# Clang offers a way to assert that AFDO profiles are accurate, which causes it
2747*8975f5c5SAndroid Build Coastguard Worker# to optimize functions not represented in a profile more aggressively for size.
2748*8975f5c5SAndroid Build Coastguard Worker# This config can be toggled in cases where shaving off binary size hurts
2749*8975f5c5SAndroid Build Coastguard Worker# performance too much.
2750*8975f5c5SAndroid Build Coastguard Workerconfig("afdo_optimize_size") {
2751*8975f5c5SAndroid Build Coastguard Worker  if (_clang_sample_profile != "" && sample_profile_is_accurate) {
2752*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-fprofile-sample-accurate" ]
2753*8975f5c5SAndroid Build Coastguard Worker  }
2754*8975f5c5SAndroid Build Coastguard Worker}
2755*8975f5c5SAndroid Build Coastguard Worker
2756*8975f5c5SAndroid Build Coastguard Worker# GCC and clang support a form of profile-guided optimization called AFDO.
2757*8975f5c5SAndroid Build Coastguard Worker# There are some targeted places that AFDO regresses, so we provide a separate
2758*8975f5c5SAndroid Build Coastguard Worker# config to allow AFDO to be disabled per-target.
2759*8975f5c5SAndroid Build Coastguard Workerconfig("afdo") {
2760*8975f5c5SAndroid Build Coastguard Worker  if (is_clang) {
2761*8975f5c5SAndroid Build Coastguard Worker    cflags = []
2762*8975f5c5SAndroid Build Coastguard Worker    if (clang_emit_debug_info_for_profiling) {
2763*8975f5c5SAndroid Build Coastguard Worker      # Add the following flags to generate debug info for profiling.
2764*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gline-tables-only" ]
2765*8975f5c5SAndroid Build Coastguard Worker      if (!is_nacl) {
2766*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-fdebug-info-for-profiling" ]
2767*8975f5c5SAndroid Build Coastguard Worker      }
2768*8975f5c5SAndroid Build Coastguard Worker    }
2769*8975f5c5SAndroid Build Coastguard Worker    if (_clang_sample_profile != "") {
2770*8975f5c5SAndroid Build Coastguard Worker      assert(chrome_pgo_phase == 0, "AFDO can't be used in PGO builds")
2771*8975f5c5SAndroid Build Coastguard Worker      rebased_clang_sample_profile =
2772*8975f5c5SAndroid Build Coastguard Worker          rebase_path(_clang_sample_profile, root_build_dir)
2773*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-fprofile-sample-use=${rebased_clang_sample_profile}" ]
2774*8975f5c5SAndroid Build Coastguard Worker      if (use_profi) {
2775*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-fsample-profile-use-profi" ]
2776*8975f5c5SAndroid Build Coastguard Worker      }
2777*8975f5c5SAndroid Build Coastguard Worker
2778*8975f5c5SAndroid Build Coastguard Worker      # crbug.com/1459429: ARM builds see failures due to -Wbackend-plugin.
2779*8975f5c5SAndroid Build Coastguard Worker      # These seem to be false positives - the complaints are about functions
2780*8975f5c5SAndroid Build Coastguard Worker      # marked with `__nodebug__` not having associated debuginfo. In the case
2781*8975f5c5SAndroid Build Coastguard Worker      # where this was observed, the `__nodebug__` function was also marked
2782*8975f5c5SAndroid Build Coastguard Worker      # `__always_inline__` and had no branches, so AFDO info is likely useless
2783*8975f5c5SAndroid Build Coastguard Worker      # there.
2784*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-Wno-backend-plugin" ]
2785*8975f5c5SAndroid Build Coastguard Worker      inputs = [ _clang_sample_profile ]
2786*8975f5c5SAndroid Build Coastguard Worker    }
2787*8975f5c5SAndroid Build Coastguard Worker  } else if (auto_profile_path != "" && is_a_target_toolchain) {
2788*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-fauto-profile=${auto_profile_path}" ]
2789*8975f5c5SAndroid Build Coastguard Worker    inputs = [ auto_profile_path ]
2790*8975f5c5SAndroid Build Coastguard Worker  }
2791*8975f5c5SAndroid Build Coastguard Worker}
2792*8975f5c5SAndroid Build Coastguard Worker
2793*8975f5c5SAndroid Build Coastguard Worker# Symbols ----------------------------------------------------------------------
2794*8975f5c5SAndroid Build Coastguard Worker
2795*8975f5c5SAndroid Build Coastguard Worker# The BUILDCONFIG file sets the "default_symbols" config on targets by
2796*8975f5c5SAndroid Build Coastguard Worker# default. It will be equivalent to one the three specific symbol levels.
2797*8975f5c5SAndroid Build Coastguard Worker#
2798*8975f5c5SAndroid Build Coastguard Worker# You can override the symbol level on a per-target basis by removing the
2799*8975f5c5SAndroid Build Coastguard Worker# default config and then adding the named one you want:
2800*8975f5c5SAndroid Build Coastguard Worker#
2801*8975f5c5SAndroid Build Coastguard Worker#   configs -= [ "//build/config/compiler:default_symbols" ]
2802*8975f5c5SAndroid Build Coastguard Worker#   configs += [ "//build/config/compiler:symbols" ]
2803*8975f5c5SAndroid Build Coastguard Worker
2804*8975f5c5SAndroid Build Coastguard Worker# A helper config that all configs passing /DEBUG to the linker should
2805*8975f5c5SAndroid Build Coastguard Worker# include as sub-config.
2806*8975f5c5SAndroid Build Coastguard Workerconfig("win_pdbaltpath") {
2807*8975f5c5SAndroid Build Coastguard Worker  visibility = [
2808*8975f5c5SAndroid Build Coastguard Worker    ":minimal_symbols",
2809*8975f5c5SAndroid Build Coastguard Worker    ":symbols",
2810*8975f5c5SAndroid Build Coastguard Worker  ]
2811*8975f5c5SAndroid Build Coastguard Worker
2812*8975f5c5SAndroid Build Coastguard Worker  # /DEBUG causes the linker to generate a pdb file, and to write the absolute
2813*8975f5c5SAndroid Build Coastguard Worker  # path to it in the executable file it generates.  This flag turns that
2814*8975f5c5SAndroid Build Coastguard Worker  # absolute path into just the basename of the pdb file, which helps with
2815*8975f5c5SAndroid Build Coastguard Worker  # build reproducibility. Debuggers look for pdb files next to executables,
2816*8975f5c5SAndroid Build Coastguard Worker  # so there's minimal downside to always using this. However, post-mortem
2817*8975f5c5SAndroid Build Coastguard Worker  # debugging of Chromium crash dumps and ETW tracing can be complicated by this
2818*8975f5c5SAndroid Build Coastguard Worker  # switch so an option to omit it is important.
2819*8975f5c5SAndroid Build Coastguard Worker  if (!use_full_pdb_paths) {
2820*8975f5c5SAndroid Build Coastguard Worker    ldflags = [ "/pdbaltpath:%_PDB%" ]
2821*8975f5c5SAndroid Build Coastguard Worker  }
2822*8975f5c5SAndroid Build Coastguard Worker}
2823*8975f5c5SAndroid Build Coastguard Worker
2824*8975f5c5SAndroid Build Coastguard Worker# Full symbols.
2825*8975f5c5SAndroid Build Coastguard Workerconfig("symbols") {
2826*8975f5c5SAndroid Build Coastguard Worker  rustflags = []
2827*8975f5c5SAndroid Build Coastguard Worker  configs = []
2828*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2829*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
2830*8975f5c5SAndroid Build Coastguard Worker      cflags = [
2831*8975f5c5SAndroid Build Coastguard Worker        # Debug information in the .obj files.
2832*8975f5c5SAndroid Build Coastguard Worker        "/Z7",
2833*8975f5c5SAndroid Build Coastguard Worker
2834*8975f5c5SAndroid Build Coastguard Worker        # Disable putting the compiler command line into the debug info to
2835*8975f5c5SAndroid Build Coastguard Worker        # prevent some types of non-determinism.
2836*8975f5c5SAndroid Build Coastguard Worker        "-gno-codeview-command-line",
2837*8975f5c5SAndroid Build Coastguard Worker      ]
2838*8975f5c5SAndroid Build Coastguard Worker    } else {
2839*8975f5c5SAndroid Build Coastguard Worker      cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
2840*8975f5c5SAndroid Build Coastguard Worker    }
2841*8975f5c5SAndroid Build Coastguard Worker
2842*8975f5c5SAndroid Build Coastguard Worker    if (is_clang && use_lld && use_ghash) {
2843*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gcodeview-ghash" ]
2844*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/DEBUG:GHASH" ]
2845*8975f5c5SAndroid Build Coastguard Worker    } else {
2846*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/DEBUG" ]
2847*8975f5c5SAndroid Build Coastguard Worker    }
2848*8975f5c5SAndroid Build Coastguard Worker
2849*8975f5c5SAndroid Build Coastguard Worker    # All configs using /DEBUG should include this:
2850*8975f5c5SAndroid Build Coastguard Worker    configs += [ ":win_pdbaltpath" ]
2851*8975f5c5SAndroid Build Coastguard Worker  } else {
2852*8975f5c5SAndroid Build Coastguard Worker    cflags = []
2853*8975f5c5SAndroid Build Coastguard Worker    if (is_mac && enable_dsyms) {
2854*8975f5c5SAndroid Build Coastguard Worker      # If generating dSYMs, specify -fno-standalone-debug. This was
2855*8975f5c5SAndroid Build Coastguard Worker      # originally specified for https://crbug.com/479841 because dsymutil
2856*8975f5c5SAndroid Build Coastguard Worker      # could not handle a 4GB dSYM file. But dsymutil from Xcodes prior to
2857*8975f5c5SAndroid Build Coastguard Worker      # version 7 also produces debug data that is incompatible with Breakpad
2858*8975f5c5SAndroid Build Coastguard Worker      # dump_syms, so this is still required (https://crbug.com/622406).
2859*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-fno-standalone-debug" ]
2860*8975f5c5SAndroid Build Coastguard Worker    }
2861*8975f5c5SAndroid Build Coastguard Worker
2862*8975f5c5SAndroid Build Coastguard Worker    # On aix -gdwarf causes linker failures due to thread_local variables.
2863*8975f5c5SAndroid Build Coastguard Worker    # On zos -gdwarf causes INSUFFICIENT ABOVE THE LINE STORAGE WAS AVAILABLE.
2864*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl && current_os != "aix" && current_os != "zos") {
2865*8975f5c5SAndroid Build Coastguard Worker      if (use_dwarf5) {
2866*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-gdwarf-5" ]
2867*8975f5c5SAndroid Build Coastguard Worker        rustflags += [ "-Zdwarf-version=5" ]
2868*8975f5c5SAndroid Build Coastguard Worker      } else {
2869*8975f5c5SAndroid Build Coastguard Worker        # Recent clang versions default to DWARF5 on Linux, and Android is about
2870*8975f5c5SAndroid Build Coastguard Worker        # to switch. TODO: Adopt that in controlled way. For now, keep DWARF4.
2871*8975f5c5SAndroid Build Coastguard Worker        # Apple platforms still default to 4 in clang, so they don't need the
2872*8975f5c5SAndroid Build Coastguard Worker        # cflags.
2873*8975f5c5SAndroid Build Coastguard Worker        if (!is_apple) {
2874*8975f5c5SAndroid Build Coastguard Worker          cflags += [ "-gdwarf-4" ]
2875*8975f5c5SAndroid Build Coastguard Worker        }
2876*8975f5c5SAndroid Build Coastguard Worker
2877*8975f5c5SAndroid Build Coastguard Worker        # On Apple, rustc defaults to DWARF2 so it needs to be told how to
2878*8975f5c5SAndroid Build Coastguard Worker        # match clang.
2879*8975f5c5SAndroid Build Coastguard Worker        rustflags += [ "-Zdwarf-version=4" ]
2880*8975f5c5SAndroid Build Coastguard Worker      }
2881*8975f5c5SAndroid Build Coastguard Worker    }
2882*8975f5c5SAndroid Build Coastguard Worker
2883*8975f5c5SAndroid Build Coastguard Worker    # The gcc-based nacl compilers don't support -fdebug-compilation-dir (see
2884*8975f5c5SAndroid Build Coastguard Worker    # elsewhere in this file), so they can't have build-dir-independent output.
2885*8975f5c5SAndroid Build Coastguard Worker    # Moreover pnacl does not support newer flags such as -fdebug-prefix-map
2886*8975f5c5SAndroid Build Coastguard Worker    # Disable symbols for nacl object files to get deterministic,
2887*8975f5c5SAndroid Build Coastguard Worker    # build-directory-independent output.
2888*8975f5c5SAndroid Build Coastguard Worker    # Keeping -g2 for saigo as it's the only toolchain whose artifacts that are
2889*8975f5c5SAndroid Build Coastguard Worker    # part of chromium release (other nacl toolchains are used only for tests).
2890*8975f5c5SAndroid Build Coastguard Worker    if ((!is_nacl || is_nacl_saigo) && current_os != "zos") {
2891*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-g2" ]
2892*8975f5c5SAndroid Build Coastguard Worker    }
2893*8975f5c5SAndroid Build Coastguard Worker
2894*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl && is_clang && !is_tsan && !is_asan) {
2895*8975f5c5SAndroid Build Coastguard Worker      # gcc generates dwarf-aranges by default on -g1 and -g2. On clang it has
2896*8975f5c5SAndroid Build Coastguard Worker      # to be manually enabled.
2897*8975f5c5SAndroid Build Coastguard Worker      #
2898*8975f5c5SAndroid Build Coastguard Worker      # It is skipped in tsan and asan because enabling it causes some
2899*8975f5c5SAndroid Build Coastguard Worker      # formatting changes in the output which would require fixing bunches
2900*8975f5c5SAndroid Build Coastguard Worker      # of expectation regexps.
2901*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gdwarf-aranges" ]
2902*8975f5c5SAndroid Build Coastguard Worker    }
2903*8975f5c5SAndroid Build Coastguard Worker
2904*8975f5c5SAndroid Build Coastguard Worker    if (is_apple) {
2905*8975f5c5SAndroid Build Coastguard Worker      swiftflags = [ "-g" ]
2906*8975f5c5SAndroid Build Coastguard Worker    }
2907*8975f5c5SAndroid Build Coastguard Worker
2908*8975f5c5SAndroid Build Coastguard Worker    if (use_debug_fission) {
2909*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gsplit-dwarf" ]
2910*8975f5c5SAndroid Build Coastguard Worker    }
2911*8975f5c5SAndroid Build Coastguard Worker    asmflags = cflags
2912*8975f5c5SAndroid Build Coastguard Worker    ldflags = []
2913*8975f5c5SAndroid Build Coastguard Worker
2914*8975f5c5SAndroid Build Coastguard Worker    # Split debug info with all thinlto builds except nacl and apple.
2915*8975f5c5SAndroid Build Coastguard Worker    # thinlto requires -gsplit-dwarf in ldflags.
2916*8975f5c5SAndroid Build Coastguard Worker    if (use_debug_fission && use_thin_lto && !is_nacl && !is_apple) {
2917*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-gsplit-dwarf" ]
2918*8975f5c5SAndroid Build Coastguard Worker    }
2919*8975f5c5SAndroid Build Coastguard Worker
2920*8975f5c5SAndroid Build Coastguard Worker    # TODO(thakis): Figure out if there's a way to make this go for 32-bit,
2921*8975f5c5SAndroid Build Coastguard Worker    # currently we get "warning:
2922*8975f5c5SAndroid Build Coastguard Worker    # obj/native_client/src/trusted/service_runtime/sel_asm/nacl_switch_32.o:
2923*8975f5c5SAndroid Build Coastguard Worker    # DWARF info may be corrupt; offsets in a range list entry are in different
2924*8975f5c5SAndroid Build Coastguard Worker    # sections" there.  Maybe just a bug in nacl_switch_32.S.
2925*8975f5c5SAndroid Build Coastguard Worker    _enable_gdb_index =
2926*8975f5c5SAndroid Build Coastguard Worker        symbol_level == 2 && !is_apple && !is_nacl && current_cpu != "x86" &&
2927*8975f5c5SAndroid Build Coastguard Worker        current_os != "zos" && use_lld &&
2928*8975f5c5SAndroid Build Coastguard Worker        # Disable on non-fission 32-bit Android because it pushes
2929*8975f5c5SAndroid Build Coastguard Worker        # libcomponents_unittests over the 4gb size limit.
2930*8975f5c5SAndroid Build Coastguard Worker        !(is_android && !use_debug_fission && current_cpu != "x64" &&
2931*8975f5c5SAndroid Build Coastguard Worker          current_cpu != "arm64")
2932*8975f5c5SAndroid Build Coastguard Worker    if (_enable_gdb_index) {
2933*8975f5c5SAndroid Build Coastguard Worker      if (is_clang) {
2934*8975f5c5SAndroid Build Coastguard Worker        # This flag enables the GNU-format pubnames and pubtypes sections,
2935*8975f5c5SAndroid Build Coastguard Worker        # which lld needs in order to generate a correct GDB index.
2936*8975f5c5SAndroid Build Coastguard Worker        # TODO(pcc): Try to make lld understand non-GNU-format pubnames
2937*8975f5c5SAndroid Build Coastguard Worker        # sections (llvm.org/PR34820).
2938*8975f5c5SAndroid Build Coastguard Worker        cflags += [ "-ggnu-pubnames" ]
2939*8975f5c5SAndroid Build Coastguard Worker      }
2940*8975f5c5SAndroid Build Coastguard Worker      ldflags += [ "-Wl,--gdb-index" ]
2941*8975f5c5SAndroid Build Coastguard Worker    }
2942*8975f5c5SAndroid Build Coastguard Worker  }
2943*8975f5c5SAndroid Build Coastguard Worker
2944*8975f5c5SAndroid Build Coastguard Worker  # Compress debug on 32-bit ARM to stay under 4GB file size limit.
2945*8975f5c5SAndroid Build Coastguard Worker  # https://b/243982712, https://crbug.com/1354616, https://crbug.com/334073642
2946*8975f5c5SAndroid Build Coastguard Worker  if (symbol_level == 2 && !use_debug_fission && !is_nacl && !is_win &&
2947*8975f5c5SAndroid Build Coastguard Worker      (current_cpu == "arm" || current_cpu == "x86")) {
2948*8975f5c5SAndroid Build Coastguard Worker    configs += [ "//build/config:compress_debug_sections" ]
2949*8975f5c5SAndroid Build Coastguard Worker  }
2950*8975f5c5SAndroid Build Coastguard Worker
2951*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && !is_nacl && is_win && !is_component_build) {
2952*8975f5c5SAndroid Build Coastguard Worker    # Remove unreferenced methods to reduce type info in symbols.
2953*8975f5c5SAndroid Build Coastguard Worker    # See: https://github.com/llvm/llvm-project/pull/87018.
2954*8975f5c5SAndroid Build Coastguard Worker    # The downside with this flag is precisely that: Unreferenced methods get
2955*8975f5c5SAndroid Build Coastguard Worker    # removed, so that methods only to be used from within a debugger become
2956*8975f5c5SAndroid Build Coastguard Worker    # unavailable. Therefore, only do this for Windows, which seems to be the
2957*8975f5c5SAndroid Build Coastguard Worker    # only platform that needs this, due to size limitations in PDF files.
2958*8975f5c5SAndroid Build Coastguard Worker    # Additionally, this limitation is only likely to be hit in non-component
2959*8975f5c5SAndroid Build Coastguard Worker    # builds, so only do it then.
2960*8975f5c5SAndroid Build Coastguard Worker    # See crbug.com/338094922
2961*8975f5c5SAndroid Build Coastguard Worker    cflags += [ "-gomit-unreferenced-methods" ]
2962*8975f5c5SAndroid Build Coastguard Worker  }
2963*8975f5c5SAndroid Build Coastguard Worker
2964*8975f5c5SAndroid Build Coastguard Worker  if (is_clang && (!is_nacl || is_nacl_saigo)) {
2965*8975f5c5SAndroid Build Coastguard Worker    if (is_apple) {
2966*8975f5c5SAndroid Build Coastguard Worker      # TODO(crbug.com/40117949): Investigate missing debug info on mac.
2967*8975f5c5SAndroid Build Coastguard Worker      # Make sure we don't use constructor homing on mac.
2968*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2969*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
2970*8975f5c5SAndroid Build Coastguard Worker        "-debug-info-kind=limited",
2971*8975f5c5SAndroid Build Coastguard Worker      ]
2972*8975f5c5SAndroid Build Coastguard Worker    } else {
2973*8975f5c5SAndroid Build Coastguard Worker      # Use constructor homing for debug info. This option reduces debug info
2974*8975f5c5SAndroid Build Coastguard Worker      # by emitting class type info only when constructors are emitted.
2975*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2976*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
2977*8975f5c5SAndroid Build Coastguard Worker        "-fuse-ctor-homing",
2978*8975f5c5SAndroid Build Coastguard Worker      ]
2979*8975f5c5SAndroid Build Coastguard Worker    }
2980*8975f5c5SAndroid Build Coastguard Worker  }
2981*8975f5c5SAndroid Build Coastguard Worker  rustflags += [ "-g" ]
2982*8975f5c5SAndroid Build Coastguard Worker}
2983*8975f5c5SAndroid Build Coastguard Worker
2984*8975f5c5SAndroid Build Coastguard Worker# Minimal symbols.
2985*8975f5c5SAndroid Build Coastguard Worker# This config guarantees to hold symbol for stack trace which are shown to user
2986*8975f5c5SAndroid Build Coastguard Worker# when crash happens in unittests running on buildbot.
2987*8975f5c5SAndroid Build Coastguard Workerconfig("minimal_symbols") {
2988*8975f5c5SAndroid Build Coastguard Worker  rustflags = []
2989*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
2990*8975f5c5SAndroid Build Coastguard Worker    # Functions, files, and line tables only.
2991*8975f5c5SAndroid Build Coastguard Worker    cflags = []
2992*8975f5c5SAndroid Build Coastguard Worker
2993*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
2994*8975f5c5SAndroid Build Coastguard Worker      cflags += [
2995*8975f5c5SAndroid Build Coastguard Worker        # Disable putting the compiler command line into the debug info to
2996*8975f5c5SAndroid Build Coastguard Worker        # prevent some types of non-determinism.
2997*8975f5c5SAndroid Build Coastguard Worker        "-gno-codeview-command-line",
2998*8975f5c5SAndroid Build Coastguard Worker      ]
2999*8975f5c5SAndroid Build Coastguard Worker    }
3000*8975f5c5SAndroid Build Coastguard Worker    if (is_clang && use_lld && use_ghash) {
3001*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gcodeview-ghash" ]
3002*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/DEBUG:GHASH" ]
3003*8975f5c5SAndroid Build Coastguard Worker    } else {
3004*8975f5c5SAndroid Build Coastguard Worker      ldflags = [ "/DEBUG" ]
3005*8975f5c5SAndroid Build Coastguard Worker    }
3006*8975f5c5SAndroid Build Coastguard Worker
3007*8975f5c5SAndroid Build Coastguard Worker    # All configs using /DEBUG should include this:
3008*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":win_pdbaltpath" ]
3009*8975f5c5SAndroid Build Coastguard Worker
3010*8975f5c5SAndroid Build Coastguard Worker    # Enable line tables for clang. MSVC doesn't have an equivalent option.
3011*8975f5c5SAndroid Build Coastguard Worker    if (is_clang) {
3012*8975f5c5SAndroid Build Coastguard Worker      # -gline-tables-only is the same as -g1, but clang-cl only exposes the
3013*8975f5c5SAndroid Build Coastguard Worker      # former.
3014*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gline-tables-only" ]
3015*8975f5c5SAndroid Build Coastguard Worker    }
3016*8975f5c5SAndroid Build Coastguard Worker  } else {
3017*8975f5c5SAndroid Build Coastguard Worker    cflags = []
3018*8975f5c5SAndroid Build Coastguard Worker    if (is_mac && !use_dwarf5) {
3019*8975f5c5SAndroid Build Coastguard Worker      # clang defaults to DWARF2 on macOS unless mac_deployment_target is
3020*8975f5c5SAndroid Build Coastguard Worker      # at least 10.11.
3021*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): Remove this once mac_deployment_target is 10.11.
3022*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gdwarf-4" ]
3023*8975f5c5SAndroid Build Coastguard Worker      rustflags += [ "-Zdwarf-version=4" ]
3024*8975f5c5SAndroid Build Coastguard Worker    } else if (!use_dwarf5 && !is_nacl && current_os != "aix") {
3025*8975f5c5SAndroid Build Coastguard Worker      # On aix -gdwarf causes linker failures due to thread_local variables.
3026*8975f5c5SAndroid Build Coastguard Worker      # Recent clang versions default to DWARF5 on Linux, and Android is about
3027*8975f5c5SAndroid Build Coastguard Worker      # to switch. TODO: Adopt that in controlled way.
3028*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gdwarf-4" ]
3029*8975f5c5SAndroid Build Coastguard Worker      rustflags += [ "-Zdwarf-version=4" ]
3030*8975f5c5SAndroid Build Coastguard Worker    }
3031*8975f5c5SAndroid Build Coastguard Worker
3032*8975f5c5SAndroid Build Coastguard Worker    if (use_dwarf5 && !is_nacl) {
3033*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gdwarf-5" ]
3034*8975f5c5SAndroid Build Coastguard Worker      rustflags += [ "-Zdwarf-version=5" ]
3035*8975f5c5SAndroid Build Coastguard Worker    }
3036*8975f5c5SAndroid Build Coastguard Worker
3037*8975f5c5SAndroid Build Coastguard Worker    # The gcc-based nacl compilers don't support -fdebug-compilation-dir (see
3038*8975f5c5SAndroid Build Coastguard Worker    # elsewhere in this file), so they can't have build-dir-independent output.
3039*8975f5c5SAndroid Build Coastguard Worker    # Moreover pnacl does not support newer flags such as -fdebug-prefix-map
3040*8975f5c5SAndroid Build Coastguard Worker    # Disable symbols for nacl object files to get deterministic,
3041*8975f5c5SAndroid Build Coastguard Worker    # build-directory-independent output.
3042*8975f5c5SAndroid Build Coastguard Worker    # Keeping -g1 for saigo as it's the only toolchain whose artifacts that are
3043*8975f5c5SAndroid Build Coastguard Worker    # part of chromium release (other nacl toolchains are used only for tests).
3044*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl || is_nacl_saigo) {
3045*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-g1" ]
3046*8975f5c5SAndroid Build Coastguard Worker    }
3047*8975f5c5SAndroid Build Coastguard Worker
3048*8975f5c5SAndroid Build Coastguard Worker    if (!is_nacl && is_clang && !is_tsan && !is_asan && current_os != "zos") {
3049*8975f5c5SAndroid Build Coastguard Worker      # See comment for -gdwarf-aranges in config("symbols").
3050*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-gdwarf-aranges" ]
3051*8975f5c5SAndroid Build Coastguard Worker    }
3052*8975f5c5SAndroid Build Coastguard Worker
3053*8975f5c5SAndroid Build Coastguard Worker    ldflags = []
3054*8975f5c5SAndroid Build Coastguard Worker    if (is_android && is_clang) {
3055*8975f5c5SAndroid Build Coastguard Worker      # Android defaults to symbol_level=1 builds, but clang, unlike gcc,
3056*8975f5c5SAndroid Build Coastguard Worker      # doesn't emit DW_AT_linkage_name in -g1 builds.
3057*8975f5c5SAndroid Build Coastguard Worker      # -fdebug-info-for-profiling enables that (and a bunch of other things we
3058*8975f5c5SAndroid Build Coastguard Worker      # don't need), so that we get qualified names in stacks.
3059*8975f5c5SAndroid Build Coastguard Worker      # TODO(thakis): Consider making clang emit DW_AT_linkage_name in -g1 mode;
3060*8975f5c5SAndroid Build Coastguard Worker      #               failing that consider doing this on non-Android too.
3061*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-fdebug-info-for-profiling" ]
3062*8975f5c5SAndroid Build Coastguard Worker    }
3063*8975f5c5SAndroid Build Coastguard Worker
3064*8975f5c5SAndroid Build Coastguard Worker    asmflags = cflags
3065*8975f5c5SAndroid Build Coastguard Worker  }
3066*8975f5c5SAndroid Build Coastguard Worker  rustflags += [ "-Cdebuginfo=1" ]
3067*8975f5c5SAndroid Build Coastguard Worker}
3068*8975f5c5SAndroid Build Coastguard Worker
3069*8975f5c5SAndroid Build Coastguard Worker# This configuration contains function names only. That is, the compiler is
3070*8975f5c5SAndroid Build Coastguard Worker# told to not generate debug information and the linker then just puts function
3071*8975f5c5SAndroid Build Coastguard Worker# names in the final debug information.
3072*8975f5c5SAndroid Build Coastguard Workerconfig("no_symbols") {
3073*8975f5c5SAndroid Build Coastguard Worker  if (is_win) {
3074*8975f5c5SAndroid Build Coastguard Worker    ldflags = [ "/DEBUG" ]
3075*8975f5c5SAndroid Build Coastguard Worker
3076*8975f5c5SAndroid Build Coastguard Worker    # All configs using /DEBUG should include this:
3077*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":win_pdbaltpath" ]
3078*8975f5c5SAndroid Build Coastguard Worker  } else {
3079*8975f5c5SAndroid Build Coastguard Worker    cflags = [ "-g0" ]
3080*8975f5c5SAndroid Build Coastguard Worker    asmflags = cflags
3081*8975f5c5SAndroid Build Coastguard Worker  }
3082*8975f5c5SAndroid Build Coastguard Worker}
3083*8975f5c5SAndroid Build Coastguard Worker
3084*8975f5c5SAndroid Build Coastguard Worker# Default symbols.
3085*8975f5c5SAndroid Build Coastguard Workerconfig("default_symbols") {
3086*8975f5c5SAndroid Build Coastguard Worker  if (symbol_level == 0) {
3087*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":no_symbols" ]
3088*8975f5c5SAndroid Build Coastguard Worker  } else if (symbol_level == 1) {
3089*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":minimal_symbols" ]
3090*8975f5c5SAndroid Build Coastguard Worker  } else if (symbol_level == 2) {
3091*8975f5c5SAndroid Build Coastguard Worker    configs = [ ":symbols" ]
3092*8975f5c5SAndroid Build Coastguard Worker  } else {
3093*8975f5c5SAndroid Build Coastguard Worker    assert(false)
3094*8975f5c5SAndroid Build Coastguard Worker  }
3095*8975f5c5SAndroid Build Coastguard Worker
3096*8975f5c5SAndroid Build Coastguard Worker  # This config is removed by base unittests apk.
3097*8975f5c5SAndroid Build Coastguard Worker  if (is_android && is_clang && strip_debug_info) {
3098*8975f5c5SAndroid Build Coastguard Worker    configs += [ ":strip_debug" ]
3099*8975f5c5SAndroid Build Coastguard Worker  }
3100*8975f5c5SAndroid Build Coastguard Worker}
3101*8975f5c5SAndroid Build Coastguard Worker
3102*8975f5c5SAndroid Build Coastguard Workerconfig("strip_debug") {
3103*8975f5c5SAndroid Build Coastguard Worker  if (!defined(ldflags)) {
3104*8975f5c5SAndroid Build Coastguard Worker    ldflags = []
3105*8975f5c5SAndroid Build Coastguard Worker  }
3106*8975f5c5SAndroid Build Coastguard Worker  ldflags += [ "-Wl,--strip-debug" ]
3107*8975f5c5SAndroid Build Coastguard Worker}
3108*8975f5c5SAndroid Build Coastguard Worker
3109*8975f5c5SAndroid Build Coastguard Workerif (is_apple) {
3110*8975f5c5SAndroid Build Coastguard Worker  # On macOS and iOS, this enables support for ARC (automatic reference
3111*8975f5c5SAndroid Build Coastguard Worker  # counting). See http://clang.llvm.org/docs/AutomaticReferenceCounting.html.
3112*8975f5c5SAndroid Build Coastguard Worker  #
3113*8975f5c5SAndroid Build Coastguard Worker  # -fobjc-arc enables ARC overall.
3114*8975f5c5SAndroid Build Coastguard Worker  #
3115*8975f5c5SAndroid Build Coastguard Worker  # ARC does not add exception handlers to pure Objective-C code, but does add
3116*8975f5c5SAndroid Build Coastguard Worker  # them to Objective-C++ code with the rationale that C++ pervasively adds them
3117*8975f5c5SAndroid Build Coastguard Worker  # in for exception safety. However, exceptions are banned in Chromium code for
3118*8975f5c5SAndroid Build Coastguard Worker  # C++ and exceptions in Objective-C code are intended to be fatal, so
3119*8975f5c5SAndroid Build Coastguard Worker  # -fno-objc-arc-exceptions is specified to disable these unwanted exception
3120*8975f5c5SAndroid Build Coastguard Worker  # handlers.
3121*8975f5c5SAndroid Build Coastguard Worker  config("enable_arc") {
3122*8975f5c5SAndroid Build Coastguard Worker    common_flags = [
3123*8975f5c5SAndroid Build Coastguard Worker      "-fobjc-arc",
3124*8975f5c5SAndroid Build Coastguard Worker      "-fno-objc-arc-exceptions",
3125*8975f5c5SAndroid Build Coastguard Worker    ]
3126*8975f5c5SAndroid Build Coastguard Worker    cflags_objc = common_flags
3127*8975f5c5SAndroid Build Coastguard Worker    cflags_objcc = common_flags
3128*8975f5c5SAndroid Build Coastguard Worker  }
3129*8975f5c5SAndroid Build Coastguard Worker}
3130*8975f5c5SAndroid Build Coastguard Worker
3131*8975f5c5SAndroid Build Coastguard Workerif (is_android) {
3132*8975f5c5SAndroid Build Coastguard Worker  # Use orderfile for linking Chrome on Android.
3133*8975f5c5SAndroid Build Coastguard Worker  # This config enables using an orderfile for linking in LLD.
3134*8975f5c5SAndroid Build Coastguard Worker  config("chrome_orderfile_config") {
3135*8975f5c5SAndroid Build Coastguard Worker    # Don't try to use an orderfile with call graph sorting, except on Android,
3136*8975f5c5SAndroid Build Coastguard Worker    # where we care about memory used by code, so we still want to mandate
3137*8975f5c5SAndroid Build Coastguard Worker    # ordering.
3138*8975f5c5SAndroid Build Coastguard Worker    if (chrome_orderfile_path != "") {
3139*8975f5c5SAndroid Build Coastguard Worker      assert(use_lld)
3140*8975f5c5SAndroid Build Coastguard Worker      _rebased_orderfile = rebase_path(chrome_orderfile_path, root_build_dir)
3141*8975f5c5SAndroid Build Coastguard Worker      ldflags = [
3142*8975f5c5SAndroid Build Coastguard Worker        "-Wl,--symbol-ordering-file",
3143*8975f5c5SAndroid Build Coastguard Worker        "-Wl,$_rebased_orderfile",
3144*8975f5c5SAndroid Build Coastguard Worker        "-Wl,--no-warn-symbol-ordering",
3145*8975f5c5SAndroid Build Coastguard Worker      ]
3146*8975f5c5SAndroid Build Coastguard Worker      inputs = [ chrome_orderfile_path ]
3147*8975f5c5SAndroid Build Coastguard Worker    }
3148*8975f5c5SAndroid Build Coastguard Worker  }
3149*8975f5c5SAndroid Build Coastguard Worker}
3150*8975f5c5SAndroid Build Coastguard Worker
3151*8975f5c5SAndroid Build Coastguard Worker# Initialize all variables on the stack if needed.
3152*8975f5c5SAndroid Build Coastguard Workerconfig("default_init_stack_vars") {
3153*8975f5c5SAndroid Build Coastguard Worker  cflags = []
3154*8975f5c5SAndroid Build Coastguard Worker  if (init_stack_vars && is_clang && !is_nacl && !using_sanitizer) {
3155*8975f5c5SAndroid Build Coastguard Worker    if (init_stack_vars_zero) {
3156*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-ftrivial-auto-var-init=zero" ]
3157*8975f5c5SAndroid Build Coastguard Worker    } else {
3158*8975f5c5SAndroid Build Coastguard Worker      cflags += [ "-ftrivial-auto-var-init=pattern" ]
3159*8975f5c5SAndroid Build Coastguard Worker    }
3160*8975f5c5SAndroid Build Coastguard Worker  }
3161*8975f5c5SAndroid Build Coastguard Worker}
3162*8975f5c5SAndroid Build Coastguard Worker
3163*8975f5c5SAndroid Build Coastguard Workerbuildflag_header("compiler_buildflags") {
3164*8975f5c5SAndroid Build Coastguard Worker  header = "compiler_buildflags.h"
3165*8975f5c5SAndroid Build Coastguard Worker
3166*8975f5c5SAndroid Build Coastguard Worker  flags = [
3167*8975f5c5SAndroid Build Coastguard Worker    "CLANG_PGO=$chrome_pgo_phase",
3168*8975f5c5SAndroid Build Coastguard Worker    "SYMBOL_LEVEL=$symbol_level",
3169*8975f5c5SAndroid Build Coastguard Worker  ]
3170*8975f5c5SAndroid Build Coastguard Worker}
3171*8975f5c5SAndroid Build Coastguard Worker
3172*8975f5c5SAndroid Build Coastguard Workerconfig("cet_shadow_stack") {
3173*8975f5c5SAndroid Build Coastguard Worker  if (enable_cet_shadow_stack && is_win) {
3174*8975f5c5SAndroid Build Coastguard Worker    assert(target_cpu == "x64")
3175*8975f5c5SAndroid Build Coastguard Worker    ldflags = [ "/CETCOMPAT" ]
3176*8975f5c5SAndroid Build Coastguard Worker  }
3177*8975f5c5SAndroid Build Coastguard Worker}
3178