BUILD.gn 88 KB

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