BUILD.gn 86 KB

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