1# Copyright (c) 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/c++/c++.gni") 6import("//build/config/clang/clang.gni") 7import("//build/config/compiler/compiler.gni") 8import("//build/config/coverage/coverage.gni") 9import("//build/config/sanitizers/sanitizers.gni") 10import("//build/toolchain/cc_wrapper.gni") 11import("//build/toolchain/toolchain.gni") 12 13import("//build/misc/overrides/build.gni") 14 15if (current_cpu == "arm" || current_cpu == "arm64") { 16 import("//build/config/arm.gni") 17} 18if (is_ohos) { 19 import("//build/config/ohos/config.gni") 20} 21if (is_mac) { 22 import("//build/config/mac/symbols.gni") 23} 24 25declare_args() { 26 # Default to warnings as errors for default workflow, where we catch 27 # warnings with known toolchains. Allow overriding this e.g. for Chromium 28 # builds on Linux that could use a different version of the compiler. 29 # With GCC, warnings in no-Chromium code are always not treated as errors. 30 treat_warnings_as_errors = true 31 32 # Whether to use the binary binutils checked into third_party/binutils. 33 # These are not multi-arch so cannot be used except on x86 and x86-64 (the 34 # only two architectures that are currently checked in). Turn this off when 35 # you are using a custom toolchain and need to control -B in cflags. 36 linux_use_bundled_binutils = 37 linux_use_bundled_binutils_override && is_linux && 38 (current_cpu == "x64" || current_cpu == "x86") 39 binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin", 40 root_build_dir) 41 42 # Compile in such a way as to make it possible for the profiler to unwind full 43 # stack frames. Setting this flag has a large effect on the performance of the 44 # generated code than just setting profiling, but gives the profiler more 45 # information to analyze. 46 # Requires profiling to be set to true. 47 enable_full_stack_frames_for_profiling = false 48 49 # When we are going to use gold we need to find it. 50 # This is initialized below, after use_gold might have been overridden. 51 gold_path = false 52 53 if (is_win) { 54 # Whether the VS xtree header has been patched to disable warning 4702. If 55 # it has, then we don't need to disable 4702 (unreachable code warning). 56 # The patch is preapplied to the internal toolchain and hence all bots. 57 msvs_xtree_patched = false 58 } 59 60 # Enable fatal linker warnings. Building Chromium with certain versions 61 # of binutils can cause linker warning. 62 # See: https://bugs.chromium.org/p/chromium/issues/detail?id=457359 63 fatal_linker_warnings = true 64 65 # Build with C++ RTTI enabled. Chromium builds without RTTI by default, 66 # but some sanitizers are known to require it, like CFI diagnostics 67 # and UBsan variants. 68 use_rtti = use_cfi_diag || is_ubsan_vptr || is_ubsan_security 69 70 # AFDO (Automatic Feedback Directed Optimizer) is a form of profile-guided 71 # optimization that GCC supports. It used by ChromeOS in their official 72 # builds. To use it, set auto_profile_path to the path to a file containing 73 # the needed gcov profiling data. 74 auto_profile_path = "" 75 76 # Optimize symbol files for maximizing goma cache hit rate. This is on by 77 # default when goma is enabled on Linux and Windows. 78 # But setting this to true may make it harder to debug binaries on Linux. 79 # See below reference for detail. 80 strip_absolute_paths_from_debug_symbols = false 81 82 # Allow projects that wish to stay on C++11 to override Chromium's default. 83 use_cxx11 = false 84 85 # Path to an AFDO profile to use while building with clang, if any. Empty 86 # implies none. 87 clang_sample_profile_path = "" 88 89 # Some configurations have default sample profiles. If this is true and 90 # clang_sample_profile_path is empty, we'll fall back to the default. 91 # 92 # We currently only have default profiles for Chromium in-tree, so we disable 93 # this by default for all downstream projects, since these profiles are likely 94 # nonsensical for said projects. 95 clang_use_default_sample_profile = 96 is_official_build && (is_ohos || is_desktop_linux) 97 98 # Turn this on to have the compiler output extra timing information. 99 compiler_timing = false 100 101 # Set to true to pass --no-rosegment to lld. This is a workaround 102 # for a KI issue in Valgrind, 103 # https://bugs.kde.org/show_bug.cgi?id=384727 104 ro_segment_workaround_for_valgrind = false 105 106 # Turn this on to use ghash feature of lld for faster debug link on Windows. 107 # http://blog.llvm.org/2018/01/improving-link-time-on-windows-with.html 108 use_ghash = false 109 110 # Whether to enable ThinLTO optimizations. Turning ThinLTO optimizations on 111 # can substantially increase link time and binary size, but they generally 112 # also make binaries a fair bit faster. 113 thin_lto_enable_optimizations = is_chromeos 114 115 # By default only the binaries in official builds get build IDs. 116 force_local_build_id = true 117} 118 119declare_args() { 120 use_cxx11_on_ohos = use_cxx11 121} 122 123declare_args() { 124 # Set to true to use icf, Identical Code Folding. 125 # 126 # icf=all is broken in older golds, see 127 # https://sourceware.org/bugzilla/show_bug.cgi?id=17704 128 # See also https://crbug.com/663886 129 # `linux_use_bundled_binutils` is to avoid breaking Linux destroys which may 130 # still have a buggy gold. 131 # chromeos binutils has been patched with the fix, so always use icf there. 132 # The bug only affects x86 and x64, so we can still use ICF when targeting 133 # other architectures. 134 # 135 # lld doesn't have the bug. 136 use_icf = 137 is_posix && !using_sanitizer && !use_clang_coverage && 138 !(is_ohos && use_order_profiling) && 139 (use_lld || 140 (use_gold && ((!is_ohos && linux_use_bundled_binutils) || is_chromeos || 141 !(current_cpu == "x86" || current_cpu == "x64")))) 142} 143 144# Apply the default logic for these values if they were not set explicitly. 145if (gold_path == false) { 146 if (use_gold) { 147 gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin", 148 root_build_dir) 149 } else { 150 gold_path = "" 151 } 152} 153 154if (use_debug_fission == "default") { 155 use_debug_fission = is_debug && !is_ohos && !is_win && 156 (use_gold || use_lld) && cc_wrapper == "" 157} 158 159# default_include_dirs --------------------------------------------------------- 160# 161# This is a separate config so that third_party code (which would not use the 162# source root and might have conflicting versions of some headers) can remove 163# this and specify their own include paths. 164config("default_include_dirs") { 165 include_dirs = [ 166 "${root_out_dir}/override/third_party", 167 "//", 168 root_gen_dir, 169 ] 170} 171 172# compiler --------------------------------------------------------------------- 173# 174# Base compiler configuration. 175# 176# See also "runtime_library" below for related stuff and a discussion about 177# where stuff should go. Put warning related stuff in the "warnings" config. 178 179config("compiler") { 180 asmflags = [] 181 cflags = [] 182 cflags_c = [] 183 cflags_cc = [] 184 cflags_objc = [] 185 cflags_objcc = [] 186 ldflags = [] 187 defines = [] 188 configs = [] 189 inputs = [] 190 191 # System-specific flags. If your compiler flags apply to one of the 192 # categories here, add it to the associated file to keep this shared config 193 # smaller. 194 if (is_win) { 195 configs += [ "//build/config/win:compiler" ] 196 } else if (is_ohos) { 197 configs += [ "//build/config/ohos:compiler" ] 198 } else if (is_linux) { 199 configs += [ "//build/config/linux:compiler" ] 200 } else if (is_nacl) { 201 configs += [ "//build/config/nacl:compiler" ] 202 } else if (is_mac) { 203 configs += [ "//build/config/mac:compiler" ] 204 } else if (current_os == "aix") { 205 configs += [ "//build/config/aix:compiler" ] 206 } else if (is_mingw) { 207 configs += [ "//build/config/mingw:compiler" ] 208 } 209 210 configs += [ 211 # See the definitions below. 212 ":compiler_cpu_abi", 213 ":compiler_codegen", 214 ] 215 216 # In general, Windows is totally different, but all the other builds share 217 # some common GCC configuration. 218 if (!is_win) { 219 # Common POSIX compiler flags setup. 220 # -------------------------------- 221 cflags += [ "-fno-strict-aliasing" ] # See http://crbug.com/32204 222 223 # Stack protection. 224 if (is_mac) { 225 # The strong variant of the stack protector significantly increases 226 # binary size, so only enable it in debug mode. 227 if (is_debug) { 228 cflags += [ "-fstack-protector-strong" ] 229 } else { 230 cflags += [ "-fstack-protector" ] 231 } 232 } else if (is_posix && !is_chromeos && !is_nacl) { 233 cflags += [ "--param=ssp-buffer-size=4" ] 234 235 # The x86 toolchain currently has problems with stack-protector. 236 if (is_ohos && current_cpu == "x86") { 237 cflags += [ "-fno-stack-protector" ] 238 } else if (is_mingw) { 239 cflags += [ "-fno-stack-protector" ] 240 } else if (current_os != "aix") { 241 # Not available on aix. 242 cflags += [ "-fstack-protector-strong" ] 243 } 244 } 245 246 # Linker warnings. 247 if (fatal_linker_warnings && !(is_chromeos && current_cpu == "arm") && 248 !(is_ohos && use_order_profiling) && !is_mac && current_os != "aix" && 249 !is_mingw) { 250 ldflags += [ "-Wl,--fatal-warnings" ] 251 } 252 } else { 253 cflags += [ 254 # Assume UTF-8 by default to avoid code page dependencies. 255 "/utf-8", 256 ] 257 if (is_clang) { 258 # Don't look for includes in %INCLUDE%. 259 cflags += [ "/X" ] 260 } 261 } 262 263 # Eliminate build metadata (__DATE__, __TIME__ and __TIMESTAMP__) for 264 # deterministic build. See https://crbug.com/314403 265 if (!is_official_build) { 266 if (is_win && !is_clang) { 267 cflags += [ 268 "/wd4117", # Trying to define or undefine a predefined macro. 269 "/D__DATE__=", 270 "/D__TIME__=", 271 "/D__TIMESTAMP__=", 272 ] 273 } else { 274 cflags += [ 275 "-Wno-builtin-macro-redefined", 276 "-D__DATE__=", 277 "-D__TIME__=", 278 "-D__TIMESTAMP__=", 279 ] 280 } 281 } 282 283 if (is_clang && is_debug) { 284 # Allow comparing the address of references and 'this' against 0 285 # in debug builds. Technically, these can never be null in 286 # well-defined C/C++ and Clang can optimize such checks away in 287 # release builds, but they may be used in asserts in debug builds. 288 cflags_cc += [ 289 "-Wno-undefined-bool-conversion", 290 "-Wno-tautological-undefined-compare", 291 ] 292 } 293 294 if (is_posix && !is_mac) { 295 if (enable_profiling) { 296 if (!is_debug) { 297 cflags += [ "-g" ] 298 299 if (enable_full_stack_frames_for_profiling) { 300 cflags += [ 301 "-fno-inline", 302 "-fno-optimize-sibling-calls", 303 ] 304 } 305 } 306 } 307 308 if (!is_mingw && (is_official_build || force_local_build_id)) { 309 # Explicitly pass --build-id to ld. Compilers used to always pass this 310 # implicitly but don't any more (in particular clang when built without 311 # ENABLE_LINKER_BUILD_ID=ON). The crash infrastructure does need a build 312 # id, so explicitly enable it in official builds. It's not needed in 313 # unofficial builds and computing it does slow down the link, so go with 314 # faster links in unofficial builds. 315 ldflags += [ "-Wl,--build-id=md5" ] 316 } 317 318 if (!is_ohos) { 319 defines += [ 320 "_FILE_OFFSET_BITS=64", 321 "_LARGEFILE_SOURCE", 322 "_LARGEFILE64_SOURCE", 323 ] 324 } 325 326 if (!is_nacl) { 327 cflags += [ "-funwind-tables" ] 328 } 329 } 330 331 if (is_linux || is_ohos) { 332 if (use_pic) { 333 cflags += [ "-fPIC" ] 334 ldflags += [ "-fPIC" ] 335 } 336 337 if (!is_clang) { 338 # Use pipes for communicating between sub-processes. Faster. 339 # (This flag doesn't do anything with Clang.) 340 cflags += [ "-pipe" ] 341 } 342 343 ldflags += [ 344 "-Wl,-z,noexecstack", 345 "-Wl,-z,now", 346 "-Wl,-z,relro", 347 ] 348 349 # Compiler instrumentation can introduce dependencies in DSOs to symbols in 350 # the executable they are loaded into, so they are unresolved at link-time. 351 if (is_ohos || (!using_sanitizer && !is_safestack)) { 352 ldflags += [ 353 "-Wl,-z,defs", 354 "-Wl,--as-needed", 355 ] 356 } 357 358 # Change default thread stack size to 2MB for asan. 359 if (is_ohos && using_sanitizer) { 360 ldflags += [ "-Wl,-z,stack-size=2097152" ] 361 } 362 } 363 364 if ((is_posix && use_lld) || (target_os == "chromeos" && is_ohos)) { 365 # NOTE: Some Chrome OS builds globally disable LLD, but they also build some 366 # targets against ohos toolchains which should use LLD. Therefore we 367 # explicitly select LLD in these cases. 368 ldflags += [ "-fuse-ld=lld" ] 369 if (current_cpu == "arm64") { 370 # Reduce the page size from 65536 in order to reduce binary size slightly 371 # by shrinking the alignment gap between segments. This also causes all 372 # segments to be mapped adjacently, which breakpad relies on. 373 ldflags += [ "-Wl,-z,max-page-size=4096" ] 374 } 375 } else if (use_gold) { 376 ldflags += [ "-fuse-ld=gold" ] 377 if (!is_ohos) { 378 # On ohos, this isn't needed. gcc in the NDK knows to look next to 379 # it with -fuse-ld=gold, and clang gets a --gcc-toolchain flag passed 380 # above. 381 ldflags += [ "-B$gold_path" ] 382 383 if (linux_use_bundled_binutils) { 384 ldflags += [ 385 # Experimentation found that using four linking threads 386 # saved ~20% of link time. 387 # Only apply this to the target linker, since the host 388 # linker might not be gold, but isn't used much anyway. 389 "-Wl,--threads", 390 "-Wl,--thread-count=4", 391 ] 392 } 393 } 394 } else if (linux_use_bundled_binutils) { 395 # Gold is the default linker for the bundled binutils so we explicitly 396 # enable the bfd linker when use_gold is not set. 397 ldflags += [ "-fuse-ld=bfd" ] 398 } 399 400 if (use_icf) { 401 ldflags += [ "-Wl,--icf=all" ] 402 } 403 404 if (linux_use_bundled_binutils) { 405 cflags += [ "-B$binutils_path" ] 406 } 407 408 if (is_linux) { 409 cflags += [ "-pthread" ] 410 # Do not use the -pthread ldflag here since it becomes a no-op 411 # when using -nodefaultlibs, which would cause an unused argument 412 # error. "-lpthread" is added in //build/config:default_libs. 413 } 414 415 # Clang-specific compiler flags setup. 416 # ------------------------------------ 417 if (is_clang) { 418 cflags += [ "-fcolor-diagnostics" ] 419 420 # Enable -fmerge-all-constants. This used to be the default in clang 421 # for over a decade. It makes clang non-conforming, but is fairly safe 422 # in practice and saves some binary size. We might want to consider 423 # disabling this (https://bugs.llvm.org/show_bug.cgi?id=18538#c13), 424 # but for now it looks like our build might rely on it 425 # (https://crbug.com/829795). 426 cflags += [ "-fmerge-all-constants" ] 427 } 428 429 if (use_lld) { 430 if (is_win) { 431 # On Windows, we call the linker directly, instead of calling it through 432 # the driver. 433 ldflags += [ "--color-diagnostics" ] 434 } else { 435 ldflags += [ "-Wl,--color-diagnostics" ] 436 } 437 } 438 439 if (is_clang && !is_nacl && !use_xcode_clang) { 440 cflags += [ 441 "-Xclang", 442 "-mllvm", 443 "-Xclang", 444 "-instcombine-lower-dbg-declare=0", 445 ] 446 } 447 448 # Print absolute paths in diagnostics. There is no precedent for doing this 449 # on Linux/Mac (GCC doesn't support it), but MSVC does this with /FC and 450 # Windows developers rely on it (crbug.com/636109) so only do this on Windows. 451 if (msvc_use_absolute_paths && is_clang && is_win) { 452 cflags += [ "-fdiagnostics-absolute-paths" ] 453 } 454 455 # Makes builds independent of absolute file path. 456 # Currently disabled for nacl since its toolchain lacks this flag (too old). 457 if (symbol_level != 0 && is_clang && !is_nacl && !is_mac && 458 strip_absolute_paths_from_debug_symbols) { 459 # If debug option is given, clang includes $cwd in debug info by default. 460 # For such build, this flag generates reproducible obj files even we use 461 # different build directory like "out/feature_a" and "out/feature_b" if 462 # we build same files with same compile flag. 463 # Other paths are already given in relative, no need to normalize them. 464 cflags += [ 465 "-Xclang", 466 "-fdebug-compilation-dir", 467 "-Xclang", 468 ".", 469 ] 470 471 if (is_win && use_lld) { 472 if (symbol_level == 2 || (is_clang && using_sanitizer)) { 473 # Absolutize source file path for PDB. Pass the real build directory 474 # if the pdb contains source-level debug information. 475 ldflags += [ "/PDBSourcePath:" + rebase_path(root_build_dir) ] 476 } else { 477 # On Windows, (non-sanitizier) symbol_level 1 builds don't contain 478 # debug information in obj files; the linker just creates enough 479 # debug info at link time to produce symbolized stacks (without line 480 # numbers). In that case, there's no downside in using a fake fixed 481 # base directory for paths in the pdb. This makes the pdb output 482 # fully deterministic and independent of the build directory. 483 assert(symbol_level == 1 && !(is_clang && using_sanitizer)) 484 ldflags += [ "/PDBSourcePath:o:\fake\prefix" ] 485 } 486 } 487 } 488 489 # Tells the compiler not to use absolute paths when passing the default 490 # paths to the tools it invokes. We don't want this because we don't 491 # really need it and it can mess up the goma cache entries. 492 if (is_clang && !is_nacl) { 493 cflags += [ "-no-canonical-prefixes" ] 494 } 495 496 # C11/C++11 compiler flags setup. 497 # --------------------------- 498 if (is_linux || is_ohos || (is_nacl && is_clang) || current_os == "aix") { 499 if (target_os == "ohos") { 500 cxx11_override = use_cxx11_on_ohos 501 } else { 502 cxx11_override = use_cxx11 503 } 504 505 if (is_clang) { 506 standard_prefix = "c" 507 508 # Since we build with -std=c* and not -std=gnu*, _GNU_SOURCE will not be 509 # defined by the compiler. However, lots of code relies on the 510 # non-standard features that _GNU_SOURCE enables, so define it manually. 511 defines += [ "_GNU_SOURCE" ] 512 513 if (is_nacl) { 514 # Undefine __STRICT_ANSI__ to get non-standard features which would 515 # otherwise not be enabled by NaCl's sysroots. 516 cflags += [ "-U__STRICT_ANSI__" ] 517 } 518 } else { 519 # Gcc does not support ##__VA_ARGS__ when in standards-conforming mode, 520 # but we use this feature in several places in Chromium. 521 standard_prefix = "gnu" 522 } 523 524 # cflags_c += [ "-std=${standard_prefix}11" ] 525 if (cxx11_override) { 526 # Override Chromium's default for projects that wish to stay on C++11. 527 cflags_cc += [ "-std=${standard_prefix}++11" ] 528 } else { 529 cflags_cc += [ "-std=${standard_prefix}++17" ] 530 } 531 } else if (!is_win && !is_nacl && !is_mingw) { 532 if (target_os == "ohos") { 533 cxx11_override = use_cxx11_on_ohos 534 } else { 535 cxx11_override = use_cxx11 536 } 537 538 if (cxx11_override) { 539 cflags_cc += [ "-std=c++11" ] 540 } else { 541 cflags_cc += [ "-std=c++17" ] 542 } 543 } 544 545 if (is_mac) { 546 # The system libc++ on Mac doesn't have aligned allocation in C++17. 547 defines += [ "_LIBCPP_HAS_NO_ALIGNED_ALLOCATION" ] 548 cflags_cc += [ "-stdlib=libc++" ] 549 ldflags += [ "-stdlib=libc++" ] 550 } 551 552 # Add flags for link-time optimization. These flags enable 553 # optimizations/transformations that require whole-program visibility at link 554 # time, so they need to be applied to all translation units, and we may end up 555 # with miscompiles if only part of the program is compiled with LTO flags. For 556 # that reason, we cannot allow targets to enable or disable these flags, for 557 # example by disabling the optimize configuration. 558 if (!is_debug && use_thin_lto && 559 (current_toolchain == default_toolchain || 560 (is_ohos && defined(ohoa_secondary_abi_toolchain) && 561 current_toolchain == ohos_secondary_abi_toolchain))) { 562 assert(use_lld || target_os == "chromeos", 563 "gold plugin only supported with ChromeOS") 564 565 cflags += [ "-flto=thin" ] 566 567 if (thin_lto_enable_optimizations) { 568 lto_opt_level = 2 569 } else { 570 lto_opt_level = 0 571 } 572 573 if (is_win) { 574 # This is a straight translation of the non-Windows flags below, 575 # except we do not use the ThinLTO cache, which leaks temporary 576 # files on Windows (https://crbug.com/871962). 577 ldflags += [ 578 "/opt:lldlto=" + lto_opt_level, 579 "/opt:lldltojobs=8", 580 ] 581 } else { 582 ldflags += [ "-flto=thin" ] 583 584 # Limit the parallelism to avoid too aggressive competition between 585 # linker jobs. This is still suboptimal to a potential dynamic 586 # resource allocation scheme, but should be good enough. 587 if (use_lld) { 588 # Limit the size of the ThinLTO cache to the lesser of 10% of available disk 589 # space, 10GB and 100000 files. 590 cache_policy = 591 "cache_size=10%:cache_size_bytes=10g:cache_size_files=100000" 592 ldflags += [ 593 "-Wl,--thinlto-jobs=8", 594 "-Wl,--thinlto-cache-dir=" + 595 rebase_path("$root_out_dir/thinlto-cache", root_build_dir), 596 "-Wl,--thinlto-cache-policy,$cache_policy", 597 ] 598 } else { 599 ldflags += [ "-Wl,-plugin-opt,jobs=8" ] 600 } 601 602 if (use_lld) { 603 ldflags += [ "-Wl,--lto-O" + lto_opt_level ] 604 if (thin_lto_enable_optimizations) { 605 if (is_ohos) { 606 ldflags += [ 607 "-Wl,-mllvm", 608 "-Wl,-import-instr-limit=5", 609 ] 610 } 611 } 612 } else { 613 not_needed([ "lto_opt_level" ]) 614 } 615 } 616 617 if (!is_ohos) { 618 cflags += [ "-fwhole-program-vtables" ] 619 if (!is_win) { 620 ldflags += [ "-fwhole-program-vtables" ] 621 } 622 } 623 624 # Work-around for http://openradar.appspot.com/20356002 625 if (is_mac) { 626 ldflags += [ "-Wl,-all_load" ] 627 } 628 629 # This flag causes LTO to create an .ARM.attributes section with the correct 630 # architecture. This is necessary because LLD will refuse to link a program 631 # unless the architecture revision in .ARM.attributes is sufficiently new. 632 if (current_cpu == "arm") { 633 ldflags += [ "-march=$arm_arch" ] 634 } 635 } 636 637 if (compiler_timing) { 638 if (is_clang) { 639 if (is_win) { 640 cflags += [ "-Xclang" ] 641 } 642 cflags += [ "-ftime-report" ] 643 } else if (is_win) { 644 cflags += [ 645 # "Documented" here: 646 # http://aras-p.info/blog/2017/10/23/Best-unknown-MSVC-flag-d2cgsummary/ 647 "/d2cgsummary", 648 ] 649 } 650 } 651 652 # Pass flag to LLD to work around issue in Valgrind related to 653 # location of debug symbols. 654 if (use_lld && ro_segment_workaround_for_valgrind) { 655 ldflags += [ "-Wl,--no-rosegment" ] 656 } 657 658 # This flag enforces that member pointer base types are complete. It helps 659 # prevent us from running into problems in the Microsoft C++ ABI (see 660 # https://crbug.com/847724). 661 if (is_clang && !is_nacl && target_os != "chromeos" && !use_xcode_clang && 662 (is_win || use_custom_libcxx)) { 663 cflags += [ "-fcomplete-member-pointers" ] 664 } 665 666 # Pass the same C/C++ flags to the objective C/C++ compiler. 667 cflags_objc += cflags_c 668 cflags_objcc += cflags_cc 669 670 # Assign any flags set for the C compiler to asmflags so that they are sent 671 # to the assembler. The Windows assembler takes different types of flags 672 # so only do so for posix platforms. 673 if (is_posix) { 674 asmflags += cflags 675 asmflags += cflags_c 676 } 677} 678 679# This provides the basic options to select the target CPU and ABI. 680# It is factored out of "compiler" so that special cases can use this 681# without using everything that "compiler" brings in. Options that 682# tweak code generation for a particular CPU do not belong here! 683# See "compiler_codegen", below. 684config("compiler_cpu_abi") { 685 cflags = [] 686 ldflags = [] 687 defines = [] 688 689 if (is_posix && !is_mac) { 690 # CPU architecture. We may or may not be doing a cross compile now, so for 691 # simplicity we always explicitly set the architecture. 692 if (current_cpu == "x64") { 693 cflags += [ 694 "-m64", 695 "-march=x86-64", 696 ] 697 ldflags += [ "-m64" ] 698 } else if (current_cpu == "x86") { 699 cflags += [ "-m32" ] 700 ldflags += [ "-m32" ] 701 if (!is_nacl) { 702 cflags += [ 703 "-msse2", 704 "-mfpmath=sse", 705 "-mmmx", 706 ] 707 } 708 } else if (current_cpu == "arm") { 709 if (is_clang && !is_ohos && !is_nacl) { 710 cflags += [ "--target=arm-linux-gnueabihf" ] 711 ldflags += [ "--target=arm-linux-gnueabihf" ] 712 } 713 if (!is_nacl) { 714 cflags += [ 715 "-march=$arm_arch", 716 "-mfloat-abi=$arm_float_abi", 717 ] 718 } 719 if (arm_tune != "") { 720 cflags += [ "-mtune=$arm_tune" ] 721 } 722 } else if (current_cpu == "arm64") { 723 if (is_clang && !is_ohos && !is_nacl) { 724 cflags += [ "--target=aarch64-linux-gnu" ] 725 ldflags += [ "--target=aarch64-linux-gnu" ] 726 } 727 if (is_clang && is_ohos) { 728 ldflags += [ "-Wl,--hash-style=gnu" ] 729 } 730 } 731 } 732 733 asmflags = cflags 734} 735 736# This provides options to tweak code generation that are necessary 737# for particular Chromium code or for working around particular 738# compiler bugs (or the combination of the two). 739config("compiler_codegen") { 740 configs = [] 741 cflags = [] 742 743 if (is_nacl) { 744 configs += [ "//build/config/nacl:compiler_codegen" ] 745 } else if (is_posix && !is_mac) { 746 if (current_cpu == "x86") { 747 if (is_clang) { 748 cflags += [ "-momit-leaf-frame-pointer" ] 749 } 750 } else if (current_cpu == "arm") { 751 if (is_ohos && !is_clang) { 752 # Clang doesn't support these flags. 753 cflags += [ 754 "-fno-tree-sra", 755 "-fno-caller-saves", 756 ] 757 } 758 } 759 } 760 761 asmflags = cflags 762} 763 764config("compiler_arm_fpu") { 765 if (current_cpu == "arm" && !is_nacl) { 766 cflags = [ "-mfpu=$arm_fpu" ] 767 asmflags = cflags 768 } 769} 770 771config("compiler_arm_thumb") { 772 if (current_cpu == "arm" && arm_use_thumb && is_posix && 773 !(is_mac || is_nacl)) { 774 cflags = [ "-mthumb" ] 775 if (is_ohos && !is_clang) { 776 # Clang doesn't support this option. 777 cflags += [ "-mthumb-interwork" ] 778 } 779 } 780} 781 782config("compiler_arm") { 783 if (current_cpu == "arm" && is_chromeos) { 784 # arm is normally the default mode for clang, but on chromeos a wrapper 785 # is used to pass -mthumb, and therefore change the default. 786 cflags = [ "-marm" ] 787 } 788} 789 790# runtime_library ------------------------------------------------------------- 791# 792# Sets the runtime library and associated options. 793# 794# How do you determine what should go in here vs. "compiler" above? Consider if 795# a target might choose to use a different runtime library (ignore for a moment 796# if this is possible or reasonable on your system). If such a target would want 797# to change or remove your option, put it in the runtime_library config. If a 798# target wants the option regardless, put it in the compiler config. 799 800config("runtime_library") { 801 defines = [] 802 configs = [] 803 804 # The order of this config is important: it must appear before 805 # ohos:runtime_library. 806 if (is_posix) { 807 configs += [ "//build/config/posix:runtime_library" ] 808 } 809 810 # System-specific flags. If your compiler flags apply to one of the 811 # categories here, add it to the associated file to keep this shared config 812 # smaller. 813 if (is_win) { 814 configs += [ "//build/config/win:runtime_library" ] 815 } else if (is_linux) { 816 configs += [ "//build/config/linux:runtime_library" ] 817 } else if (is_mac) { 818 configs += [ "//build/config/mac:runtime_library" ] 819 } else if (is_ohos) { 820 configs += [ "//build/config/ohos:runtime_library" ] 821 } 822 823 if (is_component_build) { 824 defines += [ "COMPONENT_BUILD" ] 825 } 826} 827 828# default_warnings ------------------------------------------------------------ 829# 830# Collects all warning flags that are used by default. This is used as a 831# subconfig of both chromium_code and no_chromium_code. This way these 832# flags are guaranteed to appear on the compile command line after -Wall. 833config("default_warnings") { 834 cflags = [] 835 cflags_cc = [] 836 ldflags = [] 837 838 if (is_mac && !is_nacl) { 839 # When compiling Objective-C, warns if a method is used whose 840 # availability is newer than the deployment target. 841 cflags += [ "-Wunguarded-availability" ] 842 } 843 844 # Suppress warnings about ABI changes on ARM (Clang doesn't give this 845 # warning). 846 if (current_cpu == "arm" && !is_clang) { 847 cflags += [ "-Wno-psabi" ] 848 } 849 850 if (!is_clang) { 851 cflags_cc += [ 852 # See comment for -Wno-c++11-narrowing. 853 "-Wno-narrowing", 854 ] 855 856 # -Wunused-local-typedefs is broken in gcc, 857 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872 858 cflags += [ "-Wno-unused-local-typedefs" ] 859 860 # Don't warn about "maybe" uninitialized. Clang doesn't include this 861 # in -Wall but gcc does, and it gives false positives. 862 cflags += [ "-Wno-maybe-uninitialized" ] 863 cflags += [ "-Wno-deprecated-declarations" ] 864 865 # GCC assumes 'this' is never nullptr and optimizes away code 866 # like "if (this == nullptr) ...": [1]. However, some Chromium 867 # code relies on these types of null pointer checks [2], so 868 # disable this optimization. 869 # [1] https://gcc.gnu.org/gcc-6/porting_to.html#this-cannot-be-null 870 # [2] https://crbug.com/784492#c13 871 cflags += [ "-fno-delete-null-pointer-checks" ] 872 873 # -Wcomment gives too many false positives in the case a 874 # backslash ended comment line is followed by a new line of 875 # comments 876 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638 877 cflags += [ "-Wno-comments" ] 878 } 879 880 # Common Clang and GCC warning setup. 881 if (!is_win || is_clang) { 882 cflags += [ 883 # Disables. 884 "-Wno-missing-field-initializers", # "struct foo f = {0};" 885 "-Wno-unused-parameter", # Unused function parameters. 886 ] 887 } 888 889 if (is_mingw) { 890 cflags += [ 891 "-Wno-error=c99-designator", 892 "-Wno-error=anon-enum-enum-conversion", 893 "-Wno-error=implicit-fallthrough", 894 "-Wno-error=sizeof-array-div", 895 "-Wno-error=reorder-init-list", 896 "-Wno-error=range-loop-construct", 897 "-Wno-error=deprecated-copy", 898 "-Wno-error=implicit-int-float-conversion", 899 "-Wno-error=inconsistent-dllimport", 900 "-Wno-error=unknown-warning-option", 901 "-Wno-error=abstract-final-class", 902 "-Wno-error=sign-compare", 903 ] 904 } 905 906 if (is_clang) { 907 cflags += [ 908 # This warns on using ints as initializers for floats in 909 # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|), 910 # which happens in several places in chrome code. Not sure if 911 # this is worth fixing. 912 "-Wno-c++11-narrowing", 913 "-Wno-unneeded-internal-declaration", 914 ] 915 if (use_musl) { 916 cflags += [ 917 "-Wno-error=c99-designator", 918 "-Wno-error=anon-enum-enum-conversion", 919 "-Wno-error=implicit-fallthrough", 920 "-Wno-error=sizeof-array-div", 921 "-Wno-error=reorder-init-list", 922 "-Wno-error=range-loop-construct", 923 "-Wno-error=deprecated-copy", 924 "-Wno-error=implicit-int-float-conversion", 925 "-Wno-error=inconsistent-dllimport", 926 "-Wno-error=unknown-warning-option", 927 "-Wno-error=abstract-final-class", 928 "-Wno-error=sign-compare", 929 "-Wno-error=int-in-bool-context", 930 "-Wno-error=xor-used-as-pow", 931 "-Wno-error=return-stack-address", 932 "-Wno-error=dangling-gsl", 933 ] 934 } 935 936 # use_xcode_clang only refers to the iOS toolchain, host binaries use 937 # chromium's clang always. 938 if (!is_nacl) { 939 cflags += [ "-Wno-undefined-var-template" ] 940 if (current_toolchain == host_toolchain || !use_xcode_clang || 941 xcode_version_int >= 930) { 942 cflags += [ 943 "-Wno-nonportable-include-path", 944 "-Wno-user-defined-warnings", 945 "-Wno-unused-lambda-capture", 946 ] 947 } 948 if (current_toolchain == host_toolchain || !use_xcode_clang || 949 xcode_version_int >= 1000) { 950 cflags += [ "-Wno-null-pointer-arithmetic" ] 951 } 952 if (current_toolchain == host_toolchain || !use_xcode_clang) { 953 # Flags NaCl (Clang 3.7) and Xcode 9.2 (Clang clang-900.0.39.2) do not 954 # recognize. 955 cflags += [ "-Wno-enum-compare-switch" ] 956 } 957 } 958 } 959} 960 961# chromium_code --------------------------------------------------------------- 962# 963# Toggles between higher and lower warnings for code that is (or isn't) 964# part of Chromium. 965 966config("chromium_code") { 967 if (is_win) { 968 cflags = [ "/W4" ] # Warning level 4. 969 970 if (is_clang) { 971 # Opt in to additional [[nodiscard]] on standard library methods. 972 defines = [ "_HAS_NODISCARD" ] 973 } 974 } else { 975 cflags = [ "-Wall" ] 976 if (treat_warnings_as_errors) { 977 cflags += [ "-Werror" ] 978 979 # The compiler driver can sometimes (rarely) emit warnings before calling 980 # the actual linker. Make sure these warnings are treated as errors as 981 # well. 982 ldflags = [ "-Werror" ] 983 } 984 if (is_clang) { 985 # Enable extra warnings for chromium_code when we control the compiler. 986 cflags += [ "-Wextra" ] 987 } 988 989 # In Chromium code, we define __STDC_foo_MACROS in order to get the 990 # C99 macros on Mac and Linux. 991 defines = [ 992 "__STDC_CONSTANT_MACROS", 993 "__STDC_FORMAT_MACROS", 994 ] 995 996 if (!is_debug && !using_sanitizer && 997 (!is_linux || !is_clang || is_official_build)) { 998 # _FORTIFY_SOURCE isn't really supported by Clang now, see 999 # http://llvm.org/bugs/show_bug.cgi?id=16821. 1000 # It seems to work fine with Ubuntu 12 headers though, so use it in 1001 # official builds. 1002 # 1003 # Non-chromium code is not guaranteed to compile cleanly with 1004 # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are 1005 # disabled, so only do that for Release build. 1006 defines += [ "_FORTIFY_SOURCE=2" ] 1007 } 1008 1009 if (is_mac) { 1010 cflags_objc = [ "-Wobjc-missing-property-synthesis" ] 1011 cflags_objcc = [ "-Wobjc-missing-property-synthesis" ] 1012 } 1013 } 1014 1015 if (is_clang) { 1016 cflags += [ 1017 # Warn on missing break statements at the end of switch cases. 1018 # For intentional fallthrough, use FALLTHROUGH; from 1019 # base/compiler_specific.h 1020 "-Wimplicit-fallthrough", 1021 1022 # Thread safety analysis. See base/thread_annotations.h and 1023 # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html 1024 "-Wthread-safety", 1025 ] 1026 } 1027 1028 configs = [ ":default_warnings" ] 1029} 1030 1031config("no_chromium_code") { 1032 cflags = [] 1033 cflags_cc = [] 1034 defines = [] 1035 1036 if (is_win) { 1037 cflags += [ 1038 "/W3", # Warning level 3. 1039 "/wd4800", # Disable warning when forcing value to bool. 1040 "/wd4267", # size_t to int. 1041 "/wd4996", # Deprecated function warning. 1042 ] 1043 defines += [ 1044 "_CRT_NONSTDC_NO_WARNINGS", 1045 "_CRT_NONSTDC_NO_DEPRECATE", 1046 ] 1047 } else { 1048 # GCC may emit unsuppressible warnings so don't add -Werror for no chromium 1049 # code. crbug.com/589724 1050 if (treat_warnings_as_errors && is_clang) { 1051 cflags += [ "-Werror" ] 1052 ldflags = [ "-Werror" ] 1053 } 1054 if (is_clang && !is_nacl) { 1055 cflags += [ "-Wall" ] 1056 } 1057 } 1058 1059 if (is_clang) { 1060 cflags += [ 1061 # Lots of third-party libraries have unused variables. Instead of 1062 # suppressing them individually, we just blanket suppress them here. 1063 "-Wno-unused-variable", 1064 ] 1065 } 1066 1067 configs = [ ":default_warnings" ] 1068} 1069 1070# noshadowing ----------------------------------------------------------------- 1071# 1072# Allows turning -Wshadow on. 1073 1074config("noshadowing") { 1075 # This flag has to be disabled for nacl because the nacl compiler is too 1076 # strict about shadowing. 1077 if (is_clang && !is_nacl) { 1078 cflags = [ "-Wshadow" ] 1079 } 1080} 1081 1082# rtti ------------------------------------------------------------------------ 1083# 1084# Allows turning Run-Time Type Identification on or off. 1085 1086config("rtti") { 1087 if (is_win) { 1088 cflags_cc = [ "/GR" ] 1089 } else { 1090 cflags_cc = [ "-frtti" ] 1091 } 1092} 1093 1094config("no_rtti") { 1095 # Some sanitizer configs may require RTTI to be left enabled globally 1096 if (!use_rtti) { 1097 if (is_win) { 1098 cflags_cc = [ "/GR-" ] 1099 } else { 1100 cflags_cc = [ "-fno-rtti" ] 1101 cflags_objcc = cflags_cc 1102 } 1103 } 1104} 1105 1106# export_dynamic --------------------------------------------------------------- 1107# 1108# Ensures all exported symbols are added to the dynamic symbol table. This is 1109# necessary to expose Chrome's custom operator new() and operator delete() (and 1110# other memory-related symbols) to libraries. Otherwise, they might 1111# (de)allocate memory on a different heap, which would spell trouble if pointers 1112# to heap-allocated memory are passed over shared library boundaries. 1113config("export_dynamic") { 1114 if (is_desktop_linux || export_libcxxabi_from_executables) { 1115 ldflags = [ "-rdynamic" ] 1116 } 1117} 1118 1119# thin_archive ----------------------------------------------------------------- 1120# 1121# Enables thin archives on posix. Regular archives directly include the object 1122# files used to generate it. Thin archives merely reference the object files. 1123# This makes building them faster since it requires less disk IO, but is 1124# inappropriate if you wish to redistribute your static library. 1125# This config is added to the global config, so thin archives should already be 1126# enabled. If you want to make a distributable static library, you need to do 2 1127# things: 1128# 1. Set complete_static_lib so that all dependencies of the library make it 1129# into the library. See `gn help complete_static_lib` for details. 1130# 2. Remove the thin_archive config, so that the .a file actually contains all 1131# .o files, instead of just references to .o files in the build directory 1132config("thin_archive") { 1133 # Mac and iOS use the mac-specific "libtool" command, not ar, which doesn't 1134 # have a "thin archive" mode (it does accept -T, but it means truncating 1135 # archive names to 16 characters, which is not what we want). 1136 if (is_posix && !is_nacl && !is_mac) { 1137 arflags = [ "-T" ] 1138 } 1139} 1140 1141# exceptions ------------------------------------------------------------------- 1142# 1143# Allows turning Exceptions on or off. 1144 1145config("exceptions") { 1146 if (is_win) { 1147 # Enables exceptions in the STL. 1148 if (!use_custom_libcxx) { 1149 defines = [ "_HAS_EXCEPTIONS=1" ] 1150 } 1151 cflags_cc = [ "/EHsc" ] 1152 } else { 1153 cflags_cc = [ "-fexceptions" ] 1154 cflags_objcc = cflags_cc 1155 } 1156} 1157 1158config("no_exceptions") { 1159 if (is_win) { 1160 # Disables exceptions in the STL. 1161 # libc++ uses the __has_feature macro to control whether to use exceptions, 1162 # so defining this macro is unnecessary. Defining _HAS_EXCEPTIONS to 0 also 1163 # breaks libc++ because it depends on MSVC headers that only provide certain 1164 # declarations if _HAS_EXCEPTIONS is 1. Those MSVC headers do not use 1165 # exceptions, despite being conditional on _HAS_EXCEPTIONS. 1166 if (!use_custom_libcxx) { 1167 defines = [ "_HAS_EXCEPTIONS=0" ] 1168 } 1169 } else { 1170 cflags_cc = [ "-fno-exceptions" ] 1171 cflags_objcc = cflags_cc 1172 } 1173} 1174 1175# Warnings --------------------------------------------------------------------- 1176 1177# This will generate warnings when using Clang if code generates exit-time 1178# destructors, which will slow down closing the program. 1179config("wexit_time_destructors") { 1180 if (is_clang) { 1181 cflags = [ "-Wexit-time-destructors" ] 1182 } 1183} 1184 1185# On Windows compiling on x64, VC will issue a warning when converting 1186# size_t to int because it will truncate the value. Our code should not have 1187# these warnings and one should use a static_cast or a checked_cast for the 1188# conversion depending on the case. However, a lot of code still needs to be 1189# fixed. Apply this config to such targets to disable the warning. 1190# 1191# Note that this can be applied regardless of platform and architecture to 1192# clean up the call sites. This will only apply the flag when necessary. 1193# 1194# This config is just an alias to no_shorten_64_warnings and will 1195# suppress a superset of warning 4267 and any 64-bit -> 32-bit implicit 1196# conversions. Having both for a time means not having to go through and 1197# update all references to no_size_t_to_int_warning throughout the codebase 1198# atomically. 1199# 1200# Any new warning suppressions should use the no_shorten_64_warnings 1201# config below and not this. 1202config("no_size_t_to_int_warning") { 1203 configs = [ ":no_shorten_64_warnings" ] 1204} 1205 1206# As part of re-enabling -Wconversion (see issue 588506) some code 1207# will continue to generate warnings. 1208# The first warning to be enabled will be -Wshorten-64-to-32. 1209# 1210# Code that currently generates warnings for this can include this 1211# config to disable them. 1212config("no_shorten_64_warnings") { 1213 if (current_cpu == "x64" || current_cpu == "arm64") { 1214 if (is_clang) { 1215 cflags = [ "-Wno-shorten-64-to-32" ] 1216 } else { 1217 if (is_win) { 1218 # MSVC does not have an explicit warning equivalent to 1219 # -Wshorten-64-to-32 but 4267 warns for size_t -> int 1220 # on 64-bit builds, so is the closest. 1221 cflags = [ "/wd4267" ] 1222 } 1223 } 1224 } 1225} 1226 1227# Some code presumes that pointers to structures/objects are compatible 1228# regardless of whether what they point to is already known to be valid. 1229# gcc 4.9 and earlier had no way of suppressing this warning without 1230# suppressing the rest of them. Here we centralize the identification of 1231# the gcc 4.9 toolchains. 1232config("no_incompatible_pointer_warnings") { 1233 cflags = [] 1234 if (is_clang) { 1235 cflags += [ "-Wno-incompatible-pointer-types" ] 1236 } else if (is_chromeos && current_cpu == "arm") { 1237 cflags += [ "-w" ] 1238 } 1239} 1240 1241# Optimization ----------------------------------------------------------------- 1242# 1243# The BUILDCONFIG file sets the "default_optimization" config on targets by 1244# default. It will be equivalent to either "optimize" (release) or 1245# "no_optimize" (debug) optimization configs. 1246# 1247# You can override the optimization level on a per-target basis by removing the 1248# default config and then adding the named one you want: 1249# 1250# configs -= [ "//build/config/compiler:default_optimization" ] 1251# configs += [ "//build/config/compiler:optimize_max" ] 1252 1253# Shared settings for both "optimize" and "optimize_max" configs. 1254# IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags. 1255if (is_win) { 1256 common_optimize_on_cflags = [ 1257 "/Ob2", # Both explicit and auto inlining. 1258 "/Oy-", # Disable omitting frame pointers, must be after /O2. 1259 "/Zc:inline", # Remove unreferenced COMDAT (faster links). 1260 ] 1261 if (!is_asan) { 1262 common_optimize_on_cflags += [ 1263 # Put data in separate COMDATs. This allows the linker 1264 # to put bit-identical constants at the same address even if 1265 # they're unrelated constants, which saves binary size. 1266 # This optimization can't be used when ASan is enabled because 1267 # it is not compatible with the ASan ODR checker. 1268 "/Gw", 1269 ] 1270 } 1271 common_optimize_on_ldflags = [] 1272 1273 # /OPT:ICF is not desirable in Debug builds, since code-folding can result in 1274 # misleading symbols in stack traces. It is also incompatible with 1275 # incremental linking, which we enable for both Debug and component builds. 1276 if (!is_debug && !is_component_build) { 1277 common_optimize_on_ldflags += [ "/OPT:ICF" ] # Redundant COMDAT folding. 1278 } 1279 1280 if (is_official_build) { 1281 common_optimize_on_ldflags += [ "/OPT:REF" ] # Remove unreferenced data. 1282 1283 if (!use_lld && !is_clang) { 1284 common_optimize_on_ldflags += [ 1285 # Set the number of LTCG code-gen threads to eight. The default is four. 1286 # This gives a 5-10% link speedup. 1287 "/cgthreads:8", 1288 ] 1289 if (use_incremental_wpo) { 1290 # Incremental Link-time code generation. 1291 common_optimize_on_ldflags += [ "/LTCG:INCREMENTAL" ] 1292 } else { 1293 common_optimize_on_ldflags += [ "/LTCG" ] # Link-time code generation. 1294 } 1295 } 1296 } 1297} else { 1298 common_optimize_on_cflags = [] 1299 common_optimize_on_ldflags = [] 1300 1301 if (is_ohos) { 1302 common_optimize_on_ldflags += [ 1303 # Warn in case of text relocations. 1304 "-Wl,--warn-shared-textrel", 1305 ] 1306 } 1307 1308 if (is_mac) { 1309 if (symbol_level == 2) { 1310 # Mac dead code stripping requires symbols. 1311 common_optimize_on_ldflags += [ "-Wl,-dead_strip" ] 1312 } 1313 } else if (current_os != "aix") { 1314 # Non-Mac Posix flags. 1315 # Aix does not support these. 1316 1317 common_optimize_on_cflags += [ 1318 # Don't emit the GCC version ident directives, they just end up in the 1319 # .comment section taking up binary size. 1320 "-fno-ident", 1321 1322 # Put data and code in their own sections, so that unused symbols 1323 # can be removed at link time with --gc-sections. 1324 "-fdata-sections", 1325 "-ffunction-sections", 1326 ] 1327 1328 common_optimize_on_ldflags += [ 1329 # Specifically tell the linker to perform optimizations. 1330 # See http://lwn.net/Articles/192624/ . 1331 # -O2 enables string tail merge optimization in gold and lld. 1332 "-Wl,-O2", 1333 ] 1334 if (!is_mingw) { 1335 common_optimize_on_ldflags += [ "-Wl,--gc-sections" ] 1336 } 1337 } 1338} 1339 1340config("default_stack_frames") { 1341 if (is_posix) { 1342 if (enable_frame_pointers) { 1343 cflags = [ "-fno-omit-frame-pointer" ] 1344 } else { 1345 cflags = [ "-fomit-frame-pointer" ] 1346 } 1347 } 1348 # On Windows, the flag to enable framepointers "/Oy-" must always come after 1349 # the optimization flag [e.g. "/O2"]. The optimization flag is set by one of 1350 # the "optimize" configs, see rest of this file. The ordering that cflags are 1351 # applied is well-defined by the GN spec, and there is no way to ensure that 1352 # cflags set by "default_stack_frames" is applied after those set by an 1353 # "optimize" config. Similarly, there is no way to propagate state from this 1354 # config into the "optimize" config. We always apply the "/Oy-" config in the 1355 # definition for common_optimize_on_cflags definition, even though this may 1356 # not be correct. 1357} 1358 1359# Default "optimization on" config. 1360config("optimize") { 1361 if (optimize_for_size && !is_nacl) { 1362 # Favor size over speed. 1363 if (is_clang) { 1364 cflags = [ "-O2" ] + common_optimize_on_cflags 1365 } else { 1366 cflags = [ "-Os" ] + common_optimize_on_cflags 1367 } 1368 } else { 1369 cflags = [ "-O2" ] + common_optimize_on_cflags 1370 } 1371 ldflags = common_optimize_on_ldflags 1372} 1373 1374# Same config as 'optimize' but without the WPO flag. 1375config("optimize_no_wpo") { 1376 if (is_win) { 1377 # Favor size over speed, /O1 must be before the common flags. The GYP 1378 # build also specifies /Os and /GF but these are implied by /O1. 1379 cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ] 1380 } else if (optimize_for_size && !is_nacl) { 1381 # Favor size over speed. 1382 if (is_clang) { 1383 cflags = [ "-Oz" ] + common_optimize_on_cflags 1384 } else { 1385 cflags = [ "-Os" ] + common_optimize_on_cflags 1386 } 1387 } else if (optimize_for_fuzzing) { 1388 cflags = [ "-O0" ] + common_optimize_on_cflags 1389 } else { 1390 cflags = [ "-O2" ] + common_optimize_on_cflags 1391 } 1392 ldflags = common_optimize_on_ldflags 1393} 1394 1395# Turn off optimizations. 1396config("no_optimize") { 1397 if (is_win) { 1398 cflags = [ 1399 "/Od", # Disable optimization. 1400 "/Ob0", # Disable all inlining (on by default). 1401 "/GF", # Enable string pooling (off by default). 1402 ] 1403 } else if (is_ohos) { 1404 # On ohos we kind of optimize some things that don't affect debugging 1405 # much even when optimization is disabled to get the binary size down. 1406 if (is_clang) { 1407 cflags = [ "-Oz" ] + common_optimize_on_cflags 1408 } else { 1409 cflags = [ "-Os" ] + common_optimize_on_cflags 1410 } 1411 } else { 1412 cflags = [ "-O0" ] 1413 ldflags = [] 1414 } 1415} 1416 1417# Turns up the optimization level. On Windows, this implies whole program 1418# optimization and link-time code generation which is very expensive and should 1419# be used sparingly. 1420config("optimize_max") { 1421 if (is_nacl && is_nacl_irt) { 1422 # The NaCl IRT is a special case and always wants its own config. 1423 # Various components do: 1424 # if (!is_debug) { 1425 # configs -= [ "//build/config/compiler:default_optimization" ] 1426 # configs += [ "//build/config/compiler:optimize_max" ] 1427 # } 1428 # So this config has to have the selection logic just like 1429 # "default_optimization", below. 1430 configs = [ "//build/config/nacl:irt_optimize" ] 1431 } else { 1432 ldflags = common_optimize_on_ldflags 1433 if (is_win) { 1434 # Favor speed over size, /O2 must be before the common flags. The GYP 1435 # build also specifies /Ot, /Oi, and /GF, but these are implied by /O2. 1436 cflags = [ "/O2" ] + common_optimize_on_cflags 1437 1438 if (is_official_build) { 1439 if (!is_clang) { 1440 cflags += [ 1441 "/GL", # Whole program optimization. 1442 1443 # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds. 1444 # Probably anything that this would catch that wouldn't be caught 1445 # in a normal build isn't going to actually be a bug, so the 1446 # incremental value of C4702 for PGO builds is likely very small. 1447 "/wd4702", 1448 ] 1449 } 1450 } 1451 } else if (optimize_for_fuzzing) { 1452 cflags = [ "-O0" ] + common_optimize_on_cflags 1453 } else { 1454 cflags = [ "-O2" ] + common_optimize_on_cflags 1455 } 1456 } 1457} 1458 1459# This config can be used to override the default settings for per-component 1460# and whole-program optimization, optimizing the particular target for speed 1461# instead of code size. This config is exactly the same as "optimize_max" 1462# except that we use -O3 instead of -O2 on non-win, non-IRT platforms. 1463config("optimize_speed") { 1464 if (is_nacl && is_nacl_irt) { 1465 # The NaCl IRT is a special case and always wants its own config. 1466 # Various components do: 1467 # if (!is_debug) { 1468 # configs -= [ "//build/config/compiler:default_optimization" ] 1469 # configs += [ "//build/config/compiler:optimize_max" ] 1470 # } 1471 # So this config has to have the selection logic just like 1472 # "default_optimization", below. 1473 configs = [ "//build/config/nacl:irt_optimize" ] 1474 } else { 1475 ldflags = common_optimize_on_ldflags 1476 if (is_win) { 1477 # Favor speed over size, /O2 must be before the common flags. The GYP 1478 # build also specifies /Ot, /Oi, and /GF, but these are implied by /O2. 1479 cflags = [ "/O2" ] + common_optimize_on_cflags 1480 1481 if (is_official_build && !is_clang) { 1482 cflags += [ 1483 "/GL", # Whole program optimization. 1484 1485 # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds. 1486 # Probably anything that this would catch that wouldn't be caught in a 1487 # normal build isn't going to actually be a bug, so the incremental 1488 # value of C4702 for PGO builds is likely very small. 1489 "/wd4702", 1490 ] 1491 } 1492 } else if (optimize_for_fuzzing) { 1493 cflags = [ "-O0" ] + common_optimize_on_cflags 1494 } else { 1495 cflags = [ "-O3" ] + common_optimize_on_cflags 1496 } 1497 } 1498} 1499 1500config("optimize_fuzzing") { 1501 cflags = [ "-O0" ] + common_optimize_on_cflags 1502 ldflags = common_optimize_on_ldflags 1503 visibility = [ ":default_optimization" ] 1504} 1505 1506# The default optimization applied to all targets. This will be equivalent to 1507# either "optimize" or "no_optimize", depending on the build flags. 1508config("default_optimization") { 1509 if (is_nacl && is_nacl_irt) { 1510 # The NaCl IRT is a special case and always wants its own config. 1511 # It gets optimized the same way regardless of the type of build. 1512 configs = [ "//build/config/nacl:irt_optimize" ] 1513 } else if (is_debug) { 1514 configs = [ ":no_optimize" ] 1515 } else if (optimize_for_fuzzing) { 1516 assert(!is_win, "Fuzzing optimize level not supported on Windows") 1517 1518 # Coverage build is quite slow. Using "optimize_for_fuzzing" makes it even 1519 # slower as it uses "-O1" instead of "-O3". Prevent that from happening. 1520 assert(!use_clang_coverage, 1521 "optimize_for_fuzzing=true should not be used with " + 1522 "use_clang_coverage=true.") 1523 configs = [ ":optimize_fuzzing" ] 1524 } else { 1525 configs = [ ":optimize" ] 1526 } 1527} 1528 1529_clang_sample_profile = "" 1530if (is_clang && current_toolchain == default_toolchain) { 1531 if (clang_sample_profile_path != "") { 1532 _clang_sample_profile = clang_sample_profile_path 1533 } else if (clang_use_default_sample_profile) { 1534 assert(build_with_chromium, 1535 "Our default profiles currently only apply to Chromium") 1536 assert(is_ohos || is_desktop_linux, 1537 "The current platform has no default profile") 1538 _clang_sample_profile = "" 1539 } 1540} 1541 1542# Clang offers a way to assert that AFDO profiles are accurate, which causes it 1543# to optimize functions not represented in a profile more aggressively for size. 1544# This config can be toggled in cases where shaving off binary size hurts 1545# performance too much. 1546config("afdo_optimize_size") { 1547 if (_clang_sample_profile != "" && sample_profile_is_accurate) { 1548 cflags = [ "-fprofile-sample-accurate" ] 1549 } 1550} 1551 1552# GCC and clang support a form of profile-guided optimization called AFDO. 1553# There are some targeted places that AFDO regresses (and an icky interaction 1554# between //base/allocator:tcmalloc and AFDO on GCC), so we provide a separate 1555# config to allow AFDO to be disabled per-target. 1556config("afdo") { 1557 if (is_clang) { 1558 if (_clang_sample_profile != "") { 1559 rebased_clang_sample_profile = 1560 rebase_path(_clang_sample_profile, root_build_dir) 1561 cflags = [ "-fprofile-sample-use=${rebased_clang_sample_profile}" ] 1562 inputs = [ _clang_sample_profile ] 1563 } 1564 } else if (auto_profile_path != "" && 1565 current_toolchain == default_toolchain) { 1566 cflags = [ "-fauto-profile=${auto_profile_path}" ] 1567 inputs = [ auto_profile_path ] 1568 } 1569} 1570 1571# Symbols ---------------------------------------------------------------------- 1572 1573# The BUILDCONFIG file sets the "default_symbols" config on targets by 1574# default. It will be equivalent to one the three specific symbol levels. 1575# 1576# You can override the symbol level on a per-target basis by removing the 1577# default config and then adding the named one you want: 1578# 1579# configs -= [ "//build/config/compiler:default_symbols" ] 1580# configs += [ "//build/config/compiler:symbols" ] 1581 1582# A helper config that all configs passing /DEBUG to the linker should 1583# include as sub-config. 1584config("win_pdbaltpath") { 1585 visibility = [ 1586 ":minimal_symbols", 1587 ":symbols", 1588 ] 1589 1590 # /DEBUG causes the linker to generate a pdb file, and to write the absolute 1591 # path to it in the executable file it generates. This flag turns that 1592 # absolute path into just the basename of the pdb file, which helps with 1593 # build reproducibility. Debuggers look for pdb files next to executables, 1594 # so there's no downside to always using this. 1595 ldflags = [ "/pdbaltpath:%_PDB%" ] 1596} 1597 1598# Full symbols. 1599config("symbols") { 1600 if (is_win) { 1601 if (is_clang) { 1602 # Note that with VC++ this requires is_win_fastlink, enforced elsewhere. 1603 cflags = [ "/Z7" ] # Debug information in the .obj files. 1604 } else { 1605 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue. 1606 } 1607 1608 if (is_win_fastlink && !use_lld) { 1609 # Tell VS 2015+ to create a PDB that references debug 1610 # information in .obj and .lib files instead of copying 1611 # it all. This flag is incompatible with /PROFILE 1612 ldflags = [ "/DEBUG:FASTLINK" ] 1613 } else if (is_clang && use_lld && use_ghash) { 1614 cflags += [ 1615 "-mllvm", 1616 "-emit-codeview-ghash-section", 1617 ] 1618 ldflags = [ "/DEBUG:GHASH" ] 1619 } else { 1620 ldflags = [ "/DEBUG" ] 1621 } 1622 1623 # All configs using /DEBUG should include this: 1624 configs = [ ":win_pdbaltpath" ] 1625 1626 if (is_clang) { 1627 # /DEBUG:FASTLINK requires every object file to have standalone debug 1628 # information. 1629 if (is_win_fastlink && !use_lld) { 1630 cflags += [ "-fstandalone-debug" ] 1631 } else { 1632 cflags += [ "-fno-standalone-debug" ] 1633 } 1634 } 1635 } else { 1636 if (is_mac) { 1637 cflags = [ "-gdwarf-2" ] 1638 if (is_mac && enable_dsyms) { 1639 # If generating dSYMs, specify -fno-standalone-debug. This was 1640 # originally specified for https://crbug.com/479841 because dsymutil 1641 # could not handle a 4GB dSYM file. But dsymutil from Xcodes prior to 1642 # version 7 also produces debug data that is incompatible with Breakpad 1643 # dump_syms, so this is still required (https://crbug.com/622406). 1644 cflags += [ "-fno-standalone-debug" ] 1645 } 1646 } else { 1647 cflags = [] 1648 if (!use_debug_fission && current_cpu == "arm") { 1649 # dump_syms has issues with dwarf4 on arm, https://crbug.com/744956 1650 # 1651 # debug fission needs DWARF DIEs to be emitted at version 4. 1652 # Chrome OS emits Debug Frame in DWARF1 to make breakpad happy. [1] 1653 # Unless ohos needs debug fission, DWARF3 is the simplest solution. 1654 # 1655 # [1] crrev.com/a81d5ade0b043208e06ad71a38bcf9c348a1a52f 1656 cflags += [ "-gdwarf-3" ] 1657 } 1658 cflags += [ "-g2" ] 1659 } 1660 if (use_debug_fission && !is_nacl && !is_ohos) { 1661 # NOTE: Some Chrome OS builds globally set |use_debug_fission| to true, 1662 # but they also build some targets against ohos toolchains which aren't 1663 # compatible with it. 1664 cflags += [ "-gsplit-dwarf" ] 1665 } 1666 asmflags = cflags 1667 ldflags = [] 1668 1669 if (!is_mac && !is_nacl && current_cpu != "x86" && (use_gold || use_lld)) { 1670 if (is_clang) { 1671 # This flag enables the GNU-format pubnames and pubtypes sections, 1672 # which lld needs in order to generate a correct GDB index. 1673 cflags += [ "-ggnu-pubnames" ] 1674 } 1675 ldflags += [ "-Wl,--gdb-index" ] 1676 } 1677 } 1678} 1679 1680# Minimal symbols. 1681# This config guarantees to hold symbol for stack trace which are shown to user 1682# when crash happens in unittests running on buildbot. 1683config("minimal_symbols") { 1684 if (is_win) { 1685 # Linker symbols for backtraces only. 1686 cflags = [] 1687 ldflags = [ "/DEBUG" ] 1688 1689 # All configs using /DEBUG should include this: 1690 configs = [ ":win_pdbaltpath" ] 1691 1692 # For win/asan, get stack traces with full line numbers. 1693 # AddressSanitizerTests.TestAddressSanitizer needs this, and since 1694 # win/asan isn't a default cq bot the build time hit is ok. 1695 if (is_clang && using_sanitizer) { 1696 # -gline-tables-only is the same as -g1, but clang-cl only exposes the 1697 # former. 1698 cflags += [ "-gline-tables-only" ] 1699 } 1700 } else { 1701 cflags = [] 1702 if (current_cpu == "arm") { 1703 # dump_syms has issues with dwarf4 on arm, https://crbug.com/744956 1704 cflags += [ "-gdwarf-3" ] 1705 } 1706 cflags += [ "-g1" ] 1707 ldflags = [] 1708 if (is_ohos && is_clang) { 1709 # ohos defaults to symbol_level=1 builds in production builds 1710 # (https://crbug.com/648948), but clang, unlike gcc, doesn't emit 1711 # DW_AT_linkage_name in -g1 builds. -fdebug-info-for-profiling enables 1712 # that (and a bunch of other things we don't need), so that we get 1713 # qualified names in stacks. 1714 cflags += [ "-fdebug-info-for-profiling" ] 1715 } 1716 1717 # Note: -gsplit-dwarf implicitly turns on -g2 with clang, so don't pass it. 1718 asmflags = cflags 1719 } 1720} 1721 1722# No symbols. 1723config("no_symbols") { 1724 if (!is_win) { 1725 cflags = [ "-g0" ] 1726 asmflags = cflags 1727 } 1728} 1729 1730# Default symbols. 1731config("default_symbols") { 1732 if (symbol_level == 0) { 1733 configs = [ ":no_symbols" ] 1734 } else if (symbol_level == 1) { 1735 configs = [ ":minimal_symbols" ] 1736 } else if (symbol_level == 2) { 1737 configs = [ ":symbols" ] 1738 } else { 1739 assert(false) 1740 } 1741 1742 # This config is removed by base unittests app. 1743 if (is_ohos && is_clang && strip_debug_info) { 1744 configs += [ ":strip_debug" ] 1745 } 1746} 1747 1748config("strip_debug") { 1749 if (!defined(ldflags)) { 1750 ldflags = [] 1751 } 1752 ldflags += [ "-Wl,--strip-debug" ] 1753} 1754 1755if (is_mac) { 1756 # On Mac and iOS, this enables support for ARC (automatic ref-counting). 1757 # See http://clang.llvm.org/docs/AutomaticReferenceCounting.html. 1758 config("enable_arc") { 1759 common_flags = [ "-fobjc-arc" ] 1760 cflags_objc = common_flags 1761 cflags_objcc = common_flags 1762 } 1763} 1764 1765config("no_common") { 1766 if (is_clang) { 1767 cflags = [ "-fno-common" ] 1768 asmflags = cflags 1769 } 1770} 1771