Sync defines with the GYP build.
[chromium-blink-merge.git] / build / config / compiler / BUILD.gn
blob0e28e7da8be6dce3e5b5d93500416c894b8513cd
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")
17 import("//build/toolchain/ccache.gni")
19 declare_args() {
20   # Normally, Android builds are lightly optimized, even for debug builds, to
21   # keep binary size down. Setting this flag to true disables such optimization
22   android_full_debug = false
24   # Whether to use the binary binutils checked into third_party/binutils.
25   # These are not multi-arch so cannot be used except on x86 and x86-64 (the
26   # only two architectures that are currently checked in). Turn this off when
27   # you are using a custom toolchain and need to control -B in cflags.
28   linux_use_bundled_binutils = is_linux && current_cpu == "x64"
30   # Compile in such a way as to enable profiling of the generated code. For
31   # example, don't omit the frame pointer and leave in symbols.
32   enable_profiling = false
34   # Compile in such a way as to make it possible for the profiler to unwind full
35   # stack frames. Setting this flag has a large effect on the performance of the
36   # generated code than just setting profiling, but gives the profiler more
37   # information to analyze.
38   # Requires profiling to be set to true.
39   enable_full_stack_frames_for_profiling = false
41   # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
42   # address space, and it doesn't support cross-compiling).
43   use_gold = is_linux && current_cpu == "x64"
45   # use_debug_fission: whether to use split DWARF debug info
46   # files. This can reduce link time significantly, but is incompatible
47   # with some utilities such as icecc and ccache. Requires gold and
48   # gcc >= 4.8 or clang.
49   # http://gcc.gnu.org/wiki/DebugFission
50   use_debug_fission =
51       !is_win && use_gold && linux_use_bundled_binutils && !use_ccache
53   if (is_win) {
54     # Whether the VS xtree header has been patched to disable warning 4702. If
55     # it has, then we don't need to disable 4702 (unreachable code warning).
56     # The patch is preapplied to the internal toolchain and hence all bots.
57     msvs_xtree_patched = false
58   }
61 # default_include_dirs ---------------------------------------------------------
63 # This is a separate config so that third_party code (which would not use the
64 # source root and might have conflicting versions of some headers) can remove
65 # this and specify their own include paths.
66 config("default_include_dirs") {
67   include_dirs = [
68     "//",
69     root_gen_dir,
70   ]
73 # TODO(GYP): is_ubsan, is_ubsan_vptr
74 if (!is_win) {
75   using_sanitizer = is_asan || is_lsan || is_tsan || is_msan
78 # compiler ---------------------------------------------------------------------
80 # Base compiler configuration.
82 # See also "runtime_library" below for related stuff and a discussion about
83 # where stuff should go. Put warning related stuff in the "warnings" config.
85 config("compiler") {
86   cflags = []
87   cflags_c = []
88   cflags_cc = []
89   ldflags = []
90   defines = []
92   # In general, Windows is totally different, but all the other builds share
93   # some common GCC configuration. This section sets up Windows and the common
94   # GCC flags, and then we handle the other non-Windows platforms specifically
95   # below.
96   if (is_win) {
97     # Windows compiler flags setup.
98     # -----------------------------
99     cflags += [
100       "/Gy",  # Enable function-level linking.
101       "/GS",  # Enable buffer security checking.
102       "/FS",  # Preserve previous PDB behavior.
103     ]
104   } else {
105     # Common GCC compiler flags setup.
106     # --------------------------------
107     cflags += [ "-fno-strict-aliasing" ]  # See http://crbug.com/32204
108     cflags_cc += [
109       "-fno-threadsafe-statics",
111       # Not exporting C++ inline functions can generally be applied anywhere
112       # so we do so here. Normal function visibility is controlled by
113       # //build/config/gcc:symbol_visibility_hidden.
114       "-fvisibility-inlines-hidden",
115     ]
117     # Stack protection.
118     if (is_mac) {
119       cflags += [ "-fstack-protector-all" ]
120     } else if (is_linux) {
121       cflags += [
122         "-fstack-protector",
123         "--param=ssp-buffer-size=4",
124       ]
125     }
127     # Linker warnings.
128     if (!(is_chromeos && current_cpu == "arm") && !is_mac) {
129       # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
130       ldflags += [ "-Wl,--fatal-warnings" ]
131     }
133     # Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer and
134     # MemorySanitizer
135     if (using_sanitizer) {
136       cflags += [
137         "-fno-omit-frame-pointer",
138         "-gline-tables-only",
139       ]
140     }
141     if (is_asan) {
142       cflags += [ "-fsanitize=address" ]
143       if (is_mac) {
144         cflags += [ "-mllvm -asan-globals=0" ]  # http://crbug.com/352073
145         # TODO(GYP): deal with mac_bundles.
146       }
147     }
148   }
150   if (is_clang && is_debug) {
151     # Allow comparing the address of references and 'this' against 0
152     # in debug builds. Technically, these can never be null in
153     # well-defined C/C++ and Clang can optimize such checks away in
154     # release builds, but they may be used in asserts in debug builds.
155     cflags_cc += [
156       "-Wno-undefined-bool-conversion",
157       "-Wno-tautological-undefined-compare",
158     ]
159   }
161   if (is_clang && !is_nacl) {
162     # This is here so that all files get recompiled after a clang roll and
163     # when turning clang on or off. (defines are passed via the command line,
164     # and build system rebuild things when their commandline changes). Nothing
165     # should ever read this define.
166     defines +=
167         [ "CR_CLANG_REVISION=" + exec_script("//tools/clang/scripts/update.py",
168                                              [ "--print-revision" ],
169                                              "trim string") ]
170   }
172   # Mac-specific compiler flags setup.
173   # ----------------------------------
174   if (is_mac || is_ios) {
175     # These flags are shared between the C compiler and linker.
176     common_mac_flags = []
178     # CPU architecture.
179     if (current_cpu == "x64") {
180       common_mac_flags += [
181         "-arch",
182         "x86_64",
183       ]
184     } else if (current_cpu == "x86") {
185       common_mac_flags += [
186         "-arch",
187         "i386",
188       ]
189     }
191     cflags += common_mac_flags
193     # Without this, the constructors and destructors of a C++ object inside
194     # an Objective C struct won't be called, which is very bad.
195     cflags_objcc = [ "-fobjc-call-cxx-cdtors" ]
197     cflags_c += [ "-std=c99" ]
199     ldflags += common_mac_flags
200   } else if (is_posix) {
201     # Non-Mac Posix compiler flags setup.
202     # -----------------------------------
203     if (enable_profiling && !is_debug) {
204       # The GYP build spams this define into every compilation unit, as we do
205       # here, but it only appears to be used in base and a couple other places.
206       # TODO(abarth): Should we move this define closer to where it's used?
207       defines += [ "ENABLE_PROFILING" ]
209       cflags += [
210         "-fno-omit-frame-pointer",
211         "-g",
212       ]
214       if (enable_full_stack_frames_for_profiling) {
215         cflags += [
216           "-fno-inline",
217           "-fno-optimize-sibling-calls",
218         ]
219       }
220     }
222     # CPU architecture. We may or may not be doing a cross compile now, so for
223     # simplicity we always explicitly set the architecture.
224     if (current_cpu == "x64") {
225       cflags += [
226         "-m64",
227         "-march=x86-64",
228       ]
229       ldflags += [ "-m64" ]
230     } else if (current_cpu == "x86") {
231       cflags += [ "-m32" ]
232       ldflags += [ "-m32" ]
233       if (is_clang) {
234         cflags += [
235           # Else building libyuv gives clang's register allocator issues,
236           # see llvm.org/PR15798 / crbug.com/233709
237           "-momit-leaf-frame-pointer",
239           # Align the stack on 16-byte boundaries, http://crbug.com/418554.
240           "-mstack-alignment=16",
241           "-mstackrealign",
242         ]
243       }
244     } else if (current_cpu == "arm") {
245       cflags += [
246         "-march=$arm_arch",
247         "-mfloat-abi=$arm_float_abi",
248       ]
249       if (arm_tune != "") {
250         cflags += [ "-mtune=$arm_tune" ]
251       }
252       if (arm_use_thumb) {
253         cflags += [ "-mthumb" ]
254         if (is_android && !is_clang) {  # Clang doesn't support this option.
255           cflags += [ "-mthumb-interwork" ]
256         }
257       }
258       if (!is_clang) {
259         # Clang doesn't support these flags.
260         cflags += [
261           # The tree-sra optimization (scalar replacement for
262           # aggregates enabling subsequent optimizations) leads to
263           # invalid code generation when using the Android NDK's
264           # compiler (r5-r7). This can be verified using
265           # webkit_unit_tests' WTF.Checked_int8_t test.
266           "-fno-tree-sra",
268           # The following option is disabled to improve binary
269           # size and performance in gcc 4.9.
270           "-fno-caller-saves",
271         ]
272       }
273     } else if (current_cpu == "mipsel") {
274       if (mips_arch_variant == "r6") {
275         cflags += [
276           "-mips32r6",
277           "-Wa,-mips32r6",
278         ]
279         if (is_android) {
280           ldflags += [
281             "-mips32r6",
282             "-Wl,-melf32ltsmip",
283           ]
284         }
285       } else if (mips_arch_variant == "r2") {
286         cflags += [
287           "-mips32r2",
288           "-Wa,-mips32r2",
289         ]
290         if (mips_float_abi == "hard" && mips_fpu_mode != "") {
291           cflags += [ "-m$mips_fpu_mode" ]
292         }
293       } else if (mips_arch_variant == "r1") {
294         cflags += [
295           "-mips32",
296           "-Wa,-mips32",
297         ]
298       }
300       if (mips_dsp_rev == 1) {
301         cflags += [ "-mdsp" ]
302       } else if (mips_dsp_rev == 2) {
303         cflags += [ "-mdspr2" ]
304       }
306       cflags += [ "-m${mips_float_abi}-float" ]
307     } else if (current_cpu == "mips64el") {
308       if (mips_arch_variant == "r6") {
309         cflags += [
310           "-mips64r6",
311           "-Wa,-mips64r6",
312         ]
313         ldflags += [ "-mips64r6" ]
314       } else if (mips_arch_variant == "r2") {
315         cflags += [
316           "-mips64r2",
317           "-Wa,-mips64r2",
318         ]
319         ldflags += [ "-mips64r2" ]
320       }
321     }
323     defines += [ "_FILE_OFFSET_BITS=64" ]
325     if (!is_android) {
326       defines += [
327         "_LARGEFILE_SOURCE",
328         "_LARGEFILE64_SOURCE",
329       ]
330     }
332     # Omit unwind support in official builds to save space. We can use breakpad
333     # for these builds.
334     if (is_chrome_branded && is_official_build) {
335       cflags += [
336         "-fno-unwind-tables",
337         "-fno-asynchronous-unwind-tables",
338       ]
339       defines += [ "NO_UNWIND_TABLES" ]
340     } else {
341       cflags += [ "-funwind-tables" ]
342     }
343   }
345   # Linux/Android common flags setup.
346   # ---------------------------------
347   if (is_linux || is_android) {
348     cflags += [
349       "-fPIC",
350       "-pipe",  # Use pipes for communicating between sub-processes. Faster.
351     ]
353     ldflags += [
354       "-fPIC",
355       "-Wl,-z,noexecstack",
356       "-Wl,-z,now",
357       "-Wl,-z,relro",
358     ]
359     if (!using_sanitizer) {
360       ldflags += [ "-Wl,-z,defs" ]
361     }
362   }
364   # Linux-specific compiler flags setup.
365   # ------------------------------------
366   if (is_linux) {
367     cflags += [ "-pthread" ]
368     ldflags += [ "-pthread" ]
369   }
370   if (use_gold) {
371     gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
372                             root_build_dir)
373     ldflags += [
374       "-B$gold_path",
376       # Newer gccs and clangs support -fuse-ld, use the flag to force gold
377       # selection.
378       # gcc -- http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Optimize-Options.html
379       "-fuse-ld=gold",
381       # TODO(brettw) common.gypi has this only for target toolset.
382       "-Wl,--icf=safe",
384       # Experimentation found that using four linking threads
385       # saved ~20% of link time.
386       # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
387       # Only apply this to the target linker, since the host
388       # linker might not be gold, but isn't used much anyway.
389       # TODO(raymes): Disable threading because gold is frequently
390       # crashing on the bots: crbug.com/161942.
391       #"-Wl,--threads",
392       #"-Wl,--thread-count=4",
393     ]
395     # TODO(thestig): Make this flag work with GN.
396     #if (!is_official_build && !is_chromeos && !(is_asan || is_lsan || is_tsan || is_msan)) {
397     #  ldflags += [
398     #    "-Wl,--detect-odr-violations",
399     #  ]
400     #}
401   }
403   if (linux_use_bundled_binutils) {
404     binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
405                                 root_build_dir)
406     cflags += [ "-B$binutils_path" ]
407   }
409   # Clang-specific compiler flags setup.
410   # ------------------------------------
411   if (is_clang) {
412     cflags += [ "-fcolor-diagnostics" ]
413   }
415   # C++11 compiler flags setup.
416   # ---------------------------
417   if (is_linux || is_android || is_nacl) {
418     # gnu++11 instead of c++11 is needed because some code uses typeof() (a
419     # GNU extension).
420     # TODO(thakis): Eventually switch this to c++11 instead,
421     # http://crbug.com/427584
422     cflags_cc += [ "-std=gnu++11" ]
423   } else if (!is_win) {
424     cflags_cc += [ "-std=c++11" ]
425   }
427   # Android-specific flags setup.
428   # -----------------------------
429   if (is_android) {
430     cflags += [
431       "-ffunction-sections",
432       "-funwind-tables",
433       "-fno-short-enums",
434     ]
435     if (!is_clang) {
436       # Clang doesn't support these flags.
437       cflags += [ "-finline-limit=64" ]
438     }
439     if (is_asan) {
440       # Android build relies on -Wl,--gc-sections removing unreachable code.
441       # ASan instrumentation for globals inhibits this and results in a library
442       # with unresolvable relocations.
443       # TODO(eugenis): find a way to reenable this.
444       cflags += [ "-mllvm -asan-globals=0" ]
445     }
447     defines += [ "ANDROID" ]
449     # The NDK has these things, but doesn't define the constants
450     # to say that it does. Define them here instead.
451     defines += [ "HAVE_SYS_UIO_H" ]
453     # Use gold for Android for most CPU architectures.
454     if (current_cpu == "x86" || current_cpu == "x64" || current_cpu == "arm") {
455       ldflags += [ "-fuse-ld=gold" ]
456       if (is_clang) {
457         # Let clang find the ld.gold in the NDK.
458         ldflags += [ "--gcc-toolchain=" +
459                      rebase_path(android_toolchain_root, root_build_dir) ]
460       }
461     }
463     ldflags += [
464       "-Wl,--no-undefined",
466       # Don't allow visible symbols from libgcc or libc++ to be
467       # re-exported.
468       "-Wl,--exclude-libs=libgcc.a",
469       "-Wl,--exclude-libs=libc++_static.a",
471       # Don't allow visible symbols from libraries that contain
472       # assembly code with symbols that aren't hidden properly.
473       # http://crbug.com/448386
474       "-Wl,--exclude-libs=libvpx_assembly_arm.a",
475     ]
476     if (current_cpu == "arm") {
477       ldflags += [
478         # Enable identical code folding to reduce size.
479         "-Wl,--icf=safe",
480       ]
481     }
483     if (is_clang) {
484       if (current_cpu == "arm") {
485         cflags += [ "-target arm-linux-androideabi" ]
486         ldflags += [ "-target arm-linux-androideabi" ]
487       } else if (current_cpu == "x86") {
488         cflags += [ "-target x86-linux-androideabi" ]
489         ldflags += [ "-target x86-linux-androideabi" ]
490       }
491     }
492   }
495 config("compiler_arm_fpu") {
496   if (current_cpu == "arm") {
497     cflags = [ "-mfpu=$arm_fpu" ]
498   }
501 # runtime_library -------------------------------------------------------------
503 # Sets the runtime library and associated options.
505 # How do you determine what should go in here vs. "compiler" above? Consider if
506 # a target might choose to use a different runtime library (ignore for a moment
507 # if this is possible or reasonable on your system). If such a target would want
508 # to change or remove your option, put it in the runtime_library config. If a
509 # target wants the option regardless, put it in the compiler config.
511 config("runtime_library") {
512   cflags = []
513   defines = []
514   ldflags = []
515   lib_dirs = []
516   libs = []
518   if (is_component_build) {
519     # Component mode: dynamic CRT.
520     defines += [ "COMPONENT_BUILD" ]
521     if (is_win) {
522       # Since the library is shared, it requires exceptions or will give errors
523       # about things not matching, so keep exceptions on.
524       if (is_debug) {
525         cflags += [ "/MDd" ]
526       } else {
527         cflags += [ "/MD" ]
528       }
529     }
530   } else {
531     # Static CRT.
532     if (is_win) {
533       if (is_debug) {
534         cflags += [ "/MTd" ]
535       } else {
536         cflags += [ "/MT" ]
537       }
538     }
539   }
541   if (is_win) {
542     defines += [
543       "__STD_C",
544       "_CRT_RAND_S",
545       "_CRT_SECURE_NO_DEPRECATE",
546       "_HAS_EXCEPTIONS=0",
547       "_SCL_SECURE_NO_DEPRECATE",
548     ]
549   }
551   # Android standard library setup.
552   if (is_android) {
553     if (is_clang) {
554       # Work around incompatibilities between bionic and clang headers.
555       defines += [
556         "__compiler_offsetof=__builtin_offsetof",
557         "nan=__builtin_nan",
558       ]
559     }
561     defines += [ "__GNU_SOURCE=1" ]  # Necessary for clone().
563     # TODO(jdduke) Re-enable on mips after resolving linking
564     # issues with libc++ (crbug.com/456380).
565     if (current_cpu != "mipsel" && current_cpu != "mips64el") {
566       ldflags += [ "-Wl,--warn-shared-textrel" ]
567     }
568     ldflags += [ "-nostdlib" ]
570     # NOTE: The libc++ header include paths below are specified in cflags
571     # rather than include_dirs because they need to come after include_dirs.
572     # Think of them like system headers, but don't use '-isystem' because the
573     # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
574     # strange errors. The include ordering here is important; change with
575     # caution.
576     android_libcpp_root = "$android_ndk_root/sources/cxx-stl/llvm-libc++"
578     cflags += [
579       "-isystem" +
580           rebase_path("$android_libcpp_root/libcxx/include", root_build_dir),
581       "-isystem" + rebase_path(
582               "$android_ndk_root/sources/cxx-stl/llvm-libc++abi/libcxxabi/include",
583               root_build_dir),
584       "-isystem" +
585           rebase_path("$android_ndk_root/sources/android/support/include",
586                       root_build_dir),
587     ]
589     lib_dirs += [ "$android_libcpp_root/libs/$android_app_abi" ]
591     if (component_mode == "shared_library") {
592       android_libcpp_library = "c++_shared"
593     } else {
594       android_libcpp_library = "c++_static"
595     }
597     libs += [
598       "$android_libcpp_library",
600       # Manually link the libgcc.a that the cross compiler uses. This is
601       # absolute because the linker will look inside the sysroot if it's not.
602       rebase_path(android_libgcc_file),
603       "c",
604       "dl",
605       "m",
606     ]
608     # Clang with libc++ does not require an explicit atomic library reference.
609     if (!is_clang) {
610       libs += [ "atomic" ]
611     }
612   }
615 # chromium_code ---------------------------------------------------------------
617 # Toggles between higher and lower warnings for code that is (or isn't)
618 # part of Chromium.
620 config("chromium_code") {
621   if (is_win) {
622     cflags = [ "/W4" ]  # Warning level 4.
623   } else {
624     cflags = [
625       "-Wall",
627       # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
628       # so we specify it explicitly.
629       # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
630       # http://code.google.com/p/chromium/issues/detail?id=90453
631       "-Wsign-compare",
632     ]
634     # In Chromium code, we define __STDC_foo_MACROS in order to get the
635     # C99 macros on Mac and Linux.
636     defines = [
637       "__STDC_CONSTANT_MACROS",
638       "__STDC_FORMAT_MACROS",
639     ]
641     if (using_sanitizer) {
642       # _FORTIFY_SOURCE isn't really supported by Clang now, see
643       # http://llvm.org/bugs/show_bug.cgi?id=16821.
644       # It seems to work fine with Ubuntu 12 headers though, so use it in
645       # official builds.
646       #
647       # Non-chromium code is not guaranteed to compile cleanly with
648       # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
649       # disabled, so only do that for Release build.
650       defines += [ "_FORTIFY_SOURCE=2" ]
651     }
652   }
654 config("no_chromium_code") {
655   cflags = []
656   cflags_cc = []
657   defines = []
659   if (is_win) {
660     cflags += [
661       "/W3",  # Warning level 3.
662       "/wd4800",  # Disable warning when forcing value to bool.
663       "/wd4267",  # TODO(jschuh): size_t to int.
664       "/wd4996",  # Deprecated function warning.
665     ]
666     defines += [
667       "_CRT_NONSTDC_NO_WARNINGS",
668       "_CRT_NONSTDC_NO_DEPRECATE",
669     ]
670   }
672   if (is_linux) {
673     # Don't warn about ignoring the return value from e.g. close(). This is
674     # off by default in some gccs but on by default in others. BSD systems do
675     # not support this option, since they are usually using gcc 4.2.1, which
676     # does not have this flag yet.
677     cflags += [ "-Wno-unused-result" ]
678   }
680   if (is_linux || is_android) {
681     cflags += [
682       # Don't warn about printf format problems. This is off by default in gcc
683       # but on in Ubuntu's gcc(!).
684       "-Wno-format",
685     ]
686     cflags_cc += [
687       # Don't warn about hash_map in third-party code.
688       "-Wno-deprecated",
689     ]
690   }
693 # rtti ------------------------------------------------------------------------
695 # Allows turning Run-Time Type Identification on or off.
697 config("rtti") {
698   if (is_win) {
699     cflags_cc = [ "/GR" ]
700   }
702 config("no_rtti") {
703   if (is_win) {
704     cflags_cc = [ "/GR-" ]
705   } else {
706     cflags_cc = [ "-fno-rtti" ]
707   }
710 # Warnings ---------------------------------------------------------------------
712 # This is where we disable various warnings that we've decided aren't
713 # worthwhile, and enable special warnings.
715 config("default_warnings") {
716   if (is_win) {
717     cflags = [
718       "/WX",  # Treat warnings as errors.
720       # Warnings permanently disabled:
722       # TODO(GYP) The GYP build doesn't have this globally enabled but disabled
723       # for a bunch of individual targets. Re-enable this globally when those
724       # targets are fixed.
725       "/wd4018",  # Comparing signed and unsigned values.
727       # C4127: conditional expression is constant
728       # This warning can in theory catch dead code and other problems, but
729       # triggers in far too many desirable cases where the conditional
730       # expression is either set by macros or corresponds some legitimate
731       # compile-time constant expression (due to constant template args,
732       # conditionals comparing the sizes of different types, etc.).  Some of
733       # these can be worked around, but it's not worth it.
734       "/wd4127",
736       # C4251: 'identifier' : class 'type' needs to have dll-interface to be
737       #        used by clients of class 'type2'
738       # This is necessary for the shared library build.
739       "/wd4251",
741       # C4351: new behavior: elements of array 'array' will be default
742       #        initialized
743       # This is a silly "warning" that basically just alerts you that the
744       # compiler is going to actually follow the language spec like it's
745       # supposed to, instead of not following it like old buggy versions did.
746       # There's absolutely no reason to turn this on.
747       "/wd4351",
749       # C4355: 'this': used in base member initializer list
750       # It's commonly useful to pass |this| to objects in a class' initializer
751       # list.  While this warning can catch real bugs, most of the time the
752       # constructors in question don't attempt to call methods on the passed-in
753       # pointer (until later), and annotating every legit usage of this is
754       # simply more hassle than the warning is worth.
755       "/wd4355",
757       # C4503: 'identifier': decorated name length exceeded, name was
758       #        truncated
759       # This only means that some long error messages might have truncated
760       # identifiers in the presence of lots of templates.  It has no effect on
761       # program correctness and there's no real reason to waste time trying to
762       # prevent it.
763       "/wd4503",
765       # C4611: interaction between 'function' and C++ object destruction is
766       #        non-portable
767       # This warning is unavoidable when using e.g. setjmp/longjmp.  MSDN
768       # suggests using exceptions instead of setjmp/longjmp for C++, but
769       # Chromium code compiles without exception support.  We therefore have to
770       # use setjmp/longjmp for e.g. JPEG decode error handling, which means we
771       # have to turn off this warning (and be careful about how object
772       # destruction happens in such cases).
773       "/wd4611",
775       # Warnings to evaluate and possibly fix/reenable later:
777       "/wd4100",  # Unreferenced formal function parameter.
778       "/wd4121",  # Alignment of a member was sensitive to packing.
779       "/wd4244",  # Conversion: possible loss of data.
780       "/wd4481",  # Nonstandard extension: override specifier.
781       "/wd4505",  # Unreferenced local function has been removed.
782       "/wd4510",  # Default constructor could not be generated.
783       "/wd4512",  # Assignment operator could not be generated.
784       "/wd4610",  # Class can never be instantiated, constructor required.
785       "/wd4996",  # Deprecated function warning.
786     ]
788     # VS xtree header file needs to be patched or 4702 (unreachable code
789     # warning) is reported if _HAS_EXCEPTIONS=0. Disable the warning if xtree is
790     # not patched.
791     if (!msvs_xtree_patched &&
792         exec_script("../../win_is_xtree_patched.py", [], "value") == 0) {
793       cflags += [ "/wd4702" ]  # Unreachable code.
794     }
795   } else {
796     # Common GCC warning setup.
797     cflags = [
798       # Enables.
799       "-Wendif-labels",  # Weird old-style text after an #endif.
800       "-Werror",  # Warnings as errors.
802       # Disables.
803       "-Wno-missing-field-initializers",  # "struct foo f = {0};"
804       "-Wno-unused-parameter",  # Unused function parameters.
805     ]
806     cflags_cc = []
808     if (is_mac) {
809       cflags += [ "-Wnewline-eof" ]
810     }
812     if (is_clang) {
813       cflags += [
814         # This warns on using ints as initializers for floats in
815         # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
816         # which happens in several places in chrome code. Not sure if
817         # this is worth fixing.
818         "-Wno-c++11-narrowing",
820         # Don't die on dtoa code that uses a char as an array index.
821         # This is required solely for base/third_party/dmg_fp/dtoa.cc.
822         # TODO(brettw) move this to that project then!
823         "-Wno-char-subscripts",
825         # Warns on switches on enums that cover all enum values but
826         # also contain a default: branch. Chrome is full of that.
827         "-Wno-covered-switch-default",
829         # Clang considers the `register` keyword as deprecated, but e.g.
830         # code generated by flex (used in angle) contains that keyword.
831         # http://crbug.com/255186
832         "-Wno-deprecated-register",
834         # TODO(thakis): This used to be implied by -Wno-unused-function,
835         # which we no longer use. Check if it makes sense to remove
836         # this as well. http://crbug.com/316352
837         "-Wno-unneeded-internal-declaration",
839         # TODO(thakis): Remove, http://crbug.com/263960
840         "-Wno-reserved-user-defined-literal",
841       ]
843       # NaCl's Clang compiler and Chrome's hermetic Clang compiler will almost
844       # always have different versions. Certain flags may not be recognized by
845       # one version or the other.
846       if (!is_nacl) {
847         # Flags NaCl does not recognize.
848         cflags += [
849           # TODO(hans): Get this cleaned up.
850           "-Wno-inconsistent-missing-override",
851         ]
852       }
853     }
854     if (gcc_version >= 48) {
855       cflags_cc += [
856         # See comment for -Wno-c++11-narrowing.
857         "-Wno-narrowing",
859         # TODO(thakis): Remove, http://crbug.com/263960
860         "-Wno-literal-suffix",
861       ]
862     }
864     # Suppress warnings about ABI changes on ARM (Clang doesn't give this
865     # warning).
866     if (current_cpu == "arm" && !is_clang) {
867       cflags += [ "-Wno-psabi" ]
868     }
870     if (is_android) {
871       # Disable any additional warnings enabled by the Android build system but
872       # which chromium does not build cleanly with (when treating warning as
873       # errors).
874       cflags += [
875         "-Wno-extra",
876         "-Wno-ignored-qualifiers",
877         "-Wno-type-limits",
878       ]
879       cflags_cc += [
880         # Disabling c++0x-compat should be handled in WebKit, but
881         # this currently doesn't work because gcc_version is not set
882         # correctly when building with the Android build system.
883         # TODO(torne): Fix this in WebKit.
884         "-Wno-error=c++0x-compat",
886         # Other things unrelated to -Wextra:
887         "-Wno-non-virtual-dtor",
888         "-Wno-sign-promo",
889       ]
890     }
892     if (gcc_version >= 48) {
893       # Don't warn about the "typedef 'foo' locally defined but not used"
894       # for gcc 4.8.
895       # TODO: remove this flag once all builds work. See crbug.com/227506
896       cflags += [ "-Wno-unused-local-typedefs" ]
897     }
898   }
901 # This will generate warnings when using Clang if code generates exit-time
902 # destructors, which will slow down closing the program.
903 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
904 config("wexit_time_destructors") {
905   if (is_clang) {
906     cflags = [ "-Wexit-time-destructors" ]
907   }
910 # On Windows compiling on x64, VC will issue a warning when converting
911 # size_t to int because it will truncate the value. Our code should not have
912 # these warnings and one should use a static_cast or a checked_cast for the
913 # conversion depending on the case. However, a lot of code still needs to be
914 # fixed. Apply this config to such targets to disable the warning.
916 # Note that this can be applied regardless of platform and architecture to
917 # clean up the call sites. This will only apply the flag when necessary.
919 # TODO(jschuh): crbug.com/167187 fix this and delete this config.
920 config("no_size_t_to_int_warning") {
921   if (is_win && current_cpu == "x64") {
922     cflags = [ "/wd4267" ]
923   }
926 # Optimization -----------------------------------------------------------------
928 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
929 # which it will assign to the config it implicitly applies to every target. If
930 # you want to override the optimization level for your target, remove this
931 # config (which will expand differently for debug or release builds), and then
932 # add back the one you want to override it with:
934 #   configs -= default_optimization_config
935 #   configs += [ "//build/config/compiler/optimize_max" ]
937 # Shared settings for both "optimize" and "optimize_max" configs.
938 if (is_win) {
939   common_optimize_on_cflags = [
940     "/O2",
941     "/Ob2",  # both explicit and auto inlining.
942     "/Oy-",  # disable omitting frame pointers, must be after /o2.
943     "/Os",  # favor size over speed.
944   ]
945   if (!is_asan) {
946     common_optimize_on_cflags += [
947       # Put data in separate COMDATs. This allows the linker
948       # to put bit-identical constants at the same address even if
949       # they're unrelated constants, which saves binary size.
950       # This optimization can't be used when ASan is enabled because
951       # it is not compatible with the ASan ODR checker.
952       "/Gw",
953     ]
954   }
955   common_optimize_on_ldflags = [ "/OPT:REF" ]
956 } else {
957   common_optimize_on_cflags = [
958     # Don't emit the GCC version ident directives, they just end up in the
959     # .comment section taking up binary size.
960     "-fno-ident",
962     # Put data and code in their own sections, so that unused symbols
963     # can be removed at link time with --gc-sections.
964     "-fdata-sections",
965     "-ffunction-sections",
966   ]
967   common_optimize_on_ldflags = []
969   if (is_android) {
970     if (!using_sanitizer) {
971       common_optimize_on_cflags += [ "-fomit-frame-pointer" ]
972     }
974     # TODO(jdduke) Re-enable on mips after resolving linking
975     # issues with libc++ (crbug.com/456380).
976     if (current_cpu != "mipsel" && current_cpu != "mips64el") {
977       common_optimize_on_ldflags += [
978         # Warn in case of text relocations.
979         "-Wl,--warn-shared-textrel",
980       ]
981     }
982   }
984   if (is_mac) {
985     if (symbol_level == 2) {
986       # Mac dead code stripping requires symbols.
987       common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
988     }
989   } else {
990     # Non-Mac Posix linker flags.
991     common_optimize_on_ldflags += [
992       # Specifically tell the linker to perform optimizations.
993       # See http://lwn.net/Articles/192624/ .
994       "-Wl,-O1",
995       "-Wl,--gc-sections",
996     ]
998     if (!using_sanitizer) {
999       # Functions interposed by the sanitizers can make ld think
1000       # that some libraries aren't needed when they actually are,
1001       # http://crbug.com/234010. As workaround, disable --as-needed.
1002       common_optimize_on_ldflags += [ "-Wl,--as-needed" ]
1003     }
1004   }
1007 # Default "optimization on" config. On Windows, this favors size over speed.
1008 config("optimize") {
1009   cflags = common_optimize_on_cflags
1010   ldflags = common_optimize_on_ldflags
1011   if (is_win) {
1012     cflags += [ "/Os" ]  # favor size over speed.
1013   } else if (is_android || is_ios) {
1014     cflags += [ "-Os" ]  # Favor size over speed.
1015   } else {
1016     cflags += [ "-O2" ]
1017   }
1020 # Turn off optimizations.
1021 config("no_optimize") {
1022   if (is_win) {
1023     cflags = [
1024       "/Od",  # Disable optimization.
1025       "/Ob0",  # Disable all inlining (on by default).
1026       "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
1027     ]
1028   } else if (is_android && !android_full_debug) {
1029     # On Android we kind of optimize some things that don't affect debugging
1030     # much even when optimization is disabled to get the binary size down.
1031     cflags = [
1032       "-Os",
1033       "-fdata-sections",
1034       "-ffunction-sections",
1035     ]
1036     if (!using_sanitizer) {
1037       cflags += [ "-fomit-frame-pointer" ]
1038     }
1039     ldflags = common_optimize_on_ldflags
1040   } else {
1041     cflags = [ "-O0" ]
1042   }
1045 # Turns up the optimization level. On Windows, this implies whole program
1046 # optimization and link-time code generation which is very expensive and should
1047 # be used sparingly.
1048 config("optimize_max") {
1049   cflags = common_optimize_on_cflags
1050   ldflags = common_optimize_on_ldflags
1051   if (is_win) {
1052     cflags -= [ "/Os" ]
1053     cflags += [ "/Ot" ]  # Favor speed over size.
1054     if (is_official_build) {
1055       # TODO(GYP): TODO(dpranke): Should these only be on in an official
1056       # build, or on all the time? For now we'll require official build so
1057       # that the compile is clean.
1058       cflags += [
1059         "/GL",  # Whole program optimization.
1061         # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
1062         # Probably anything that this would catch that wouldn't be caught in a
1063         # normal build isn't going to actually be a bug, so the incremental
1064         # value of C4702 for PGO builds is likely very small.
1065         "/wd4702",
1066       ]
1067       ldflags += [ "/LTCG" ]
1068     }
1069   } else {
1070     cflags += [ "-O2" ]
1071   }
1074 # Symbols ----------------------------------------------------------------------
1076 config("symbols") {
1077   if (is_win) {
1078     import("//build/toolchain/goma.gni")
1079     if (use_goma) {
1080       cflags = [ "/Z7" ]  # No PDB file
1081     } else {
1082       cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
1083     }
1084     ldflags = [ "/DEBUG" ]
1085   } else {
1086     cflags = [ "-g2" ]
1087     if (use_debug_fission) {
1088       cflags += [ "-gsplit-dwarf" ]
1089     }
1090   }
1093 config("minimal_symbols") {
1094   if (is_win) {
1095     # Linker symbols for backtraces only.
1096     ldflags = [ "/DEBUG" ]
1097   } else {
1098     cflags = [ "-g1" ]
1099     if (use_debug_fission) {
1100       cflags += [ "-gsplit-dwarf" ]
1101     }
1102   }
1105 config("no_symbols") {
1106   if (!is_win) {
1107     cflags = [ "-g0" ]
1108   }