Advertisement
Guest User

chromium/src/build/config/compiler/BUILD.gn

a guest
Oct 5th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 92.86 KB | None | 0 0
  1. # Copyright 2013 The Chromium Authors
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4.  
  5. import("//build/buildflag_header.gni")
  6. import("//build/config/android/config.gni")
  7. import("//build/config/c++/c++.gni")
  8. import("//build/config/chrome_build.gni")
  9. import("//build/config/chromeos/args.gni")
  10. import("//build/config/chromeos/ui_mode.gni")
  11. import("//build/config/clang/clang.gni")
  12. import("//build/config/compiler/compiler.gni")
  13. import("//build/config/coverage/coverage.gni")
  14. import("//build/config/dcheck_always_on.gni")
  15. import("//build/config/gclient_args.gni")
  16. import("//build/config/host_byteorder.gni")
  17. import("//build/config/rust.gni")
  18. import("//build/config/sanitizers/sanitizers.gni")
  19. import("//build/config/ui.gni")
  20. import("//build/toolchain/cc_wrapper.gni")
  21. import("//build/toolchain/goma.gni")
  22. import("//build/toolchain/rbe.gni")
  23. import("//build/toolchain/toolchain.gni")
  24. import("//build_overrides/build.gni")
  25.  
  26. if (current_cpu == "arm" || current_cpu == "arm64") {
  27. import("//build/config/arm.gni")
  28. }
  29. if (current_cpu == "mipsel" || current_cpu == "mips64el" ||
  30. current_cpu == "mips" || current_cpu == "mips64") {
  31. import("//build/config/mips.gni")
  32. }
  33. if (is_mac) {
  34. import("//build/config/apple/symbols.gni")
  35. }
  36. if (is_ios) {
  37. import("//build/config/ios/ios_sdk.gni")
  38. }
  39. if (is_nacl) {
  40. # To keep NaCl variables out of builds that don't include NaCl, all
  41. # variables defined in nacl/config.gni referenced here should be protected by
  42. # is_nacl conditions.
  43. import("//build/config/nacl/config.gni")
  44. }
  45.  
  46. lld_path = ""
  47. if (!is_clang) {
  48. declare_args() {
  49. # This allows overriding the location of lld.
  50. lld_path = rebase_path("$clang_base_path/bin", root_build_dir)
  51. }
  52. } else {
  53. # clang looks for lld next to it, no need for -B.
  54. lld_path = ""
  55. }
  56.  
  57. declare_args() {
  58. # Normally, Android builds are lightly optimized, even for debug builds, to
  59. # keep binary size down. Setting this flag to true disables such optimization
  60. android_full_debug = false
  61.  
  62. # Compile in such a way as to make it possible for the profiler to unwind full
  63. # stack frames. Setting this flag has a large effect on the performance of the
  64. # generated code than just setting profiling, but gives the profiler more
  65. # information to analyze.
  66. # Requires profiling to be set to true.
  67. enable_full_stack_frames_for_profiling = false
  68.  
  69. # When we are going to use gold we need to find it.
  70. # This is initialized below, after use_gold might have been overridden.
  71. gold_path = ""
  72.  
  73. # Enable fatal linker warnings. Building Chromium with certain versions
  74. # of binutils can cause linker warning.
  75. fatal_linker_warnings = true
  76.  
  77. # Build with C++ RTTI enabled. Chromium builds without RTTI by default,
  78. # but some sanitizers are known to require it, like CFI diagnostics
  79. # and UBsan variants.
  80. use_rtti = use_cfi_diag || is_ubsan_vptr || is_ubsan_security
  81.  
  82. # AFDO (Automatic Feedback Directed Optimizer) is a form of profile-guided
  83. # optimization that GCC supports. It used by ChromeOS in their official
  84. # builds. To use it, set auto_profile_path to the path to a file containing
  85. # the needed gcov profiling data.
  86. auto_profile_path = ""
  87.  
  88. # Allow projects that wish to stay on C++11 to override Chromium's default.
  89. use_cxx11 = false
  90.  
  91. # Path to an AFDO profile to use while building with clang, if any. Empty
  92. # implies none.
  93. clang_sample_profile_path = ""
  94.  
  95. # Some configurations have default sample profiles. If this is true and
  96. # clang_sample_profile_path is empty, we'll fall back to the default.
  97. #
  98. # We currently only have default profiles for Chromium in-tree, so we disable
  99. # this by default for all downstream projects, since these profiles are likely
  100. # nonsensical for said projects.
  101. clang_use_default_sample_profile =
  102. chrome_pgo_phase == 0 && build_with_chromium && is_official_build &&
  103. (is_android || chromeos_is_browser_only)
  104.  
  105. # This configuration is used to select a default profile in Chrome OS based on
  106. # the microarchitectures we are using. This is only used if
  107. # clang_use_default_sample_profile is true and clang_sample_profile_path is
  108. # empty.
  109. chromeos_afdo_platform = "atom"
  110.  
  111. # Emit debug information for profiling wile building with clang.
  112. clang_emit_debug_info_for_profiling = false
  113.  
  114. # Turn this on to have the compiler output extra timing information.
  115. compiler_timing = false
  116.  
  117. # Turn this on to use ghash feature of lld for faster debug link on Windows.
  118. # http://blog.llvm.org/2018/01/improving-link-time-on-windows-with.html
  119. use_ghash = true
  120.  
  121. # Whether to enable ThinLTO optimizations. Turning ThinLTO optimizations on
  122. # can substantially increase link time and binary size, but they generally
  123. # also make binaries a fair bit faster.
  124. #
  125. # TODO(gbiv): We disable optimizations by default on most platforms because
  126. # the space overhead is too great. We should use some mixture of profiles and
  127. # optimization settings to better tune the size increase.
  128. thin_lto_enable_optimizations =
  129. (is_chromeos || is_android || is_win || is_linux || is_mac ||
  130. (is_ios && use_lld)) && is_official_build
  131.  
  132. # Initialize all local variables with a pattern. This flag will fill
  133. # uninitialized floating-point types (and 32-bit pointers) with 0xFF and the
  134. # rest with 0xAA. This makes behavior of uninitialized memory bugs consistent,
  135. # recognizable in the debugger, and crashes on memory accesses through
  136. # uninitialized pointers.
  137. #
  138. # TODO(crbug.com/1131993): Enabling this when 'is_android' is true breaks
  139. # content_shell_test_apk on both ARM and x86.
  140. init_stack_vars = !is_android
  141.  
  142. # This argument is to control whether enabling text section splitting in the
  143. # final binary. When enabled, the separated text sections with prefix
  144. # '.text.hot', '.text.unlikely', '.text.startup' and '.text.exit' will not be
  145. # merged to '.text' section. This allows us to identify the hot code section
  146. # ('.text.hot') in the binary which may be mlocked or mapped to huge page to
  147. # reduce TLB misses which gives performance improvement on cpu usage.
  148. # The gold linker by default has text section splitting enabled.
  149. use_text_section_splitting = false
  150.  
  151. # Turn off the --call-graph-profile-sort flag for lld by default. Enable
  152. # selectively for targets where it's beneficial.
  153. enable_call_graph_profile_sort = chrome_pgo_phase == 2
  154.  
  155. # Enable DWARF v5.
  156. use_dwarf5 = false
  157.  
  158. # Override this to put full paths to PDBs in Windows PE files. This helps
  159. # windbg and Windows Performance Analyzer with finding the PDBs in some local-
  160. # build scenarios. This is never needed for bots or official builds. Because
  161. # this puts the output directory in the DLLs/EXEs it breaks build determinism.
  162. # Bugs have been reported to the windbg/WPA teams and this workaround will be
  163. # removed when they are fixed.
  164. use_full_pdb_paths = false
  165.  
  166. # Enable -H, which prints the include tree during compilation.
  167. # For use by tools/clang/scripts/analyze_includes.py
  168. show_includes = false
  169. }
  170.  
  171. declare_args() {
  172. # Set to true to use icf, Identical Code Folding.
  173. #
  174. # icf=all is broken in older golds, see
  175. # https://sourceware.org/bugzilla/show_bug.cgi?id=17704
  176. # chromeos binutils has been patched with the fix, so always use icf there.
  177. # The bug only affects x86 and x64, so we can still use ICF when targeting
  178. # other architectures.
  179. #
  180. # lld doesn't have the bug.
  181. use_icf = (is_posix || is_fuchsia) && !is_debug && !using_sanitizer &&
  182. !use_clang_coverage && current_os != "zos" &&
  183. !(is_android && use_order_profiling) &&
  184. (use_lld || (use_gold && (is_chromeos || !(current_cpu == "x86" ||
  185. current_cpu == "x64"))))
  186. }
  187.  
  188. if (is_android || (is_chromeos_ash && is_chromeos_device)) {
  189. # Set the path to use orderfile for linking Chrome
  190. # Note that this is for using only one orderfile for linking
  191. # the Chrome binary/library.
  192. declare_args() {
  193. chrome_orderfile_path = ""
  194.  
  195. if (defined(default_chrome_orderfile)) {
  196. # Allow downstream tools to set orderfile path with
  197. # another variable.
  198. chrome_orderfile_path = default_chrome_orderfile
  199. } else if (is_chromeos_ash && is_chromeos_device) {
  200. chrome_orderfile_path = "//chromeos/profiles/chromeos.orderfile.txt"
  201. }
  202. }
  203. }
  204.  
  205. assert(!(llvm_force_head_revision && use_goma),
  206. "can't use goma with trunk clang")
  207. assert(!(llvm_force_head_revision && use_remoteexec),
  208. "can't use rbe with trunk clang")
  209.  
  210. # default_include_dirs ---------------------------------------------------------
  211. #
  212. # This is a separate config so that third_party code (which would not use the
  213. # source root and might have conflicting versions of some headers) can remove
  214. # this and specify their own include paths.
  215. config("default_include_dirs") {
  216. include_dirs = [
  217. "//",
  218. root_gen_dir,
  219. ]
  220. }
  221.  
  222. # Compiler instrumentation can introduce dependencies in DSOs to symbols in
  223. # the executable they are loaded into, so they are unresolved at link-time.
  224. config("no_unresolved_symbols") {
  225. if (!using_sanitizer &&
  226. (is_linux || is_chromeos || is_android || is_fuchsia)) {
  227. ldflags = [
  228. "-Wl,-z,defs",
  229. "-Wl,--as-needed",
  230. ]
  231. }
  232. }
  233.  
  234. # compiler ---------------------------------------------------------------------
  235. #
  236. # Base compiler configuration.
  237. #
  238. # See also "runtime_library" below for related stuff and a discussion about
  239. # where stuff should go. Put warning related stuff in the "warnings" config.
  240.  
  241. config("compiler") {
  242. asmflags = []
  243. cflags = []
  244. cflags_c = []
  245. cflags_cc = []
  246. cflags_objc = []
  247. cflags_objcc = []
  248. ldflags = []
  249. defines = []
  250. configs = []
  251.  
  252. # System-specific flags. If your compiler flags apply to one of the
  253. # categories here, add it to the associated file to keep this shared config
  254. # smaller.
  255. if (is_win) {
  256. configs += [ "//build/config/win:compiler" ]
  257. } else if (is_android) {
  258. configs += [ "//build/config/android:compiler" ]
  259. } else if (is_linux || is_chromeos) {
  260. configs += [ "//build/config/linux:compiler" ]
  261. } else if (is_nacl) {
  262. configs += [ "//build/config/nacl:compiler" ]
  263. } else if (is_mac) {
  264. configs += [ "//build/config/mac:compiler" ]
  265. } else if (is_ios) {
  266. configs += [ "//build/config/ios:compiler" ]
  267. } else if (is_fuchsia) {
  268. configs += [ "//build/config/fuchsia:compiler" ]
  269. } else if (current_os == "aix") {
  270. configs += [ "//build/config/aix:compiler" ]
  271. } else if (current_os == "zos") {
  272. configs += [ "//build/config/zos:compiler" ]
  273. }
  274.  
  275. configs += [
  276. # See the definitions below.
  277. ":clang_revision",
  278. ":rustc_revision",
  279. ":compiler_cpu_abi",
  280. ":compiler_codegen",
  281. ":compiler_deterministic",
  282. ]
  283.  
  284. # Here we enable -fno-delete-null-pointer-checks, which makes various nullptr
  285. # operations (e.g. dereferencing) into defined behavior. This avoids deletion
  286. # of some security-critical code: see https://crbug.com/1139129.
  287. # Nacl does not support the flag. And, we still want UBSAN to catch undefined
  288. # behavior related to nullptrs, so do not add this flag if UBSAN is enabled.
  289. # GCC seems to have some bugs compiling constexpr code when this is defined,
  290. # so only enable it if using_clang. See: https://gcc.gnu.org/PR97913
  291. # TODO(mpdenton): remove is_clang once GCC bug is fixed.
  292. if (!is_nacl && !is_ubsan && is_clang) {
  293. cflags += [ "-fno-delete-null-pointer-checks" ]
  294. }
  295.  
  296. # Don't emit the GCC version ident directives, they just end up in the
  297. # .comment section or debug info taking up binary size, and makes comparing
  298. # .o files built with different compiler versions harder.
  299. if (!is_win || is_clang) {
  300. cflags += [ "-fno-ident" ]
  301. }
  302.  
  303. # In general, Windows is totally different, but all the other builds share
  304. # some common compiler and linker configuration.
  305. if (!is_win) {
  306. # Common POSIX compiler flags setup.
  307. # --------------------------------
  308. cflags += [ "-fno-strict-aliasing" ] # See http://crbug.com/32204
  309.  
  310. # Stack protection.
  311. if (is_apple) {
  312. # The strong variant of the stack protector significantly increases
  313. # binary size, so only enable it in debug mode.
  314. if (is_debug) {
  315. cflags += [ "-fstack-protector-strong" ]
  316. } else {
  317. cflags += [ "-fstack-protector" ]
  318. }
  319. } else if ((is_posix && !is_chromeos && !is_nacl) || is_fuchsia) {
  320. # TODO(phajdan.jr): Use -fstack-protector-strong when our gcc supports it.
  321. # See also https://crbug.com/533294
  322. if (current_os != "zos") {
  323. cflags += [ "--param=ssp-buffer-size=4" ]
  324. }
  325.  
  326. # The x86 toolchain currently has problems with stack-protector.
  327. if (is_android && current_cpu == "x86") {
  328. cflags += [ "-fno-stack-protector" ]
  329. } else if (current_os != "aix") {
  330. # Not available on aix.
  331. cflags += [ "-fstack-protector" ]
  332. }
  333. }
  334.  
  335. if (use_lld) {
  336. ldflags += [ "-fuse-ld=lld" ]
  337. if (lld_path != "") {
  338. ldflags += [ "-B$lld_path" ]
  339. }
  340. }
  341.  
  342. # Linker warnings.
  343. if (fatal_linker_warnings && !is_apple && current_os != "aix" &&
  344. current_os != "zos") {
  345. ldflags += [ "-Wl,--fatal-warnings" ]
  346. }
  347. if (fatal_linker_warnings && is_apple) {
  348. ldflags += [ "-Wl,-fatal_warnings" ]
  349. }
  350. }
  351.  
  352. if (is_clang && is_debug) {
  353. # Allow comparing the address of references and 'this' against 0
  354. # in debug builds. Technically, these can never be null in
  355. # well-defined C/C++ and Clang can optimize such checks away in
  356. # release builds, but they may be used in asserts in debug builds.
  357. cflags_cc += [
  358. "-Wno-undefined-bool-conversion",
  359. "-Wno-tautological-undefined-compare",
  360. ]
  361. }
  362.  
  363. # Non-Apple Posix and Fuchsia compiler flags setup.
  364. # -----------------------------------
  365. if ((is_posix && !is_apple) || is_fuchsia) {
  366. if (enable_profiling) {
  367. if (!is_debug) {
  368. cflags += [ "-g" ]
  369.  
  370. if (enable_full_stack_frames_for_profiling) {
  371. cflags += [
  372. "-fno-inline",
  373. "-fno-optimize-sibling-calls",
  374. ]
  375. }
  376. }
  377. }
  378.  
  379. # Explicitly pass --build-id to ld. Compilers used to always pass this
  380. # implicitly but don't any more (in particular clang when built without
  381. # ENABLE_LINKER_BUILD_ID=ON).
  382. if (is_official_build) {
  383. # The sha1 build id has lower risk of collision but is more expensive to
  384. # compute, so only use it in the official build to avoid slowing down
  385. # links.
  386. ldflags += [ "-Wl,--build-id=sha1" ]
  387. } else if (current_os != "aix" && current_os != "zos") {
  388. ldflags += [ "-Wl,--build-id" ]
  389. }
  390.  
  391. if (!is_android) {
  392. defines += [
  393. # _FILE_OFFSET_BITS=64 should not be set on Android in order to maintain
  394. # the behavior of the Android NDK from earlier versions.
  395. # See https://android-developers.googleblog.com/2017/09/introducing-android-native-development.html
  396. "_FILE_OFFSET_BITS=64",
  397. "_LARGEFILE_SOURCE",
  398. "_LARGEFILE64_SOURCE",
  399. ]
  400. }
  401.  
  402. if (!is_nacl) {
  403. if (exclude_unwind_tables) {
  404. cflags += [
  405. "-fno-unwind-tables",
  406. "-fno-asynchronous-unwind-tables",
  407. ]
  408. defines += [ "NO_UNWIND_TABLES" ]
  409. } else {
  410. cflags += [ "-funwind-tables" ]
  411. }
  412. }
  413. }
  414.  
  415. # Apple compiler flags setup.
  416. # ---------------------------------
  417. if (is_apple) {
  418. # On Intel, clang emits both Apple's "compact unwind" information and
  419. # DWARF eh_frame unwind information by default, for compatibility reasons.
  420. # This flag limits emission of eh_frame information to functions
  421. # whose unwind information can't be expressed in the compact unwind format
  422. # (which in practice means almost everything gets only compact unwind
  423. # entries). This reduces object file size a bit and makes linking a bit
  424. # faster.
  425. # On arm64, this is already the default behavior.
  426. if (current_cpu == "x64") {
  427. asmflags += [ "-femit-dwarf-unwind=no-compact-unwind" ]
  428. cflags += [ "-femit-dwarf-unwind=no-compact-unwind" ]
  429. }
  430. }
  431.  
  432. # Linux/Android/Fuchsia common flags setup.
  433. # ---------------------------------
  434. if (is_linux || is_chromeos || is_android || is_fuchsia) {
  435. asmflags += [ "-fPIC" ]
  436. cflags += [ "-fPIC" ]
  437. ldflags += [ "-fPIC" ]
  438.  
  439. if (!is_clang) {
  440. # Use pipes for communicating between sub-processes. Faster.
  441. # (This flag doesn't do anything with Clang.)
  442. cflags += [ "-pipe" ]
  443. }
  444.  
  445. ldflags += [
  446. "-Wl,-z,noexecstack",
  447. "-Wl,-z,relro",
  448. ]
  449.  
  450. if (!is_component_build) {
  451. ldflags += [ "-Wl,-z,now" ]
  452. }
  453. }
  454.  
  455. # Linux-specific compiler flags setup.
  456. # ------------------------------------
  457. if (use_gold) {
  458. ldflags += [ "-fuse-ld=gold" ]
  459. if (!is_android) {
  460. # On Android, this isn't needed. gcc in the NDK knows to look next to
  461. # it with -fuse-ld=gold, and clang gets a --gcc-toolchain flag passed
  462. # above.
  463. if (gold_path != "") {
  464. ldflags += [ "-B$gold_path" ]
  465. }
  466.  
  467. ldflags += [
  468. # Experimentation found that using four linking threads
  469. # saved ~20% of link time.
  470. # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
  471. # Only apply this to the target linker, since the host
  472. # linker might not be gold, but isn't used much anyway.
  473. "-Wl,--threads",
  474. "-Wl,--thread-count=4",
  475. ]
  476. }
  477.  
  478. # TODO(thestig): Make this flag work with GN.
  479. #if (!is_official_build && !is_chromeos && !(is_asan || is_lsan || is_tsan || is_msan)) {
  480. # ldflags += [
  481. # "-Wl,--detect-odr-violations",
  482. # ]
  483. #}
  484. }
  485.  
  486. if (use_icf && (!is_apple || use_lld)) {
  487. ldflags += [ "-Wl,--icf=all" ]
  488. }
  489.  
  490. if (is_linux || is_chromeos) {
  491. cflags += [ "-pthread" ]
  492. # Do not use the -pthread ldflag here since it becomes a no-op
  493. # when using -nodefaultlibs, which would cause an unused argument
  494. # error. "-lpthread" is added in //build/config:default_libs.
  495. }
  496.  
  497. # Clang-specific compiler flags setup.
  498. # ------------------------------------
  499. if (is_clang) {
  500. cflags += [ "-fcolor-diagnostics" ]
  501.  
  502. # Enable -fmerge-all-constants. This used to be the default in clang
  503. # for over a decade. It makes clang non-conforming, but is fairly safe
  504. # in practice and saves some binary size. We might want to consider
  505. # disabling this (https://bugs.llvm.org/show_bug.cgi?id=18538#c13),
  506. # but for now it looks like our build might rely on it
  507. # (https://crbug.com/829795).
  508. cflags += [ "-fmerge-all-constants" ]
  509. }
  510.  
  511. if (use_lld) {
  512. # TODO(thakis): Make the driver pass --color-diagnostics to the linker
  513. # if -fcolor-diagnostics is passed to it, and pass -fcolor-diagnostics
  514. # in ldflags instead.
  515. if (is_win) {
  516. # On Windows, we call the linker directly, instead of calling it through
  517. # the driver.
  518. ldflags += [ "--color-diagnostics" ]
  519. } else {
  520. ldflags += [ "-Wl,--color-diagnostics" ]
  521. }
  522. }
  523.  
  524. # Enable text section splitting only on linux when using lld for now. Other
  525. # platforms can be added later if needed.
  526. if ((is_linux || is_chromeos) && use_lld && use_text_section_splitting) {
  527. ldflags += [ "-Wl,-z,keep-text-section-prefix" ]
  528. }
  529.  
  530. if (is_clang && !is_nacl && current_os != "zos") {
  531. cflags += [ "-fcrash-diagnostics-dir=" + clang_diagnostic_dir ]
  532.  
  533. # TODO(hans): Remove this once Clang generates better optimized debug info
  534. # by default. https://crbug.com/765793
  535. cflags += [
  536. "-mllvm",
  537. "-instcombine-lower-dbg-declare=0",
  538. ]
  539. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  540. if (is_win) {
  541. ldflags += [ "-mllvm:-instcombine-lower-dbg-declare=0" ]
  542. } else {
  543. ldflags += [ "-Wl,-mllvm,-instcombine-lower-dbg-declare=0" ]
  544. }
  545. }
  546.  
  547. # TODO(crbug.com/1235145): Investigate why/if this should be needed.
  548. if (is_win) {
  549. cflags += [ "/clang:-ffp-contract=off" ]
  550. } else {
  551. # cflags += [ "-ffp-contract=off" ]
  552. }
  553. }
  554.  
  555. # Rust compiler setup (for either clang or rustc).
  556. if (enable_rust) {
  557. defines += [ "RUST_ENABLED" ]
  558. }
  559.  
  560. # C11/C++11 compiler flags setup.
  561. # ---------------------------
  562. if (is_linux || is_chromeos || is_android || (is_nacl && is_clang) ||
  563. current_os == "aix") {
  564. if (is_clang) {
  565. standard_prefix = "c"
  566.  
  567. # Since we build with -std=c* and not -std=gnu*, _GNU_SOURCE will not be
  568. # defined by the compiler. However, lots of code relies on the
  569. # non-standard features that _GNU_SOURCE enables, so define it manually.
  570. defines += [ "_GNU_SOURCE" ]
  571.  
  572. if (is_nacl) {
  573. # Undefine __STRICT_ANSI__ to get non-standard features which would
  574. # otherwise not be enabled by NaCl's sysroots.
  575. cflags += [ "-U__STRICT_ANSI__" ]
  576. }
  577. } else {
  578. # Gcc does not support ##__VA_ARGS__ when in standards-conforming mode,
  579. # but we use this feature in several places in Chromium.
  580. # TODO(thomasanderson): Replace usages of ##__VA_ARGS__ with the
  581. # standard-compliant __VA_OPT__ added by C++20, and switch the gcc build
  582. # to -std=c*.
  583. standard_prefix = "gnu"
  584. }
  585.  
  586. cflags_c += [ "-std=${standard_prefix}11" ]
  587. if (is_nacl && !is_nacl_saigo) {
  588. # This is for the pnacl_newlib toolchain. It's only used to build
  589. # a few independent ppapi test files that don't pull in any other
  590. # dependencies.
  591. cflags_cc += [ "-std=${standard_prefix}++14" ]
  592. if (is_clang) {
  593. cflags_cc += [ "-fno-trigraphs" ]
  594. }
  595. } else if (is_linux) {
  596. # TODO(crbug.com/1284275): Switch to C++20 on all platforms.
  597. if (is_clang) {
  598. cflags_cc += [ "-std=${standard_prefix}++20" ]
  599. } else {
  600. # The gcc bots are currently using GCC 9, which is not new enough to
  601. # support "c++20"/"gnu++20".
  602. cflags_cc += [ "-std=${standard_prefix}++2a" ]
  603. }
  604. } else {
  605. cflags_cc += [ "-std=${standard_prefix}++17" ]
  606. }
  607. } else if (is_win) {
  608. cflags_c += [ "/std:c11" ]
  609. cflags_cc += [ "/std:c++17" ]
  610. } else if (!is_nacl) {
  611. # TODO(mcgrathr) - the NaCl GCC toolchain doesn't support either
  612. # gnu11/gnu++11 or c11/c++11; we technically don't need this toolchain any
  613. # more, but there are still a few buildbots using it, so until those are
  614. # turned off we need the !is_nacl clause and the (is_nacl && is_clang)
  615. # clause, above.
  616. cflags_c += [ "-std=c11" ]
  617. if (is_mac) {
  618. # TODO(crbug.com/1284275): Switch to C++20 on all platforms.
  619. cflags_cc += [ "-std=c++20" ]
  620. } else {
  621. cflags_cc += [ "-std=c++17" ]
  622. }
  623. }
  624.  
  625. if (is_clang && current_os != "zos") {
  626. # C++17 removes trigraph support, but clang still warns that it ignores
  627. # them when seeing them. Don't.
  628. cflags_cc += [ "-Wno-trigraphs" ]
  629. }
  630.  
  631. # Add flags for link-time optimization. These flags enable
  632. # optimizations/transformations that require whole-program visibility at link
  633. # time, so they need to be applied to all translation units, and we may end up
  634. # with miscompiles if only part of the program is compiled with LTO flags. For
  635. # that reason, we cannot allow targets to enable or disable these flags, for
  636. # example by disabling the optimize configuration.
  637. # TODO(pcc): Make this conditional on is_official_build rather than on gn
  638. # flags for specific features.
  639. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  640. assert(use_lld, "LTO is only supported with lld")
  641.  
  642. cflags += [
  643. "-flto=thin",
  644. "-fsplit-lto-unit",
  645. ]
  646.  
  647. cflags += [
  648. "-Wno-enum-constexpr-conversion",
  649. "-mllvm", "-adce-remove-loops",
  650. "-mllvm", "-enable-cse-in-irtranslator",
  651. "-mllvm", "-enable-cse-in-legalizer",
  652. "-mllvm", "-scalar-evolution-use-expensive-range-sharpening",
  653. "-mllvm", "-loop-rotate-multi",
  654. "-mllvm", "-enable-loopinterchange",
  655. "-mllvm", "-enable-loop-distribute",
  656. "-mllvm", "-enable-loop-flatten",
  657. "-mllvm", "-enable-loop-versioning-licm",
  658. "-mllvm", "-interleave-small-loop-scalar-reduction",
  659. "-mllvm", "-enable-interleaved-mem-accesses",
  660. "-mllvm", "-enable-masked-interleaved-mem-accesses",
  661. "-mllvm", "-enable-gvn-hoist",
  662. "-mllvm", "-gvn-hoist-max-bbs=-1",
  663. "-mllvm", "-gvn-hoist-max-depth=-1",
  664. "-mllvm", "-gvn-hoist-max-chain-length=-1",
  665. ]
  666.  
  667. # Limit the size of the ThinLTO cache to the lesser of 10% of
  668. # available disk space, 40GB and 100000 files.
  669. # cache_policy = "cache_size=10%:cache_size_bytes=40g:cache_size_files=100000"
  670.  
  671. # An import limit of 30 has better performance (per speedometer) and lower
  672. # binary size than the default setting of 100.
  673. # TODO(gbiv): We ideally shouldn't need to specify this; ThinLTO
  674. # should be able to better manage binary size increases on its own.
  675. import_instr_limit = 30
  676.  
  677. if (is_win) {
  678. ldflags += [
  679. "/opt:lldltojobs=all",
  680. "-mllvm:-import-instr-limit=$import_instr_limit",
  681. "/lldltocache:" +
  682. rebase_path("$root_out_dir/thinlto-cache", root_build_dir),
  683. "/lldltocachepolicy:$cache_policy",
  684. ]
  685. } else {
  686. ldflags += [ "-flto=thin" ]
  687.  
  688. ldflags += [
  689. "-Wl,-mllvm,-adce-remove-loops",
  690. "-Wl,-mllvm,-enable-cse-in-irtranslator",
  691. "-Wl,-mllvm,-enable-cse-in-legalizer",
  692. "-Wl,-mllvm,-scalar-evolution-use-expensive-range-sharpening",
  693. "-Wl,-mllvm,-loop-rotate-multi",
  694. "-Wl,-mllvm,-enable-loopinterchange",
  695. "-Wl,-mllvm,-enable-loop-distribute",
  696. "-Wl,-mllvm,-enable-loop-flatten",
  697. "-Wl,-mllvm,-enable-loop-versioning-licm",
  698. "-Wl,-mllvm,-interleave-small-loop-scalar-reduction",
  699. "-Wl,-mllvm,-enable-interleaved-mem-accesses",
  700. "-Wl,-mllvm,-enable-masked-interleaved-mem-accesses",
  701. "-Wl,-mllvm,-enable-gvn-hoist",
  702. "-Wl,-mllvm,-gvn-hoist-max-bbs=-1",
  703. "-Wl,-mllvm,-gvn-hoist-max-depth=-1",
  704. "-Wl,-mllvm,-gvn-hoist-max-chain-length=-1",
  705. ]
  706.  
  707. ldflags += [
  708. "-Wl,-mllvm,-polly",
  709. "-Wl,-mllvm,-polly-detect-profitability-min-per-loop-insts=40",
  710. "-Wl,-mllvm,-polly-invariant-load-hoisting",
  711. "-Wl,-mllvm,-polly-vectorizer=stripmine",
  712. # "-Wl,-mllvm,-pass-remarks-analysis=polly",
  713. ]
  714.  
  715. # Enabling ThinLTO on Chrome OS too, in an effort to reduce the memory
  716. # usage in crbug.com/1038040. Note this will increase build time in
  717. # Chrome OS.
  718.  
  719. # In ThinLTO builds, we run at most one link process at a time,
  720. # and let it use all cores.
  721. # TODO(thakis): Check if '=0' (that is, number of cores, instead
  722. # of "all" which means number of hardware threads) is faster.
  723. ldflags += [ "-Wl,--thinlto-jobs=all" ]
  724. if (is_apple) {
  725. ldflags += [
  726. "-Wl,-cache_path_lto," +
  727. rebase_path("$root_out_dir/thinlto-cache", root_build_dir),
  728. "-Wcrl,object_path_lto",
  729. ]
  730. } else {
  731. # ldflags +=
  732. # [ "-Wl,--thinlto-cache-dir=" +
  733. # rebase_path("$root_out_dir/thinlto-cache", root_build_dir) ]
  734. }
  735.  
  736. # ldflags += [ "-Wl,--thinlto-cache-policy=$cache_policy" ]
  737.  
  738. if (is_chromeos) {
  739. # ARM was originally set lower than x86 to keep the size
  740. # bloat of ThinLTO to <10%, but that's potentially no longer true.
  741. # FIXME(inglorion): maybe tune these?
  742. if (target_cpu == "arm" || target_cpu == "arm64") {
  743. import_instr_limit = 20
  744. }
  745. } else if (is_android) {
  746. # TODO(crbug.com/1308318): Investigate if we can get the > 6% perf win
  747. # of import_instr_limit 30 with a binary size hit smaller than ~2 MiB.
  748. import_instr_limit = 5
  749. }
  750.  
  751. ldflags += [ "-Wl,-mllvm,-import-instr-limit=$import_instr_limit" ]
  752. }
  753.  
  754. # TODO(https://crbug.com/1211155): investigate why this isn't effective on
  755. # arm32.
  756. if (!is_android || current_cpu == "arm64") {
  757. cflags += [ "-fwhole-program-vtables" ]
  758. if (!is_win) {
  759. ldflags += [ "-fwhole-program-vtables" ]
  760. }
  761. }
  762.  
  763. # This flag causes LTO to create an .ARM.attributes section with the correct
  764. # architecture. This is necessary because LLD will refuse to link a program
  765. # unless the architecture revision in .ARM.attributes is sufficiently new.
  766. # TODO(pcc): The contents of .ARM.attributes should be based on the
  767. # -march flag passed at compile time (see llvm.org/pr36291).
  768. if (current_cpu == "arm") {
  769. ldflags += [ "-march=$arm_arch" ]
  770. }
  771. }
  772.  
  773. if (compiler_timing) {
  774. if (is_clang && !is_nacl) {
  775. cflags += [ "-ftime-trace" ]
  776. } else if (is_win) {
  777. cflags += [
  778. # "Documented" here:
  779. # http://aras-p.info/blog/2017/10/23/Best-unknown-MSVC-flag-d2cgsummary/
  780. "/d2cgsummary",
  781. ]
  782. }
  783. }
  784.  
  785. # Pass flag to LLD so Android builds can allow debuggerd to properly symbolize
  786. # stack crashes (http://crbug.com/919499).
  787. if (use_lld && is_android) {
  788. ldflags += [ "-Wl,--no-rosegment" ]
  789. }
  790.  
  791. # LLD does call-graph-sorted binary layout by default when profile data is
  792. # present. On Android this increases binary size due to more thinks for long
  793. # jumps. Turn it off by default and enable selectively for targets where it's
  794. # beneficial.
  795. if (use_lld && !enable_call_graph_profile_sort) {
  796. if (is_win) {
  797. ldflags += [ "/call-graph-profile-sort:no" ]
  798. } else {
  799. ldflags += [ "-Wl,--no-call-graph-profile-sort" ]
  800. }
  801. }
  802.  
  803. if (is_clang && !is_nacl && show_includes) {
  804. if (is_win) {
  805. # TODO(crbug.com/1223741): Goma mixes the -H and /showIncludes output.
  806. assert(!use_goma, "show_includes on Windows is not reliable with goma")
  807. cflags += [
  808. "/clang:-H",
  809. "/clang:-fshow-skipped-includes",
  810. ]
  811. } else {
  812. cflags += [
  813. "-H",
  814. "-fshow-skipped-includes",
  815. ]
  816. }
  817. }
  818.  
  819. # This flag enforces that member pointer base types are complete. It helps
  820. # prevent us from running into problems in the Microsoft C++ ABI (see
  821. # https://crbug.com/847724).
  822. if (is_clang && !is_nacl && target_os != "chromeos" &&
  823. (is_win || use_custom_libcxx)) {
  824. cflags += [ "-fcomplete-member-pointers" ]
  825. }
  826.  
  827. # MLGO specific flags. These flags enable an ML-based inliner trained on
  828. # Chrome on Android (arm32) with ThinLTO enabled, optimizing for size.
  829. # The "release" ML model is embedded into clang as part of its build.
  830. # Currently, the ML inliner is only enabled when targeting Android due to:
  831. # a) Android is where size matters the most.
  832. # b) MLGO presently has the limitation of only being able to embed one model
  833. # at a time; It is unclear if the embedded model is beneficial for
  834. # non-Android targets.
  835. # MLGO is only officially supported on linux.
  836. if (use_ml_inliner && is_a_target_toolchain) {
  837. assert(
  838. is_android && host_os == "linux",
  839. "MLGO is currently only supported for targeting Android on a linux host")
  840. if (use_thin_lto) {
  841. ldflags += [ "-Wl,-mllvm,-enable-ml-inliner=release" ]
  842. }
  843. }
  844.  
  845. # Pass the same C/C++ flags to the objective C/C++ compiler.
  846. cflags_objc += cflags_c
  847. cflags_objcc += cflags_cc
  848.  
  849. # Assign any flags set for the C compiler to asmflags so that they are sent
  850. # to the assembler. The Windows assembler takes different types of flags
  851. # so only do so for posix platforms.
  852. if (is_posix || is_fuchsia) {
  853. asmflags += cflags
  854. asmflags += cflags_c
  855. }
  856.  
  857. # Rust compiler flags setup.
  858. # ---------------------------
  859. rustflags = [
  860. # Overflow checks are optional in Rust, but even if switched
  861. # off they do not cause undefined behavior (the overflowing
  862. # behavior is defined). Because containers are bounds-checked
  863. # in safe Rust, they also can't provoke buffer overflows.
  864. # As such these checks may be less important in Rust than C++.
  865. # But in (simplistic) testing they have negligible performance
  866. # overhead, and this helps to provide consistent behavior
  867. # between different configurations, so we'll keep them on until
  868. # we discover a reason to turn them off.
  869. "-Coverflow-checks=on",
  870.  
  871. # To make Rust .d files compatible with ninja
  872. "-Zdep-info-omit-d-target",
  873.  
  874. # If a macro panics during compilation, show which macro and where it is
  875. # defined.
  876. "-Zmacro-backtrace",
  877.  
  878. # For deterministic builds, keep the local machine's current working
  879. # directory from appearing in build outputs.
  880. "-Zremap-cwd-prefix=.",
  881. ]
  882. if (rust_abi_target != "") {
  883. rustflags += [ "--target=$rust_abi_target" ]
  884. }
  885. if (use_lto_in_rustc_linking) {
  886. rustflags += [ "-Clinker-plugin-lto" ]
  887. }
  888. if (!use_thin_lto || !use_chromium_rust_toolchain) {
  889. # Don't include bitcode if it won't be used, or can't be used. When
  890. # use_thin_lto is true, we will try to apply LTO to any objects that have
  891. # the appropriate bitcode. But we have to use Chromium's toolchain in order
  892. # to use LTO with rust code. Chromium's rustc will have an LLVM backend that
  893. # matches the C++ clang compiler.
  894. rustflags += [ "-Cembed-bitcode=no" ]
  895. }
  896. if (is_official_build) {
  897. rustflags += [ "-Ccodegen-units=1" ]
  898. }
  899. }
  900.  
  901. # The BUILDCONFIG file sets this config on targets by default, which means when
  902. # building with ThinLTO, no optimization is performed in the link step.
  903. config("thinlto_optimize_default") {
  904. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  905. lto_opt_level = 2
  906.  
  907. if (is_win) {
  908. ldflags = [ "/opt:lldlto=" + lto_opt_level ]
  909. } else {
  910. ldflags = [ "-Wl,--lto-O" + lto_opt_level ]
  911. }
  912. }
  913. }
  914.  
  915. # Use this to enable optimization in the ThinLTO link step for select targets
  916. # when thin_lto_enable_optimizations is set by doing:
  917. #
  918. # configs -= [ "//build/config/compiler:thinlto_optimize_default" ]
  919. # configs += [ "//build/config/compiler:thinlto_optimize_max" ]
  920. #
  921. # Since it makes linking significantly slower and more resource intensive, only
  922. # use it on important targets such as the main browser executable or dll.
  923. config("thinlto_optimize_max") {
  924. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  925. if (thin_lto_enable_optimizations) {
  926. lto_opt_level = 2
  927. } else {
  928. lto_opt_level = 2
  929. }
  930.  
  931. if (is_win) {
  932. ldflags = [ "/opt:lldlto=" + lto_opt_level ]
  933. } else {
  934. ldflags = [ "-Wl,--lto-O" + lto_opt_level ]
  935. ldflags += [ "-Wl,-mllvm,-enable-pre=false", ]
  936. cflags = [ "-march=haswell", "-ffp-contract=fast", ]
  937. }
  938. }
  939. }
  940.  
  941. # This provides the basic options to select the target CPU and ABI.
  942. # It is factored out of "compiler" so that special cases can use this
  943. # without using everything that "compiler" brings in. Options that
  944. # tweak code generation for a particular CPU do not belong here!
  945. # See "compiler_codegen", below.
  946. config("compiler_cpu_abi") {
  947. cflags = []
  948. ldflags = []
  949. defines = []
  950.  
  951. configs = []
  952. if (is_chromeos) {
  953. configs += [ "//build/config/chromeos:compiler_cpu_abi" ]
  954. }
  955.  
  956. if ((is_posix && !is_apple) || is_fuchsia) {
  957. # CPU architecture. We may or may not be doing a cross compile now, so for
  958. # simplicity we always explicitly set the architecture.
  959. if (current_cpu == "x64") {
  960. cflags += [
  961. "-m64",
  962. "-mavx", "-maes", "-mvaes", "-mtune=haswell",
  963. ]
  964. ldflags += [ "-m64" ]
  965. } else if (current_cpu == "x86") {
  966. cflags += [ "-m32" ]
  967. ldflags += [ "-m32" ]
  968. if (!is_nacl) {
  969. cflags += [
  970. "-mfpmath=sse",
  971. "-msse3",
  972. ]
  973. }
  974. } else if (current_cpu == "arm") {
  975. if (is_clang && !is_android && !is_nacl &&
  976. !(is_chromeos_lacros && is_chromeos_device)) {
  977. cflags += [ "--target=arm-linux-gnueabihf" ]
  978. ldflags += [ "--target=arm-linux-gnueabihf" ]
  979. }
  980. if (!is_nacl) {
  981. cflags += [
  982. "-march=$arm_arch",
  983. "-mfloat-abi=$arm_float_abi",
  984. ]
  985. }
  986. if (arm_tune != "") {
  987. cflags += [ "-mtune=$arm_tune" ]
  988. }
  989. } else if (current_cpu == "arm64") {
  990. if (is_clang && !is_android && !is_nacl && !is_fuchsia &&
  991. !(is_chromeos_lacros && is_chromeos_device)) {
  992. cflags += [ "--target=aarch64-linux-gnu" ]
  993. ldflags += [ "--target=aarch64-linux-gnu" ]
  994. }
  995. if (is_android) {
  996. # Outline atomics crash on Exynos 9810. http://crbug.com/1272795
  997. cflags += [ "-mno-outline-atomics" ]
  998. }
  999. } else if (current_cpu == "mipsel" && !is_nacl) {
  1000. ldflags += [ "-Wl,--hash-style=sysv" ]
  1001. if (custom_toolchain == "") {
  1002. if (is_clang) {
  1003. if (is_android) {
  1004. cflags += [ "--target=mipsel-linux-android" ]
  1005. ldflags += [ "--target=mipsel-linux-android" ]
  1006. } else {
  1007. cflags += [ "--target=mipsel-linux-gnu" ]
  1008. ldflags += [ "--target=mipsel-linux-gnu" ]
  1009. }
  1010. } else {
  1011. cflags += [ "-EL" ]
  1012. ldflags += [ "-EL" ]
  1013. }
  1014. }
  1015.  
  1016. if (mips_arch_variant == "r6") {
  1017. cflags += [ "-mno-odd-spreg" ]
  1018. ldflags += [ "-mips32r6" ]
  1019. if (is_clang) {
  1020. cflags += [
  1021. "-march=mipsel",
  1022. "-mcpu=mips32r6",
  1023. ]
  1024. } else {
  1025. cflags += [
  1026. "-mips32r6",
  1027. "-Wa,-mips32r6",
  1028. ]
  1029. if (is_android) {
  1030. ldflags += [ "-Wl,-melf32ltsmip" ]
  1031. }
  1032. }
  1033. if (mips_use_msa == true) {
  1034. cflags += [
  1035. "-mmsa",
  1036. "-mfp64",
  1037. ]
  1038. }
  1039. } else if (mips_arch_variant == "r2") {
  1040. ldflags += [ "-mips32r2" ]
  1041. if (is_clang) {
  1042. cflags += [
  1043. "-march=mipsel",
  1044. "-mcpu=mips32r2",
  1045. ]
  1046. } else {
  1047. cflags += [
  1048. "-mips32r2",
  1049. "-Wa,-mips32r2",
  1050. ]
  1051. if (mips_float_abi == "hard" && mips_fpu_mode != "") {
  1052. cflags += [ "-m$mips_fpu_mode" ]
  1053. }
  1054. }
  1055. } else if (mips_arch_variant == "r1") {
  1056. ldflags += [ "-mips32" ]
  1057. if (is_clang) {
  1058. cflags += [
  1059. "-march=mipsel",
  1060. "-mcpu=mips32",
  1061. ]
  1062. } else {
  1063. cflags += [
  1064. "-mips32",
  1065. "-Wa,-mips32",
  1066. ]
  1067. }
  1068. } else if (mips_arch_variant == "loongson3") {
  1069. defines += [ "_MIPS_ARCH_LOONGSON" ]
  1070. cflags += [
  1071. "-march=loongson3a",
  1072. "-mno-branch-likely",
  1073. "-Wa,-march=loongson3a",
  1074. ]
  1075. }
  1076.  
  1077. if (mips_dsp_rev == 1) {
  1078. cflags += [ "-mdsp" ]
  1079. } else if (mips_dsp_rev == 2) {
  1080. cflags += [ "-mdspr2" ]
  1081. }
  1082.  
  1083. cflags += [ "-m${mips_float_abi}-float" ]
  1084. } else if (current_cpu == "mips" && !is_nacl) {
  1085. ldflags += [ "-Wl,--hash-style=sysv" ]
  1086. if (custom_toolchain == "") {
  1087. if (is_clang) {
  1088. cflags += [ "--target=mips-linux-gnu" ]
  1089. ldflags += [ "--target=mips-linux-gnu" ]
  1090. } else {
  1091. cflags += [ "-EB" ]
  1092. ldflags += [ "-EB" ]
  1093. }
  1094. }
  1095.  
  1096. if (mips_arch_variant == "r6") {
  1097. cflags += [
  1098. "-mips32r6",
  1099. "-Wa,-mips32r6",
  1100. ]
  1101. if (mips_use_msa == true) {
  1102. cflags += [
  1103. "-mmsa",
  1104. "-mfp64",
  1105. ]
  1106. }
  1107. } else if (mips_arch_variant == "r2") {
  1108. cflags += [
  1109. "-mips32r2",
  1110. "-Wa,-mips32r2",
  1111. ]
  1112. if (mips_float_abi == "hard" && mips_fpu_mode != "") {
  1113. cflags += [ "-m$mips_fpu_mode" ]
  1114. }
  1115. } else if (mips_arch_variant == "r1") {
  1116. cflags += [
  1117. "-mips32",
  1118. "-Wa,-mips32",
  1119. ]
  1120. }
  1121.  
  1122. if (mips_dsp_rev == 1) {
  1123. cflags += [ "-mdsp" ]
  1124. } else if (mips_dsp_rev == 2) {
  1125. cflags += [ "-mdspr2" ]
  1126. }
  1127.  
  1128. cflags += [ "-m${mips_float_abi}-float" ]
  1129. } else if (current_cpu == "mips64el") {
  1130. cflags += [ "-D__SANE_USERSPACE_TYPES__" ]
  1131. ldflags += [ "-Wl,--hash-style=sysv" ]
  1132. if (custom_toolchain == "") {
  1133. if (is_clang) {
  1134. if (is_android) {
  1135. cflags += [ "--target=mips64el-linux-android" ]
  1136. ldflags += [ "--target=mips64el-linux-android" ]
  1137. } else {
  1138. cflags += [ "--target=mips64el-linux-gnuabi64" ]
  1139. ldflags += [ "--target=mips64el-linux-gnuabi64" ]
  1140. }
  1141. } else {
  1142. cflags += [
  1143. "-EL",
  1144. "-mabi=64",
  1145. ]
  1146. ldflags += [
  1147. "-EL",
  1148. "-mabi=64",
  1149. ]
  1150. }
  1151. }
  1152.  
  1153. if (mips_arch_variant == "r6") {
  1154. if (is_clang) {
  1155. cflags += [
  1156. "-march=mips64el",
  1157. "-mcpu=mips64r6",
  1158. ]
  1159. } else {
  1160. cflags += [
  1161. "-mips64r6",
  1162. "-Wa,-mips64r6",
  1163. ]
  1164. ldflags += [ "-mips64r6" ]
  1165. }
  1166. if (mips_use_msa == true) {
  1167. cflags += [
  1168. "-mmsa",
  1169. "-mfp64",
  1170. ]
  1171. }
  1172. } else if (mips_arch_variant == "r2") {
  1173. ldflags += [ "-mips64r2" ]
  1174. if (is_clang) {
  1175. cflags += [
  1176. "-march=mips64el",
  1177. "-mcpu=mips64r2",
  1178. ]
  1179. } else {
  1180. cflags += [
  1181. "-mips64r2",
  1182. "-Wa,-mips64r2",
  1183. ]
  1184. }
  1185. } else if (mips_arch_variant == "loongson3") {
  1186. defines += [ "_MIPS_ARCH_LOONGSON" ]
  1187. cflags += [
  1188. "-march=loongson3a",
  1189. "-mno-branch-likely",
  1190. "-Wa,-march=loongson3a",
  1191. ]
  1192. }
  1193. } else if (current_cpu == "mips64") {
  1194. ldflags += [ "-Wl,--hash-style=sysv" ]
  1195. if (custom_toolchain == "") {
  1196. if (is_clang) {
  1197. cflags += [ "--target=mips64-linux-gnuabi64" ]
  1198. ldflags += [ "--target=mips64-linux-gnuabi64" ]
  1199. } else {
  1200. cflags += [
  1201. "-EB",
  1202. "-mabi=64",
  1203. ]
  1204. ldflags += [
  1205. "-EB",
  1206. "-mabi=64",
  1207. ]
  1208. }
  1209. }
  1210.  
  1211. if (mips_arch_variant == "r6") {
  1212. cflags += [
  1213. "-mips64r6",
  1214. "-Wa,-mips64r6",
  1215. ]
  1216. ldflags += [ "-mips64r6" ]
  1217.  
  1218. if (mips_use_msa == true) {
  1219. cflags += [
  1220. "-mmsa",
  1221. "-mfp64",
  1222. ]
  1223. }
  1224. } else if (mips_arch_variant == "r2") {
  1225. cflags += [
  1226. "-mips64r2",
  1227. "-Wa,-mips64r2",
  1228. ]
  1229. ldflags += [ "-mips64r2" ]
  1230. }
  1231. } else if (current_cpu == "ppc64") {
  1232. if (current_os == "aix") {
  1233. cflags += [ "-maix64" ]
  1234. ldflags += [ "-maix64" ]
  1235. } else {
  1236. cflags += [ "-m64" ]
  1237. ldflags += [ "-m64" ]
  1238. }
  1239. } else if (current_cpu == "riscv64") {
  1240. if (is_clang) {
  1241. cflags += [ "--target=riscv64-linux-gnu" ]
  1242. ldflags += [ "--target=riscv64-linux-gnu" ]
  1243. }
  1244. cflags += [ "-mabi=lp64d" ]
  1245. } else if (current_cpu == "loong64") {
  1246. if (is_clang) {
  1247. cflags += [ "--target=loongarch64-linux-gnu" ]
  1248. ldflags += [ "--target=loongarch64-linux-gnu" ]
  1249. }
  1250. cflags += [
  1251. "-mabi=lp64d",
  1252. "-mcmodel=medium",
  1253. ]
  1254. } else if (current_cpu == "s390x") {
  1255. cflags += [ "-m64" ]
  1256. ldflags += [ "-m64" ]
  1257. }
  1258. }
  1259.  
  1260. asmflags = cflags
  1261. }
  1262.  
  1263. # This provides options to tweak code generation that are necessary
  1264. # for particular Chromium code or for working around particular
  1265. # compiler bugs (or the combination of the two).
  1266. config("compiler_codegen") {
  1267. configs = []
  1268. cflags = []
  1269. ldflags = []
  1270.  
  1271. if (is_nacl) {
  1272. configs += [ "//build/config/nacl:compiler_codegen" ]
  1273. }
  1274.  
  1275. if (current_cpu == "arm64" && is_android) {
  1276. # On arm64 disable outlining for Android. See crbug.com/931297 for more
  1277. # information.
  1278. cflags += [ "-mno-outline" ]
  1279.  
  1280. # This can be removed once https://bugs.llvm.org/show_bug.cgi?id=40348
  1281. # has been resolved, and -mno-outline is obeyed by the linker during
  1282. # ThinLTO.
  1283. ldflags += [ "-Wl,-mllvm,-enable-machine-outliner=never" ]
  1284. }
  1285.  
  1286. asmflags = cflags
  1287. }
  1288.  
  1289. # This provides options that make the build deterministic, so that the same
  1290. # revision produces the same output, independent of the name of the build
  1291. # directory and of the computer the build is done on.
  1292. # The relative path from build dir to source dir makes it into the build
  1293. # outputs, so it's recommended that you use a build dir two levels deep
  1294. # (e.g. "out/Release") so that you get the same "../.." path as all the bots
  1295. # in your build outputs.
  1296. config("compiler_deterministic") {
  1297. cflags = []
  1298. ldflags = []
  1299. swiftflags = []
  1300.  
  1301. # Eliminate build metadata (__DATE__, __TIME__ and __TIMESTAMP__) for
  1302. # deterministic build. See https://crbug.com/314403
  1303. if (!is_official_build) {
  1304. if (is_win && !is_clang) {
  1305. cflags += [
  1306. "/wd4117", # Trying to define or undefine a predefined macro.
  1307. "/D__DATE__=",
  1308. "/D__TIME__=",
  1309. "/D__TIMESTAMP__=",
  1310. ]
  1311. } else {
  1312. cflags += [
  1313. "-Wno-builtin-macro-redefined",
  1314. "-D__DATE__=",
  1315. "-D__TIME__=",
  1316. "-D__TIMESTAMP__=",
  1317. ]
  1318. }
  1319. }
  1320.  
  1321. # Makes builds independent of absolute file path.
  1322. if (is_clang && strip_absolute_paths_from_debug_symbols) {
  1323. # If debug option is given, clang includes $cwd in debug info by default.
  1324. # For such build, this flag generates reproducible obj files even we use
  1325. # different build directory like "out/feature_a" and "out/feature_b" if
  1326. # we build same files with same compile flag.
  1327. # Other paths are already given in relative, no need to normalize them.
  1328. if (is_nacl) {
  1329. # TODO(https://crbug.com/1231236): Use -ffile-compilation-dir= here.
  1330. cflags += [
  1331. "-Xclang",
  1332. "-fdebug-compilation-dir",
  1333. "-Xclang",
  1334. ".",
  1335. ]
  1336. } else {
  1337. # -ffile-compilation-dir is an alias for both -fdebug-compilation-dir=
  1338. # and -fcoverage-compilation-dir=.
  1339. cflags += [ "-ffile-compilation-dir=." ]
  1340. swiftflags += [ "-file-compilation-dir=." ]
  1341. }
  1342. if (!is_win) {
  1343. # We don't use clang -cc1as on Windows (yet? https://crbug.com/762167)
  1344. asmflags = [ "-Wa,-fdebug-compilation-dir,." ]
  1345. }
  1346.  
  1347. if (is_win && use_lld) {
  1348. if (symbol_level == 2 || (is_clang && using_sanitizer)) {
  1349. # Absolutize source file paths for PDB. Pass the real build directory
  1350. # if the pdb contains source-level debug information and if linker
  1351. # reproducibility is not critical.
  1352. ldflags += [ "/PDBSourcePath:" + rebase_path(root_build_dir) ]
  1353. } else {
  1354. # Use a fake fixed base directory for paths in the pdb to make the pdb
  1355. # output fully deterministic and independent of the build directory.
  1356. ldflags += [ "/PDBSourcePath:o:\fake\prefix" ]
  1357. }
  1358. }
  1359. }
  1360.  
  1361. # Tells the compiler not to use absolute paths when passing the default
  1362. # paths to the tools it invokes. We don't want this because we don't
  1363. # really need it and it can mess up the goma cache entries.
  1364. if (is_clang && (!is_nacl || is_nacl_saigo)) {
  1365. cflags += [ "-no-canonical-prefixes" ]
  1366.  
  1367. # Same for links: Let the compiler driver invoke the linker
  1368. # with a relative path and pass relative paths to built-in
  1369. # libraries. Not needed on Windows because we call the linker
  1370. # directly there, not through the compiler driver.
  1371. # We don't link on goma, so this change is just for cleaner
  1372. # internal linker invocations, for people who work on the build.
  1373. if (!is_win) {
  1374. ldflags += [ "-no-canonical-prefixes" ]
  1375. }
  1376. }
  1377. }
  1378.  
  1379. config("clang_revision") {
  1380. if (is_clang && clang_base_path == default_clang_base_path) {
  1381. update_args = [
  1382. "--print-revision",
  1383. "--verify-version=$clang_version",
  1384. ]
  1385. if (llvm_force_head_revision) {
  1386. update_args += [ "--llvm-force-head-revision" ]
  1387. }
  1388. clang_revision = exec_script("//tools/clang/scripts/update.py",
  1389. update_args,
  1390. "trim string")
  1391.  
  1392. # This is here so that all files get recompiled after a clang roll and
  1393. # when turning clang on or off. (defines are passed via the command line,
  1394. # and build system rebuild things when their commandline changes). Nothing
  1395. # should ever read this define.
  1396. defines = [ "CR_CLANG_REVISION=\"$clang_revision\"" ]
  1397. }
  1398. }
  1399.  
  1400. config("rustc_revision") {
  1401. if (enable_rust && defined(rustc_version)) {
  1402. # Similar to the above config, this is here so that all files get
  1403. # recompiled after a rustc roll. Nothing should ever read this cfg.
  1404. # $rustc_version is a gn arg set within //build/config/rust.gni
  1405. # so that users using a custom Rust toolchain can override it.
  1406. # Its accuracy is checked in //build/rust/std:find_stdlib, which
  1407. # most of our Rust targets depend upon.
  1408. rustflags = [
  1409. "--cfg",
  1410. "rustc_version=\"$rustc_version\"",
  1411. ]
  1412. }
  1413. }
  1414.  
  1415. config("compiler_arm_fpu") {
  1416. if (current_cpu == "arm" && !is_ios && !is_nacl) {
  1417. cflags = [ "-mfpu=$arm_fpu" ]
  1418. if (!arm_use_thumb) {
  1419. cflags += [ "-marm" ]
  1420. }
  1421. asmflags = cflags
  1422. }
  1423. }
  1424.  
  1425. config("compiler_arm_thumb") {
  1426. if (current_cpu == "arm" && arm_use_thumb && is_posix &&
  1427. !(is_apple || is_nacl)) {
  1428. cflags = [ "-mthumb" ]
  1429. }
  1430. }
  1431.  
  1432. config("compiler_arm") {
  1433. if (current_cpu == "arm" && is_chromeos) {
  1434. # arm is normally the default mode for clang, but on chromeos a wrapper
  1435. # is used to pass -mthumb, and therefor change the default.
  1436. cflags = [ "-marm" ]
  1437. }
  1438. }
  1439.  
  1440. # runtime_library -------------------------------------------------------------
  1441. #
  1442. # Sets the runtime library and associated options.
  1443. #
  1444. # How do you determine what should go in here vs. "compiler" above? Consider if
  1445. # a target might choose to use a different runtime library (ignore for a moment
  1446. # if this is possible or reasonable on your system). If such a target would want
  1447. # to change or remove your option, put it in the runtime_library config. If a
  1448. # target wants the option regardless, put it in the compiler config.
  1449.  
  1450. config("runtime_library") {
  1451. configs = []
  1452.  
  1453. # The order of this config is important: it must appear before
  1454. # android:runtime_library. This is to ensure libc++ appears before
  1455. # libandroid_support in the -isystem include order. Otherwise, there will be
  1456. # build errors related to symbols declared in math.h.
  1457. if (use_custom_libcxx) {
  1458. configs += [ "//build/config/c++:runtime_library" ]
  1459. }
  1460.  
  1461. # TODO(crbug.com/830987): Come up with a better name for is POSIX + Fuchsia
  1462. # configuration.
  1463. if (is_posix || is_fuchsia) {
  1464. configs += [ "//build/config/posix:runtime_library" ]
  1465. }
  1466.  
  1467. # System-specific flags. If your compiler flags apply to one of the
  1468. # categories here, add it to the associated file to keep this shared config
  1469. # smaller.
  1470. if (is_win) {
  1471. configs += [ "//build/config/win:runtime_library" ]
  1472. } else if (is_linux || is_chromeos) {
  1473. configs += [ "//build/config/linux:runtime_library" ]
  1474. if (is_chromeos) {
  1475. configs += [ "//build/config/chromeos:runtime_library" ]
  1476. }
  1477. } else if (is_ios) {
  1478. configs += [ "//build/config/ios:runtime_library" ]
  1479. } else if (is_mac) {
  1480. configs += [ "//build/config/mac:runtime_library" ]
  1481. } else if (is_android) {
  1482. configs += [ "//build/config/android:runtime_library" ]
  1483. }
  1484.  
  1485. if (is_component_build) {
  1486. defines = [ "COMPONENT_BUILD" ]
  1487. }
  1488. }
  1489.  
  1490. # default_warnings ------------------------------------------------------------
  1491. #
  1492. # Collects all warning flags that are used by default. This is used as a
  1493. # subconfig of both chromium_code and no_chromium_code. This way these
  1494. # flags are guaranteed to appear on the compile command line after -Wall.
  1495. config("default_warnings") {
  1496. cflags = []
  1497. cflags_c = []
  1498. cflags_cc = []
  1499. ldflags = []
  1500.  
  1501. if (is_win) {
  1502. if (treat_warnings_as_errors) {
  1503. cflags += [ "/WX" ]
  1504. }
  1505. if (fatal_linker_warnings) {
  1506. arflags = [ "/WX" ]
  1507. ldflags = [ "/WX" ]
  1508. }
  1509. defines = [
  1510. # Without this, Windows headers warn that functions like wcsnicmp
  1511. # should be spelled _wcsnicmp. But all other platforms keep spelling
  1512. # it wcsnicmp, making this warning unhelpful. We don't want it.
  1513. "_CRT_NONSTDC_NO_WARNINGS",
  1514.  
  1515. # TODO(thakis): winsock wants us to use getaddrinfo instead of
  1516. # gethostbyname. Fires mostly in non-Chromium code. We probably
  1517. # want to remove this define eventually.
  1518. "_WINSOCK_DEPRECATED_NO_WARNINGS",
  1519. ]
  1520. if (!is_clang) {
  1521. # TODO(thakis): Remove this once
  1522. # https://swiftshader-review.googlesource.com/c/SwiftShader/+/57968 has
  1523. # rolled into angle.
  1524. cflags += [ "/wd4244" ]
  1525. }
  1526. } else {
  1527. if (is_apple && !is_nacl) {
  1528. # When compiling Objective-C, warns if a method is used whose
  1529. # availability is newer than the deployment target.
  1530. cflags += [ "-Wunguarded-availability" ]
  1531. }
  1532.  
  1533. if (is_ios) {
  1534. # When compiling Objective-C, warns if a selector named via @selector has
  1535. # not been defined in any visible interface.
  1536. cflags += [ "-Wundeclared-selector" ]
  1537. }
  1538.  
  1539. # Suppress warnings about ABI changes on ARM (Clang doesn't give this
  1540. # warning).
  1541. if (current_cpu == "arm" && !is_clang) {
  1542. cflags += [ "-Wno-psabi" ]
  1543. }
  1544.  
  1545. if (!is_clang) {
  1546. cflags_cc += [
  1547. # See comment for -Wno-c++11-narrowing.
  1548. "-Wno-narrowing",
  1549. ]
  1550.  
  1551. # -Wno-class-memaccess warns about hash table and vector in blink.
  1552. # But the violation is intentional.
  1553. if (!is_nacl) {
  1554. cflags_cc += [ "-Wno-class-memaccess" ]
  1555. }
  1556.  
  1557. # -Wunused-local-typedefs is broken in gcc,
  1558. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872
  1559. cflags += [ "-Wno-unused-local-typedefs" ]
  1560.  
  1561. # Don't warn about "maybe" uninitialized. Clang doesn't include this
  1562. # in -Wall but gcc does, and it gives false positives.
  1563. cflags += [ "-Wno-maybe-uninitialized" ]
  1564. cflags += [ "-Wno-deprecated-declarations" ]
  1565.  
  1566. # -Wcomment gives too many false positives in the case a
  1567. # backslash ended comment line is followed by a new line of
  1568. # comments
  1569. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638
  1570. cflags += [ "-Wno-comments" ]
  1571.  
  1572. # -Wpacked-not-aligned complains all generated mojom-shared-internal.h
  1573. # files.
  1574. cflags += [ "-Wno-packed-not-aligned" ]
  1575. }
  1576. }
  1577.  
  1578. # Common Clang and GCC warning setup.
  1579. if (!is_win || is_clang) {
  1580. cflags += [
  1581. # Disables.
  1582. "-Wno-missing-field-initializers", # "struct foo f = {0};"
  1583. "-Wno-unused-parameter", # Unused function parameters.
  1584. ]
  1585. }
  1586.  
  1587. if (is_clang) {
  1588. cflags += [
  1589. "-Wloop-analysis",
  1590.  
  1591. # TODO(thakis): This used to be implied by -Wno-unused-function,
  1592. # which we no longer use. Check if it makes sense to remove
  1593. # this as well. http://crbug.com/316352
  1594. "-Wno-unneeded-internal-declaration",
  1595. ]
  1596.  
  1597. if (!is_nacl || is_nacl_saigo) {
  1598. if (is_win) {
  1599. # TODO(thakis): https://crbug.com/617318
  1600. # Currently goma can not handle case sensitiveness for windows well.
  1601. cflags += [ "-Wno-nonportable-include-path" ]
  1602. }
  1603.  
  1604. cflags += [
  1605. "-Wenum-compare-conditional",
  1606.  
  1607. # An ABI compat warning we don't care about, https://crbug.com/1102157
  1608. # TODO(thakis): Push this to the (few) targets that need it,
  1609. # instead of having a global flag.
  1610. "-Wno-psabi",
  1611.  
  1612. # Ignore warnings about MSVC optimization pragmas.
  1613. # TODO(thakis): Only for no_chromium_code? http://crbug.com/912662
  1614. "-Wno-ignored-pragma-optimize",
  1615. ]
  1616.  
  1617. if (!is_nacl) {
  1618. cflags += [
  1619. # TODO(crbug.com/1343975) Evaluate and possibly enable.
  1620. "-Wno-deprecated-builtins",
  1621.  
  1622. # TODO(crbug.com/1352183) Evaluate and possibly enable.
  1623. "-Wno-bitfield-constant-conversion",
  1624. ]
  1625. }
  1626. }
  1627. }
  1628. }
  1629.  
  1630. # prevent_unsafe_narrowing ----------------------------------------------------
  1631. #
  1632. # Warnings that prevent narrowing or comparisons of integer types that are
  1633. # likely to cause out-of-bound read/writes or Undefined Behaviour. In
  1634. # particular, size_t is used for memory sizes, allocation, indexing, and
  1635. # offsets. Using other integer types along with size_t produces risk of
  1636. # memory-safety bugs and thus security exploits.
  1637. #
  1638. # In order to prevent these bugs, allocation sizes were historically limited to
  1639. # sizes that can be represented within 31 bits of information, allowing `int` to
  1640. # be safely misused instead of `size_t` (https://crbug.com/169327). In order to
  1641. # support increasing the allocation limit we require strictly adherence to
  1642. # using the correct types, avoiding lossy conversions, and preventing overflow.
  1643. # To do so, enable this config and fix errors by converting types to be
  1644. # `size_t`, which is both large enough and unsigned, when dealing with memory
  1645. # sizes, allocations, indices, or offsets.In cases where type conversion is not
  1646. # possible or is superfluous, use base::strict_cast<> or base::checked_cast<>
  1647. # to convert to size_t as needed.
  1648. # See also: https://docs.google.com/document/d/14yKUwDaorqqNfgdGqHY_nck2nn02XBQcB5N0ue4fax8
  1649. #
  1650. # To enable in a GN target, use:
  1651. # configs += [ "//build/config/compiler:prevent_unsafe_narrowing" ]
  1652.  
  1653. config("prevent_unsafe_narrowing") {
  1654. if (is_clang) {
  1655. cflags = [
  1656. "-Wshorten-64-to-32",
  1657. "-Wimplicit-int-conversion",
  1658. "-Wsign-compare",
  1659. "-Wsign-conversion",
  1660. ]
  1661. if (!is_nacl) {
  1662. cflags += [
  1663. # Avoid bugs of the form `if (size_t i = size; i >= 0; --i)` while
  1664. # fixing types to be sign-correct.
  1665. "-Wtautological-unsigned-zero-compare",
  1666. ]
  1667. }
  1668. }
  1669. }
  1670.  
  1671. # chromium_code ---------------------------------------------------------------
  1672. #
  1673. # Toggles between higher and lower warnings for code that is (or isn't)
  1674. # part of Chromium.
  1675.  
  1676. config("chromium_code") {
  1677. if (is_win) {
  1678. if (is_clang) {
  1679. cflags = [ "/W4" ] # Warning level 4.
  1680.  
  1681. # Opt in to additional [[nodiscard]] on standard library methods.
  1682. defines = [ "_HAS_NODISCARD" ]
  1683. }
  1684. } else {
  1685. cflags = [ "-Wall" ]
  1686. if (treat_warnings_as_errors) {
  1687. cflags += [ "-Werror" ]
  1688.  
  1689. # The compiler driver can sometimes (rarely) emit warnings before calling
  1690. # the actual linker. Make sure these warnings are treated as errors as
  1691. # well.
  1692. ldflags = [ "-Werror" ]
  1693. }
  1694. if (is_clang) {
  1695. # Enable extra warnings for chromium_code when we control the compiler.
  1696. cflags += [ "-Wextra" ]
  1697. }
  1698.  
  1699. # In Chromium code, we define __STDC_foo_MACROS in order to get the
  1700. # C99 macros on Mac and Linux.
  1701. defines = [
  1702. "__STDC_CONSTANT_MACROS",
  1703. "__STDC_FORMAT_MACROS",
  1704. ]
  1705.  
  1706. if (!is_debug && !using_sanitizer && current_cpu != "s390x" &&
  1707. current_cpu != "s390" && current_cpu != "ppc64" &&
  1708. current_cpu != "mips" && current_cpu != "mips64" &&
  1709. current_cpu != "riscv64" && current_cpu != "loong64") {
  1710. # Non-chromium code is not guaranteed to compile cleanly with
  1711. # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
  1712. # disabled, so only do that for Release build.
  1713. defines += [ "_FORTIFY_SOURCE=2" ]
  1714. }
  1715.  
  1716. if (is_mac) {
  1717. cflags_objc = [ "-Wobjc-missing-property-synthesis" ]
  1718. cflags_objcc = [ "-Wobjc-missing-property-synthesis" ]
  1719. }
  1720.  
  1721. if (is_ios) {
  1722. cflags_objc = [ "-Wimplicit-retain-self" ]
  1723. cflags_objcc = cflags_objc
  1724. }
  1725. }
  1726.  
  1727. if (is_clang) {
  1728. cflags += [
  1729. # Warn on missing break statements at the end of switch cases.
  1730. # For intentional fallthrough, use [[fallthrough]].
  1731. "-Wimplicit-fallthrough",
  1732.  
  1733. # Warn on unnecessary extra semicolons outside of function definitions.
  1734. "-Wextra-semi",
  1735. ]
  1736.  
  1737. # TODO(thakis): Enable this more often, https://crbug.com/346399
  1738. # use_libfuzzer: https://crbug.com/1063180
  1739. if ((!is_nacl || is_nacl_saigo) && !use_libfuzzer) {
  1740. cflags += [ "-Wunreachable-code-aggressive" ]
  1741. }
  1742.  
  1743. # Thread safety analysis is broken under nacl: https://crbug.com/982423.
  1744. if (!is_nacl || is_nacl_saigo) {
  1745. cflags += [
  1746. # Thread safety analysis. See base/thread_annotations.h and
  1747. # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
  1748. "-Wthread-safety",
  1749. ]
  1750. }
  1751. }
  1752.  
  1753. configs = [
  1754. ":default_warnings",
  1755. ":noshadowing",
  1756. ]
  1757. }
  1758.  
  1759. config("no_chromium_code") {
  1760. cflags = []
  1761. cflags_cc = []
  1762. defines = []
  1763.  
  1764. if (is_win) {
  1765. if (is_clang) {
  1766. cflags += [ "/W3" ] # Warning level 3.
  1767. }
  1768. cflags += [
  1769. "/wd4800", # Disable warning when forcing value to bool.
  1770. "/wd4267", # TODO(jschuh): size_t to int.
  1771. ]
  1772. } else {
  1773. # GCC may emit unsuppressible warnings so don't add -Werror for no chromium
  1774. # code. crbug.com/589724
  1775. if (treat_warnings_as_errors && is_clang) {
  1776. cflags += [ "-Werror" ]
  1777. ldflags = [ "-Werror" ]
  1778. }
  1779. if (is_clang && !is_nacl) {
  1780. # TODO(thakis): Remove !is_nacl once
  1781. # https://codereview.webrtc.org/1552863002/ made its way into chromium.
  1782. cflags += [ "-Wall" ]
  1783. }
  1784. }
  1785.  
  1786. if (is_clang) {
  1787. cflags += [
  1788. # Lots of third-party libraries have unused variables. Instead of
  1789. # suppressing them individually, we just blanket suppress them here.
  1790. "-Wno-unused-variable",
  1791.  
  1792. # Similarly, we're not going to fix all the C++11 narrowing issues in
  1793. # third-party libraries.
  1794. "-Wno-c++11-narrowing",
  1795. ]
  1796. if (!is_nacl) {
  1797. cflags += [
  1798. # Disabled for similar reasons as -Wunused-variable.
  1799. "-Wno-unused-but-set-variable",
  1800.  
  1801. # TODO(https://crbug.com/1202159): Clean up and enable.
  1802. "-Wno-misleading-indentation",
  1803. ]
  1804. }
  1805. }
  1806.  
  1807. # Rust warnings to ignore in third party dependencies. This list is
  1808. # built from those warnings which are currently in our various Rust
  1809. # third party dependencies, but aren't serious (they're largely
  1810. # stylistic).
  1811. # An alternative policy would be to suppress all warnings in third
  1812. # party Rust code using "--cap-lints allow". This is what cargo does
  1813. # for code outside your own crate, so is worth considering if it
  1814. # turns out that maintaining this list is onerous.
  1815. # (https://doc.rust-lang.org/rustc/lints/levels.html#capping-lints)
  1816. rustflags = [
  1817. "-A",
  1818. "unused_parens",
  1819. "-A",
  1820. "bare_trait_objects",
  1821. "-A",
  1822. "non_fmt_panics",
  1823. "-A",
  1824. "redundant_semicolons",
  1825. "-A",
  1826. "unused_parens",
  1827. "-A",
  1828. "anonymous_parameters",
  1829. "-A",
  1830. "bare_trait_objects",
  1831. "-A",
  1832. "deprecated",
  1833. "-A",
  1834. "non_camel_case_types",
  1835. "-A",
  1836. "unused_imports",
  1837. ]
  1838.  
  1839. configs = [ ":default_warnings" ]
  1840. }
  1841.  
  1842. # noshadowing -----------------------------------------------------------------
  1843. #
  1844. # Allows turning -Wshadow on.
  1845.  
  1846. config("noshadowing") {
  1847. # This flag has to be disabled for nacl because the nacl compiler is too
  1848. # strict about shadowing.
  1849. if (is_clang && (!is_nacl || is_nacl_saigo)) {
  1850. cflags = [ "-Wshadow" ]
  1851. }
  1852. }
  1853.  
  1854. # rtti ------------------------------------------------------------------------
  1855. #
  1856. # Allows turning Run-Time Type Identification on or off.
  1857.  
  1858. config("rtti") {
  1859. if (is_win) {
  1860. cflags_cc = [ "/GR" ]
  1861. } else {
  1862. cflags_cc = [ "-frtti" ]
  1863. }
  1864. }
  1865.  
  1866. config("no_rtti") {
  1867. # Some sanitizer configs may require RTTI to be left enabled globally
  1868. if (!use_rtti) {
  1869. if (is_win) {
  1870. cflags_cc = [ "/GR-" ]
  1871. } else {
  1872. cflags_cc = [ "-fno-rtti" ]
  1873. cflags_objcc = cflags_cc
  1874. }
  1875. }
  1876. }
  1877.  
  1878. # export_dynamic ---------------------------------------------------------------
  1879. #
  1880. # Ensures all exported symbols are added to the dynamic symbol table. This is
  1881. # necessary to expose Chrome's custom operator new() and operator delete() (and
  1882. # other memory-related symbols) to libraries. Otherwise, they might
  1883. # (de)allocate memory on a different heap, which would spell trouble if pointers
  1884. # to heap-allocated memory are passed over shared library boundaries.
  1885. config("export_dynamic") {
  1886. # TODO(crbug.com/1052397): Revisit after target_os flip is completed.
  1887. if (is_linux || is_chromeos_lacros || export_libcxxabi_from_executables) {
  1888. ldflags = [ "-rdynamic" ]
  1889. }
  1890. }
  1891.  
  1892. # thin_archive -----------------------------------------------------------------
  1893. #
  1894. # Enables thin archives on posix, and on windows when the lld linker is used.
  1895. # Regular archives directly include the object files used to generate it.
  1896. # Thin archives merely reference the object files.
  1897. # This makes building them faster since it requires less disk IO, but is
  1898. # inappropriate if you wish to redistribute your static library.
  1899. # This config is added to the global config, so thin archives should already be
  1900. # enabled. If you want to make a distributable static library, you need to do 2
  1901. # things:
  1902. # 1. Set complete_static_lib so that all dependencies of the library make it
  1903. # into the library. See `gn help complete_static_lib` for details.
  1904. # 2. Remove the thin_archive config, so that the .a file actually contains all
  1905. # .o files, instead of just references to .o files in the build directoy
  1906. config("thin_archive") {
  1907. # The macOS and iOS default linker ld64 does not support reading thin
  1908. # archives.
  1909. # TODO(crbug.com/1221615): Enable on is_apple if use_lld once that no longer
  1910. # confuses lldb.
  1911. if ((is_posix && !is_nacl && !is_apple) || is_fuchsia) {
  1912. arflags = [ "-T" ]
  1913. } else if (is_win && use_lld) {
  1914. arflags = [ "/llvmlibthin" ]
  1915. }
  1916. }
  1917.  
  1918. # exceptions -------------------------------------------------------------------
  1919. #
  1920. # Allows turning Exceptions on or off.
  1921. # Note: exceptions are disallowed in Google code.
  1922.  
  1923. config("exceptions") {
  1924. if (is_win) {
  1925. # Enables exceptions in the STL.
  1926. if (!use_custom_libcxx) {
  1927. defines = [ "_HAS_EXCEPTIONS=1" ]
  1928. }
  1929. cflags_cc = [ "/EHsc" ]
  1930. } else {
  1931. cflags_cc = [ "-fexceptions" ]
  1932. cflags_objcc = cflags_cc
  1933. }
  1934. }
  1935.  
  1936. config("no_exceptions") {
  1937. if (is_win) {
  1938. # Disables exceptions in the STL.
  1939. # libc++ uses the __has_feature macro to control whether to use exceptions,
  1940. # so defining this macro is unnecessary. Defining _HAS_EXCEPTIONS to 0 also
  1941. # breaks libc++ because it depends on MSVC headers that only provide certain
  1942. # declarations if _HAS_EXCEPTIONS is 1. Those MSVC headers do not use
  1943. # exceptions, despite being conditional on _HAS_EXCEPTIONS.
  1944. if (!use_custom_libcxx) {
  1945. defines = [ "_HAS_EXCEPTIONS=0" ]
  1946. }
  1947. } else {
  1948. cflags_cc = [ "-fno-exceptions" ]
  1949. cflags_objcc = cflags_cc
  1950. }
  1951. }
  1952.  
  1953. # Warnings ---------------------------------------------------------------------
  1954.  
  1955. # Generate a warning for code that might emit a static initializer.
  1956. # See: //docs/static_initializers.md
  1957. # See: https://groups.google.com/a/chromium.org/d/topic/chromium-dev/B9Q5KTD7iCo/discussion
  1958. config("wglobal_constructors") {
  1959. if (is_clang) {
  1960. cflags = [ "-Wglobal-constructors" ]
  1961. }
  1962. }
  1963.  
  1964. # This will generate warnings when using Clang if code generates exit-time
  1965. # destructors, which will slow down closing the program.
  1966. # TODO(thakis): Make this a blocklist instead, http://crbug.com/101600
  1967. config("wexit_time_destructors") {
  1968. if (is_clang) {
  1969. cflags = [ "-Wexit-time-destructors" ]
  1970. }
  1971. }
  1972.  
  1973. # Some code presumes that pointers to structures/objects are compatible
  1974. # regardless of whether what they point to is already known to be valid.
  1975. # gcc 4.9 and earlier had no way of suppressing this warning without
  1976. # suppressing the rest of them. Here we centralize the identification of
  1977. # the gcc 4.9 toolchains.
  1978. config("no_incompatible_pointer_warnings") {
  1979. cflags = []
  1980. if (is_clang) {
  1981. cflags += [ "-Wno-incompatible-pointer-types" ]
  1982. } else if (current_cpu == "mipsel" || current_cpu == "mips64el") {
  1983. cflags += [ "-w" ]
  1984. } else if (is_chromeos_ash && current_cpu == "arm") {
  1985. cflags += [ "-w" ]
  1986. }
  1987. }
  1988.  
  1989. # Optimization -----------------------------------------------------------------
  1990. #
  1991. # The BUILDCONFIG file sets the "default_optimization" config on targets by
  1992. # default. It will be equivalent to either "optimize" (release) or
  1993. # "no_optimize" (debug) optimization configs.
  1994. #
  1995. # You can override the optimization level on a per-target basis by removing the
  1996. # default config and then adding the named one you want:
  1997. #
  1998. # configs -= [ "//build/config/compiler:default_optimization" ]
  1999. # configs += [ "//build/config/compiler:optimize_max" ]
  2000.  
  2001. # Shared settings for both "optimize" and "optimize_max" configs.
  2002. # IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags.
  2003. if (is_win) {
  2004. common_optimize_on_cflags = [
  2005. "/Ob2", # Both explicit and auto inlining.
  2006. "/Oy-", # Disable omitting frame pointers, must be after /O2.
  2007. "/Zc:inline", # Remove unreferenced COMDAT (faster links).
  2008. ]
  2009. if (!is_asan) {
  2010. common_optimize_on_cflags += [
  2011. # Put data in separate COMDATs. This allows the linker
  2012. # to put bit-identical constants at the same address even if
  2013. # they're unrelated constants, which saves binary size.
  2014. # This optimization can't be used when ASan is enabled because
  2015. # it is not compatible with the ASan ODR checker.
  2016. "/Gw",
  2017. ]
  2018. }
  2019. common_optimize_on_ldflags = []
  2020.  
  2021. # /OPT:ICF is not desirable in Debug builds, since code-folding can result in
  2022. # misleading symbols in stack traces.
  2023. if (!is_debug && !is_component_build) {
  2024. common_optimize_on_ldflags += [ "/OPT:ICF" ] # Redundant COMDAT folding.
  2025. }
  2026.  
  2027. if (is_official_build) {
  2028. common_optimize_on_ldflags += [ "/OPT:REF" ] # Remove unreferenced data.
  2029. # TODO(thakis): Add LTO/PGO clang flags eventually, https://crbug.com/598772
  2030. }
  2031. } else {
  2032. common_optimize_on_cflags = []
  2033. common_optimize_on_ldflags = []
  2034.  
  2035. if (is_android) {
  2036. # TODO(jdduke) Re-enable on mips after resolving linking
  2037. # issues with libc++ (crbug.com/456380).
  2038. if (current_cpu != "mipsel" && current_cpu != "mips64el") {
  2039. common_optimize_on_ldflags += [
  2040. # Warn in case of text relocations.
  2041. "-Wl,--warn-shared-textrel",
  2042. ]
  2043. }
  2044. }
  2045.  
  2046. if (is_apple) {
  2047. common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
  2048.  
  2049. if (is_official_build) {
  2050. common_optimize_on_ldflags += [
  2051. "-Wl,-no_data_in_code_info",
  2052. "-Wl,-no_function_starts",
  2053. ]
  2054. }
  2055. } else if (current_os != "aix" && current_os != "zos") {
  2056. # Non-Mac Posix flags.
  2057. # Aix does not support these.
  2058.  
  2059. common_optimize_on_cflags += [
  2060. # Put data and code in their own sections, so that unused symbols
  2061. # can be removed at link time with --gc-sections.
  2062. "-fdata-sections",
  2063. "-ffunction-sections",
  2064. ]
  2065. if ((!is_nacl || is_nacl_saigo) && is_clang) {
  2066. # We don't care about unique section names, this makes object files a bit
  2067. # smaller.
  2068. # common_optimize_on_cflags += [ "-fno-unique-section-names" ]
  2069. }
  2070.  
  2071. common_optimize_on_ldflags += [
  2072. # Specifically tell the linker to perform optimizations.
  2073. # See http://lwn.net/Articles/192624/ .
  2074. # -O2 enables string tail merge optimization in gold and lld.
  2075. "-Wl,-O2",
  2076. "-Wl,--gc-sections",
  2077. ]
  2078. }
  2079. }
  2080.  
  2081. config("default_stack_frames") {
  2082. if (is_posix || is_fuchsia) {
  2083. if (enable_frame_pointers) {
  2084. cflags = [ "-fno-omit-frame-pointer" ]
  2085.  
  2086. # Omit frame pointers for leaf functions on x86, otherwise building libyuv
  2087. # gives clang's register allocator issues, see llvm.org/PR15798 /
  2088. # crbug.com/233709
  2089. if (is_clang && current_cpu == "x86" && !is_apple) {
  2090. cflags += [ "-momit-leaf-frame-pointer" ]
  2091. }
  2092. } else {
  2093. cflags = [ "-fomit-frame-pointer" ]
  2094. }
  2095. }
  2096. # On Windows, the flag to enable framepointers "/Oy-" must always come after
  2097. # the optimization flag [e.g. "/O2"]. The optimization flag is set by one of
  2098. # the "optimize" configs, see rest of this file. The ordering that cflags are
  2099. # applied is well-defined by the GN spec, and there is no way to ensure that
  2100. # cflags set by "default_stack_frames" is applied after those set by an
  2101. # "optimize" config. Similarly, there is no way to propagate state from this
  2102. # config into the "optimize" config. We always apply the "/Oy-" config in the
  2103. # definition for common_optimize_on_cflags definition, even though this may
  2104. # not be correct.
  2105. }
  2106.  
  2107. # Default "optimization on" config.
  2108. config("optimize") {
  2109. if (is_win) {
  2110. if (chrome_pgo_phase != 2) {
  2111. # Favor size over speed, /O1 must be before the common flags.
  2112. # /O1 implies /Os and /GF.
  2113. cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ]
  2114. } else {
  2115. # PGO requires all translation units to be compiled with /O2. The actual
  2116. # optimization level will be decided based on the profiling data.
  2117. cflags = [ "/O2" ] + common_optimize_on_cflags + [ "/Oi" ]
  2118. }
  2119. } else if (optimize_for_size) {
  2120. # Favor size over speed.
  2121. if (is_clang) {
  2122. cflags = [ "-O2" ] + common_optimize_on_cflags
  2123.  
  2124. if (use_ml_inliner && is_a_target_toolchain) {
  2125. cflags += [
  2126. "-mllvm",
  2127. "-enable-ml-inliner=release",
  2128. ]
  2129. }
  2130. } else {
  2131. cflags = [ "-Os" ] + common_optimize_on_cflags
  2132. }
  2133. } else if (is_chromeos) {
  2134. # TODO(gbiv): This is partially favoring size over speed. CrOS exclusively
  2135. # uses clang, and -Os in clang is more of a size-conscious -O2 than "size at
  2136. # any cost" (AKA -Oz). It'd be nice to:
  2137. # - Make `optimize_for_size` apply to all platforms where we're optimizing
  2138. # for size by default (so, also Windows)
  2139. # - Investigate -Oz here, maybe just for ARM?
  2140. cflags = [ "-Os" ] + common_optimize_on_cflags
  2141. } else {
  2142. cflags = [ "-O2" ] + common_optimize_on_cflags
  2143. }
  2144. if (optimize_for_size) {
  2145. rustflags = [ "-Copt-level=s", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2146. } else {
  2147. rustflags = [ "-Copt-level=3", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2148. }
  2149. ldflags = common_optimize_on_ldflags
  2150. }
  2151.  
  2152. # Turn off optimizations.
  2153. config("no_optimize") {
  2154. if (is_win) {
  2155. cflags = [
  2156. "/Od", # Disable optimization.
  2157. "/Ob0", # Disable all inlining (on by default).
  2158. "/GF", # Enable string pooling (off by default).
  2159. ]
  2160.  
  2161. if (target_cpu == "arm64") {
  2162. # Disable omitting frame pointers for no_optimize build because stack
  2163. # traces on Windows ARM64 rely on it.
  2164. cflags += [ "/Oy-" ]
  2165. }
  2166. } else if (is_android && !android_full_debug) {
  2167. # On Android we kind of optimize some things that don't affect debugging
  2168. # much even when optimization is disabled to get the binary size down.
  2169. if (is_clang) {
  2170. cflags = [ "-Oz" ] + common_optimize_on_cflags
  2171. } else {
  2172. cflags = [ "-Os" ] + common_optimize_on_cflags
  2173. }
  2174.  
  2175. if (!is_component_build) {
  2176. # Required for library partitions. Without this all symbols just end up
  2177. # in the base partition.
  2178. ldflags = [ "-Wl,--gc-sections" ]
  2179. }
  2180. } else if (is_fuchsia) {
  2181. # On Fuchsia, we optimize for size here to reduce the size of debug build
  2182. # packages so they can be run in a KVM. See crbug.com/910243 for details.
  2183. cflags = [ "-Og" ]
  2184. } else {
  2185. cflags = [ "-O0" ]
  2186. ldflags = []
  2187. }
  2188. }
  2189.  
  2190. # Turns up the optimization level. On Windows, this implies whole program
  2191. # optimization and link-time code generation which is very expensive and should
  2192. # be used sparingly.
  2193. config("optimize_max") {
  2194. if (is_nacl && is_nacl_irt) {
  2195. # The NaCl IRT is a special case and always wants its own config.
  2196. # Various components do:
  2197. # if (!is_debug) {
  2198. # configs -= [ "//build/config/compiler:default_optimization" ]
  2199. # configs += [ "//build/config/compiler:optimize_max" ]
  2200. # }
  2201. # So this config has to have the selection logic just like
  2202. # "default_optimization", below.
  2203. configs = [ "//build/config/nacl:irt_optimize" ]
  2204. } else {
  2205. ldflags = common_optimize_on_ldflags
  2206. if (is_win) {
  2207. # Favor speed over size, /O2 must be before the common flags.
  2208. # /O2 implies /Ot, /Oi, and /GF.
  2209. cflags = [ "/O2" ] + common_optimize_on_cflags
  2210. } else if (optimize_for_fuzzing) {
  2211. cflags = [ "-O1" ] + common_optimize_on_cflags
  2212. } else {
  2213. cflags = [ "-O3" ] + common_optimize_on_cflags
  2214. }
  2215. rustflags = [ "-Copt-level=3", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2216. }
  2217. }
  2218.  
  2219. # This config can be used to override the default settings for per-component
  2220. # and whole-program optimization, optimizing the particular target for speed
  2221. # instead of code size. This config is exactly the same as "optimize_max"
  2222. # except that we use -O3 instead of -O2 on non-win, non-IRT platforms.
  2223. #
  2224. # TODO(crbug.com/621335) - rework how all of these configs are related
  2225. # so that we don't need this disclaimer.
  2226. config("optimize_speed") {
  2227. if (is_nacl && is_nacl_irt) {
  2228. # The NaCl IRT is a special case and always wants its own config.
  2229. # Various components do:
  2230. # if (!is_debug) {
  2231. # configs -= [ "//build/config/compiler:default_optimization" ]
  2232. # configs += [ "//build/config/compiler:optimize_max" ]
  2233. # }
  2234. # So this config has to have the selection logic just like
  2235. # "default_optimization", below.
  2236. configs = [ "//build/config/nacl:irt_optimize" ]
  2237. } else {
  2238. ldflags = common_optimize_on_ldflags
  2239. if (is_win) {
  2240. # Favor speed over size, /O2 must be before the common flags.
  2241. # /O2 implies /Ot, /Oi, and /GF.
  2242. cflags = [ "/O2" ] + common_optimize_on_cflags
  2243. } else if (optimize_for_fuzzing) {
  2244. cflags = [ "-O1" ] + common_optimize_on_cflags
  2245. } else {
  2246. cflags = [ "-O3" ] + common_optimize_on_cflags
  2247. }
  2248. rustflags = [ "-Copt-level=3", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2249. }
  2250. }
  2251.  
  2252. config("optimize_fuzzing") {
  2253. cflags = [ "-O1" ] + common_optimize_on_cflags
  2254. rustflags = [ "-Copt-level=1" ]
  2255. ldflags = common_optimize_on_ldflags
  2256. visibility = [ ":default_optimization" ]
  2257. }
  2258.  
  2259. # The default optimization applied to all targets. This will be equivalent to
  2260. # either "optimize" or "no_optimize", depending on the build flags.
  2261. config("default_optimization") {
  2262. if (is_nacl && is_nacl_irt) {
  2263. # The NaCl IRT is a special case and always wants its own config.
  2264. # It gets optimized the same way regardless of the type of build.
  2265. configs = [ "//build/config/nacl:irt_optimize" ]
  2266. } else if (is_debug) {
  2267. configs = [ ":no_optimize" ]
  2268. } else if (optimize_for_fuzzing) {
  2269. assert(!is_win, "Fuzzing optimize level not supported on Windows")
  2270.  
  2271. # Coverage build is quite slow. Using "optimize_for_fuzzing" makes it even
  2272. # slower as it uses "-O1" instead of "-O3". Prevent that from happening.
  2273. assert(!use_clang_coverage,
  2274. "optimize_for_fuzzing=true should not be used with " +
  2275. "use_clang_coverage=true.")
  2276. configs = [ ":optimize_fuzzing" ]
  2277. } else {
  2278. configs = [ ":optimize" ]
  2279. }
  2280. }
  2281.  
  2282. _clang_sample_profile = ""
  2283. if (is_clang && is_a_target_toolchain) {
  2284. if (clang_sample_profile_path != "") {
  2285. _clang_sample_profile = clang_sample_profile_path
  2286. } else if (clang_use_default_sample_profile) {
  2287. assert(build_with_chromium,
  2288. "Our default profiles currently only apply to Chromium")
  2289. assert(is_android || is_chromeos || is_castos,
  2290. "The current platform has no default profile")
  2291. if (is_android || is_castos) {
  2292. _clang_sample_profile = "//chrome/android/profiles/afdo.prof"
  2293. } else {
  2294. assert(chromeos_afdo_platform == "atom" ||
  2295. chromeos_afdo_platform == "bigcore" ||
  2296. chromeos_afdo_platform == "arm",
  2297. "Only atom, bigcore and arm are valid Chrome OS profiles.")
  2298. _clang_sample_profile =
  2299. "//chromeos/profiles/${chromeos_afdo_platform}.afdo.prof"
  2300. }
  2301. }
  2302. }
  2303.  
  2304. # Clang offers a way to assert that AFDO profiles are accurate, which causes it
  2305. # to optimize functions not represented in a profile more aggressively for size.
  2306. # This config can be toggled in cases where shaving off binary size hurts
  2307. # performance too much.
  2308. config("afdo_optimize_size") {
  2309. if (_clang_sample_profile != "" && sample_profile_is_accurate) {
  2310. cflags = [ "-fprofile-sample-accurate" ]
  2311. }
  2312. }
  2313.  
  2314. # GCC and clang support a form of profile-guided optimization called AFDO.
  2315. # There are some targeted places that AFDO regresses, so we provide a separate
  2316. # config to allow AFDO to be disabled per-target.
  2317. config("afdo") {
  2318. if (is_clang) {
  2319. cflags = []
  2320. if (clang_emit_debug_info_for_profiling) {
  2321. # Add the following flags to generate debug info for profiling.
  2322. cflags += [ "-gline-tables-only" ]
  2323. if (!is_nacl) {
  2324. cflags += [ "-fdebug-info-for-profiling" ]
  2325. }
  2326. }
  2327. if (_clang_sample_profile != "") {
  2328. assert(chrome_pgo_phase == 0, "AFDO can't be used in PGO builds")
  2329. rebased_clang_sample_profile =
  2330. rebase_path(_clang_sample_profile, root_build_dir)
  2331. cflags += [ "-fprofile-sample-use=${rebased_clang_sample_profile}" ]
  2332. inputs = [ _clang_sample_profile ]
  2333. }
  2334. } else if (auto_profile_path != "" && is_a_target_toolchain) {
  2335. cflags = [ "-fauto-profile=${auto_profile_path}" ]
  2336. inputs = [ auto_profile_path ]
  2337. }
  2338. }
  2339.  
  2340. # Symbols ----------------------------------------------------------------------
  2341.  
  2342. # The BUILDCONFIG file sets the "default_symbols" config on targets by
  2343. # default. It will be equivalent to one the three specific symbol levels.
  2344. #
  2345. # You can override the symbol level on a per-target basis by removing the
  2346. # default config and then adding the named one you want:
  2347. #
  2348. # configs -= [ "//build/config/compiler:default_symbols" ]
  2349. # configs += [ "//build/config/compiler:symbols" ]
  2350.  
  2351. # A helper config that all configs passing /DEBUG to the linker should
  2352. # include as sub-config.
  2353. config("win_pdbaltpath") {
  2354. visibility = [
  2355. ":minimal_symbols",
  2356. ":symbols",
  2357. ]
  2358.  
  2359. # /DEBUG causes the linker to generate a pdb file, and to write the absolute
  2360. # path to it in the executable file it generates. This flag turns that
  2361. # absolute path into just the basename of the pdb file, which helps with
  2362. # build reproducibility. Debuggers look for pdb files next to executables,
  2363. # so there's minimal downside to always using this. However, post-mortem
  2364. # debugging of Chromium crash dumps and ETW tracing can be complicated by this
  2365. # switch so an option to omit it is important.
  2366. if (!use_full_pdb_paths) {
  2367. ldflags = [ "/pdbaltpath:%_PDB%" ]
  2368. }
  2369. }
  2370.  
  2371. # Full symbols.
  2372. config("symbols") {
  2373. if (is_win) {
  2374. if (is_clang) {
  2375. cflags = [ "/Z7" ] # Debug information in the .obj files.
  2376. } else {
  2377. cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
  2378. }
  2379.  
  2380. if (is_clang && use_lld && use_ghash) {
  2381. cflags += [ "-gcodeview-ghash" ]
  2382. ldflags = [ "/DEBUG:GHASH" ]
  2383. } else {
  2384. ldflags = [ "/DEBUG" ]
  2385. }
  2386.  
  2387. # All configs using /DEBUG should include this:
  2388. configs = [ ":win_pdbaltpath" ]
  2389. } else {
  2390. cflags = []
  2391. if (is_mac && enable_dsyms) {
  2392. # If generating dSYMs, specify -fno-standalone-debug. This was
  2393. # originally specified for https://crbug.com/479841 because dsymutil
  2394. # could not handle a 4GB dSYM file. But dsymutil from Xcodes prior to
  2395. # version 7 also produces debug data that is incompatible with Breakpad
  2396. # dump_syms, so this is still required (https://crbug.com/622406).
  2397. cflags += [ "-fno-standalone-debug" ]
  2398. }
  2399.  
  2400. if (!is_nacl) {
  2401. if (use_dwarf5) {
  2402. cflags += [ "-gdwarf-5" ]
  2403. } else if (!is_apple) {
  2404. # Recent clang versions default to DWARF5 on Linux, and Android is about
  2405. # to switch. TODO: Adopt that in controlled way.
  2406. # Apple platforms still default to 4, so the flag is not needed there.
  2407. cflags += [ "-gdwarf-4" ]
  2408. }
  2409. }
  2410.  
  2411. # The gcc-based nacl compilers don't support -fdebug-compilation-dir (see
  2412. # elsewhere in this file), so they can't have build-dir-independent output.
  2413. # Disable symbols for nacl object files to get deterministic,
  2414. # build-directory-independent output. pnacl and nacl-clang do support that
  2415. # flag, so we can use use -g1 for pnacl and nacl-clang compiles.
  2416. # gcc nacl is is_nacl && !is_clang, pnacl and nacl-clang are && is_clang.
  2417. if ((!is_nacl || is_clang) && current_os != "zos") {
  2418. cflags += [ "-g2" ]
  2419. }
  2420.  
  2421. if (!is_nacl && is_clang && !is_tsan && !is_asan) {
  2422. # gcc generates dwarf-aranges by default on -g1 and -g2. On clang it has
  2423. # to be manually enabled.
  2424. #
  2425. # It is skipped in tsan and asan because enabling it causes some
  2426. # formatting changes in the output which would require fixing bunches
  2427. # of expectation regexps.
  2428. #
  2429. # It is skipped when generating bitcode is enabled as -gdwars-aranges
  2430. # is incompatible with -fembed-bitcode/-fembed-bitcode-marker.
  2431. cflags += [ "-gdwarf-aranges" ]
  2432. }
  2433.  
  2434. if (is_apple) {
  2435. swiftflags = [ "-g" ]
  2436. }
  2437.  
  2438. if (use_debug_fission) {
  2439. cflags += [ "-gsplit-dwarf" ]
  2440. }
  2441. asmflags = cflags
  2442. ldflags = []
  2443.  
  2444. # Split debug info with all thinlto builds except nacl and apple.
  2445. # thinlto requires -gsplit-dwarf in ldflags.
  2446. if (use_debug_fission && use_thin_lto && !is_nacl && !is_apple) {
  2447. ldflags += [ "-gsplit-dwarf" ]
  2448. }
  2449.  
  2450. # TODO(thakis): Figure out if there's a way to make this go for 32-bit,
  2451. # currently we get "warning:
  2452. # obj/native_client/src/trusted/service_runtime/sel_asm/nacl_switch_32.o:
  2453. # DWARF info may be corrupt; offsets in a range list entry are in different
  2454. # sections" there. Maybe just a bug in nacl_switch_32.S.
  2455. _enable_gdb_index =
  2456. symbol_level == 2 && !is_apple && !is_nacl && current_cpu != "x86" &&
  2457. current_os != "zos" && (use_gold || use_lld) &&
  2458. # Disable on non-fission 32-bit Android because it pushes
  2459. # libcomponents_unittests over the 4gb size limit.
  2460. !(is_android && !use_debug_fission && current_cpu != "x64" &&
  2461. current_cpu != "arm64")
  2462. if (_enable_gdb_index) {
  2463. if (is_clang) {
  2464. # This flag enables the GNU-format pubnames and pubtypes sections,
  2465. # which lld needs in order to generate a correct GDB index.
  2466. # TODO(pcc): Try to make lld understand non-GNU-format pubnames
  2467. # sections (llvm.org/PR34820).
  2468. cflags += [ "-ggnu-pubnames" ]
  2469. }
  2470. ldflags += [ "-Wl,--gdb-index" ]
  2471. }
  2472. }
  2473.  
  2474. configs = []
  2475.  
  2476. # Compress debug on 32-bit ARM to stay under 4GB for ChromeOS
  2477. # https://b/243982712.
  2478. if (symbol_level == 2 && is_chromeos_device && !use_debug_fission &&
  2479. !is_nacl && current_cpu == "arm") {
  2480. configs += [ "//build/config:compress_debug_sections" ]
  2481. }
  2482.  
  2483. if (is_clang && (!is_nacl || is_nacl_saigo) && current_os != "zos") {
  2484. if (is_apple) {
  2485. # TODO(https://crbug.com/1050118): Investigate missing debug info on mac.
  2486. # Make sure we don't use constructor homing on mac.
  2487. cflags += [
  2488. "-Xclang",
  2489. "-debug-info-kind=limited",
  2490. ]
  2491. } else {
  2492. # Use constructor homing for debug info. This option reduces debug info
  2493. # by emitting class type info only when constructors are emitted.
  2494. cflags += [
  2495. "-Xclang",
  2496. "-fuse-ctor-homing",
  2497. ]
  2498. }
  2499. }
  2500. rustflags = [ "-g" ]
  2501. }
  2502.  
  2503. # Minimal symbols.
  2504. # This config guarantees to hold symbol for stack trace which are shown to user
  2505. # when crash happens in unittests running on buildbot.
  2506. config("minimal_symbols") {
  2507. if (is_win) {
  2508. # Functions, files, and line tables only.
  2509. cflags = []
  2510.  
  2511. if (is_clang && use_lld && use_ghash) {
  2512. cflags += [ "-gcodeview-ghash" ]
  2513. ldflags = [ "/DEBUG:GHASH" ]
  2514. } else {
  2515. ldflags = [ "/DEBUG" ]
  2516. }
  2517.  
  2518. # All configs using /DEBUG should include this:
  2519. configs = [ ":win_pdbaltpath" ]
  2520.  
  2521. # Enable line tables for clang. MSVC doesn't have an equivalent option.
  2522. if (is_clang) {
  2523. # -gline-tables-only is the same as -g1, but clang-cl only exposes the
  2524. # former.
  2525. cflags += [ "-gline-tables-only" ]
  2526. }
  2527. } else {
  2528. cflags = []
  2529. if (is_mac && !use_dwarf5) {
  2530. # clang defaults to DWARF2 on macOS unless mac_deployment_target is
  2531. # at least 10.11.
  2532. # TODO(thakis): Remove this once mac_deployment_target is 10.11.
  2533. cflags += [ "-gdwarf-4" ]
  2534. } else if (!use_dwarf5 && !is_nacl) {
  2535. # Recent clang versions default to DWARF5 on Linux, and Android is about
  2536. # to switch. TODO: Adopt that in controlled way.
  2537. cflags += [ "-gdwarf-4" ]
  2538. }
  2539.  
  2540. if (use_dwarf5 && !is_nacl) {
  2541. cflags += [ "-gdwarf-5" ]
  2542. }
  2543.  
  2544. # The gcc-based nacl compilers don't support -fdebug-compilation-dir (see
  2545. # elsewhere in this file), so they can't have build-dir-independent output.
  2546. # Disable symbols for nacl object files to get deterministic,
  2547. # build-directory-independent output. pnacl and nacl-clang do support that
  2548. # flag, so we can use use -g1 for pnacl and nacl-clang compiles.
  2549. # gcc nacl is is_nacl && !is_clang, pnacl and nacl-clang are && is_clang.
  2550. if (!is_nacl || is_clang) {
  2551. cflags += [ "-g1" ]
  2552. }
  2553.  
  2554. if (!is_nacl && is_clang && !is_tsan && !is_asan) {
  2555. # See comment for -gdwarf-aranges in config("symbols").
  2556. cflags += [ "-gdwarf-aranges" ]
  2557. }
  2558.  
  2559. ldflags = []
  2560. if (is_android && is_clang) {
  2561. # Android defaults to symbol_level=1 builds, but clang, unlike gcc,
  2562. # doesn't emit DW_AT_linkage_name in -g1 builds.
  2563. # -fdebug-info-for-profiling enables that (and a bunch of other things we
  2564. # don't need), so that we get qualified names in stacks.
  2565. # TODO(thakis): Consider making clang emit DW_AT_linkage_name in -g1 mode;
  2566. # failing that consider doing this on non-Android too.
  2567. cflags += [ "-fdebug-info-for-profiling" ]
  2568. }
  2569.  
  2570. asmflags = cflags
  2571. }
  2572. rustflags = [ "-Cdebuginfo=1" ]
  2573. }
  2574.  
  2575. # This configuration contains function names only. That is, the compiler is
  2576. # told to not generate debug information and the linker then just puts function
  2577. # names in the final debug information.
  2578. config("no_symbols") {
  2579. if (is_win) {
  2580. ldflags = [ "/DEBUG" ]
  2581.  
  2582. # All configs using /DEBUG should include this:
  2583. configs = [ ":win_pdbaltpath" ]
  2584. } else {
  2585. cflags = [ "-g0" ]
  2586. asmflags = cflags
  2587. }
  2588. }
  2589.  
  2590. # Default symbols.
  2591. config("default_symbols") {
  2592. if (symbol_level == 0) {
  2593. configs = [ ":no_symbols" ]
  2594. } else if (symbol_level == 1) {
  2595. configs = [ ":minimal_symbols" ]
  2596. } else if (symbol_level == 2) {
  2597. configs = [ ":symbols" ]
  2598. } else {
  2599. assert(false)
  2600. }
  2601.  
  2602. # This config is removed by base unittests apk.
  2603. if (is_android && is_clang && strip_debug_info) {
  2604. configs += [ ":strip_debug" ]
  2605. }
  2606. }
  2607.  
  2608. config("strip_debug") {
  2609. if (!defined(ldflags)) {
  2610. ldflags = []
  2611. }
  2612. ldflags += [ "-Wl,--strip-debug" ]
  2613. }
  2614.  
  2615. if (is_apple) {
  2616. # On Mac and iOS, this enables support for ARC (automatic ref-counting).
  2617. # See http://clang.llvm.org/docs/AutomaticReferenceCounting.html.
  2618. config("enable_arc") {
  2619. common_flags = [ "-fobjc-arc" ]
  2620. cflags_objc = common_flags
  2621. cflags_objcc = common_flags
  2622. }
  2623. }
  2624.  
  2625. if (is_chromeos_ash && is_chromeos_device) {
  2626. # This config is intended to be a temporary to facilitate
  2627. # the transition to use orderfile in Chrome OS. Once orderfile
  2628. # use becomes a default in Chrome OS, this config should not
  2629. # be needed.
  2630. config("use_orderfile_for_hugepage") {
  2631. if (chrome_orderfile_path != "") {
  2632. defines = [ "CHROMEOS_ORDERFILE_USE" ]
  2633. }
  2634. }
  2635. }
  2636.  
  2637. if (is_android || (is_chromeos_ash && is_chromeos_device)) {
  2638. # Use orderfile for linking Chrome on Android and Chrome OS.
  2639. # This config enables using an orderfile for linking in LLD.
  2640. # TODO: Consider using call graph sort instead, at least on Android.
  2641. config("chrome_orderfile_config") {
  2642. if (chrome_orderfile_path != "" && !enable_call_graph_profile_sort) {
  2643. assert(use_lld)
  2644. _rebased_orderfile = rebase_path(chrome_orderfile_path, root_build_dir)
  2645. ldflags = [
  2646. "-Wl,--symbol-ordering-file",
  2647. "-Wl,$_rebased_orderfile",
  2648. "-Wl,--no-warn-symbol-ordering",
  2649. ]
  2650. inputs = [ chrome_orderfile_path ]
  2651. }
  2652. }
  2653. }
  2654.  
  2655. # Initialize all variables on the stack if needed.
  2656. config("default_init_stack_vars") {
  2657. cflags = []
  2658. if (init_stack_vars && is_clang && !is_nacl && !using_sanitizer) {
  2659. cflags += [ "-ftrivial-auto-var-init=pattern" ]
  2660. }
  2661. }
  2662.  
  2663. buildflag_header("compiler_buildflags") {
  2664. header = "compiler_buildflags.h"
  2665.  
  2666. flags = [
  2667. "CLANG_PGO=$chrome_pgo_phase",
  2668. "SYMBOL_LEVEL=$symbol_level",
  2669. ]
  2670. }
  2671.  
  2672. config("cet_shadow_stack") {
  2673. if (enable_cet_shadow_stack && is_win) {
  2674. assert(target_cpu == "x64")
  2675. ldflags = [ "/CETCOMPAT" ]
  2676. }
  2677. }
  2678.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement