Add new certificateProvider extension API.
[chromium-blink-merge.git] / build / config / compiler / BUILD.gn
blob68084109f686d00d324b723a3a1657d907806c1e
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import("//build/config/android/config.gni")
6 import("//build/config/chrome_build.gni")
7 if (current_cpu == "arm") {
8   import("//build/config/arm.gni")
10 if (current_cpu == "mipsel" || current_cpu == "mips64el") {
11   import("//build/config/mips.gni")
13 if (is_posix) {
14   import("//build/config/gcc/gcc_version.gni")
16 if (is_win) {
17   import("//build/config/win/visual_studio_version.gni")
20 import("//build/toolchain/ccache.gni")
21 import("//build/config/sanitizers/sanitizers.gni")
23 declare_args() {
24   # Normally, Android builds are lightly optimized, even for debug builds, to
25   # keep binary size down. Setting this flag to true disables such optimization
26   android_full_debug = false
28   # Whether to use the binary binutils checked into third_party/binutils.
29   # These are not multi-arch so cannot be used except on x86 and x86-64 (the
30   # only two architectures that are currently checked in). Turn this off when
31   # you are using a custom toolchain and need to control -B in cflags.
32   linux_use_bundled_binutils = is_linux && current_cpu == "x64"
33   binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
34                               root_build_dir)
36   # Compile in such a way as to enable profiling of the generated code. For
37   # example, don't omit the frame pointer and leave in symbols.
38   enable_profiling = false
40   # Compile in such a way as to make it possible for the profiler to unwind full
41   # stack frames. Setting this flag has a large effect on the performance of the
42   # generated code than just setting profiling, but gives the profiler more
43   # information to analyze.
44   # Requires profiling to be set to true.
45   enable_full_stack_frames_for_profiling = false
47   # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
48   # address space, and it doesn't support cross-compiling).
49   use_gold = is_linux && current_cpu == "x64"
51   # When we are going to use gold we need to find it.
52   if (use_gold) {
53     gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
54                             root_build_dir)
55   } else {
56     gold_path = ""
57   }
59   # use_debug_fission: whether to use split DWARF debug info
60   # files. This can reduce link time significantly, but is incompatible
61   # with some utilities such as icecc and ccache. Requires gold and
62   # gcc >= 4.8 or clang.
63   # http://gcc.gnu.org/wiki/DebugFission
64   use_debug_fission = is_debug && !is_win && use_gold &&
65                       linux_use_bundled_binutils && !use_ccache
67   if (is_win) {
68     # Whether the VS xtree header has been patched to disable warning 4702. If
69     # it has, then we don't need to disable 4702 (unreachable code warning).
70     # The patch is preapplied to the internal toolchain and hence all bots.
71     msvs_xtree_patched = false
72   }
75 # default_include_dirs ---------------------------------------------------------
77 # This is a separate config so that third_party code (which would not use the
78 # source root and might have conflicting versions of some headers) can remove
79 # this and specify their own include paths.
80 config("default_include_dirs") {
81   include_dirs = [
82     "//",
83     root_gen_dir,
84   ]
87 # TODO(GYP) bug 527515: is_ubsan, is_ubsan_vptr
88 if (!is_win) {
89   using_sanitizer = is_asan || is_lsan || is_tsan || is_msan
92 # compiler ---------------------------------------------------------------------
94 # Base compiler configuration.
96 # See also "runtime_library" below for related stuff and a discussion about
97 # where stuff should go. Put warning related stuff in the "warnings" config.
99 config("compiler") {
100   cflags = []
101   cflags_c = []
102   cflags_cc = []
103   cflags_objc = []
104   cflags_objcc = []
105   ldflags = []
106   defines = []
108   # In general, Windows is totally different, but all the other builds share
109   # some common GCC configuration. This section sets up Windows and the common
110   # GCC flags, and then we handle the other non-Windows platforms specifically
111   # below.
112   if (is_win) {
113     # Windows compiler flags setup.
114     # -----------------------------
115     cflags += [
116       "/Gy",  # Enable function-level linking.
117       "/GS",  # Enable buffer security checking.
118       "/FS",  # Preserve previous PDB behavior.
119       "/bigobj",  # Some of our files are bigger than the regular limits.
120     ]
122     if (visual_studio_version == "2015") {
123       # Work around crbug.com/526851, bug in VS 2015 RTM compiler.
124       cflags += [ "/Zc:sizedDealloc-" ]
125     }
127     # Force C/C++ mode for the given GN detected file type. This is necessary
128     # for precompiled headers where the same source file is compiled in both
129     # modes.
130     cflags_c += [ "/TC" ]
131     cflags_cc += [ "/TP" ]
133     # Building with Clang on Windows is a work in progress and very
134     # experimental. See crbug.com/82385.
135     # Keep this in sync with the similar block in build/common.gypi
136     if (is_clang) {
137       cflags += [
138         # Many files use intrinsics without including this header.
139         # TODO(hans): Fix those files, or move this to sub-GYPs.
140         "/FIIntrin.h",
141       ]
143       if (visual_studio_version == "2013") {
144         cflags += [ "-fmsc-version=1800" ]
145       } else if (visual_studio_version == "2015") {
146         cflags += [ "-fmsc-version=1900" ]
147       }
149       if (current_cpu == "x86") {
150         cflags += [ "-m32" ]
151       } else {
152         cflags += [ "-m64" ]
153       }
155       if (exec_script("//build/win/use_ansi_codes.py", [], "trim string") ==
156           "True") {
157         cflags += [
158           # cmd.exe doesn't understand ANSI escape codes by default,
159           # so only enable them if something emulating them is around.
160           "-fansi-escape-codes",
161         ]
162       }
163     }
165     if (is_syzyasan) {
166       # SyzyAsan needs /PROFILE turned on to produce appropriate pdbs.
167       assert(!is_win_fastlink, "/PROFILE and /DEBUG:FASTLINK are incompatible")
168       ldflags += [ "/PROFILE" ]
169     }
170   } else {
171     # Common GCC compiler flags setup.
172     # --------------------------------
173     cflags += [ "-fno-strict-aliasing" ]  # See http://crbug.com/32204
174     cflags_cc += [
175       "-fno-threadsafe-statics",
177       # Not exporting C++ inline functions can generally be applied anywhere
178       # so we do so here. Normal function visibility is controlled by
179       # //build/config/gcc:symbol_visibility_hidden.
180       "-fvisibility-inlines-hidden",
181     ]
183     # Stack protection.
184     if (is_mac) {
185       cflags += [ "-fstack-protector-all" ]
186     } else if (is_posix && !is_chromeos && !is_nacl) {
187       # TODO(phajdan.jr): Use -fstack-protector-strong when our gcc supports it.
188       cflags += [ "--param=ssp-buffer-size=4" ]
189       if (is_android && (current_cpu == "arm64" || current_cpu == "x86")) {
190         cflags += [ "-fno-stack-protector" ]
191       } else {
192         cflags += [ "-fstack-protector" ]
193       }
194     }
196     # Linker warnings.
197     if (!(is_chromeos && current_cpu == "arm") && !is_mac && !is_ios) {
198       # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
199       ldflags += [ "-Wl,--fatal-warnings" ]
200     }
202     # Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer and
203     # MemorySanitizer
204     if (using_sanitizer) {
205       cflags += [
206         "-fno-omit-frame-pointer",
207         "-gline-tables-only",
208       ]
209     }
210     if (is_asan) {
211       asan_blacklist_path =
212           rebase_path("//tools/memory/asan/blacklist.txt", root_build_dir)
213       cflags += [
214         "-fsanitize=address",
215         "-fsanitize-blacklist=$asan_blacklist_path",
216       ]
217       if (is_mac) {
218         cflags += [ "-mllvm -asan-globals=0" ]  # http://crbug.com/352073
219         # TODO(GYP): deal with mac_bundles.
220       }
221     }
222     if (is_lsan) {
223       cflags += [ "-fsanitize=leak" ]
224     }
225     if (is_tsan) {
226       tsan_blacklist_path =
227           rebase_path("//tools/memory/tsan_v2/ignores.txt", root_build_dir)
228       cflags += [
229         "-fsanitize=thread",
230         "-fsanitize-blacklist=$tsan_blacklist_path",
231       ]
232     }
233     if (is_msan) {
234       msan_blacklist_path =
235           rebase_path("//tools/msan/blacklist.txt", root_build_dir)
236       cflags += [
237         "-fsanitize=memory",
238         "-fsanitize-memory-track-origins=$msan_track_origins",
239         "-fsanitize-blacklist=$msan_blacklist_path",
240       ]
241     }
243     if (use_custom_libcxx) {
244       cflags_cc += [ "-nostdinc++" ]
245       include_dirs = [
246         "//buildtools/third_party/libc++/trunk/include",
247         "//buildtools/third_party/libc++abi/trunk/include",
248       ]
249     }
250   }
252   if (is_clang && is_debug) {
253     # Allow comparing the address of references and 'this' against 0
254     # in debug builds. Technically, these can never be null in
255     # well-defined C/C++ and Clang can optimize such checks away in
256     # release builds, but they may be used in asserts in debug builds.
257     cflags_cc += [
258       "-Wno-undefined-bool-conversion",
259       "-Wno-tautological-undefined-compare",
260     ]
261   }
263   if (is_clang && !is_nacl) {
264     # This is here so that all files get recompiled after a clang roll and
265     # when turning clang on or off. (defines are passed via the command line,
266     # and build system rebuild things when their commandline changes). Nothing
267     # should ever read this define.
268     defines +=
269         [ "CR_CLANG_REVISION=" + exec_script("//tools/clang/scripts/update.py",
270                                              [ "--print-revision" ],
271                                              "trim string") ]
272   }
274   # Mac-specific compiler flags setup.
275   # ----------------------------------
276   if (is_mac || is_ios) {
277     # These flags are shared between the C compiler and linker.
278     common_mac_flags = []
280     # CPU architecture.
281     if (current_cpu == "x64") {
282       common_mac_flags += [
283         "-arch",
284         "x86_64",
285       ]
286     } else if (current_cpu == "x86") {
287       common_mac_flags += [
288         "-arch",
289         "i386",
290       ]
291     } else if (current_cpu == "arm") {
292       # TODO(GYP): we may need to distinguish between "arm64", "armv7",
293       # and "armv7s" for iOS, and hence need multiple current_cpu values
294       # rather than just "arm".
295       common_mac_flags += [
296         "-arch",
297         "arm64",
298       ]
299     }
301     cflags += common_mac_flags
303     # Without this, the constructors and destructors of a C++ object inside
304     # an Objective C struct won't be called, which is very bad.
305     cflags_objcc += [ "-fobjc-call-cxx-cdtors" ]
307     cflags_c += [ "-std=c99" ]
309     ldflags += common_mac_flags
310   } else if (is_posix) {
311     # Non-Mac Posix compiler flags setup.
312     # -----------------------------------
313     if (enable_profiling && !is_debug) {
314       # The GYP build spams this define into every compilation unit, as we do
315       # here, but it only appears to be used in base and a couple other places.
316       # TODO(abarth): Should we move this define closer to where it's used?
317       defines += [ "ENABLE_PROFILING" ]
319       cflags += [
320         "-fno-omit-frame-pointer",
321         "-g",
322       ]
324       if (enable_full_stack_frames_for_profiling) {
325         cflags += [
326           "-fno-inline",
327           "-fno-optimize-sibling-calls",
328         ]
329       }
330     }
332     # CPU architecture. We may or may not be doing a cross compile now, so for
333     # simplicity we always explicitly set the architecture.
334     if (current_cpu == "x64") {
335       cflags += [
336         "-m64",
337         "-march=x86-64",
338       ]
339       ldflags += [ "-m64" ]
340     } else if (current_cpu == "x86") {
341       cflags += [ "-m32" ]
342       ldflags += [ "-m32" ]
343       if (is_clang) {
344         cflags += [
345           # Else building libyuv gives clang's register allocator issues,
346           # see llvm.org/PR15798 / crbug.com/233709
347           "-momit-leaf-frame-pointer",
349           # Align the stack on 16-byte boundaries, http://crbug.com/418554.
350           "-mstack-alignment=16",
351           "-mstackrealign",
352         ]
353       }
354     } else if (current_cpu == "arm") {
355       cflags += [
356         "-march=$arm_arch",
357         "-mfloat-abi=$arm_float_abi",
358       ]
359       if (arm_tune != "") {
360         cflags += [ "-mtune=$arm_tune" ]
361       }
362       if (arm_use_thumb) {
363         cflags += [ "-mthumb" ]
364         if (is_android && !is_clang) {  # Clang doesn't support this option.
365           cflags += [ "-mthumb-interwork" ]
366         }
367       }
368       if (!is_clang) {
369         # Clang doesn't support these flags.
370         cflags += [
371           # The tree-sra optimization (scalar replacement for
372           # aggregates enabling subsequent optimizations) leads to
373           # invalid code generation when using the Android NDK's
374           # compiler (r5-r7). This can be verified using
375           # webkit_unit_tests' WTF.Checked_int8_t test.
376           "-fno-tree-sra",
378           # The following option is disabled to improve binary
379           # size and performance in gcc 4.9.
380           "-fno-caller-saves",
381         ]
382       }
383     } else if (current_cpu == "mipsel") {
384       if (mips_arch_variant == "r6") {
385         cflags += [
386           "-mips32r6",
387           "-Wa,-mips32r6",
388         ]
389         if (is_android) {
390           ldflags += [
391             "-mips32r6",
392             "-Wl,-melf32ltsmip",
393           ]
394         }
395       } else if (mips_arch_variant == "r2") {
396         cflags += [
397           "-mips32r2",
398           "-Wa,-mips32r2",
399         ]
400         if (mips_float_abi == "hard" && mips_fpu_mode != "") {
401           cflags += [ "-m$mips_fpu_mode" ]
402         }
403       } else if (mips_arch_variant == "r1") {
404         cflags += [
405           "-mips32",
406           "-Wa,-mips32",
407         ]
408       }
410       if (mips_dsp_rev == 1) {
411         cflags += [ "-mdsp" ]
412       } else if (mips_dsp_rev == 2) {
413         cflags += [ "-mdspr2" ]
414       }
416       cflags += [ "-m${mips_float_abi}-float" ]
417     } else if (current_cpu == "mips64el") {
418       if (mips_arch_variant == "r6") {
419         cflags += [
420           "-mips64r6",
421           "-Wa,-mips64r6",
422         ]
423         ldflags += [ "-mips64r6" ]
424       } else if (mips_arch_variant == "r2") {
425         cflags += [
426           "-mips64r2",
427           "-Wa,-mips64r2",
428         ]
429         ldflags += [ "-mips64r2" ]
430       }
431     }
433     defines += [ "_FILE_OFFSET_BITS=64" ]
435     if (!is_android) {
436       defines += [
437         "_LARGEFILE_SOURCE",
438         "_LARGEFILE64_SOURCE",
439       ]
440     }
442     # Omit unwind support in official builds to save space. We can use breakpad
443     # for these builds.
444     if (is_chrome_branded && is_official_build) {
445       cflags += [
446         "-fno-unwind-tables",
447         "-fno-asynchronous-unwind-tables",
448       ]
449       defines += [ "NO_UNWIND_TABLES" ]
450     } else {
451       cflags += [ "-funwind-tables" ]
452     }
453   }
455   # Linux/Android common flags setup.
456   # ---------------------------------
457   if (is_linux || is_android) {
458     cflags += [
459       "-fPIC",
460       "-pipe",  # Use pipes for communicating between sub-processes. Faster.
461     ]
463     ldflags += [
464       "-fPIC",
465       "-Wl,-z,noexecstack",
466       "-Wl,-z,now",
467       "-Wl,-z,relro",
468     ]
469     if (!using_sanitizer) {
470       ldflags += [ "-Wl,-z,defs" ]
471     }
472   }
474   # Linux-specific compiler flags setup.
475   # ------------------------------------
476   if (is_linux) {
477     cflags += [ "-pthread" ]
478     ldflags += [ "-pthread" ]
479   }
480   if (use_gold) {
481     ldflags += [
482       "-B$gold_path",
484       # Newer gccs and clangs support -fuse-ld, use the flag to force gold
485       # selection.
486       # gcc -- http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Optimize-Options.html
487       "-fuse-ld=gold",
489       # Experimentation found that using four linking threads
490       # saved ~20% of link time.
491       # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
492       # Only apply this to the target linker, since the host
493       # linker might not be gold, but isn't used much anyway.
494       # TODO(raymes): Disable threading because gold is frequently
495       # crashing on the bots: crbug.com/161942.
496       #"-Wl,--threads",
497       #"-Wl,--thread-count=4",
498     ]
500     if (!is_asan && !is_msan && !is_lsan && !is_tsan) {
501       # TODO(brettw) common.gypi has this only for target toolset.
502       ldflags += [ "-Wl,--icf=all" ]
503     }
505     # TODO(thestig): Make this flag work with GN.
506     #if (!is_official_build && !is_chromeos && !(is_asan || is_lsan || is_tsan || is_msan)) {
507     #  ldflags += [
508     #    "-Wl,--detect-odr-violations",
509     #  ]
510     #}
511   }
513   if (linux_use_bundled_binutils) {
514     cflags += [ "-B$binutils_path" ]
515   }
517   # Clang-specific compiler flags setup.
518   # ------------------------------------
519   if (is_clang) {
520     cflags += [ "-fcolor-diagnostics" ]
521   }
523   # C++11 compiler flags setup.
524   # ---------------------------
525   if (is_linux || is_android || (is_nacl && is_clang)) {
526     # gnu++11 instead of c++11 is needed because some code uses typeof() (a
527     # GNU extension).
528     # TODO(thakis): Eventually switch this to c++11 instead,
529     # http://crbug.com/427584
530     cflags_cc += [ "-std=gnu++11" ]
531   } else if (!is_win && !is_nacl) {
532     # TODO(mcgrathr) - the NaCl GCC toolchain doesn't support either gnu++11
533     # or c++11; we technically don't need this toolchain any more, but there
534     # are still a few buildbots using it, so until those are turned off
535     # we need the !is_nacl clause and the (is_nacl && is_clang) clause, above.
536     cflags_cc += [ "-std=c++11" ]
537   }
539   # Android-specific flags setup.
540   # -----------------------------
541   if (is_android) {
542     cflags += [
543       "-ffunction-sections",
544       "-funwind-tables",
545       "-fno-short-enums",
546     ]
547     if (!is_clang) {
548       # Clang doesn't support these flags.
549       cflags += [ "-finline-limit=64" ]
550     }
551     if (is_clang) {
552       rebased_android_toolchain_root =
553           rebase_path(android_toolchain_root, root_build_dir)
554       if (current_cpu == "arm") {
555         cflags += [
556           # TODO(hans) Enable integrated-as (crbug.com/124610).
557           "-no-integrated-as",
558           "-B${rebased_android_toolchain_root}/bin",  # Else /usr/bin/as gets picked up.
559         ]
560       }
561     }
562     if (is_asan) {
563       # Android build relies on -Wl,--gc-sections removing unreachable code.
564       # ASan instrumentation for globals inhibits this and results in a library
565       # with unresolvable relocations.
566       # TODO(eugenis): find a way to reenable this.
567       cflags += [ "-mllvm -asan-globals=0" ]
568     }
570     defines += [ "ANDROID" ]
572     # The NDK has these things, but doesn't define the constants
573     # to say that it does. Define them here instead.
574     defines += [ "HAVE_SYS_UIO_H" ]
576     # Use gold for Android for most CPU architectures.
577     if (current_cpu == "x86" || current_cpu == "x64" || current_cpu == "arm") {
578       ldflags += [ "-fuse-ld=gold" ]
579       if (is_clang) {
580         # Let clang find the ld.gold in the NDK.
581         ldflags += [ "--gcc-toolchain=$rebased_android_toolchain_root" ]
582       }
584       # Use -mstackrealign due to a bug on ia32 Jelly Bean.
585       # See crbug.com/521527
586       if (current_cpu == "x86") {
587         cflags += [ "-mstackrealign" ]
588       }
589     }
591     ldflags += [
592       "-Wl,--no-undefined",
594       # Don't allow visible symbols from libgcc or libc++ to be
595       # re-exported.
596       "-Wl,--exclude-libs=libgcc.a",
597       "-Wl,--exclude-libs=libc++_static.a",
599       # Don't allow visible symbols from libraries that contain
600       # assembly code with symbols that aren't hidden properly.
601       # http://crbug.com/448386
602       "-Wl,--exclude-libs=libvpx_assembly_arm.a",
603     ]
604     if (current_cpu == "arm") {
605       ldflags += [
606         # Enable identical code folding to reduce size.
607         "-Wl,--icf=safe",
608       ]
609     }
611     if (is_clang) {
612       if (current_cpu == "arm") {
613         _abi_target = "arm-linux-androideabi"
614       } else if (current_cpu == "x86") {
615         _abi_target = "i686-linux-androideabi"
616       } else if (current_cpu == "arm64") {
617         # Place holder for arm64 support, not tested.
618         _abi_target = "aarch64-linux-androideabi"
619       } else if (current_cpu == "x64") {
620         # Place holder for x64 support, not tested.
621         # TODO: Enable clang support for Android x64. http://crbug.com/346626
622         _abi_target = "x86_64-linux-androideabi"
623       } else if (current_cpu == "mipsel") {
624         # Place holder for mips support, not tested.
625         _abi_target = "mipsel-linux-androideabi"
626       } else if (current_cpu == "mips64el") {
627         # Place holder for mips64 support, not tested.
628         _abi_target = "mips64el-linux-androideabi"
629       } else {
630         assert(false, "Architecture not supported")
631       }
632       cflags += [
633         "-target",
634         _abi_target,
635       ]
636       ldflags += [
637         "-target",
638         _abi_target,
639       ]
640     }
641   }
643   # Pass the same C/C++ flags to the objective C/C++ compiler.
644   cflags_objc += cflags_c
645   cflags_objcc += cflags_cc
648 config("compiler_arm_fpu") {
649   if (current_cpu == "arm" && !is_ios) {
650     cflags = [ "-mfpu=$arm_fpu" ]
651   }
654 # runtime_library -------------------------------------------------------------
656 # Sets the runtime library and associated options.
658 # How do you determine what should go in here vs. "compiler" above? Consider if
659 # a target might choose to use a different runtime library (ignore for a moment
660 # if this is possible or reasonable on your system). If such a target would want
661 # to change or remove your option, put it in the runtime_library config. If a
662 # target wants the option regardless, put it in the compiler config.
664 config("runtime_library") {
665   cflags = []
666   defines = []
667   ldflags = []
668   lib_dirs = []
669   libs = []
671   if (is_component_build) {
672     # Component mode: dynamic CRT.
673     defines += [ "COMPONENT_BUILD" ]
674     if (is_win) {
675       # Since the library is shared, it requires exceptions or will give errors
676       # about things not matching, so keep exceptions on.
677       if (is_debug) {
678         cflags += [ "/MDd" ]
679       } else {
680         cflags += [ "/MD" ]
681       }
682     }
683   } else {
684     if (is_win && current_os != "win") {
685       # WindowsRT: use the dynamic CRT.
686       if (is_debug) {
687         cflags += [ "/MDd" ]
688       } else {
689         cflags += [ "/MD" ]
690       }
691     } else if (is_win) {
692       # Desktop Windows: static CRT.
693       if (is_debug) {
694         cflags += [ "/MTd" ]
695       } else {
696         cflags += [ "/MT" ]
697       }
698     }
699   }
701   if (is_win) {
702     defines += [
703       "__STD_C",
704       "_CRT_RAND_S",
705       "_CRT_SECURE_NO_DEPRECATE",
706       "_HAS_EXCEPTIONS=0",
707       "_SCL_SECURE_NO_DEPRECATE",
708     ]
709   }
711   # Android standard library setup.
712   if (is_android) {
713     if (is_clang) {
714       # Work around incompatibilities between bionic and clang headers.
715       defines += [
716         "__compiler_offsetof=__builtin_offsetof",
717         "nan=__builtin_nan",
718       ]
719     }
721     defines += [ "__GNU_SOURCE=1" ]  # Necessary for clone().
723     # TODO(jdduke) Re-enable on mips after resolving linking
724     # issues with libc++ (crbug.com/456380).
725     if (current_cpu != "mipsel" && current_cpu != "mips64el") {
726       ldflags += [ "-Wl,--warn-shared-textrel" ]
727     }
728     ldflags += [ "-nostdlib" ]
730     # NOTE: The libc++ header include paths below are specified in cflags
731     # rather than include_dirs because they need to come after include_dirs.
732     # Think of them like system headers, but don't use '-isystem' because the
733     # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
734     # strange errors. The include ordering here is important; change with
735     # caution.
736     cflags += [
737       "-isystem" +
738           rebase_path("$android_libcpp_root/libcxx/include", root_build_dir),
739       "-isystem" + rebase_path(
740               "$android_ndk_root/sources/cxx-stl/llvm-libc++abi/libcxxabi/include",
741               root_build_dir),
742       "-isystem" +
743           rebase_path("$android_ndk_root/sources/android/support/include",
744                       root_build_dir),
745     ]
747     lib_dirs += [ "$android_libcpp_root/libs/$android_app_abi" ]
748     libs += [ "$android_libcpp_library" ]
750     if (current_cpu == "mipsel") {
751       libs += [
752         # ld linker is used for mips Android, and ld does not accept library
753         # absolute path prefixed by "-l"; Since libgcc does not exist in mips
754         # sysroot the proper library will be linked.
755         # TODO(gordanac): Remove once gold linker is used for mips Android.
756         "gcc",
757       ]
758     } else {
759       libs += [
760         # Manually link the libgcc.a that the cross compiler uses. This is
761         # absolute because the linker will look inside the sysroot if it's not.
762         rebase_path(android_libgcc_file),
763       ]
764     }
766     libs += [
767       "c",
768       "dl",
769       "m",
770     ]
772     # Clang with libc++ does not require an explicit atomic library reference.
773     if (!is_clang) {
774       libs += [ "atomic" ]
775     }
776   }
779 # default_warning_flags collects all warning flags that are used by default.
780 # This is in a variable instead of a config so that it can be used in
781 # both chromium_code and no_chromium_code.  This way these flags are guaranteed
782 # to appear on the compile command line after -Wall.
784 default_warning_flags = []
785 default_warning_flags_cc = []
786 if (is_win) {
787   default_warning_flags += [
788     # Treat warnings as errors.
789     "/WX",
791     # Warnings permanently disabled:
793     # C4127: conditional expression is constant
794     # This warning can in theory catch dead code and other problems, but
795     # triggers in far too many desirable cases where the conditional
796     # expression is either set by macros or corresponds some legitimate
797     # compile-time constant expression (due to constant template args,
798     # conditionals comparing the sizes of different types, etc.).  Some of
799     # these can be worked around, but it's not worth it.
800     "/wd4127",
802     # C4251: 'identifier' : class 'type' needs to have dll-interface to be
803     #        used by clients of class 'type2'
804     # This is necessary for the shared library build.
805     "/wd4251",
807     # C4351: new behavior: elements of array 'array' will be default
808     #        initialized
809     # This is a silly "warning" that basically just alerts you that the
810     # compiler is going to actually follow the language spec like it's
811     # supposed to, instead of not following it like old buggy versions did.
812     # There's absolutely no reason to turn this on.
813     "/wd4351",
815     # C4355: 'this': used in base member initializer list
816     # It's commonly useful to pass |this| to objects in a class' initializer
817     # list.  While this warning can catch real bugs, most of the time the
818     # constructors in question don't attempt to call methods on the passed-in
819     # pointer (until later), and annotating every legit usage of this is
820     # simply more hassle than the warning is worth.
821     "/wd4355",
823     # C4503: 'identifier': decorated name length exceeded, name was
824     #        truncated
825     # This only means that some long error messages might have truncated
826     # identifiers in the presence of lots of templates.  It has no effect on
827     # program correctness and there's no real reason to waste time trying to
828     # prevent it.
829     "/wd4503",
831     # Warning C4589 says: "Constructor of abstract class ignores
832     # initializer for virtual base class." Disable this warning because it
833     # is flaky in VS 2015 RTM. It triggers on compiler generated
834     # copy-constructors in some cases.
835     "/wd4589",
837     # C4611: interaction between 'function' and C++ object destruction is
838     #        non-portable
839     # This warning is unavoidable when using e.g. setjmp/longjmp.  MSDN
840     # suggests using exceptions instead of setjmp/longjmp for C++, but
841     # Chromium code compiles without exception support.  We therefore have to
842     # use setjmp/longjmp for e.g. JPEG decode error handling, which means we
843     # have to turn off this warning (and be careful about how object
844     # destruction happens in such cases).
845     "/wd4611",
847     # Warnings to evaluate and possibly fix/reenable later:
849     "/wd4100",  # Unreferenced formal function parameter.
850     "/wd4121",  # Alignment of a member was sensitive to packing.
851     "/wd4244",  # Conversion: possible loss of data.
852     "/wd4481",  # Nonstandard extension: override specifier.
853     "/wd4505",  # Unreferenced local function has been removed.
854     "/wd4510",  # Default constructor could not be generated.
855     "/wd4512",  # Assignment operator could not be generated.
856     "/wd4610",  # Class can never be instantiated, constructor required.
857     "/wd4995",  # 'X': name was marked as #pragma deprecated
858     "/wd4996",  # Deprecated function warning.
859   ]
861   # VS xtree header file needs to be patched or 4702 (unreachable code
862   # warning) is reported if _HAS_EXCEPTIONS=0. Disable the warning if xtree is
863   # not patched.
864   if (!msvs_xtree_patched &&
865       exec_script("../../win_is_xtree_patched.py", [], "value") == 0) {
866     default_warning_flags += [ "/wd4702" ]  # Unreachable code.
867   }
869   # Building with Clang on Windows is a work in progress and very
870   # experimental. See crbug.com/82385.
871   # Keep this in sync with the similar block in build/common.gypi
872   if (is_clang) {
873     default_warning_flags += [
874       # TODO(hans): Make this list shorter eventually, http://crbug.com/504657
875       "-Qunused-arguments",  # http://crbug.com/504658
876       "-Wno-microsoft-enum-value",  # http://crbug.com/505296
877       "-Wno-unknown-pragmas",  # http://crbug.com/505314
878       "-Wno-unused-value",  # http://crbug.com/505318
879     ]
880   }
881 } else {
882   # Common GCC warning setup.
883   default_warning_flags += [
884     # Enables.
885     "-Wendif-labels",  # Weird old-style text after an #endif.
886     "-Werror",  # Warnings as errors.
888     # Disables.
889     "-Wno-missing-field-initializers",  # "struct foo f = {0};"
890     "-Wno-unused-parameter",  # Unused function parameters.
891   ]
893   if (is_mac) {
894     default_warning_flags += [ "-Wnewline-eof" ]
895     if (!is_nacl) {
896       # When compiling Objective-C, warns if a method is used whose
897       # availability is newer than the deployment target. This is not
898       # required when compiling Chrome for iOS.
899       default_warning_flags += [ "-Wpartial-availability" ]
900     }
901   }
903   if (gcc_version >= 48) {
904     default_warning_flags_cc += [
905       # See comment for -Wno-c++11-narrowing.
906       "-Wno-narrowing",
907     ]
908     if (!is_clang) {
909       default_warning_flags_cc += [
910         # TODO(thakis): Remove, http://crbug.com/263960
911         "-Wno-literal-suffix",
912       ]
913     }
914   }
916   # Suppress warnings about ABI changes on ARM (Clang doesn't give this
917   # warning).
918   if (current_cpu == "arm" && !is_clang) {
919     default_warning_flags += [ "-Wno-psabi" ]
920   }
922   if (is_android) {
923     # Disable any additional warnings enabled by the Android build system but
924     # which chromium does not build cleanly with (when treating warning as
925     # errors).
926     default_warning_flags += [
927       "-Wno-extra",
928       "-Wno-ignored-qualifiers",
929       "-Wno-type-limits",
930     ]
931     default_warning_flags_cc += [
932       # Disabling c++0x-compat should be handled in WebKit, but
933       # this currently doesn't work because gcc_version is not set
934       # correctly when building with the Android build system.
935       # TODO(torne): Fix this in WebKit.
936       "-Wno-error=c++0x-compat",
938       # Other things unrelated to -Wextra:
939       "-Wno-non-virtual-dtor",
940       "-Wno-sign-promo",
941     ]
942   }
944   if (gcc_version >= 48) {
945     # Don't warn about the "typedef 'foo' locally defined but not used"
946     # for gcc 4.8.
947     # TODO: remove this flag once all builds work. See crbug.com/227506
948     default_warning_flags += [ "-Wno-unused-local-typedefs" ]
949   }
951 if (is_clang) {
952   default_warning_flags += [
953     # This warns on using ints as initializers for floats in
954     # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
955     # which happens in several places in chrome code. Not sure if
956     # this is worth fixing.
957     "-Wno-c++11-narrowing",
959     # Don't die on dtoa code that uses a char as an array index.
960     # This is required solely for base/third_party/dmg_fp/dtoa.cc.
961     # TODO(brettw) move this to that project then!
962     "-Wno-char-subscripts",
964     # Warns on switches on enums that cover all enum values but
965     # also contain a default: branch. Chrome is full of that.
966     "-Wno-covered-switch-default",
968     # Clang considers the `register` keyword as deprecated, but e.g.
969     # code generated by flex (used in angle) contains that keyword.
970     # http://crbug.com/255186
971     "-Wno-deprecated-register",
973     # TODO(thakis): This used to be implied by -Wno-unused-function,
974     # which we no longer use. Check if it makes sense to remove
975     # this as well. http://crbug.com/316352
976     "-Wno-unneeded-internal-declaration",
977   ]
979   if (is_posix && !is_mac && !is_ios) {
980     default_warning_flags += [
981       # TODO(thakis): Remove, http://crbug.com/263960
982       "-Wno-reserved-user-defined-literal",
983     ]
984   }
986   # NaCl's Clang compiler and Chrome's hermetic Clang compiler will almost
987   # always have different versions. Certain flags may not be recognized by
988   # one version or the other.
989   if (!is_nacl) {
990     # Flags NaCl does not recognize.
991     default_warning_flags += [
992       # TODO(hans): Get this cleaned up, http://crbug.com/428099
993       "-Wno-inconsistent-missing-override",
995       # TODO(thakis): Enable this, crbug.com/507717
996       "-Wno-shift-negative-value",
997     ]
998   }
1001 # chromium_code ---------------------------------------------------------------
1003 # Toggles between higher and lower warnings for code that is (or isn't)
1004 # part of Chromium.
1006 config("chromium_code") {
1007   if (is_win) {
1008     cflags = [ "/W4" ]  # Warning level 4.
1009   } else {
1010     cflags = [
1011       "-Wall",
1013       # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
1014       # so we specify it explicitly.
1015       # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
1016       # http://code.google.com/p/chromium/issues/detail?id=90453
1017       "-Wsign-compare",
1018     ]
1020     # In Chromium code, we define __STDC_foo_MACROS in order to get the
1021     # C99 macros on Mac and Linux.
1022     defines = [
1023       "__STDC_CONSTANT_MACROS",
1024       "__STDC_FORMAT_MACROS",
1025     ]
1027     if (!is_debug && !using_sanitizer &&
1028         (!is_linux || !is_clang || is_official_build)) {
1029       # _FORTIFY_SOURCE isn't really supported by Clang now, see
1030       # http://llvm.org/bugs/show_bug.cgi?id=16821.
1031       # It seems to work fine with Ubuntu 12 headers though, so use it in
1032       # official builds.
1033       #
1034       # Non-chromium code is not guaranteed to compile cleanly with
1035       # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
1036       # disabled, so only do that for Release build.
1037       defines += [ "_FORTIFY_SOURCE=2" ]
1038     }
1039   }
1040   cflags += default_warning_flags
1041   cflags_cc = default_warning_flags_cc
1043 config("no_chromium_code") {
1044   cflags = []
1045   cflags_cc = []
1046   defines = []
1048   if (is_win) {
1049     cflags += [
1050       "/W3",  # Warning level 3.
1051       "/wd4800",  # Disable warning when forcing value to bool.
1052       "/wd4267",  # TODO(jschuh): size_t to int.
1053       "/wd4996",  # Deprecated function warning.
1054     ]
1055     defines += [
1056       "_CRT_NONSTDC_NO_WARNINGS",
1057       "_CRT_NONSTDC_NO_DEPRECATE",
1058     ]
1059   }
1061   if (is_linux) {
1062     # Don't warn about ignoring the return value from e.g. close(). This is
1063     # off by default in some gccs but on by default in others. BSD systems do
1064     # not support this option, since they are usually using gcc 4.2.1, which
1065     # does not have this flag yet.
1066     cflags += [ "-Wno-unused-result" ]
1067   }
1069   if (is_clang) {
1070     cflags += [
1071       # TODO(thakis): Move this suppression into individual third-party
1072       # libraries as required. http://crbug.com/505316.
1073       "-Wno-unused-function",
1075       # Lots of third-party libraries have unused variables. Instead of
1076       # suppressing them individually, we just blanket suppress them here.
1077       "-Wno-unused-variable",
1078     ]
1079   }
1081   if (is_linux || is_android) {
1082     cflags += [
1083       # Don't warn about printf format problems. This is off by default in gcc
1084       # but on in Ubuntu's gcc(!).
1085       "-Wno-format",
1086     ]
1087     cflags_cc += [
1088       # Don't warn about hash_map in third-party code.
1089       "-Wno-deprecated",
1090     ]
1091   }
1092   cflags += default_warning_flags
1093   cflags_cc += default_warning_flags_cc
1096 # rtti ------------------------------------------------------------------------
1098 # Allows turning Run-Time Type Identification on or off.
1100 config("rtti") {
1101   if (is_win) {
1102     cflags_cc = [ "/GR" ]
1103   }
1105 config("no_rtti") {
1106   if (is_win) {
1107     cflags_cc = [ "/GR-" ]
1108   } else {
1109     cflags_cc = [ "-fno-rtti" ]
1110     cflags_objcc = cflags_cc
1111   }
1114 # Warnings ---------------------------------------------------------------------
1116 # This will generate warnings when using Clang if code generates exit-time
1117 # destructors, which will slow down closing the program.
1118 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
1119 config("wexit_time_destructors") {
1120   # TODO: Enable on Windows too, http://crbug.com/404525
1121   if (is_clang && !is_win) {
1122     cflags = [ "-Wexit-time-destructors" ]
1123   }
1126 # On Windows compiling on x64, VC will issue a warning when converting
1127 # size_t to int because it will truncate the value. Our code should not have
1128 # these warnings and one should use a static_cast or a checked_cast for the
1129 # conversion depending on the case. However, a lot of code still needs to be
1130 # fixed. Apply this config to such targets to disable the warning.
1132 # Note that this can be applied regardless of platform and architecture to
1133 # clean up the call sites. This will only apply the flag when necessary.
1135 # TODO(jschuh): crbug.com/167187 fix this and delete this config.
1136 config("no_size_t_to_int_warning") {
1137   if (is_win && current_cpu == "x64") {
1138     cflags = [ "/wd4267" ]
1139   }
1142 # Some code presumes that pointers to structures/objects are compatible
1143 # regardless of whether what they point to is already known to be valid.
1144 # gcc 4.9 and earlier had no way of suppressing this warning without
1145 # supressing the rest of them.  Here we centralize the identification of
1146 # the gcc 4.9 toolchains.
1147 config("no_incompatible_pointer_warnings") {
1148   cflags = []
1149   if (is_clang) {
1150     cflags += [ "-Wno-incompatible-pointer-types" ]
1151   } else if (current_cpu == "mipsel") {
1152     cflags += [ "-w" ]
1153   } else if (is_chromeos && current_cpu == "arm") {
1154     cflags += [ "-w" ]
1155   }
1158 # Optimization -----------------------------------------------------------------
1160 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
1161 # which it will assign to the config it implicitly applies to every target. If
1162 # you want to override the optimization level for your target, remove this
1163 # config (which will expand differently for debug or release builds), and then
1164 # add back the one you want to override it with:
1166 #   configs -= default_optimization_config
1167 #   configs += [ "//build/config/compiler/optimize_max" ]
1169 # Shared settings for both "optimize" and "optimize_max" configs.
1170 # IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags.
1171 if (is_win) {
1172   common_optimize_on_cflags = [
1173     "/Ob2",  # Both explicit and auto inlining.
1174     "/Oy-",  # Disable omitting frame pointers, must be after /O2.
1175     "/d2Zi+",  # Improve debugging of optimized code.
1176     "/Zc:inline",  # Remove unreferenced COMDAT (faster links).
1177   ]
1178   if (!is_asan) {
1179     common_optimize_on_cflags += [
1180       # Put data in separate COMDATs. This allows the linker
1181       # to put bit-identical constants at the same address even if
1182       # they're unrelated constants, which saves binary size.
1183       # This optimization can't be used when ASan is enabled because
1184       # it is not compatible with the ASan ODR checker.
1185       "/Gw",
1186     ]
1187   }
1188   common_optimize_on_ldflags = [ "/OPT:ICF" ]
1189   if (is_official_build) {
1190     common_optimize_on_ldflags += [
1191       # Link-time code generation.
1192       "/LTCG",
1194       # Set the number of LTCG code-gen threads to eight. The default is four.
1195       # This gives a 5-10% link speedup.
1196       "/cgthreads:8",
1197     ]
1198   }
1199 } else {
1200   common_optimize_on_cflags = [
1201     # Don't emit the GCC version ident directives, they just end up in the
1202     # .comment section taking up binary size.
1203     "-fno-ident",
1205     # Put data and code in their own sections, so that unused symbols
1206     # can be removed at link time with --gc-sections.
1207     "-fdata-sections",
1208     "-ffunction-sections",
1209   ]
1210   common_optimize_on_ldflags = []
1212   if (is_android) {
1213     if (!using_sanitizer) {
1214       common_optimize_on_cflags += [ "-fomit-frame-pointer" ]
1215     }
1217     # TODO(jdduke) Re-enable on mips after resolving linking
1218     # issues with libc++ (crbug.com/456380).
1219     if (current_cpu != "mipsel" && current_cpu != "mips64el") {
1220       common_optimize_on_ldflags += [
1221         # Warn in case of text relocations.
1222         "-Wl,--warn-shared-textrel",
1223       ]
1224     }
1225   }
1227   if (is_mac || is_ios) {
1228     if (symbol_level == 2) {
1229       # Mac dead code stripping requires symbols.
1230       common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
1231     }
1232   } else {
1233     # Non-Mac Posix linker flags.
1234     common_optimize_on_ldflags += [
1235       # Specifically tell the linker to perform optimizations.
1236       # See http://lwn.net/Articles/192624/ .
1237       "-Wl,-O1",
1238       "-Wl,--gc-sections",
1239     ]
1241     if (!using_sanitizer) {
1242       # Functions interposed by the sanitizers can make ld think
1243       # that some libraries aren't needed when they actually are,
1244       # http://crbug.com/234010. As workaround, disable --as-needed.
1245       common_optimize_on_ldflags += [ "-Wl,--as-needed" ]
1246     }
1247   }
1250 # Default "optimization on" config. On Windows, this favors size over speed.
1251 config("optimize") {
1252   if (is_win) {
1253     # Favor size over speed, /O1 must be before the common flags. The GYP
1254     # build also specifies /Os and /GF but these are implied by /O1.
1255     cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ]
1256   } else if (is_android || is_ios) {
1257     cflags = [ "-Os" ] + common_optimize_on_cflags  # Favor size over speed.
1258   } else {
1259     cflags = [ "-O2" ] + common_optimize_on_cflags
1260   }
1261   ldflags = common_optimize_on_ldflags
1264 # Turn off optimizations.
1265 config("no_optimize") {
1266   if (is_win) {
1267     cflags = [
1268       "/Od",  # Disable optimization.
1269       "/Ob0",  # Disable all inlining (on by default).
1270       "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
1271     ]
1272   } else if (is_android && !android_full_debug) {
1273     # On Android we kind of optimize some things that don't affect debugging
1274     # much even when optimization is disabled to get the binary size down.
1275     cflags = [
1276       "-Os",
1277       "-fdata-sections",
1278       "-ffunction-sections",
1279     ]
1280     if (!using_sanitizer) {
1281       cflags += [ "-fomit-frame-pointer" ]
1282     }
1283     ldflags = common_optimize_on_ldflags
1284   } else {
1285     cflags = [ "-O0" ]
1286   }
1289 # Turns up the optimization level. On Windows, this implies whole program
1290 # optimization and link-time code generation which is very expensive and should
1291 # be used sparingly.
1292 config("optimize_max") {
1293   ldflags = common_optimize_on_ldflags
1294   if (is_win) {
1295     # Favor speed over size, /O2 must be before the common flags. The GYP
1296     # build also specifies /Ot, /Oi, and /GF, but these are implied by /O2.
1297     cflags = [ "/O2" ] + common_optimize_on_cflags
1298     if (is_official_build) {
1299       # TODO(GYP): TODO(dpranke): Should these only be on in an official
1300       # build, or on all the time? For now we'll require official build so
1301       # that the compile is clean.
1302       cflags += [
1303         "/GL",  # Whole program optimization.
1305         # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
1306         # Probably anything that this would catch that wouldn't be caught in a
1307         # normal build isn't going to actually be a bug, so the incremental
1308         # value of C4702 for PGO builds is likely very small.
1309         "/wd4702",
1310       ]
1311     }
1312   } else {
1313     cflags = [ "-O2" ] + common_optimize_on_cflags
1314   }
1317 # Symbols ----------------------------------------------------------------------
1319 config("symbols") {
1320   if (is_win) {
1321     import("//build/toolchain/goma.gni")
1322     if (use_goma) {
1323       cflags = [ "/Z7" ]  # No PDB file
1324     } else {
1325       cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
1326     }
1327     if (is_win_fastlink) {
1328       # Tell VS 2015+ to create a PDB that references debug
1329       # information in .obj and .lib files instead of copying
1330       # it all. This flag is incompatible with /PROFILE
1331       ldflags = [ "/DEBUG:FASTLINK" ]
1332     } else {
1333       ldflags = [ "/DEBUG" ]
1334     }
1335   } else {
1336     cflags = [ "-g2" ]
1337     if (use_debug_fission) {
1338       cflags += [ "-gsplit-dwarf" ]
1339     }
1340   }
1343 config("minimal_symbols") {
1344   if (is_win) {
1345     # Linker symbols for backtraces only.
1346     if (is_win_fastlink) {
1347       # Tell VS 2015+ to create a PDB that references debug
1348       # information in .obj and .lib files instead of copying
1349       # it all. This flag is incompatible with /PROFILE
1350       ldflags = [ "/DEBUG:FASTLINK" ]
1351     } else {
1352       ldflags = [ "/DEBUG" ]
1353     }
1354   } else {
1355     cflags = [ "-g1" ]
1356     if (use_debug_fission) {
1357       cflags += [ "-gsplit-dwarf" ]
1358     }
1359   }
1362 config("no_symbols") {
1363   if (!is_win) {
1364     cflags = [ "-g0" ]
1365   }