Guest User

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

a guest
Oct 1st, 2022
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 92.84 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=fast" ]
  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 {
  596. cflags_cc += [ "-std=${standard_prefix}++17" ]
  597. }
  598. } else if (is_win) {
  599. cflags_c += [ "/std:c11" ]
  600. cflags_cc += [ "/std:c++17" ]
  601. } else if (!is_nacl) {
  602. # TODO(mcgrathr) - the NaCl GCC toolchain doesn't support either
  603. # gnu11/gnu++11 or c11/c++11; we technically don't need this toolchain any
  604. # more, but there are still a few buildbots using it, so until those are
  605. # turned off we need the !is_nacl clause and the (is_nacl && is_clang)
  606. # clause, above.
  607. cflags_c += [ "-std=c11" ]
  608. if (is_mac) {
  609. # TODO(crbug.com/1284275): Switch to C++20 on all platforms.
  610. cflags_cc += [ "-std=c++20" ]
  611. } else {
  612. cflags_cc += [ "-std=c++17" ]
  613. }
  614. }
  615.  
  616. if (is_clang && current_os != "zos") {
  617. # C++17 removes trigraph support, but clang still warns that it ignores
  618. # them when seeing them. Don't.
  619. cflags_cc += [ "-Wno-trigraphs" ]
  620. }
  621.  
  622. # Add flags for link-time optimization. These flags enable
  623. # optimizations/transformations that require whole-program visibility at link
  624. # time, so they need to be applied to all translation units, and we may end up
  625. # with miscompiles if only part of the program is compiled with LTO flags. For
  626. # that reason, we cannot allow targets to enable or disable these flags, for
  627. # example by disabling the optimize configuration.
  628. # TODO(pcc): Make this conditional on is_official_build rather than on gn
  629. # flags for specific features.
  630. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  631. assert(use_lld, "LTO is only supported with lld")
  632.  
  633. cflags += [
  634. "-flto=thin",
  635. "-fsplit-lto-unit",
  636. ]
  637.  
  638. # Limit the size of the ThinLTO cache to the lesser of 10% of
  639. # available disk space, 40GB and 100000 files.
  640. # cache_policy = "cache_size=10%:cache_size_bytes=40g:cache_size_files=100000"
  641.  
  642. # An import limit of 30 has better performance (per speedometer) and lower
  643. # binary size than the default setting of 100.
  644. # TODO(gbiv): We ideally shouldn't need to specify this; ThinLTO
  645. # should be able to better manage binary size increases on its own.
  646. import_instr_limit = 30
  647.  
  648. cflags += [
  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. if (is_win) {
  668. ldflags += [
  669. "/opt:lldltojobs=all",
  670. "-mllvm:-import-instr-limit=$import_instr_limit",
  671. # "/lldltocache:" +
  672. # rebase_path("$root_out_dir/thinlto-cache", root_build_dir),
  673. # "/lldltocachepolicy:$cache_policy",
  674. ]
  675.  
  676. ldflags += [
  677. "-mllvm:-adce-remove-loops",
  678. "-mllvm:-enable-cse-in-irtranslator",
  679. "-mllvm:-enable-cse-in-legalizer",
  680. "-mllvm:-scalar-evolution-use-expensive-range-sharpening",
  681. "-mllvm:-loop-rotate-multi",
  682. "-mllvm:-enable-loopinterchange",
  683. "-mllvm:-enable-loop-distribute",
  684. "-mllvm:-enable-loop-flatten",
  685. "-mllvm:-enable-loop-versioning-licm",
  686. "-mllvm:-interleave-small-loop-scalar-reduction",
  687. "-mllvm:-enable-interleaved-mem-accesses",
  688. "-mllvm:-enable-masked-interleaved-mem-accesses",
  689. "-mllvm:-enable-gvn-hoist",
  690. "-mllvm:-gvn-hoist-max-bbs=-1",
  691. "-mllvm:-gvn-hoist-max-depth=-1",
  692. "-mllvm:-gvn-hoist-max-chain-length=-1",
  693. ]
  694.  
  695. } else {
  696. ldflags += [ "-flto=thin" ]
  697.  
  698. ldflags += [
  699. "-Wl,-mllvm,-adce-remove-loops",
  700. "-Wl,-mllvm,-enable-cse-in-irtranslator",
  701. "-Wl,-mllvm,-enable-cse-in-legalizer",
  702. "-Wl,-mllvm,-scalar-evolution-use-expensive-range-sharpening",
  703. "-Wl,-mllvm,-loop-rotate-multi",
  704. "-Wl,-mllvm,-enable-loopinterchange",
  705. "-Wl,-mllvm,-enable-loop-distribute",
  706. "-Wl,-mllvm,-enable-loop-flatten",
  707. "-Wl,-mllvm,-enable-loop-versioning-licm",
  708. "-Wl,-mllvm,-interleave-small-loop-scalar-reduction",
  709. "-Wl,-mllvm,-enable-interleaved-mem-accesses",
  710. "-Wl,-mllvm,-enable-masked-interleaved-mem-accesses",
  711. "-Wl,-mllvm,-enable-gvn-hoist",
  712. "-Wl,-mllvm,-gvn-hoist-max-bbs=-1",
  713. "-Wl,-mllvm,-gvn-hoist-max-depth=-1",
  714. "-Wl,-mllvm,-gvn-hoist-max-chain-length=-1",
  715. ]
  716.  
  717. # Enabling ThinLTO on Chrome OS too, in an effort to reduce the memory
  718. # usage in crbug.com/1038040. Note this will increase build time in
  719. # Chrome OS.
  720.  
  721. # In ThinLTO builds, we run at most one link process at a time,
  722. # and let it use all cores.
  723. # TODO(thakis): Check if '=0' (that is, number of cores, instead
  724. # of "all" which means number of hardware threads) is faster.
  725. ldflags += [ "-Wl,--thinlto-jobs=all" ]
  726. if (is_apple) {
  727. ldflags += [
  728. "-Wl,-cache_path_lto," +
  729. rebase_path("$root_out_dir/thinlto-cache", root_build_dir),
  730. "-Wcrl,object_path_lto",
  731. ]
  732. } else {
  733. # ldflags +=
  734. # [ "-Wl,--thinlto-cache-dir=" +
  735. # rebase_path("$root_out_dir/thinlto-cache", root_build_dir) ]
  736. }
  737.  
  738. # ldflags += [ "-Wl,--thinlto-cache-policy=$cache_policy" ]
  739.  
  740. if (is_chromeos) {
  741. # ARM was originally set lower than x86 to keep the size
  742. # bloat of ThinLTO to <10%, but that's potentially no longer true.
  743. # FIXME(inglorion): maybe tune these?
  744. if (target_cpu == "arm" || target_cpu == "arm64") {
  745. import_instr_limit = 20
  746. }
  747. } else if (is_android) {
  748. # TODO(crbug.com/1308318): Investigate if we can get the > 6% perf win
  749. # of import_instr_limit 30 with a binary size hit smaller than ~2 MiB.
  750. import_instr_limit = 5
  751. }
  752.  
  753. ldflags += [ "-Wl,-mllvm,-import-instr-limit=$import_instr_limit" ]
  754. }
  755.  
  756. # TODO(https://crbug.com/1211155): investigate why this isn't effective on
  757. # arm32.
  758. if (!is_android || current_cpu == "arm64") {
  759. cflags += [ "-fwhole-program-vtables" ]
  760. if (!is_win) {
  761. ldflags += [ "-fwhole-program-vtables" ]
  762. }
  763. }
  764.  
  765. # This flag causes LTO to create an .ARM.attributes section with the correct
  766. # architecture. This is necessary because LLD will refuse to link a program
  767. # unless the architecture revision in .ARM.attributes is sufficiently new.
  768. # TODO(pcc): The contents of .ARM.attributes should be based on the
  769. # -march flag passed at compile time (see llvm.org/pr36291).
  770. if (current_cpu == "arm") {
  771. ldflags += [ "-march=$arm_arch" ]
  772. }
  773. }
  774.  
  775. if (compiler_timing) {
  776. if (is_clang && !is_nacl) {
  777. cflags += [ "-ftime-trace" ]
  778. } else if (is_win) {
  779. cflags += [
  780. # "Documented" here:
  781. # http://aras-p.info/blog/2017/10/23/Best-unknown-MSVC-flag-d2cgsummary/
  782. "/d2cgsummary",
  783. ]
  784. }
  785. }
  786.  
  787. # Pass flag to LLD so Android builds can allow debuggerd to properly symbolize
  788. # stack crashes (http://crbug.com/919499).
  789. if (use_lld && is_android) {
  790. ldflags += [ "-Wl,--no-rosegment" ]
  791. }
  792.  
  793. # LLD does call-graph-sorted binary layout by default when profile data is
  794. # present. On Android this increases binary size due to more thinks for long
  795. # jumps. Turn it off by default and enable selectively for targets where it's
  796. # beneficial.
  797. if (use_lld && !enable_call_graph_profile_sort) {
  798. if (is_win) {
  799. ldflags += [ "/call-graph-profile-sort:no" ]
  800. } else {
  801. ldflags += [ "-Wl,--no-call-graph-profile-sort" ]
  802. }
  803. }
  804.  
  805. if (is_clang && !is_nacl && show_includes) {
  806. if (is_win) {
  807. # TODO(crbug.com/1223741): Goma mixes the -H and /showIncludes output.
  808. assert(!use_goma, "show_includes on Windows is not reliable with goma")
  809. cflags += [
  810. "/clang:-H",
  811. "/clang:-fshow-skipped-includes",
  812. ]
  813. } else {
  814. cflags += [
  815. "-H",
  816. "-fshow-skipped-includes",
  817. ]
  818. }
  819. }
  820.  
  821. # This flag enforces that member pointer base types are complete. It helps
  822. # prevent us from running into problems in the Microsoft C++ ABI (see
  823. # https://crbug.com/847724).
  824. if (is_clang && !is_nacl && target_os != "chromeos" &&
  825. (is_win || use_custom_libcxx)) {
  826. cflags += [ "-fcomplete-member-pointers" ]
  827. }
  828.  
  829. # MLGO specific flags. These flags enable an ML-based inliner trained on
  830. # Chrome on Android (arm32) with ThinLTO enabled, optimizing for size.
  831. # The "release" ML model is embedded into clang as part of its build.
  832. # Currently, the ML inliner is only enabled when targeting Android due to:
  833. # a) Android is where size matters the most.
  834. # b) MLGO presently has the limitation of only being able to embed one model
  835. # at a time; It is unclear if the embedded model is beneficial for
  836. # non-Android targets.
  837. # MLGO is only officially supported on linux.
  838. if (use_ml_inliner && is_a_target_toolchain) {
  839. assert(
  840. is_android && host_os == "linux",
  841. "MLGO is currently only supported for targeting Android on a linux host")
  842. if (use_thin_lto) {
  843. ldflags += [ "-Wl,-mllvm,-enable-ml-inliner=release" ]
  844. }
  845. }
  846.  
  847. # Pass the same C/C++ flags to the objective C/C++ compiler.
  848. cflags_objc += cflags_c
  849. cflags_objcc += cflags_cc
  850.  
  851. # Assign any flags set for the C compiler to asmflags so that they are sent
  852. # to the assembler. The Windows assembler takes different types of flags
  853. # so only do so for posix platforms.
  854. if (is_posix || is_fuchsia) {
  855. asmflags += cflags
  856. asmflags += cflags_c
  857. }
  858.  
  859. # Rust compiler flags setup.
  860. # ---------------------------
  861. rustflags = [
  862. # Overflow checks are optional in Rust, but even if switched
  863. # off they do not cause undefined behavior (the overflowing
  864. # behavior is defined). Because containers are bounds-checked
  865. # in safe Rust, they also can't provoke buffer overflows.
  866. # As such these checks may be less important in Rust than C++.
  867. # But in (simplistic) testing they have negligible performance
  868. # overhead, and this helps to provide consistent behavior
  869. # between different configurations, so we'll keep them on until
  870. # we discover a reason to turn them off.
  871. "-Coverflow-checks=on",
  872.  
  873. # To make Rust .d files compatible with ninja
  874. "-Zdep-info-omit-d-target",
  875.  
  876. # If a macro panics during compilation, show which macro and where it is
  877. # defined.
  878. "-Zmacro-backtrace",
  879.  
  880. # For deterministic builds, keep the local machine's current working
  881. # directory from appearing in build outputs.
  882. "-Zremap-cwd-prefix=.",
  883. ]
  884. if (rust_abi_target != "") {
  885. rustflags += [ "--target=$rust_abi_target" ]
  886. }
  887. if (use_lto_in_rustc_linking) {
  888. rustflags += [ "-Clinker-plugin-lto" ]
  889. }
  890. if (!use_thin_lto || !use_chromium_rust_toolchain) {
  891. # Don't include bitcode if it won't be used, or can't be used. When
  892. # use_thin_lto is true, we will try to apply LTO to any objects that have
  893. # the appropriate bitcode. But we have to use Chromium's toolchain in order
  894. # to use LTO with rust code. Chromium's rustc will have an LLVM backend that
  895. # matches the C++ clang compiler.
  896. rustflags += [ "-Cembed-bitcode=no" ]
  897. }
  898. if (is_official_build) {
  899. rustflags += [ "-Ccodegen-units=1" ]
  900. }
  901. }
  902.  
  903. # The BUILDCONFIG file sets this config on targets by default, which means when
  904. # building with ThinLTO, no optimization is performed in the link step.
  905. config("thinlto_optimize_default") {
  906. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  907. lto_opt_level = 2
  908.  
  909. if (is_win) {
  910. ldflags = [ "/opt:lldlto=" + lto_opt_level ]
  911. } else {
  912. ldflags = [ "-Wl,--lto-O" + lto_opt_level ]
  913. }
  914. }
  915. }
  916.  
  917. # Use this to enable optimization in the ThinLTO link step for select targets
  918. # when thin_lto_enable_optimizations is set by doing:
  919. #
  920. # configs -= [ "//build/config/compiler:thinlto_optimize_default" ]
  921. # configs += [ "//build/config/compiler:thinlto_optimize_max" ]
  922. #
  923. # Since it makes linking significantly slower and more resource intensive, only
  924. # use it on important targets such as the main browser executable or dll.
  925. config("thinlto_optimize_max") {
  926. if (!is_debug && use_thin_lto && is_a_target_toolchain) {
  927. if (thin_lto_enable_optimizations) {
  928. lto_opt_level = 2
  929. } else {
  930. lto_opt_level = 2
  931. }
  932.  
  933. if (is_win) {
  934. ldflags = [ "/opt:lldlto=" + lto_opt_level ]
  935. } else {
  936. ldflags = [ "-Wl,--lto-O" + lto_opt_level ]
  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. # "-msse3",
  963. "-mavx",
  964. ]
  965. ldflags += [ "-m64" ]
  966. } else if (current_cpu == "x86") {
  967. cflags += [ "-m32" ]
  968. ldflags += [ "-m32" ]
  969. if (!is_nacl) {
  970. cflags += [
  971. "-mfpmath=sse",
  972. "-msse3",
  973. ]
  974. }
  975. } else if (current_cpu == "arm") {
  976. if (is_clang && !is_android && !is_nacl &&
  977. !(is_chromeos_lacros && is_chromeos_device)) {
  978. cflags += [ "--target=arm-linux-gnueabihf" ]
  979. ldflags += [ "--target=arm-linux-gnueabihf" ]
  980. }
  981. if (!is_nacl) {
  982. cflags += [
  983. "-march=$arm_arch",
  984. "-mfloat-abi=$arm_float_abi",
  985. ]
  986. }
  987. if (arm_tune != "") {
  988. cflags += [ "-mtune=$arm_tune" ]
  989. }
  990. } else if (current_cpu == "arm64") {
  991. if (is_clang && !is_android && !is_nacl && !is_fuchsia &&
  992. !(is_chromeos_lacros && is_chromeos_device)) {
  993. cflags += [ "--target=aarch64-linux-gnu" ]
  994. ldflags += [ "--target=aarch64-linux-gnu" ]
  995. }
  996. if (is_android) {
  997. # Outline atomics crash on Exynos 9810. http://crbug.com/1272795
  998. cflags += [ "-mno-outline-atomics" ]
  999. }
  1000. } else if (current_cpu == "mipsel" && !is_nacl) {
  1001. ldflags += [ "-Wl,--hash-style=sysv" ]
  1002. if (custom_toolchain == "") {
  1003. if (is_clang) {
  1004. if (is_android) {
  1005. cflags += [ "--target=mipsel-linux-android" ]
  1006. ldflags += [ "--target=mipsel-linux-android" ]
  1007. } else {
  1008. cflags += [ "--target=mipsel-linux-gnu" ]
  1009. ldflags += [ "--target=mipsel-linux-gnu" ]
  1010. }
  1011. } else {
  1012. cflags += [ "-EL" ]
  1013. ldflags += [ "-EL" ]
  1014. }
  1015. }
  1016.  
  1017. if (mips_arch_variant == "r6") {
  1018. cflags += [ "-mno-odd-spreg" ]
  1019. ldflags += [ "-mips32r6" ]
  1020. if (is_clang) {
  1021. cflags += [
  1022. "-march=mipsel",
  1023. "-mcpu=mips32r6",
  1024. ]
  1025. } else {
  1026. cflags += [
  1027. "-mips32r6",
  1028. "-Wa,-mips32r6",
  1029. ]
  1030. if (is_android) {
  1031. ldflags += [ "-Wl,-melf32ltsmip" ]
  1032. }
  1033. }
  1034. if (mips_use_msa == true) {
  1035. cflags += [
  1036. "-mmsa",
  1037. "-mfp64",
  1038. ]
  1039. }
  1040. } else if (mips_arch_variant == "r2") {
  1041. ldflags += [ "-mips32r2" ]
  1042. if (is_clang) {
  1043. cflags += [
  1044. "-march=mipsel",
  1045. "-mcpu=mips32r2",
  1046. ]
  1047. } else {
  1048. cflags += [
  1049. "-mips32r2",
  1050. "-Wa,-mips32r2",
  1051. ]
  1052. if (mips_float_abi == "hard" && mips_fpu_mode != "") {
  1053. cflags += [ "-m$mips_fpu_mode" ]
  1054. }
  1055. }
  1056. } else if (mips_arch_variant == "r1") {
  1057. ldflags += [ "-mips32" ]
  1058. if (is_clang) {
  1059. cflags += [
  1060. "-march=mipsel",
  1061. "-mcpu=mips32",
  1062. ]
  1063. } else {
  1064. cflags += [
  1065. "-mips32",
  1066. "-Wa,-mips32",
  1067. ]
  1068. }
  1069. } else if (mips_arch_variant == "loongson3") {
  1070. defines += [ "_MIPS_ARCH_LOONGSON" ]
  1071. cflags += [
  1072. "-march=loongson3a",
  1073. "-mno-branch-likely",
  1074. "-Wa,-march=loongson3a",
  1075. ]
  1076. }
  1077.  
  1078. if (mips_dsp_rev == 1) {
  1079. cflags += [ "-mdsp" ]
  1080. } else if (mips_dsp_rev == 2) {
  1081. cflags += [ "-mdspr2" ]
  1082. }
  1083.  
  1084. cflags += [ "-m${mips_float_abi}-float" ]
  1085. } else if (current_cpu == "mips" && !is_nacl) {
  1086. ldflags += [ "-Wl,--hash-style=sysv" ]
  1087. if (custom_toolchain == "") {
  1088. if (is_clang) {
  1089. cflags += [ "--target=mips-linux-gnu" ]
  1090. ldflags += [ "--target=mips-linux-gnu" ]
  1091. } else {
  1092. cflags += [ "-EB" ]
  1093. ldflags += [ "-EB" ]
  1094. }
  1095. }
  1096.  
  1097. if (mips_arch_variant == "r6") {
  1098. cflags += [
  1099. "-mips32r6",
  1100. "-Wa,-mips32r6",
  1101. ]
  1102. if (mips_use_msa == true) {
  1103. cflags += [
  1104. "-mmsa",
  1105. "-mfp64",
  1106. ]
  1107. }
  1108. } else if (mips_arch_variant == "r2") {
  1109. cflags += [
  1110. "-mips32r2",
  1111. "-Wa,-mips32r2",
  1112. ]
  1113. if (mips_float_abi == "hard" && mips_fpu_mode != "") {
  1114. cflags += [ "-m$mips_fpu_mode" ]
  1115. }
  1116. } else if (mips_arch_variant == "r1") {
  1117. cflags += [
  1118. "-mips32",
  1119. "-Wa,-mips32",
  1120. ]
  1121. }
  1122.  
  1123. if (mips_dsp_rev == 1) {
  1124. cflags += [ "-mdsp" ]
  1125. } else if (mips_dsp_rev == 2) {
  1126. cflags += [ "-mdspr2" ]
  1127. }
  1128.  
  1129. cflags += [ "-m${mips_float_abi}-float" ]
  1130. } else if (current_cpu == "mips64el") {
  1131. cflags += [ "-D__SANE_USERSPACE_TYPES__" ]
  1132. ldflags += [ "-Wl,--hash-style=sysv" ]
  1133. if (custom_toolchain == "") {
  1134. if (is_clang) {
  1135. if (is_android) {
  1136. cflags += [ "--target=mips64el-linux-android" ]
  1137. ldflags += [ "--target=mips64el-linux-android" ]
  1138. } else {
  1139. cflags += [ "--target=mips64el-linux-gnuabi64" ]
  1140. ldflags += [ "--target=mips64el-linux-gnuabi64" ]
  1141. }
  1142. } else {
  1143. cflags += [
  1144. "-EL",
  1145. "-mabi=64",
  1146. ]
  1147. ldflags += [
  1148. "-EL",
  1149. "-mabi=64",
  1150. ]
  1151. }
  1152. }
  1153.  
  1154. if (mips_arch_variant == "r6") {
  1155. if (is_clang) {
  1156. cflags += [
  1157. "-march=mips64el",
  1158. "-mcpu=mips64r6",
  1159. ]
  1160. } else {
  1161. cflags += [
  1162. "-mips64r6",
  1163. "-Wa,-mips64r6",
  1164. ]
  1165. ldflags += [ "-mips64r6" ]
  1166. }
  1167. if (mips_use_msa == true) {
  1168. cflags += [
  1169. "-mmsa",
  1170. "-mfp64",
  1171. ]
  1172. }
  1173. } else if (mips_arch_variant == "r2") {
  1174. ldflags += [ "-mips64r2" ]
  1175. if (is_clang) {
  1176. cflags += [
  1177. "-march=mips64el",
  1178. "-mcpu=mips64r2",
  1179. ]
  1180. } else {
  1181. cflags += [
  1182. "-mips64r2",
  1183. "-Wa,-mips64r2",
  1184. ]
  1185. }
  1186. } else if (mips_arch_variant == "loongson3") {
  1187. defines += [ "_MIPS_ARCH_LOONGSON" ]
  1188. cflags += [
  1189. "-march=loongson3a",
  1190. "-mno-branch-likely",
  1191. "-Wa,-march=loongson3a",
  1192. ]
  1193. }
  1194. } else if (current_cpu == "mips64") {
  1195. ldflags += [ "-Wl,--hash-style=sysv" ]
  1196. if (custom_toolchain == "") {
  1197. if (is_clang) {
  1198. cflags += [ "--target=mips64-linux-gnuabi64" ]
  1199. ldflags += [ "--target=mips64-linux-gnuabi64" ]
  1200. } else {
  1201. cflags += [
  1202. "-EB",
  1203. "-mabi=64",
  1204. ]
  1205. ldflags += [
  1206. "-EB",
  1207. "-mabi=64",
  1208. ]
  1209. }
  1210. }
  1211.  
  1212. if (mips_arch_variant == "r6") {
  1213. cflags += [
  1214. "-mips64r6",
  1215. "-Wa,-mips64r6",
  1216. ]
  1217. ldflags += [ "-mips64r6" ]
  1218.  
  1219. if (mips_use_msa == true) {
  1220. cflags += [
  1221. "-mmsa",
  1222. "-mfp64",
  1223. ]
  1224. }
  1225. } else if (mips_arch_variant == "r2") {
  1226. cflags += [
  1227. "-mips64r2",
  1228. "-Wa,-mips64r2",
  1229. ]
  1230. ldflags += [ "-mips64r2" ]
  1231. }
  1232. } else if (current_cpu == "ppc64") {
  1233. if (current_os == "aix") {
  1234. cflags += [ "-maix64" ]
  1235. ldflags += [ "-maix64" ]
  1236. } else {
  1237. cflags += [ "-m64" ]
  1238. ldflags += [ "-m64" ]
  1239. }
  1240. } else if (current_cpu == "riscv64") {
  1241. if (is_clang) {
  1242. cflags += [ "--target=riscv64-linux-gnu" ]
  1243. ldflags += [ "--target=riscv64-linux-gnu" ]
  1244. }
  1245. cflags += [ "-mabi=lp64d" ]
  1246. } else if (current_cpu == "loong64") {
  1247. if (is_clang) {
  1248. cflags += [ "--target=loongarch64-linux-gnu" ]
  1249. ldflags += [ "--target=loongarch64-linux-gnu" ]
  1250. }
  1251. cflags += [
  1252. "-mabi=lp64d",
  1253. "-mcmodel=medium",
  1254. ]
  1255. } else if (current_cpu == "s390x") {
  1256. cflags += [ "-m64" ]
  1257. ldflags += [ "-m64" ]
  1258. }
  1259. }
  1260.  
  1261. asmflags = cflags
  1262. }
  1263.  
  1264. # This provides options to tweak code generation that are necessary
  1265. # for particular Chromium code or for working around particular
  1266. # compiler bugs (or the combination of the two).
  1267. config("compiler_codegen") {
  1268. configs = []
  1269. cflags = []
  1270. ldflags = []
  1271.  
  1272. if (is_nacl) {
  1273. configs += [ "//build/config/nacl:compiler_codegen" ]
  1274. }
  1275.  
  1276. if (current_cpu == "arm64" && is_android) {
  1277. # On arm64 disable outlining for Android. See crbug.com/931297 for more
  1278. # information.
  1279. cflags += [ "-mno-outline" ]
  1280.  
  1281. # This can be removed once https://bugs.llvm.org/show_bug.cgi?id=40348
  1282. # has been resolved, and -mno-outline is obeyed by the linker during
  1283. # ThinLTO.
  1284. ldflags += [ "-Wl,-mllvm,-enable-machine-outliner=never" ]
  1285. }
  1286.  
  1287. asmflags = cflags
  1288. }
  1289.  
  1290. # This provides options that make the build deterministic, so that the same
  1291. # revision produces the same output, independent of the name of the build
  1292. # directory and of the computer the build is done on.
  1293. # The relative path from build dir to source dir makes it into the build
  1294. # outputs, so it's recommended that you use a build dir two levels deep
  1295. # (e.g. "out/Release") so that you get the same "../.." path as all the bots
  1296. # in your build outputs.
  1297. config("compiler_deterministic") {
  1298. cflags = []
  1299. ldflags = []
  1300. swiftflags = []
  1301.  
  1302. # Eliminate build metadata (__DATE__, __TIME__ and __TIMESTAMP__) for
  1303. # deterministic build. See https://crbug.com/314403
  1304. if (!is_official_build) {
  1305. if (is_win && !is_clang) {
  1306. cflags += [
  1307. "/wd4117", # Trying to define or undefine a predefined macro.
  1308. "/D__DATE__=",
  1309. "/D__TIME__=",
  1310. "/D__TIMESTAMP__=",
  1311. ]
  1312. } else {
  1313. cflags += [
  1314. "-Wno-builtin-macro-redefined",
  1315. "-D__DATE__=",
  1316. "-D__TIME__=",
  1317. "-D__TIMESTAMP__=",
  1318. ]
  1319. }
  1320. }
  1321.  
  1322. # Makes builds independent of absolute file path.
  1323. if (is_clang && strip_absolute_paths_from_debug_symbols) {
  1324. # If debug option is given, clang includes $cwd in debug info by default.
  1325. # For such build, this flag generates reproducible obj files even we use
  1326. # different build directory like "out/feature_a" and "out/feature_b" if
  1327. # we build same files with same compile flag.
  1328. # Other paths are already given in relative, no need to normalize them.
  1329. if (is_nacl) {
  1330. # TODO(https://crbug.com/1231236): Use -ffile-compilation-dir= here.
  1331. cflags += [
  1332. "-Xclang",
  1333. "-fdebug-compilation-dir",
  1334. "-Xclang",
  1335. ".",
  1336. ]
  1337. } else {
  1338. # -ffile-compilation-dir is an alias for both -fdebug-compilation-dir=
  1339. # and -fcoverage-compilation-dir=.
  1340. cflags += [ "-ffile-compilation-dir=." ]
  1341. swiftflags += [ "-file-compilation-dir=." ]
  1342. }
  1343. if (!is_win) {
  1344. # We don't use clang -cc1as on Windows (yet? https://crbug.com/762167)
  1345. asmflags = [ "-Wa,-fdebug-compilation-dir,." ]
  1346. }
  1347.  
  1348. if (is_win && use_lld) {
  1349. if (symbol_level == 2 || (is_clang && using_sanitizer)) {
  1350. # Absolutize source file paths for PDB. Pass the real build directory
  1351. # if the pdb contains source-level debug information and if linker
  1352. # reproducibility is not critical.
  1353. ldflags += [ "/PDBSourcePath:" + rebase_path(root_build_dir) ]
  1354. } else {
  1355. # Use a fake fixed base directory for paths in the pdb to make the pdb
  1356. # output fully deterministic and independent of the build directory.
  1357. ldflags += [ "/PDBSourcePath:o:\fake\prefix" ]
  1358. }
  1359. }
  1360. }
  1361.  
  1362. # Tells the compiler not to use absolute paths when passing the default
  1363. # paths to the tools it invokes. We don't want this because we don't
  1364. # really need it and it can mess up the goma cache entries.
  1365. if (is_clang && (!is_nacl || is_nacl_saigo)) {
  1366. cflags += [ "-no-canonical-prefixes" ]
  1367.  
  1368. # Same for links: Let the compiler driver invoke the linker
  1369. # with a relative path and pass relative paths to built-in
  1370. # libraries. Not needed on Windows because we call the linker
  1371. # directly there, not through the compiler driver.
  1372. # We don't link on goma, so this change is just for cleaner
  1373. # internal linker invocations, for people who work on the build.
  1374. if (!is_win) {
  1375. ldflags += [ "-no-canonical-prefixes" ]
  1376. }
  1377. }
  1378. }
  1379.  
  1380. config("clang_revision") {
  1381. if (is_clang && clang_base_path == default_clang_base_path) {
  1382. update_args = [
  1383. "--print-revision",
  1384. "--verify-version=$clang_version",
  1385. ]
  1386. if (llvm_force_head_revision) {
  1387. update_args += [ "--llvm-force-head-revision" ]
  1388. }
  1389. clang_revision = exec_script("//tools/clang/scripts/update.py",
  1390. update_args,
  1391. "trim string")
  1392.  
  1393. # This is here so that all files get recompiled after a clang roll and
  1394. # when turning clang on or off. (defines are passed via the command line,
  1395. # and build system rebuild things when their commandline changes). Nothing
  1396. # should ever read this define.
  1397. defines = [ "CR_CLANG_REVISION=\"$clang_revision\"" ]
  1398. }
  1399. }
  1400.  
  1401. config("rustc_revision") {
  1402. if (enable_rust && defined(rustc_version)) {
  1403. # Similar to the above config, this is here so that all files get
  1404. # recompiled after a rustc roll. Nothing should ever read this cfg.
  1405. # $rustc_version is a gn arg set within //build/config/rust.gni
  1406. # so that users using a custom Rust toolchain can override it.
  1407. # Its accuracy is checked in //build/rust/std:find_stdlib, which
  1408. # most of our Rust targets depend upon.
  1409. rustflags = [
  1410. "--cfg",
  1411. "rustc_version=\"$rustc_version\"",
  1412. ]
  1413. }
  1414. }
  1415.  
  1416. config("compiler_arm_fpu") {
  1417. if (current_cpu == "arm" && !is_ios && !is_nacl) {
  1418. cflags = [ "-mfpu=$arm_fpu" ]
  1419. if (!arm_use_thumb) {
  1420. cflags += [ "-marm" ]
  1421. }
  1422. asmflags = cflags
  1423. }
  1424. }
  1425.  
  1426. config("compiler_arm_thumb") {
  1427. if (current_cpu == "arm" && arm_use_thumb && is_posix &&
  1428. !(is_apple || is_nacl)) {
  1429. cflags = [ "-mthumb" ]
  1430. }
  1431. }
  1432.  
  1433. config("compiler_arm") {
  1434. if (current_cpu == "arm" && is_chromeos) {
  1435. # arm is normally the default mode for clang, but on chromeos a wrapper
  1436. # is used to pass -mthumb, and therefor change the default.
  1437. cflags = [ "-marm" ]
  1438. }
  1439. }
  1440.  
  1441. # runtime_library -------------------------------------------------------------
  1442. #
  1443. # Sets the runtime library and associated options.
  1444. #
  1445. # How do you determine what should go in here vs. "compiler" above? Consider if
  1446. # a target might choose to use a different runtime library (ignore for a moment
  1447. # if this is possible or reasonable on your system). If such a target would want
  1448. # to change or remove your option, put it in the runtime_library config. If a
  1449. # target wants the option regardless, put it in the compiler config.
  1450.  
  1451. config("runtime_library") {
  1452. configs = []
  1453.  
  1454. # The order of this config is important: it must appear before
  1455. # android:runtime_library. This is to ensure libc++ appears before
  1456. # libandroid_support in the -isystem include order. Otherwise, there will be
  1457. # build errors related to symbols declared in math.h.
  1458. if (use_custom_libcxx) {
  1459. configs += [ "//build/config/c++:runtime_library" ]
  1460. }
  1461.  
  1462. # TODO(crbug.com/830987): Come up with a better name for is POSIX + Fuchsia
  1463. # configuration.
  1464. if (is_posix || is_fuchsia) {
  1465. configs += [ "//build/config/posix:runtime_library" ]
  1466. }
  1467.  
  1468. # System-specific flags. If your compiler flags apply to one of the
  1469. # categories here, add it to the associated file to keep this shared config
  1470. # smaller.
  1471. if (is_win) {
  1472. configs += [ "//build/config/win:runtime_library" ]
  1473. } else if (is_linux || is_chromeos) {
  1474. configs += [ "//build/config/linux:runtime_library" ]
  1475. if (is_chromeos) {
  1476. configs += [ "//build/config/chromeos:runtime_library" ]
  1477. }
  1478. } else if (is_ios) {
  1479. configs += [ "//build/config/ios:runtime_library" ]
  1480. } else if (is_mac) {
  1481. configs += [ "//build/config/mac:runtime_library" ]
  1482. } else if (is_android) {
  1483. configs += [ "//build/config/android:runtime_library" ]
  1484. }
  1485.  
  1486. if (is_component_build) {
  1487. defines = [ "COMPONENT_BUILD" ]
  1488. }
  1489. }
  1490.  
  1491. # default_warnings ------------------------------------------------------------
  1492. #
  1493. # Collects all warning flags that are used by default. This is used as a
  1494. # subconfig of both chromium_code and no_chromium_code. This way these
  1495. # flags are guaranteed to appear on the compile command line after -Wall.
  1496. config("default_warnings") {
  1497. cflags = []
  1498. cflags_c = []
  1499. cflags_cc = []
  1500. ldflags = []
  1501.  
  1502. if (is_win) {
  1503. if (treat_warnings_as_errors) {
  1504. cflags += [ "/WX" ]
  1505. }
  1506. if (fatal_linker_warnings) {
  1507. arflags = [ "/WX" ]
  1508. ldflags = [ "/WX" ]
  1509. }
  1510. defines = [
  1511. # Without this, Windows headers warn that functions like wcsnicmp
  1512. # should be spelled _wcsnicmp. But all other platforms keep spelling
  1513. # it wcsnicmp, making this warning unhelpful. We don't want it.
  1514. "_CRT_NONSTDC_NO_WARNINGS",
  1515.  
  1516. # TODO(thakis): winsock wants us to use getaddrinfo instead of
  1517. # gethostbyname. Fires mostly in non-Chromium code. We probably
  1518. # want to remove this define eventually.
  1519. "_WINSOCK_DEPRECATED_NO_WARNINGS",
  1520. ]
  1521. if (!is_clang) {
  1522. # TODO(thakis): Remove this once
  1523. # https://swiftshader-review.googlesource.com/c/SwiftShader/+/57968 has
  1524. # rolled into angle.
  1525. cflags += [ "/wd4244" ]
  1526. }
  1527. } else {
  1528. if (is_apple && !is_nacl) {
  1529. # When compiling Objective-C, warns if a method is used whose
  1530. # availability is newer than the deployment target.
  1531. cflags += [ "-Wunguarded-availability" ]
  1532. }
  1533.  
  1534. if (is_ios) {
  1535. # When compiling Objective-C, warns if a selector named via @selector has
  1536. # not been defined in any visible interface.
  1537. cflags += [ "-Wundeclared-selector" ]
  1538. }
  1539.  
  1540. # Suppress warnings about ABI changes on ARM (Clang doesn't give this
  1541. # warning).
  1542. if (current_cpu == "arm" && !is_clang) {
  1543. cflags += [ "-Wno-psabi" ]
  1544. }
  1545.  
  1546. if (!is_clang) {
  1547. cflags_cc += [
  1548. # See comment for -Wno-c++11-narrowing.
  1549. "-Wno-narrowing",
  1550. ]
  1551.  
  1552. # -Wno-class-memaccess warns about hash table and vector in blink.
  1553. # But the violation is intentional.
  1554. if (!is_nacl) {
  1555. cflags_cc += [ "-Wno-class-memaccess" ]
  1556. }
  1557.  
  1558. # -Wunused-local-typedefs is broken in gcc,
  1559. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872
  1560. cflags += [ "-Wno-unused-local-typedefs" ]
  1561.  
  1562. # Don't warn about "maybe" uninitialized. Clang doesn't include this
  1563. # in -Wall but gcc does, and it gives false positives.
  1564. cflags += [ "-Wno-maybe-uninitialized" ]
  1565. cflags += [ "-Wno-deprecated-declarations" ]
  1566.  
  1567. # -Wcomment gives too many false positives in the case a
  1568. # backslash ended comment line is followed by a new line of
  1569. # comments
  1570. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638
  1571. cflags += [ "-Wno-comments" ]
  1572.  
  1573. # -Wpacked-not-aligned complains all generated mojom-shared-internal.h
  1574. # files.
  1575. cflags += [ "-Wno-packed-not-aligned" ]
  1576. }
  1577. }
  1578.  
  1579. # Common Clang and GCC warning setup.
  1580. if (!is_win || is_clang) {
  1581. cflags += [
  1582. # Disables.
  1583. "-Wno-missing-field-initializers", # "struct foo f = {0};"
  1584. "-Wno-unused-parameter", # Unused function parameters.
  1585. ]
  1586. }
  1587.  
  1588. if (is_clang) {
  1589. cflags += [
  1590. "-Wloop-analysis",
  1591.  
  1592. # TODO(thakis): This used to be implied by -Wno-unused-function,
  1593. # which we no longer use. Check if it makes sense to remove
  1594. # this as well. http://crbug.com/316352
  1595. "-Wno-unneeded-internal-declaration",
  1596. ]
  1597.  
  1598. if (!is_nacl || is_nacl_saigo) {
  1599. if (is_win) {
  1600. # TODO(thakis): https://crbug.com/617318
  1601. # Currently goma can not handle case sensitiveness for windows well.
  1602. cflags += [ "-Wno-nonportable-include-path" ]
  1603. }
  1604.  
  1605. cflags += [
  1606. "-Wenum-compare-conditional",
  1607.  
  1608. # An ABI compat warning we don't care about, https://crbug.com/1102157
  1609. # TODO(thakis): Push this to the (few) targets that need it,
  1610. # instead of having a global flag.
  1611. "-Wno-psabi",
  1612.  
  1613. # Ignore warnings about MSVC optimization pragmas.
  1614. # TODO(thakis): Only for no_chromium_code? http://crbug.com/912662
  1615. "-Wno-ignored-pragma-optimize",
  1616. ]
  1617.  
  1618. if (!is_nacl) {
  1619. cflags += [
  1620. # TODO(crbug.com/1343975) Evaluate and possibly enable.
  1621. "-Wno-deprecated-builtins",
  1622.  
  1623. # TODO(crbug.com/1352183) Evaluate and possibly enable.
  1624. "-Wno-bitfield-constant-conversion",
  1625. ]
  1626. }
  1627. }
  1628. }
  1629. }
  1630.  
  1631. # prevent_unsafe_narrowing ----------------------------------------------------
  1632. #
  1633. # Warnings that prevent narrowing or comparisons of integer types that are
  1634. # likely to cause out-of-bound read/writes or Undefined Behaviour. In
  1635. # particular, size_t is used for memory sizes, allocation, indexing, and
  1636. # offsets. Using other integer types along with size_t produces risk of
  1637. # memory-safety bugs and thus security exploits.
  1638. #
  1639. # In order to prevent these bugs, allocation sizes were historically limited to
  1640. # sizes that can be represented within 31 bits of information, allowing `int` to
  1641. # be safely misused instead of `size_t` (https://crbug.com/169327). In order to
  1642. # support increasing the allocation limit we require strictly adherence to
  1643. # using the correct types, avoiding lossy conversions, and preventing overflow.
  1644. # To do so, enable this config and fix errors by converting types to be
  1645. # `size_t`, which is both large enough and unsigned, when dealing with memory
  1646. # sizes, allocations, indices, or offsets.In cases where type conversion is not
  1647. # possible or is superfluous, use base::strict_cast<> or base::checked_cast<>
  1648. # to convert to size_t as needed.
  1649. # See also: https://docs.google.com/document/d/14yKUwDaorqqNfgdGqHY_nck2nn02XBQcB5N0ue4fax8
  1650. #
  1651. # To enable in a GN target, use:
  1652. # configs += [ "//build/config/compiler:prevent_unsafe_narrowing" ]
  1653.  
  1654. config("prevent_unsafe_narrowing") {
  1655. if (is_clang) {
  1656. cflags = [
  1657. "-Wshorten-64-to-32",
  1658. "-Wimplicit-int-conversion",
  1659. "-Wsign-compare",
  1660. "-Wsign-conversion",
  1661. ]
  1662. if (!is_nacl) {
  1663. cflags += [
  1664. # Avoid bugs of the form `if (size_t i = size; i >= 0; --i)` while
  1665. # fixing types to be sign-correct.
  1666. "-Wtautological-unsigned-zero-compare",
  1667. ]
  1668. }
  1669. }
  1670. }
  1671.  
  1672. # chromium_code ---------------------------------------------------------------
  1673. #
  1674. # Toggles between higher and lower warnings for code that is (or isn't)
  1675. # part of Chromium.
  1676.  
  1677. config("chromium_code") {
  1678. if (is_win) {
  1679. if (is_clang) {
  1680. cflags = [ "/W4" ] # Warning level 4.
  1681.  
  1682. # Opt in to additional [[nodiscard]] on standard library methods.
  1683. defines = [ "_HAS_NODISCARD" ]
  1684. }
  1685. } else {
  1686. cflags = [ "-Wall" ]
  1687. if (treat_warnings_as_errors) {
  1688. cflags += [ "-Werror" ]
  1689.  
  1690. # The compiler driver can sometimes (rarely) emit warnings before calling
  1691. # the actual linker. Make sure these warnings are treated as errors as
  1692. # well.
  1693. ldflags = [ "-Werror" ]
  1694. }
  1695. if (is_clang) {
  1696. # Enable extra warnings for chromium_code when we control the compiler.
  1697. cflags += [ "-Wextra" ]
  1698. }
  1699.  
  1700. # In Chromium code, we define __STDC_foo_MACROS in order to get the
  1701. # C99 macros on Mac and Linux.
  1702. defines = [
  1703. "__STDC_CONSTANT_MACROS",
  1704. "__STDC_FORMAT_MACROS",
  1705. ]
  1706.  
  1707. if (!is_debug && !using_sanitizer && current_cpu != "s390x" &&
  1708. current_cpu != "s390" && current_cpu != "ppc64" &&
  1709. current_cpu != "mips" && current_cpu != "mips64" &&
  1710. current_cpu != "riscv64" && current_cpu != "loong64") {
  1711. # Non-chromium code is not guaranteed to compile cleanly with
  1712. # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
  1713. # disabled, so only do that for Release build.
  1714. defines += [ "_FORTIFY_SOURCE=2" ]
  1715. }
  1716.  
  1717. if (is_mac) {
  1718. cflags_objc = [ "-Wobjc-missing-property-synthesis" ]
  1719. cflags_objcc = [ "-Wobjc-missing-property-synthesis" ]
  1720. }
  1721.  
  1722. if (is_ios) {
  1723. cflags_objc = [ "-Wimplicit-retain-self" ]
  1724. cflags_objcc = cflags_objc
  1725. }
  1726. }
  1727.  
  1728. if (is_clang) {
  1729. cflags += [
  1730. # Warn on missing break statements at the end of switch cases.
  1731. # For intentional fallthrough, use [[fallthrough]].
  1732. "-Wimplicit-fallthrough",
  1733.  
  1734. # Warn on unnecessary extra semicolons outside of function definitions.
  1735. "-Wextra-semi",
  1736. ]
  1737.  
  1738. # TODO(thakis): Enable this more often, https://crbug.com/346399
  1739. # use_libfuzzer: https://crbug.com/1063180
  1740. if ((!is_nacl || is_nacl_saigo) && !use_libfuzzer) {
  1741. cflags += [ "-Wunreachable-code-aggressive" ]
  1742. }
  1743.  
  1744. # Thread safety analysis is broken under nacl: https://crbug.com/982423.
  1745. if (!is_nacl || is_nacl_saigo) {
  1746. cflags += [
  1747. # Thread safety analysis. See base/thread_annotations.h and
  1748. # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
  1749. "-Wthread-safety",
  1750. ]
  1751. }
  1752. }
  1753.  
  1754. configs = [
  1755. ":default_warnings",
  1756. ":noshadowing",
  1757. ]
  1758. }
  1759.  
  1760. config("no_chromium_code") {
  1761. cflags = []
  1762. cflags_cc = []
  1763. defines = []
  1764.  
  1765. if (is_win) {
  1766. if (is_clang) {
  1767. cflags += [ "/W3" ] # Warning level 3.
  1768. }
  1769. cflags += [
  1770. "/wd4800", # Disable warning when forcing value to bool.
  1771. "/wd4267", # TODO(jschuh): size_t to int.
  1772. ]
  1773. } else {
  1774. # GCC may emit unsuppressible warnings so don't add -Werror for no chromium
  1775. # code. crbug.com/589724
  1776. if (treat_warnings_as_errors && is_clang) {
  1777. cflags += [ "-Werror" ]
  1778. ldflags = [ "-Werror" ]
  1779. }
  1780. if (is_clang && !is_nacl) {
  1781. # TODO(thakis): Remove !is_nacl once
  1782. # https://codereview.webrtc.org/1552863002/ made its way into chromium.
  1783. cflags += [ "-Wall" ]
  1784. }
  1785. }
  1786.  
  1787. if (is_clang) {
  1788. cflags += [
  1789. # Lots of third-party libraries have unused variables. Instead of
  1790. # suppressing them individually, we just blanket suppress them here.
  1791. "-Wno-unused-variable",
  1792.  
  1793. # Similarly, we're not going to fix all the C++11 narrowing issues in
  1794. # third-party libraries.
  1795. "-Wno-c++11-narrowing",
  1796. ]
  1797. if (!is_nacl) {
  1798. cflags += [
  1799. # Disabled for similar reasons as -Wunused-variable.
  1800. "-Wno-unused-but-set-variable",
  1801.  
  1802. # TODO(https://crbug.com/1202159): Clean up and enable.
  1803. "-Wno-misleading-indentation",
  1804. ]
  1805. }
  1806. }
  1807.  
  1808. # Rust warnings to ignore in third party dependencies. This list is
  1809. # built from those warnings which are currently in our various Rust
  1810. # third party dependencies, but aren't serious (they're largely
  1811. # stylistic).
  1812. # An alternative policy would be to suppress all warnings in third
  1813. # party Rust code using "--cap-lints allow". This is what cargo does
  1814. # for code outside your own crate, so is worth considering if it
  1815. # turns out that maintaining this list is onerous.
  1816. # (https://doc.rust-lang.org/rustc/lints/levels.html#capping-lints)
  1817. rustflags = [
  1818. "-A",
  1819. "unused_parens",
  1820. "-A",
  1821. "bare_trait_objects",
  1822. "-A",
  1823. "non_fmt_panics",
  1824. "-A",
  1825. "redundant_semicolons",
  1826. "-A",
  1827. "unused_parens",
  1828. "-A",
  1829. "anonymous_parameters",
  1830. "-A",
  1831. "bare_trait_objects",
  1832. "-A",
  1833. "deprecated",
  1834. "-A",
  1835. "non_camel_case_types",
  1836. "-A",
  1837. "unused_imports",
  1838. ]
  1839.  
  1840. configs = [ ":default_warnings" ]
  1841. }
  1842.  
  1843. # noshadowing -----------------------------------------------------------------
  1844. #
  1845. # Allows turning -Wshadow on.
  1846.  
  1847. config("noshadowing") {
  1848. # This flag has to be disabled for nacl because the nacl compiler is too
  1849. # strict about shadowing.
  1850. if (is_clang && (!is_nacl || is_nacl_saigo)) {
  1851. cflags = [ "-Wshadow" ]
  1852. }
  1853. }
  1854.  
  1855. # rtti ------------------------------------------------------------------------
  1856. #
  1857. # Allows turning Run-Time Type Identification on or off.
  1858.  
  1859. config("rtti") {
  1860. if (is_win) {
  1861. cflags_cc = [ "/GR" ]
  1862. } else {
  1863. cflags_cc = [ "-frtti" ]
  1864. }
  1865. }
  1866.  
  1867. config("no_rtti") {
  1868. # Some sanitizer configs may require RTTI to be left enabled globally
  1869. if (!use_rtti) {
  1870. if (is_win) {
  1871. cflags_cc = [ "/GR-" ]
  1872. } else {
  1873. cflags_cc = [ "-fno-rtti" ]
  1874. cflags_objcc = cflags_cc
  1875. }
  1876. }
  1877. }
  1878.  
  1879. # export_dynamic ---------------------------------------------------------------
  1880. #
  1881. # Ensures all exported symbols are added to the dynamic symbol table. This is
  1882. # necessary to expose Chrome's custom operator new() and operator delete() (and
  1883. # other memory-related symbols) to libraries. Otherwise, they might
  1884. # (de)allocate memory on a different heap, which would spell trouble if pointers
  1885. # to heap-allocated memory are passed over shared library boundaries.
  1886. config("export_dynamic") {
  1887. # TODO(crbug.com/1052397): Revisit after target_os flip is completed.
  1888. if (is_linux || is_chromeos_lacros || export_libcxxabi_from_executables) {
  1889. ldflags = [ "-rdynamic" ]
  1890. }
  1891. }
  1892.  
  1893. # thin_archive -----------------------------------------------------------------
  1894. #
  1895. # Enables thin archives on posix, and on windows when the lld linker is used.
  1896. # Regular archives directly include the object files used to generate it.
  1897. # Thin archives merely reference the object files.
  1898. # This makes building them faster since it requires less disk IO, but is
  1899. # inappropriate if you wish to redistribute your static library.
  1900. # This config is added to the global config, so thin archives should already be
  1901. # enabled. If you want to make a distributable static library, you need to do 2
  1902. # things:
  1903. # 1. Set complete_static_lib so that all dependencies of the library make it
  1904. # into the library. See `gn help complete_static_lib` for details.
  1905. # 2. Remove the thin_archive config, so that the .a file actually contains all
  1906. # .o files, instead of just references to .o files in the build directoy
  1907. config("thin_archive") {
  1908. # The macOS and iOS default linker ld64 does not support reading thin
  1909. # archives.
  1910. # TODO(crbug.com/1221615): Enable on is_apple if use_lld once that no longer
  1911. # confuses lldb.
  1912. if ((is_posix && !is_nacl && !is_apple) || is_fuchsia) {
  1913. arflags = [ "-T" ]
  1914. } else if (is_win && use_lld) {
  1915. arflags = [ "/llvmlibthin" ]
  1916. }
  1917. }
  1918.  
  1919. # exceptions -------------------------------------------------------------------
  1920. #
  1921. # Allows turning Exceptions on or off.
  1922. # Note: exceptions are disallowed in Google code.
  1923.  
  1924. config("exceptions") {
  1925. if (is_win) {
  1926. # Enables exceptions in the STL.
  1927. if (!use_custom_libcxx) {
  1928. defines = [ "_HAS_EXCEPTIONS=1" ]
  1929. }
  1930. cflags_cc = [ "/EHsc" ]
  1931. } else {
  1932. cflags_cc = [ "-fexceptions" ]
  1933. cflags_objcc = cflags_cc
  1934. }
  1935. }
  1936.  
  1937. config("no_exceptions") {
  1938. if (is_win) {
  1939. # Disables exceptions in the STL.
  1940. # libc++ uses the __has_feature macro to control whether to use exceptions,
  1941. # so defining this macro is unnecessary. Defining _HAS_EXCEPTIONS to 0 also
  1942. # breaks libc++ because it depends on MSVC headers that only provide certain
  1943. # declarations if _HAS_EXCEPTIONS is 1. Those MSVC headers do not use
  1944. # exceptions, despite being conditional on _HAS_EXCEPTIONS.
  1945. if (!use_custom_libcxx) {
  1946. defines = [ "_HAS_EXCEPTIONS=0" ]
  1947. }
  1948. } else {
  1949. cflags_cc = [ "-fno-exceptions" ]
  1950. cflags_objcc = cflags_cc
  1951. }
  1952. }
  1953.  
  1954. # Warnings ---------------------------------------------------------------------
  1955.  
  1956. # Generate a warning for code that might emit a static initializer.
  1957. # See: //docs/static_initializers.md
  1958. # See: https://groups.google.com/a/chromium.org/d/topic/chromium-dev/B9Q5KTD7iCo/discussion
  1959. config("wglobal_constructors") {
  1960. if (is_clang) {
  1961. cflags = [ "-Wglobal-constructors" ]
  1962. }
  1963. }
  1964.  
  1965. # This will generate warnings when using Clang if code generates exit-time
  1966. # destructors, which will slow down closing the program.
  1967. # TODO(thakis): Make this a blocklist instead, http://crbug.com/101600
  1968. config("wexit_time_destructors") {
  1969. if (is_clang) {
  1970. cflags = [ "-Wexit-time-destructors" ]
  1971. }
  1972. }
  1973.  
  1974. # Some code presumes that pointers to structures/objects are compatible
  1975. # regardless of whether what they point to is already known to be valid.
  1976. # gcc 4.9 and earlier had no way of suppressing this warning without
  1977. # suppressing the rest of them. Here we centralize the identification of
  1978. # the gcc 4.9 toolchains.
  1979. config("no_incompatible_pointer_warnings") {
  1980. cflags = []
  1981. if (is_clang) {
  1982. cflags += [ "-Wno-incompatible-pointer-types" ]
  1983. } else if (current_cpu == "mipsel" || current_cpu == "mips64el") {
  1984. cflags += [ "-w" ]
  1985. } else if (is_chromeos_ash && current_cpu == "arm") {
  1986. cflags += [ "-w" ]
  1987. }
  1988. }
  1989.  
  1990. # Optimization -----------------------------------------------------------------
  1991. #
  1992. # The BUILDCONFIG file sets the "default_optimization" config on targets by
  1993. # default. It will be equivalent to either "optimize" (release) or
  1994. # "no_optimize" (debug) optimization configs.
  1995. #
  1996. # You can override the optimization level on a per-target basis by removing the
  1997. # default config and then adding the named one you want:
  1998. #
  1999. # configs -= [ "//build/config/compiler:default_optimization" ]
  2000. # configs += [ "//build/config/compiler:optimize_max" ]
  2001.  
  2002. # Shared settings for both "optimize" and "optimize_max" configs.
  2003. # IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags.
  2004. if (is_win) {
  2005. common_optimize_on_cflags = [
  2006. "/Ob2", # Both explicit and auto inlining.
  2007. "/Oy-", # Disable omitting frame pointers, must be after /O2.
  2008. "/Zc:inline", # Remove unreferenced COMDAT (faster links).
  2009. ]
  2010. if (!is_asan) {
  2011. common_optimize_on_cflags += [
  2012. # Put data in separate COMDATs. This allows the linker
  2013. # to put bit-identical constants at the same address even if
  2014. # they're unrelated constants, which saves binary size.
  2015. # This optimization can't be used when ASan is enabled because
  2016. # it is not compatible with the ASan ODR checker.
  2017. "/Gw",
  2018. ]
  2019. }
  2020. common_optimize_on_ldflags = []
  2021.  
  2022. # /OPT:ICF is not desirable in Debug builds, since code-folding can result in
  2023. # misleading symbols in stack traces.
  2024. if (!is_debug && !is_component_build) {
  2025. common_optimize_on_ldflags += [ "/OPT:ICF" ] # Redundant COMDAT folding.
  2026. }
  2027.  
  2028. if (is_official_build) {
  2029. common_optimize_on_ldflags += [ "/OPT:REF" ] # Remove unreferenced data.
  2030. # TODO(thakis): Add LTO/PGO clang flags eventually, https://crbug.com/598772
  2031. }
  2032. } else {
  2033. common_optimize_on_cflags = []
  2034. common_optimize_on_ldflags = []
  2035.  
  2036. if (is_android) {
  2037. # TODO(jdduke) Re-enable on mips after resolving linking
  2038. # issues with libc++ (crbug.com/456380).
  2039. if (current_cpu != "mipsel" && current_cpu != "mips64el") {
  2040. common_optimize_on_ldflags += [
  2041. # Warn in case of text relocations.
  2042. "-Wl,--warn-shared-textrel",
  2043. ]
  2044. }
  2045. }
  2046.  
  2047. if (is_apple) {
  2048. common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
  2049.  
  2050. if (is_official_build) {
  2051. common_optimize_on_ldflags += [
  2052. "-Wl,-no_data_in_code_info",
  2053. "-Wl,-no_function_starts",
  2054. ]
  2055. }
  2056. } else if (current_os != "aix" && current_os != "zos") {
  2057. # Non-Mac Posix flags.
  2058. # Aix does not support these.
  2059.  
  2060. common_optimize_on_cflags += [
  2061. # Put data and code in their own sections, so that unused symbols
  2062. # can be removed at link time with --gc-sections.
  2063. "-fdata-sections",
  2064. "-ffunction-sections",
  2065. ]
  2066. if ((!is_nacl || is_nacl_saigo) && is_clang) {
  2067. # We don't care about unique section names, this makes object files a bit
  2068. # smaller.
  2069. common_optimize_on_cflags += [ "-fno-unique-section-names" ]
  2070. }
  2071.  
  2072. common_optimize_on_ldflags += [
  2073. # Specifically tell the linker to perform optimizations.
  2074. # See http://lwn.net/Articles/192624/ .
  2075. # -O2 enables string tail merge optimization in gold and lld.
  2076. "-Wl,-O2",
  2077. "-Wl,--gc-sections",
  2078. ]
  2079. }
  2080. }
  2081.  
  2082. config("default_stack_frames") {
  2083. if (is_posix || is_fuchsia) {
  2084. if (enable_frame_pointers) {
  2085. cflags = [ "-fno-omit-frame-pointer" ]
  2086.  
  2087. # Omit frame pointers for leaf functions on x86, otherwise building libyuv
  2088. # gives clang's register allocator issues, see llvm.org/PR15798 /
  2089. # crbug.com/233709
  2090. if (is_clang && current_cpu == "x86" && !is_apple) {
  2091. cflags += [ "-momit-leaf-frame-pointer" ]
  2092. }
  2093. } else {
  2094. cflags = [ "-fomit-frame-pointer" ]
  2095. }
  2096. }
  2097. # On Windows, the flag to enable framepointers "/Oy-" must always come after
  2098. # the optimization flag [e.g. "/O2"]. The optimization flag is set by one of
  2099. # the "optimize" configs, see rest of this file. The ordering that cflags are
  2100. # applied is well-defined by the GN spec, and there is no way to ensure that
  2101. # cflags set by "default_stack_frames" is applied after those set by an
  2102. # "optimize" config. Similarly, there is no way to propagate state from this
  2103. # config into the "optimize" config. We always apply the "/Oy-" config in the
  2104. # definition for common_optimize_on_cflags definition, even though this may
  2105. # not be correct.
  2106. }
  2107.  
  2108. # Default "optimization on" config.
  2109. config("optimize") {
  2110. if (is_win) {
  2111. if (chrome_pgo_phase != 2) {
  2112. # Favor size over speed, /O1 must be before the common flags.
  2113. # /O1 implies /Os and /GF.
  2114. cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ]
  2115. } else {
  2116. # PGO requires all translation units to be compiled with /O2. The actual
  2117. # optimization level will be decided based on the profiling data.
  2118. cflags = [ "/O2" ] + common_optimize_on_cflags + [ "/Oi" ]
  2119. }
  2120. } else if (optimize_for_size) {
  2121. # Favor size over speed.
  2122. if (is_clang) {
  2123. cflags = [ "-O2" ] + common_optimize_on_cflags
  2124.  
  2125. if (use_ml_inliner && is_a_target_toolchain) {
  2126. cflags += [
  2127. "-mllvm",
  2128. "-enable-ml-inliner=release",
  2129. ]
  2130. }
  2131. } else {
  2132. cflags = [ "-Os" ] + common_optimize_on_cflags
  2133. }
  2134. } else if (is_chromeos) {
  2135. # TODO(gbiv): This is partially favoring size over speed. CrOS exclusively
  2136. # uses clang, and -Os in clang is more of a size-conscious -O2 than "size at
  2137. # any cost" (AKA -Oz). It'd be nice to:
  2138. # - Make `optimize_for_size` apply to all platforms where we're optimizing
  2139. # for size by default (so, also Windows)
  2140. # - Investigate -Oz here, maybe just for ARM?
  2141. cflags = [ "-Os" ] + common_optimize_on_cflags
  2142. } else {
  2143. cflags = [ "-O2" ] + common_optimize_on_cflags
  2144. }
  2145. if (optimize_for_size) {
  2146. rustflags = [ "-Copt-level=s", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2147. } else {
  2148. rustflags = [ "-Copt-level=3", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2149. }
  2150. ldflags = common_optimize_on_ldflags
  2151. }
  2152.  
  2153. # Turn off optimizations.
  2154. config("no_optimize") {
  2155. if (is_win) {
  2156. cflags = [
  2157. "/Od", # Disable optimization.
  2158. "/Ob0", # Disable all inlining (on by default).
  2159. "/GF", # Enable string pooling (off by default).
  2160. ]
  2161.  
  2162. if (target_cpu == "arm64") {
  2163. # Disable omitting frame pointers for no_optimize build because stack
  2164. # traces on Windows ARM64 rely on it.
  2165. cflags += [ "/Oy-" ]
  2166. }
  2167. } else if (is_android && !android_full_debug) {
  2168. # On Android we kind of optimize some things that don't affect debugging
  2169. # much even when optimization is disabled to get the binary size down.
  2170. if (is_clang) {
  2171. cflags = [ "-Oz" ] + common_optimize_on_cflags
  2172. } else {
  2173. cflags = [ "-Os" ] + common_optimize_on_cflags
  2174. }
  2175.  
  2176. if (!is_component_build) {
  2177. # Required for library partitions. Without this all symbols just end up
  2178. # in the base partition.
  2179. ldflags = [ "-Wl,--gc-sections" ]
  2180. }
  2181. } else if (is_fuchsia) {
  2182. # On Fuchsia, we optimize for size here to reduce the size of debug build
  2183. # packages so they can be run in a KVM. See crbug.com/910243 for details.
  2184. cflags = [ "-Og" ]
  2185. } else {
  2186. cflags = [ "-O0" ]
  2187. ldflags = []
  2188. }
  2189. }
  2190.  
  2191. # Turns up the optimization level. On Windows, this implies whole program
  2192. # optimization and link-time code generation which is very expensive and should
  2193. # be used sparingly.
  2194. config("optimize_max") {
  2195. if (is_nacl && is_nacl_irt) {
  2196. # The NaCl IRT is a special case and always wants its own config.
  2197. # Various components do:
  2198. # if (!is_debug) {
  2199. # configs -= [ "//build/config/compiler:default_optimization" ]
  2200. # configs += [ "//build/config/compiler:optimize_max" ]
  2201. # }
  2202. # So this config has to have the selection logic just like
  2203. # "default_optimization", below.
  2204. configs = [ "//build/config/nacl:irt_optimize" ]
  2205. } else {
  2206. ldflags = common_optimize_on_ldflags
  2207. if (is_win) {
  2208. # Favor speed over size, /O2 must be before the common flags.
  2209. # /O2 implies /Ot, /Oi, and /GF.
  2210. cflags = [ "/O2" ] + common_optimize_on_cflags + [ "/Oi" ]
  2211. } else if (optimize_for_fuzzing) {
  2212. cflags = [ "-O1" ] + common_optimize_on_cflags
  2213. } else {
  2214. cflags = [ "-O3" ] + common_optimize_on_cflags
  2215. }
  2216. rustflags = [ "-Copt-level=3", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2217. }
  2218. }
  2219.  
  2220. # This config can be used to override the default settings for per-component
  2221. # and whole-program optimization, optimizing the particular target for speed
  2222. # instead of code size. This config is exactly the same as "optimize_max"
  2223. # except that we use -O3 instead of -O2 on non-win, non-IRT platforms.
  2224. #
  2225. # TODO(crbug.com/621335) - rework how all of these configs are related
  2226. # so that we don't need this disclaimer.
  2227. config("optimize_speed") {
  2228. if (is_nacl && is_nacl_irt) {
  2229. # The NaCl IRT is a special case and always wants its own config.
  2230. # Various components do:
  2231. # if (!is_debug) {
  2232. # configs -= [ "//build/config/compiler:default_optimization" ]
  2233. # configs += [ "//build/config/compiler:optimize_max" ]
  2234. # }
  2235. # So this config has to have the selection logic just like
  2236. # "default_optimization", below.
  2237. configs = [ "//build/config/nacl:irt_optimize" ]
  2238. } else {
  2239. ldflags = common_optimize_on_ldflags
  2240. if (is_win) {
  2241. # Favor speed over size, /O2 must be before the common flags.
  2242. # /O2 implies /Ot, /Oi, and /GF.
  2243. cflags = [ "/O2" ] + common_optimize_on_cflags + [ "/Oi" ]
  2244. } else if (optimize_for_fuzzing) {
  2245. cflags = [ "-O3" ] + common_optimize_on_cflags
  2246. } else {
  2247. cflags = [ "-O3" ] + common_optimize_on_cflags
  2248. }
  2249. rustflags = [ "-Copt-level=3", "-Ctarget-feature=+avx,+aes,+vaes", ]
  2250. }
  2251. }
  2252.  
  2253. config("optimize_fuzzing") {
  2254. cflags = [ "-O1" ] + common_optimize_on_cflags
  2255. rustflags = [ "-Copt-level=1" ]
  2256. ldflags = common_optimize_on_ldflags
  2257. visibility = [ ":default_optimization" ]
  2258. }
  2259.  
  2260. # The default optimization applied to all targets. This will be equivalent to
  2261. # either "optimize" or "no_optimize", depending on the build flags.
  2262. config("default_optimization") {
  2263. if (is_nacl && is_nacl_irt) {
  2264. # The NaCl IRT is a special case and always wants its own config.
  2265. # It gets optimized the same way regardless of the type of build.
  2266. configs = [ "//build/config/nacl:irt_optimize" ]
  2267. } else if (is_debug) {
  2268. configs = [ ":no_optimize" ]
  2269. } else if (optimize_for_fuzzing) {
  2270. assert(!is_win, "Fuzzing optimize level not supported on Windows")
  2271.  
  2272. # Coverage build is quite slow. Using "optimize_for_fuzzing" makes it even
  2273. # slower as it uses "-O1" instead of "-O3". Prevent that from happening.
  2274. assert(!use_clang_coverage,
  2275. "optimize_for_fuzzing=true should not be used with " +
  2276. "use_clang_coverage=true.")
  2277. configs = [ ":optimize_fuzzing" ]
  2278. } else {
  2279. configs = [ ":optimize" ]
  2280. }
  2281. }
  2282.  
  2283. _clang_sample_profile = ""
  2284. if (is_clang && is_a_target_toolchain) {
  2285. if (clang_sample_profile_path != "") {
  2286. _clang_sample_profile = clang_sample_profile_path
  2287. } else if (clang_use_default_sample_profile) {
  2288. assert(build_with_chromium,
  2289. "Our default profiles currently only apply to Chromium")
  2290. assert(is_android || is_chromeos || is_castos,
  2291. "The current platform has no default profile")
  2292. if (is_android || is_castos) {
  2293. _clang_sample_profile = "//chrome/android/profiles/afdo.prof"
  2294. } else {
  2295. assert(chromeos_afdo_platform == "atom" ||
  2296. chromeos_afdo_platform == "bigcore" ||
  2297. chromeos_afdo_platform == "arm",
  2298. "Only atom, bigcore and arm are valid Chrome OS profiles.")
  2299. _clang_sample_profile =
  2300. "//chromeos/profiles/${chromeos_afdo_platform}.afdo.prof"
  2301. }
  2302. }
  2303. }
  2304.  
  2305. # Clang offers a way to assert that AFDO profiles are accurate, which causes it
  2306. # to optimize functions not represented in a profile more aggressively for size.
  2307. # This config can be toggled in cases where shaving off binary size hurts
  2308. # performance too much.
  2309. config("afdo_optimize_size") {
  2310. if (_clang_sample_profile != "" && sample_profile_is_accurate) {
  2311. cflags = [ "-fprofile-sample-accurate" ]
  2312. }
  2313. }
  2314.  
  2315. # GCC and clang support a form of profile-guided optimization called AFDO.
  2316. # There are some targeted places that AFDO regresses, so we provide a separate
  2317. # config to allow AFDO to be disabled per-target.
  2318. config("afdo") {
  2319. if (is_clang) {
  2320. cflags = []
  2321. if (clang_emit_debug_info_for_profiling) {
  2322. # Add the following flags to generate debug info for profiling.
  2323. cflags += [ "-gline-tables-only" ]
  2324. if (!is_nacl) {
  2325. cflags += [ "-fdebug-info-for-profiling" ]
  2326. }
  2327. }
  2328. if (_clang_sample_profile != "") {
  2329. assert(chrome_pgo_phase == 0, "AFDO can't be used in PGO builds")
  2330. rebased_clang_sample_profile =
  2331. rebase_path(_clang_sample_profile, root_build_dir)
  2332. cflags += [ "-fprofile-sample-use=${rebased_clang_sample_profile}" ]
  2333. inputs = [ _clang_sample_profile ]
  2334. }
  2335. } else if (auto_profile_path != "" && is_a_target_toolchain) {
  2336. cflags = [ "-fauto-profile=${auto_profile_path}" ]
  2337. inputs = [ auto_profile_path ]
  2338. }
  2339. }
  2340.  
  2341. # Symbols ----------------------------------------------------------------------
  2342.  
  2343. # The BUILDCONFIG file sets the "default_symbols" config on targets by
  2344. # default. It will be equivalent to one the three specific symbol levels.
  2345. #
  2346. # You can override the symbol level on a per-target basis by removing the
  2347. # default config and then adding the named one you want:
  2348. #
  2349. # configs -= [ "//build/config/compiler:default_symbols" ]
  2350. # configs += [ "//build/config/compiler:symbols" ]
  2351.  
  2352. # A helper config that all configs passing /DEBUG to the linker should
  2353. # include as sub-config.
  2354. config("win_pdbaltpath") {
  2355. visibility = [
  2356. ":minimal_symbols",
  2357. ":symbols",
  2358. ]
  2359.  
  2360. # /DEBUG causes the linker to generate a pdb file, and to write the absolute
  2361. # path to it in the executable file it generates. This flag turns that
  2362. # absolute path into just the basename of the pdb file, which helps with
  2363. # build reproducibility. Debuggers look for pdb files next to executables,
  2364. # so there's minimal downside to always using this. However, post-mortem
  2365. # debugging of Chromium crash dumps and ETW tracing can be complicated by this
  2366. # switch so an option to omit it is important.
  2367. if (!use_full_pdb_paths) {
  2368. ldflags = [ "/pdbaltpath:%_PDB%" ]
  2369. }
  2370. }
  2371.  
  2372. # Full symbols.
  2373. config("symbols") {
  2374. if (is_win) {
  2375. if (is_clang) {
  2376. cflags = [ "/Z7" ] # Debug information in the .obj files.
  2377. } else {
  2378. cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
  2379. }
  2380.  
  2381. if (is_clang && use_lld && use_ghash) {
  2382. cflags += [ "-gcodeview-ghash" ]
  2383. ldflags = [ "/DEBUG:GHASH" ]
  2384. } else {
  2385. ldflags = [ "/DEBUG" ]
  2386. }
  2387.  
  2388. # All configs using /DEBUG should include this:
  2389. configs = [ ":win_pdbaltpath" ]
  2390. } else {
  2391. cflags = []
  2392. if (is_mac && enable_dsyms) {
  2393. # If generating dSYMs, specify -fno-standalone-debug. This was
  2394. # originally specified for https://crbug.com/479841 because dsymutil
  2395. # could not handle a 4GB dSYM file. But dsymutil from Xcodes prior to
  2396. # version 7 also produces debug data that is incompatible with Breakpad
  2397. # dump_syms, so this is still required (https://crbug.com/622406).
  2398. cflags += [ "-fno-standalone-debug" ]
  2399. }
  2400.  
  2401. if (!is_nacl) {
  2402. if (use_dwarf5) {
  2403. cflags += [ "-gdwarf-5" ]
  2404. } else if (!is_apple) {
  2405. # Recent clang versions default to DWARF5 on Linux, and Android is about
  2406. # to switch. TODO: Adopt that in controlled way.
  2407. # Apple platforms still default to 4, so the flag is not needed there.
  2408. cflags += [ "-gdwarf-4" ]
  2409. }
  2410. }
  2411.  
  2412. # The gcc-based nacl compilers don't support -fdebug-compilation-dir (see
  2413. # elsewhere in this file), so they can't have build-dir-independent output.
  2414. # Disable symbols for nacl object files to get deterministic,
  2415. # build-directory-independent output. pnacl and nacl-clang do support that
  2416. # flag, so we can use use -g1 for pnacl and nacl-clang compiles.
  2417. # gcc nacl is is_nacl && !is_clang, pnacl and nacl-clang are && is_clang.
  2418. if ((!is_nacl || is_clang) && current_os != "zos") {
  2419. cflags += [ "-g2" ]
  2420. }
  2421.  
  2422. if (!is_nacl && is_clang && !is_tsan && !is_asan) {
  2423. # gcc generates dwarf-aranges by default on -g1 and -g2. On clang it has
  2424. # to be manually enabled.
  2425. #
  2426. # It is skipped in tsan and asan because enabling it causes some
  2427. # formatting changes in the output which would require fixing bunches
  2428. # of expectation regexps.
  2429. #
  2430. # It is skipped when generating bitcode is enabled as -gdwars-aranges
  2431. # is incompatible with -fembed-bitcode/-fembed-bitcode-marker.
  2432. cflags += [ "-gdwarf-aranges" ]
  2433. }
  2434.  
  2435. if (is_apple) {
  2436. swiftflags = [ "-g" ]
  2437. }
  2438.  
  2439. if (use_debug_fission) {
  2440. cflags += [ "-gsplit-dwarf" ]
  2441. }
  2442. asmflags = cflags
  2443. ldflags = []
  2444.  
  2445. # Split debug info with all thinlto builds except nacl and apple.
  2446. # thinlto requires -gsplit-dwarf in ldflags.
  2447. if (use_debug_fission && use_thin_lto && !is_nacl && !is_apple) {
  2448. ldflags += [ "-gsplit-dwarf" ]
  2449. }
  2450.  
  2451. # TODO(thakis): Figure out if there's a way to make this go for 32-bit,
  2452. # currently we get "warning:
  2453. # obj/native_client/src/trusted/service_runtime/sel_asm/nacl_switch_32.o:
  2454. # DWARF info may be corrupt; offsets in a range list entry are in different
  2455. # sections" there. Maybe just a bug in nacl_switch_32.S.
  2456. _enable_gdb_index =
  2457. symbol_level == 2 && !is_apple && !is_nacl && current_cpu != "x86" &&
  2458. current_os != "zos" && (use_gold || use_lld) &&
  2459. # Disable on non-fission 32-bit Android because it pushes
  2460. # libcomponents_unittests over the 4gb size limit.
  2461. !(is_android && !use_debug_fission && current_cpu != "x64" &&
  2462. current_cpu != "arm64")
  2463. if (_enable_gdb_index) {
  2464. if (is_clang) {
  2465. # This flag enables the GNU-format pubnames and pubtypes sections,
  2466. # which lld needs in order to generate a correct GDB index.
  2467. # TODO(pcc): Try to make lld understand non-GNU-format pubnames
  2468. # sections (llvm.org/PR34820).
  2469. cflags += [ "-ggnu-pubnames" ]
  2470. }
  2471. ldflags += [ "-Wl,--gdb-index" ]
  2472. }
  2473. }
  2474.  
  2475. configs = []
  2476.  
  2477. # Compress debug on 32-bit ARM to stay under 4GB for ChromeOS
  2478. # https://b/243982712.
  2479. if (symbol_level == 2 && is_chromeos_device && !use_debug_fission &&
  2480. !is_nacl && current_cpu == "arm") {
  2481. configs += [ "//build/config:compress_debug_sections" ]
  2482. }
  2483.  
  2484. if (is_clang && (!is_nacl || is_nacl_saigo) && current_os != "zos") {
  2485. if (is_apple) {
  2486. # TODO(https://crbug.com/1050118): Investigate missing debug info on mac.
  2487. # Make sure we don't use constructor homing on mac.
  2488. cflags += [
  2489. "-Xclang",
  2490. "-debug-info-kind=limited",
  2491. ]
  2492. } else {
  2493. # Use constructor homing for debug info. This option reduces debug info
  2494. # by emitting class type info only when constructors are emitted.
  2495. cflags += [
  2496. "-Xclang",
  2497. "-fuse-ctor-homing",
  2498. ]
  2499. }
  2500. }
  2501. rustflags = [ "-g" ]
  2502. }
  2503.  
  2504. # Minimal symbols.
  2505. # This config guarantees to hold symbol for stack trace which are shown to user
  2506. # when crash happens in unittests running on buildbot.
  2507. config("minimal_symbols") {
  2508. if (is_win) {
  2509. # Functions, files, and line tables only.
  2510. cflags = []
  2511.  
  2512. if (is_clang && use_lld && use_ghash) {
  2513. cflags += [ "-gcodeview-ghash" ]
  2514. ldflags = [ "/DEBUG:GHASH" ]
  2515. } else {
  2516. ldflags = [ "/DEBUG" ]
  2517. }
  2518.  
  2519. # All configs using /DEBUG should include this:
  2520. configs = [ ":win_pdbaltpath" ]
  2521.  
  2522. # Enable line tables for clang. MSVC doesn't have an equivalent option.
  2523. if (is_clang) {
  2524. # -gline-tables-only is the same as -g1, but clang-cl only exposes the
  2525. # former.
  2526. cflags += [ "-gline-tables-only" ]
  2527. }
  2528. } else {
  2529. cflags = []
  2530. if (is_mac && !use_dwarf5) {
  2531. # clang defaults to DWARF2 on macOS unless mac_deployment_target is
  2532. # at least 10.11.
  2533. # TODO(thakis): Remove this once mac_deployment_target is 10.11.
  2534. cflags += [ "-gdwarf-4" ]
  2535. } else if (!use_dwarf5 && !is_nacl) {
  2536. # Recent clang versions default to DWARF5 on Linux, and Android is about
  2537. # to switch. TODO: Adopt that in controlled way.
  2538. cflags += [ "-gdwarf-4" ]
  2539. }
  2540.  
  2541. if (use_dwarf5 && !is_nacl) {
  2542. cflags += [ "-gdwarf-5" ]
  2543. }
  2544.  
  2545. # The gcc-based nacl compilers don't support -fdebug-compilation-dir (see
  2546. # elsewhere in this file), so they can't have build-dir-independent output.
  2547. # Disable symbols for nacl object files to get deterministic,
  2548. # build-directory-independent output. pnacl and nacl-clang do support that
  2549. # flag, so we can use use -g1 for pnacl and nacl-clang compiles.
  2550. # gcc nacl is is_nacl && !is_clang, pnacl and nacl-clang are && is_clang.
  2551. if (!is_nacl || is_clang) {
  2552. cflags += [ "-g1" ]
  2553. }
  2554.  
  2555. if (!is_nacl && is_clang && !is_tsan && !is_asan) {
  2556. # See comment for -gdwarf-aranges in config("symbols").
  2557. cflags += [ "-gdwarf-aranges" ]
  2558. }
  2559.  
  2560. ldflags = []
  2561. if (is_android && is_clang) {
  2562. # Android defaults to symbol_level=1 builds, but clang, unlike gcc,
  2563. # doesn't emit DW_AT_linkage_name in -g1 builds.
  2564. # -fdebug-info-for-profiling enables that (and a bunch of other things we
  2565. # don't need), so that we get qualified names in stacks.
  2566. # TODO(thakis): Consider making clang emit DW_AT_linkage_name in -g1 mode;
  2567. # failing that consider doing this on non-Android too.
  2568. cflags += [ "-fdebug-info-for-profiling" ]
  2569. }
  2570.  
  2571. asmflags = cflags
  2572. }
  2573. rustflags = [ "-Cdebuginfo=1" ]
  2574. }
  2575.  
  2576. # This configuration contains function names only. That is, the compiler is
  2577. # told to not generate debug information and the linker then just puts function
  2578. # names in the final debug information.
  2579. config("no_symbols") {
  2580. if (is_win) {
  2581. ldflags = [ "/DEBUG:NONE" ]
  2582.  
  2583. # All configs using /DEBUG should include this:
  2584. configs = [ ":win_pdbaltpath" ]
  2585. } else {
  2586. cflags = [ "-g0" ]
  2587. asmflags = cflags
  2588. }
  2589. }
  2590.  
  2591. # Default symbols.
  2592. config("default_symbols") {
  2593. if (symbol_level == 0) {
  2594. configs = [ ":no_symbols" ]
  2595. } else if (symbol_level == 1) {
  2596. configs = [ ":minimal_symbols" ]
  2597. } else if (symbol_level == 2) {
  2598. configs = [ ":symbols" ]
  2599. } else {
  2600. assert(false)
  2601. }
  2602.  
  2603. # This config is removed by base unittests apk.
  2604. if (is_android && is_clang && strip_debug_info) {
  2605. configs += [ ":strip_debug" ]
  2606. }
  2607. }
  2608.  
  2609. config("strip_debug") {
  2610. if (!defined(ldflags)) {
  2611. ldflags = []
  2612. }
  2613. ldflags += [ "-Wl,--strip-debug" ]
  2614. }
  2615.  
  2616. if (is_apple) {
  2617. # On Mac and iOS, this enables support for ARC (automatic ref-counting).
  2618. # See http://clang.llvm.org/docs/AutomaticReferenceCounting.html.
  2619. config("enable_arc") {
  2620. common_flags = [ "-fobjc-arc" ]
  2621. cflags_objc = common_flags
  2622. cflags_objcc = common_flags
  2623. }
  2624. }
  2625.  
  2626. if (is_chromeos_ash && is_chromeos_device) {
  2627. # This config is intended to be a temporary to facilitate
  2628. # the transition to use orderfile in Chrome OS. Once orderfile
  2629. # use becomes a default in Chrome OS, this config should not
  2630. # be needed.
  2631. config("use_orderfile_for_hugepage") {
  2632. if (chrome_orderfile_path != "") {
  2633. defines = [ "CHROMEOS_ORDERFILE_USE" ]
  2634. }
  2635. }
  2636. }
  2637.  
  2638. if (is_android || (is_chromeos_ash && is_chromeos_device)) {
  2639. # Use orderfile for linking Chrome on Android and Chrome OS.
  2640. # This config enables using an orderfile for linking in LLD.
  2641. # TODO: Consider using call graph sort instead, at least on Android.
  2642. config("chrome_orderfile_config") {
  2643. if (chrome_orderfile_path != "" && !enable_call_graph_profile_sort) {
  2644. assert(use_lld)
  2645. _rebased_orderfile = rebase_path(chrome_orderfile_path, root_build_dir)
  2646. ldflags = [
  2647. "-Wl,--symbol-ordering-file",
  2648. "-Wl,$_rebased_orderfile",
  2649. "-Wl,--no-warn-symbol-ordering",
  2650. ]
  2651. inputs = [ chrome_orderfile_path ]
  2652. }
  2653. }
  2654. }
  2655.  
  2656. # Initialize all variables on the stack if needed.
  2657. config("default_init_stack_vars") {
  2658. cflags = []
  2659. if (init_stack_vars && is_clang && !is_nacl && !using_sanitizer) {
  2660. cflags += [ "-ftrivial-auto-var-init=pattern" ]
  2661. }
  2662. }
  2663.  
  2664. buildflag_header("compiler_buildflags") {
  2665. header = "compiler_buildflags.h"
  2666.  
  2667. flags = [
  2668. "CLANG_PGO=$chrome_pgo_phase",
  2669. "SYMBOL_LEVEL=$symbol_level",
  2670. ]
  2671. }
  2672.  
  2673. config("cet_shadow_stack") {
  2674. if (enable_cet_shadow_stack && is_win) {
  2675. assert(target_cpu == "x64")
  2676. ldflags = [ "/CETCOMPAT" ]
  2677. }
  2678. }
  2679.  
Advertisement
Add Comment
Please, Sign In to add comment