Advertisement
Guest User

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

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