Sync Windows build flags between GYP and GN
[chromium-blink-merge.git] / build / config / compiler / BUILD.gn
blob1ef75b13530ea984e4a1b5311c7296da8782e71d
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"
34   # Compile in such a way as to enable profiling of the generated code. For
35   # example, don't omit the frame pointer and leave in symbols.
36   enable_profiling = false
38   # Compile in such a way as to make it possible for the profiler to unwind full
39   # stack frames. Setting this flag has a large effect on the performance of the
40   # generated code than just setting profiling, but gives the profiler more
41   # information to analyze.
42   # Requires profiling to be set to true.
43   enable_full_stack_frames_for_profiling = false
45   # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
46   # address space, and it doesn't support cross-compiling).
47   use_gold = is_linux && current_cpu == "x64"
49   # use_debug_fission: whether to use split DWARF debug info
50   # files. This can reduce link time significantly, but is incompatible
51   # with some utilities such as icecc and ccache. Requires gold and
52   # gcc >= 4.8 or clang.
53   # http://gcc.gnu.org/wiki/DebugFission
54   use_debug_fission = is_debug && !is_win && use_gold &&
55                       linux_use_bundled_binutils && !use_ccache
57   if (is_win) {
58     # Whether the VS xtree header has been patched to disable warning 4702. If
59     # it has, then we don't need to disable 4702 (unreachable code warning).
60     # The patch is preapplied to the internal toolchain and hence all bots.
61     msvs_xtree_patched = false
62   }
65 # default_include_dirs ---------------------------------------------------------
67 # This is a separate config so that third_party code (which would not use the
68 # source root and might have conflicting versions of some headers) can remove
69 # this and specify their own include paths.
70 config("default_include_dirs") {
71   include_dirs = [
72     "//",
73     root_gen_dir,
74   ]
77 # TODO(GYP): is_ubsan, is_ubsan_vptr
78 if (!is_win) {
79   using_sanitizer = is_asan || is_lsan || is_tsan || is_msan
82 # compiler ---------------------------------------------------------------------
84 # Base compiler configuration.
86 # See also "runtime_library" below for related stuff and a discussion about
87 # where stuff should go. Put warning related stuff in the "warnings" config.
89 config("compiler") {
90   cflags = []
91   cflags_c = []
92   cflags_cc = []
93   ldflags = []
94   defines = []
96   # In general, Windows is totally different, but all the other builds share
97   # some common GCC configuration. This section sets up Windows and the common
98   # GCC flags, and then we handle the other non-Windows platforms specifically
99   # below.
100   if (is_win) {
101     # Windows compiler flags setup.
102     # -----------------------------
103     cflags += [
104       "/Gy",  # Enable function-level linking.
105       "/GS",  # Enable buffer security checking.
106       "/FS",  # Preserve previous PDB behavior.
107       "/bigobj",  # Always use big .obj format.
108     ]
110     # Building with Clang on Windows is a work in progress and very
111     # experimental. See crbug.com/82385.
112     # Keep this in sync with the similar block in build/common.gypi
113     if (is_clang) {
114       cflags += [
115         # Many files use intrinsics without including this header.
116         # TODO(hans): Fix those files, or move this to sub-GYPs.
117         "/FIIntrin.h",
118       ]
120       if (visual_studio_version == "2013") {
121         cflags += [ "-fmsc-version=1800" ]
122       } else if (visual_studio_version == "2015") {
123         cflags += [ "-fmsc-version=1900" ]
124       }
126       if (current_cpu == "x86") {
127         cflags += [
128           "/fallback",
129           "-m32",
130         ]
131       } else {
132         cflags += [ "-m64" ]
133       }
134     }
135   } else {
136     # Common GCC compiler flags setup.
137     # --------------------------------
138     cflags += [ "-fno-strict-aliasing" ]  # See http://crbug.com/32204
139     cflags_cc += [
140       "-fno-threadsafe-statics",
142       # Not exporting C++ inline functions can generally be applied anywhere
143       # so we do so here. Normal function visibility is controlled by
144       # //build/config/gcc:symbol_visibility_hidden.
145       "-fvisibility-inlines-hidden",
146     ]
148     # Stack protection.
149     if (is_mac) {
150       cflags += [ "-fstack-protector-all" ]
151     } else if (is_linux) {
152       cflags += [
153         "-fstack-protector",
154         "--param=ssp-buffer-size=4",
155       ]
156     }
158     # Linker warnings.
159     if (!(is_chromeos && current_cpu == "arm") && !is_mac && !is_ios) {
160       # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
161       ldflags += [ "-Wl,--fatal-warnings" ]
162     }
164     # Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer and
165     # MemorySanitizer
166     if (using_sanitizer) {
167       cflags += [
168         "-fno-omit-frame-pointer",
169         "-gline-tables-only",
170       ]
171     }
172     if (is_asan) {
173       asan_blacklist_path =
174           rebase_path("//tools/memory/asan/blacklist.txt", root_build_dir)
175       cflags += [
176         "-fsanitize=address",
177         "-fsanitize-blacklist=$asan_blacklist_path",
178       ]
179       if (is_mac) {
180         cflags += [ "-mllvm -asan-globals=0" ]  # http://crbug.com/352073
181         # TODO(GYP): deal with mac_bundles.
182       }
183     }
184     if (is_lsan) {
185       cflags += [ "-fsanitize=leak" ]
186     }
187     if (is_tsan) {
188       tsan_blacklist_path =
189           rebase_path("//tools/memory/tsan_v2/ignores.txt", root_build_dir)
190       cflags += [
191         "-fsanitize=thread",
192         "-fsanitize-blacklist=$tsan_blacklist_path",
193       ]
194     }
195     if (is_msan) {
196       msan_blacklist_path =
197           rebase_path("//tools/msan/blacklist.txt", root_build_dir)
198       cflags += [
199         "-fsanitize=memory",
200         "-fsanitize-memory-track-origins=$msan_track_origins",
201         "-fsanitize-blacklist=$msan_blacklist_path",
202       ]
203     }
205     if (use_custom_libcxx) {
206       cflags_cc += [ "-nostdinc++" ]
207       include_dirs = [
208         "//buildtools/third_party/libc++/trunk/include",
209         "//buildtools/third_party/libc++abi/trunk/include",
210       ]
211     }
212   }
214   if (is_clang && is_debug) {
215     # Allow comparing the address of references and 'this' against 0
216     # in debug builds. Technically, these can never be null in
217     # well-defined C/C++ and Clang can optimize such checks away in
218     # release builds, but they may be used in asserts in debug builds.
219     cflags_cc += [
220       "-Wno-undefined-bool-conversion",
221       "-Wno-tautological-undefined-compare",
222     ]
223   }
225   if (is_clang && !is_nacl) {
226     # This is here so that all files get recompiled after a clang roll and
227     # when turning clang on or off. (defines are passed via the command line,
228     # and build system rebuild things when their commandline changes). Nothing
229     # should ever read this define.
230     defines +=
231         [ "CR_CLANG_REVISION=" + exec_script("//tools/clang/scripts/update.py",
232                                              [ "--print-revision" ],
233                                              "trim string") ]
234   }
236   # Mac-specific compiler flags setup.
237   # ----------------------------------
238   if (is_mac || is_ios) {
239     # These flags are shared between the C compiler and linker.
240     common_mac_flags = []
242     # CPU architecture.
243     if (current_cpu == "x64") {
244       common_mac_flags += [
245         "-arch",
246         "x86_64",
247       ]
248     } else if (current_cpu == "x86") {
249       common_mac_flags += [
250         "-arch",
251         "i386",
252       ]
253     } else if (current_cpu == "arm") {
254       common_mac_flags += [
255         "-arch",
256         "armv7",
257       ]
258     }
260     cflags += common_mac_flags
262     # Without this, the constructors and destructors of a C++ object inside
263     # an Objective C struct won't be called, which is very bad.
264     cflags_objcc = [ "-fobjc-call-cxx-cdtors" ]
266     cflags_c += [ "-std=c99" ]
268     ldflags += common_mac_flags
269   } else if (is_posix) {
270     # Non-Mac Posix compiler flags setup.
271     # -----------------------------------
272     if (enable_profiling && !is_debug) {
273       # The GYP build spams this define into every compilation unit, as we do
274       # here, but it only appears to be used in base and a couple other places.
275       # TODO(abarth): Should we move this define closer to where it's used?
276       defines += [ "ENABLE_PROFILING" ]
278       cflags += [
279         "-fno-omit-frame-pointer",
280         "-g",
281       ]
283       if (enable_full_stack_frames_for_profiling) {
284         cflags += [
285           "-fno-inline",
286           "-fno-optimize-sibling-calls",
287         ]
288       }
289     }
291     # CPU architecture. We may or may not be doing a cross compile now, so for
292     # simplicity we always explicitly set the architecture.
293     if (current_cpu == "x64") {
294       cflags += [
295         "-m64",
296         "-march=x86-64",
297       ]
298       ldflags += [ "-m64" ]
299     } else if (current_cpu == "x86") {
300       cflags += [ "-m32" ]
301       ldflags += [ "-m32" ]
302       if (is_clang) {
303         cflags += [
304           # Else building libyuv gives clang's register allocator issues,
305           # see llvm.org/PR15798 / crbug.com/233709
306           "-momit-leaf-frame-pointer",
308           # Align the stack on 16-byte boundaries, http://crbug.com/418554.
309           "-mstack-alignment=16",
310           "-mstackrealign",
311         ]
312       }
313     } else if (current_cpu == "arm") {
314       cflags += [
315         "-march=$arm_arch",
316         "-mfloat-abi=$arm_float_abi",
317       ]
318       if (arm_tune != "") {
319         cflags += [ "-mtune=$arm_tune" ]
320       }
321       if (arm_use_thumb) {
322         cflags += [ "-mthumb" ]
323         if (is_android && !is_clang) {  # Clang doesn't support this option.
324           cflags += [ "-mthumb-interwork" ]
325         }
326       }
327       if (!is_clang) {
328         # Clang doesn't support these flags.
329         cflags += [
330           # The tree-sra optimization (scalar replacement for
331           # aggregates enabling subsequent optimizations) leads to
332           # invalid code generation when using the Android NDK's
333           # compiler (r5-r7). This can be verified using
334           # webkit_unit_tests' WTF.Checked_int8_t test.
335           "-fno-tree-sra",
337           # The following option is disabled to improve binary
338           # size and performance in gcc 4.9.
339           "-fno-caller-saves",
340         ]
341       }
342     } else if (current_cpu == "mipsel") {
343       if (mips_arch_variant == "r6") {
344         cflags += [
345           "-mips32r6",
346           "-Wa,-mips32r6",
347         ]
348         if (is_android) {
349           ldflags += [
350             "-mips32r6",
351             "-Wl,-melf32ltsmip",
352           ]
353         }
354       } else if (mips_arch_variant == "r2") {
355         cflags += [
356           "-mips32r2",
357           "-Wa,-mips32r2",
358         ]
359         if (mips_float_abi == "hard" && mips_fpu_mode != "") {
360           cflags += [ "-m$mips_fpu_mode" ]
361         }
362       } else if (mips_arch_variant == "r1") {
363         cflags += [
364           "-mips32",
365           "-Wa,-mips32",
366         ]
367       }
369       if (mips_dsp_rev == 1) {
370         cflags += [ "-mdsp" ]
371       } else if (mips_dsp_rev == 2) {
372         cflags += [ "-mdspr2" ]
373       }
375       cflags += [ "-m${mips_float_abi}-float" ]
376     } else if (current_cpu == "mips64el") {
377       if (mips_arch_variant == "r6") {
378         cflags += [
379           "-mips64r6",
380           "-Wa,-mips64r6",
381         ]
382         ldflags += [ "-mips64r6" ]
383       } else if (mips_arch_variant == "r2") {
384         cflags += [
385           "-mips64r2",
386           "-Wa,-mips64r2",
387         ]
388         ldflags += [ "-mips64r2" ]
389       }
390     }
392     defines += [ "_FILE_OFFSET_BITS=64" ]
394     if (!is_android) {
395       defines += [
396         "_LARGEFILE_SOURCE",
397         "_LARGEFILE64_SOURCE",
398       ]
399     }
401     # Omit unwind support in official builds to save space. We can use breakpad
402     # for these builds.
403     if (is_chrome_branded && is_official_build) {
404       cflags += [
405         "-fno-unwind-tables",
406         "-fno-asynchronous-unwind-tables",
407       ]
408       defines += [ "NO_UNWIND_TABLES" ]
409     } else {
410       cflags += [ "-funwind-tables" ]
411     }
413     if (is_clang && !is_nacl && !is_debug) {
414       # Non-unique section names appears to make linker dead stripping
415       # less effective. See http://crbug.com/483026#c20
416       # TODO(hans): Remove this if resolved upstream.
417       cflags += [ "-funique-section-names" ]
418     }
419   }
421   # Linux/Android common flags setup.
422   # ---------------------------------
423   if (is_linux || is_android) {
424     cflags += [
425       "-fPIC",
426       "-pipe",  # Use pipes for communicating between sub-processes. Faster.
427     ]
429     ldflags += [
430       "-fPIC",
431       "-Wl,-z,noexecstack",
432       "-Wl,-z,now",
433       "-Wl,-z,relro",
434     ]
435     if (!using_sanitizer) {
436       ldflags += [ "-Wl,-z,defs" ]
437     }
438   }
440   # Linux-specific compiler flags setup.
441   # ------------------------------------
442   if (is_linux) {
443     cflags += [ "-pthread" ]
444     ldflags += [ "-pthread" ]
445   }
446   if (use_gold) {
447     gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
448                             root_build_dir)
449     ldflags += [
450       "-B$gold_path",
452       # Newer gccs and clangs support -fuse-ld, use the flag to force gold
453       # selection.
454       # gcc -- http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Optimize-Options.html
455       "-fuse-ld=gold",
457       # Experimentation found that using four linking threads
458       # saved ~20% of link time.
459       # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
460       # Only apply this to the target linker, since the host
461       # linker might not be gold, but isn't used much anyway.
462       # TODO(raymes): Disable threading because gold is frequently
463       # crashing on the bots: crbug.com/161942.
464       #"-Wl,--threads",
465       #"-Wl,--thread-count=4",
466     ]
468     if (!is_asan && !is_msan && !is_lsan && !is_tsan) {
469       # TODO(brettw) common.gypi has this only for target toolset.
470       if (current_cpu == "x64") {
471         # --icf=safe disables much more folding on x86_64 than elsewhere, see
472         # http://crbug.com/492177.  Turning it on saves over 12 MB of binary
473         # size, but it seems to regress cold startup time by over a second
474         # (see http://crbug.com/492809).
475         # TODO(thakis): Check if disabling ICF would inmprove android cold start
476         # times by several seconds too.
477         ldflags += [ "-Wl,--icf=safe" ]
478       } else {
479         ldflags += [ "-Wl,--icf=all" ]
480       }
481     }
483     # TODO(thestig): Make this flag work with GN.
484     #if (!is_official_build && !is_chromeos && !(is_asan || is_lsan || is_tsan || is_msan)) {
485     #  ldflags += [
486     #    "-Wl,--detect-odr-violations",
487     #  ]
488     #}
489   }
491   if (linux_use_bundled_binutils) {
492     binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
493                                 root_build_dir)
494     cflags += [ "-B$binutils_path" ]
495   }
497   # Clang-specific compiler flags setup.
498   # ------------------------------------
499   if (is_clang) {
500     cflags += [ "-fcolor-diagnostics" ]
501   }
503   # C++11 compiler flags setup.
504   # ---------------------------
505   if (is_linux || is_android || is_nacl) {
506     # gnu++11 instead of c++11 is needed because some code uses typeof() (a
507     # GNU extension).
508     # TODO(thakis): Eventually switch this to c++11 instead,
509     # http://crbug.com/427584
510     cflags_cc += [ "-std=gnu++11" ]
511   } else if (!is_win) {
512     cflags_cc += [ "-std=c++11" ]
513   }
515   # Android-specific flags setup.
516   # -----------------------------
517   if (is_android) {
518     cflags += [
519       "-ffunction-sections",
520       "-funwind-tables",
521       "-fno-short-enums",
522     ]
523     if (!is_clang) {
524       # Clang doesn't support these flags.
525       cflags += [ "-finline-limit=64" ]
526     }
527     if (is_asan) {
528       # Android build relies on -Wl,--gc-sections removing unreachable code.
529       # ASan instrumentation for globals inhibits this and results in a library
530       # with unresolvable relocations.
531       # TODO(eugenis): find a way to reenable this.
532       cflags += [ "-mllvm -asan-globals=0" ]
533     }
535     defines += [ "ANDROID" ]
537     # The NDK has these things, but doesn't define the constants
538     # to say that it does. Define them here instead.
539     defines += [ "HAVE_SYS_UIO_H" ]
541     # Use gold for Android for most CPU architectures.
542     if (current_cpu == "x86" || current_cpu == "x64" || current_cpu == "arm") {
543       ldflags += [ "-fuse-ld=gold" ]
544       if (is_clang) {
545         # Let clang find the ld.gold in the NDK.
546         ldflags += [ "--gcc-toolchain=" +
547                      rebase_path(android_toolchain_root, root_build_dir) ]
548       }
549     }
551     ldflags += [
552       "-Wl,--no-undefined",
554       # Don't allow visible symbols from libgcc or libc++ to be
555       # re-exported.
556       "-Wl,--exclude-libs=libgcc.a",
557       "-Wl,--exclude-libs=libc++_static.a",
559       # Don't allow visible symbols from libraries that contain
560       # assembly code with symbols that aren't hidden properly.
561       # http://crbug.com/448386
562       "-Wl,--exclude-libs=libvpx_assembly_arm.a",
563     ]
564     if (current_cpu == "arm") {
565       ldflags += [
566         # Enable identical code folding to reduce size.
567         "-Wl,--icf=safe",
568       ]
569     }
571     if (is_clang) {
572       if (current_cpu == "arm") {
573         cflags += [ "-target arm-linux-androideabi" ]
574         ldflags += [ "-target arm-linux-androideabi" ]
575       } else if (current_cpu == "x86") {
576         cflags += [ "-target x86-linux-androideabi" ]
577         ldflags += [ "-target x86-linux-androideabi" ]
578       }
579     }
580   }
583 config("compiler_arm_fpu") {
584   if (current_cpu == "arm" && !is_ios) {
585     cflags = [ "-mfpu=$arm_fpu" ]
586   }
589 # runtime_library -------------------------------------------------------------
591 # Sets the runtime library and associated options.
593 # How do you determine what should go in here vs. "compiler" above? Consider if
594 # a target might choose to use a different runtime library (ignore for a moment
595 # if this is possible or reasonable on your system). If such a target would want
596 # to change or remove your option, put it in the runtime_library config. If a
597 # target wants the option regardless, put it in the compiler config.
599 config("runtime_library") {
600   cflags = []
601   defines = []
602   ldflags = []
603   lib_dirs = []
604   libs = []
606   if (is_component_build) {
607     # Component mode: dynamic CRT.
608     defines += [ "COMPONENT_BUILD" ]
609     if (is_win) {
610       # Since the library is shared, it requires exceptions or will give errors
611       # about things not matching, so keep exceptions on.
612       if (is_debug) {
613         cflags += [ "/MDd" ]
614       } else {
615         cflags += [ "/MD" ]
616       }
617     }
618   } else {
619     # Static CRT.
620     if (is_win) {
621       if (is_debug) {
622         cflags += [ "/MTd" ]
623       } else {
624         cflags += [ "/MT" ]
625       }
626     }
627   }
629   if (is_win) {
630     defines += [
631       "__STD_C",
632       "_CRT_RAND_S",
633       "_CRT_SECURE_NO_DEPRECATE",
634       "_HAS_EXCEPTIONS=0",
635       "_SCL_SECURE_NO_DEPRECATE",
636     ]
637   }
639   # Android standard library setup.
640   if (is_android) {
641     if (is_clang) {
642       # Work around incompatibilities between bionic and clang headers.
643       defines += [
644         "__compiler_offsetof=__builtin_offsetof",
645         "nan=__builtin_nan",
646       ]
647     }
649     defines += [ "__GNU_SOURCE=1" ]  # Necessary for clone().
651     # TODO(jdduke) Re-enable on mips after resolving linking
652     # issues with libc++ (crbug.com/456380).
653     if (current_cpu != "mipsel" && current_cpu != "mips64el") {
654       ldflags += [ "-Wl,--warn-shared-textrel" ]
655     }
656     ldflags += [ "-nostdlib" ]
658     # NOTE: The libc++ header include paths below are specified in cflags
659     # rather than include_dirs because they need to come after include_dirs.
660     # Think of them like system headers, but don't use '-isystem' because the
661     # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
662     # strange errors. The include ordering here is important; change with
663     # caution.
664     android_libcpp_root = "$android_ndk_root/sources/cxx-stl/llvm-libc++"
666     cflags += [
667       "-isystem" +
668           rebase_path("$android_libcpp_root/libcxx/include", root_build_dir),
669       "-isystem" + rebase_path(
670               "$android_ndk_root/sources/cxx-stl/llvm-libc++abi/libcxxabi/include",
671               root_build_dir),
672       "-isystem" +
673           rebase_path("$android_ndk_root/sources/android/support/include",
674                       root_build_dir),
675     ]
677     lib_dirs += [ "$android_libcpp_root/libs/$android_app_abi" ]
679     if (component_mode == "shared_library") {
680       android_libcpp_library = "c++_shared"
681     } else {
682       android_libcpp_library = "c++_static"
683     }
685     libs += [ "$android_libcpp_library" ]
687     if (current_cpu == "mipsel") {
688       libs += [
689         # ld linker is used for mips Android, and ld does not accept library
690         # absolute path prefixed by "-l"; Since libgcc does not exist in mips
691         # sysroot the proper library will be linked.
692         # TODO(gordanac): Remove once gold linker is used for mips Android.
693         "gcc",
694       ]
695     } else {
696       libs += [
697         # Manually link the libgcc.a that the cross compiler uses. This is
698         # absolute because the linker will look inside the sysroot if it's not.
699         rebase_path(android_libgcc_file),
700       ]
701     }
703     libs += [
704       "c",
705       "dl",
706       "m",
707     ]
709     # Clang with libc++ does not require an explicit atomic library reference.
710     if (!is_clang) {
711       libs += [ "atomic" ]
712     }
713   }
716 # default_warning_flags collects all warning flags that are used by default.
717 # This is in a variable instead of a config so that it can be used in
718 # both chromium_code and no_chromium_code.  This way these flags are guaranteed
719 # to appear on the compile command line after -Wall.
721 default_warning_flags = []
722 default_warning_flags_cc = []
723 if (is_win) {
724   if (!is_clang || current_cpu != "x86") {
725     default_warning_flags += [ "/WX" ]  # Treat warnings as errors.
726   }
728   default_warning_flags += [
729     # Warnings permanently disabled:
731     # TODO(GYP) The GYP build doesn't have this globally enabled but disabled
732     # for a bunch of individual targets. Re-enable this globally when those
733     # targets are fixed.
734     "/wd4018",  # Comparing signed and unsigned values.
736     # C4127: conditional expression is constant
737     # This warning can in theory catch dead code and other problems, but
738     # triggers in far too many desirable cases where the conditional
739     # expression is either set by macros or corresponds some legitimate
740     # compile-time constant expression (due to constant template args,
741     # conditionals comparing the sizes of different types, etc.).  Some of
742     # these can be worked around, but it's not worth it.
743     "/wd4127",
745     # C4251: 'identifier' : class 'type' needs to have dll-interface to be
746     #        used by clients of class 'type2'
747     # This is necessary for the shared library build.
748     "/wd4251",
750     # C4351: new behavior: elements of array 'array' will be default
751     #        initialized
752     # This is a silly "warning" that basically just alerts you that the
753     # compiler is going to actually follow the language spec like it's
754     # supposed to, instead of not following it like old buggy versions did.
755     # There's absolutely no reason to turn this on.
756     "/wd4351",
758     # C4355: 'this': used in base member initializer list
759     # It's commonly useful to pass |this| to objects in a class' initializer
760     # list.  While this warning can catch real bugs, most of the time the
761     # constructors in question don't attempt to call methods on the passed-in
762     # pointer (until later), and annotating every legit usage of this is
763     # simply more hassle than the warning is worth.
764     "/wd4355",
766     # C4503: 'identifier': decorated name length exceeded, name was
767     #        truncated
768     # This only means that some long error messages might have truncated
769     # identifiers in the presence of lots of templates.  It has no effect on
770     # program correctness and there's no real reason to waste time trying to
771     # prevent it.
772     "/wd4503",
774     # C4611: interaction between 'function' and C++ object destruction is
775     #        non-portable
776     # This warning is unavoidable when using e.g. setjmp/longjmp.  MSDN
777     # suggests using exceptions instead of setjmp/longjmp for C++, but
778     # Chromium code compiles without exception support.  We therefore have to
779     # use setjmp/longjmp for e.g. JPEG decode error handling, which means we
780     # have to turn off this warning (and be careful about how object
781     # destruction happens in such cases).
782     "/wd4611",
784     # Warnings to evaluate and possibly fix/reenable later:
786     "/wd4100",  # Unreferenced formal function parameter.
787     "/wd4121",  # Alignment of a member was sensitive to packing.
788     "/wd4244",  # Conversion: possible loss of data.
789     "/wd4481",  # Nonstandard extension: override specifier.
790     "/wd4505",  # Unreferenced local function has been removed.
791     "/wd4510",  # Default constructor could not be generated.
792     "/wd4512",  # Assignment operator could not be generated.
793     "/wd4610",  # Class can never be instantiated, constructor required.
794     "/wd4996",  # Deprecated function warning.
795   ]
797   # VS xtree header file needs to be patched or 4702 (unreachable code
798   # warning) is reported if _HAS_EXCEPTIONS=0. Disable the warning if xtree is
799   # not patched.
800   if (!msvs_xtree_patched &&
801       exec_script("../../win_is_xtree_patched.py", [], "value") == 0) {
802     default_warning_flags += [ "/wd4702" ]  # Unreachable code.
803   }
805   # Building with Clang on Windows is a work in progress and very
806   # experimental. See crbug.com/82385.
807   # Keep this in sync with the similar block in build/common.gypi
808   if (is_clang) {
809     default_warning_flags += [
810       # TODO(hans): Make this list shorter eventually.
811       "-Qunused-arguments",
812       "-Wno-c++11-compat-deprecated-writable-strings",
813       "-Wno-deprecated-declarations",
814       "-Wno-empty-body",
815       "-Wno-enum-conversion",
816       "-Wno-extra-tokens",
817       "-Wno-ignored-attributes",
818       "-Wno-incompatible-pointer-types",
819       "-Wno-int-to-void-pointer-cast",
820       "-Wno-invalid-noreturn",
821       "-Wno-logical-op-parentheses",
822       "-Wno-microsoft",
823       "-Wno-missing-braces",
824       "-Wno-missing-declarations",
825       "-Wno-msvc-include",
826       "-Wno-null-dereference",
827       "-Wno-overloaded-virtual",
828       "-Wno-parentheses",
829       "-Wno-pointer-sign",
830       "-Wno-reorder",
831       "-Wno-return-type-c-linkage",
832       "-Wno-self-assign",
833       "-Wno-sometimes-uninitialized",
834       "-Wno-switch",
835       "-Wno-tautological-compare",
836       "-Wno-unknown-pragmas",
837       "-Wno-unsequenced",
838       "-Wno-unused-function",
839       "-Wno-unused-private-field",
840       "-Wno-unused-value",
841       "-Wno-unused-variable",
842       "-Wno-unused-local-typedef",  # http://crbug.com/411648
843       "-Wno-inconsistent-missing-override",  #http://crbug.com/428099
844     ]
845   }
846 } else {
847   # Common GCC warning setup.
848   default_warning_flags += [
849     # Enables.
850     "-Wendif-labels",  # Weird old-style text after an #endif.
851     "-Werror",  # Warnings as errors.
853     # Disables.
854     "-Wno-missing-field-initializers",  # "struct foo f = {0};"
855     "-Wno-unused-parameter",  # Unused function parameters.
856   ]
858   if (is_mac) {
859     default_warning_flags += [ "-Wnewline-eof" ]
860     if (!is_nacl) {
861       # When compiling Objective-C, warns if a method is used whose
862       # availability is newer than the deployment target. This is not
863       # required when compiling Chrome for iOS.
864       default_warning_flags += [ "-Wpartial-availability" ]
865     }
866   }
868   if (gcc_version >= 48) {
869     default_warning_flags_cc += [
870       # See comment for -Wno-c++11-narrowing.
871       "-Wno-narrowing",
873       # TODO(thakis): Remove, http://crbug.com/263960
874       "-Wno-literal-suffix",
875     ]
876   }
878   # Suppress warnings about ABI changes on ARM (Clang doesn't give this
879   # warning).
880   if (current_cpu == "arm" && !is_clang) {
881     default_warning_flags += [ "-Wno-psabi" ]
882   }
884   if (is_android) {
885     # Disable any additional warnings enabled by the Android build system but
886     # which chromium does not build cleanly with (when treating warning as
887     # errors).
888     default_warning_flags += [
889       "-Wno-extra",
890       "-Wno-ignored-qualifiers",
891       "-Wno-type-limits",
892     ]
893     default_warning_flags_cc += [
894       # Disabling c++0x-compat should be handled in WebKit, but
895       # this currently doesn't work because gcc_version is not set
896       # correctly when building with the Android build system.
897       # TODO(torne): Fix this in WebKit.
898       "-Wno-error=c++0x-compat",
900       # Other things unrelated to -Wextra:
901       "-Wno-non-virtual-dtor",
902       "-Wno-sign-promo",
903     ]
904   }
906   if (gcc_version >= 48) {
907     # Don't warn about the "typedef 'foo' locally defined but not used"
908     # for gcc 4.8.
909     # TODO: remove this flag once all builds work. See crbug.com/227506
910     default_warning_flags += [ "-Wno-unused-local-typedefs" ]
911   }
913 if (is_clang) {
914   default_warning_flags += [
915     # This warns on using ints as initializers for floats in
916     # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
917     # which happens in several places in chrome code. Not sure if
918     # this is worth fixing.
919     "-Wno-c++11-narrowing",
921     # Don't die on dtoa code that uses a char as an array index.
922     # This is required solely for base/third_party/dmg_fp/dtoa.cc.
923     # TODO(brettw) move this to that project then!
924     "-Wno-char-subscripts",
926     # Warns on switches on enums that cover all enum values but
927     # also contain a default: branch. Chrome is full of that.
928     "-Wno-covered-switch-default",
930     # Clang considers the `register` keyword as deprecated, but e.g.
931     # code generated by flex (used in angle) contains that keyword.
932     # http://crbug.com/255186
933     "-Wno-deprecated-register",
935     # TODO(thakis): This used to be implied by -Wno-unused-function,
936     # which we no longer use. Check if it makes sense to remove
937     # this as well. http://crbug.com/316352
938     "-Wno-unneeded-internal-declaration",
940     # TODO(thakis): Remove, http://crbug.com/263960
941     "-Wno-reserved-user-defined-literal",
942   ]
944   # NaCl's Clang compiler and Chrome's hermetic Clang compiler will almost
945   # always have different versions. Certain flags may not be recognized by
946   # one version or the other.
947   if (!is_nacl) {
948     # Flags NaCl does not recognize.
949     default_warning_flags += [
950       # TODO(hans): Get this cleaned up.
951       "-Wno-inconsistent-missing-override",
952     ]
953   }
956 # chromium_code ---------------------------------------------------------------
958 # Toggles between higher and lower warnings for code that is (or isn't)
959 # part of Chromium.
961 config("chromium_code") {
962   if (is_win) {
963     cflags = [ "/W4" ]  # Warning level 4.
964   } else {
965     cflags = [
966       "-Wall",
968       # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
969       # so we specify it explicitly.
970       # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
971       # http://code.google.com/p/chromium/issues/detail?id=90453
972       "-Wsign-compare",
973     ]
975     # In Chromium code, we define __STDC_foo_MACROS in order to get the
976     # C99 macros on Mac and Linux.
977     defines = [
978       "__STDC_CONSTANT_MACROS",
979       "__STDC_FORMAT_MACROS",
980     ]
982     if (!using_sanitizer && (!is_linux || !is_clang || is_official_build)) {
983       # _FORTIFY_SOURCE isn't really supported by Clang now, see
984       # http://llvm.org/bugs/show_bug.cgi?id=16821.
985       # It seems to work fine with Ubuntu 12 headers though, so use it in
986       # official builds.
987       #
988       # Non-chromium code is not guaranteed to compile cleanly with
989       # _FORTIFY_SOURCE. Also, fortified build may fail when optimizations are
990       # disabled, so only do that for Release build.
991       defines += [ "_FORTIFY_SOURCE=2" ]
992     }
993   }
994   cflags += default_warning_flags
995   cflags_cc = default_warning_flags_cc
997 config("no_chromium_code") {
998   cflags = []
999   cflags_cc = []
1000   defines = []
1002   if (is_win) {
1003     cflags += [
1004       "/W3",  # Warning level 3.
1005       "/wd4800",  # Disable warning when forcing value to bool.
1006       "/wd4267",  # TODO(jschuh): size_t to int.
1007       "/wd4996",  # Deprecated function warning.
1008     ]
1009     defines += [
1010       "_CRT_NONSTDC_NO_WARNINGS",
1011       "_CRT_NONSTDC_NO_DEPRECATE",
1012     ]
1013   }
1015   if (is_linux) {
1016     # Don't warn about ignoring the return value from e.g. close(). This is
1017     # off by default in some gccs but on by default in others. BSD systems do
1018     # not support this option, since they are usually using gcc 4.2.1, which
1019     # does not have this flag yet.
1020     cflags += [ "-Wno-unused-result" ]
1021   }
1023   if (is_linux || is_android) {
1024     cflags += [
1025       # Don't warn about printf format problems. This is off by default in gcc
1026       # but on in Ubuntu's gcc(!).
1027       "-Wno-format",
1028     ]
1029     cflags_cc += [
1030       # Don't warn about hash_map in third-party code.
1031       "-Wno-deprecated",
1032     ]
1033   }
1034   cflags += default_warning_flags
1035   cflags_cc += default_warning_flags_cc
1038 # rtti ------------------------------------------------------------------------
1040 # Allows turning Run-Time Type Identification on or off.
1042 config("rtti") {
1043   if (is_win) {
1044     cflags_cc = [ "/GR" ]
1045   }
1047 config("no_rtti") {
1048   if (is_win) {
1049     cflags_cc = [ "/GR-" ]
1050   } else {
1051     cflags_cc = [ "-fno-rtti" ]
1052   }
1055 # Warnings ---------------------------------------------------------------------
1057 # This will generate warnings when using Clang if code generates exit-time
1058 # destructors, which will slow down closing the program.
1059 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
1060 config("wexit_time_destructors") {
1061   # TODO: Enable on Windows too, http://crbug.com/404525
1062   if (is_clang && !is_win) {
1063     cflags = [ "-Wexit-time-destructors" ]
1064   }
1067 # On Windows compiling on x64, VC will issue a warning when converting
1068 # size_t to int because it will truncate the value. Our code should not have
1069 # these warnings and one should use a static_cast or a checked_cast for the
1070 # conversion depending on the case. However, a lot of code still needs to be
1071 # fixed. Apply this config to such targets to disable the warning.
1073 # Note that this can be applied regardless of platform and architecture to
1074 # clean up the call sites. This will only apply the flag when necessary.
1076 # TODO(jschuh): crbug.com/167187 fix this and delete this config.
1077 config("no_size_t_to_int_warning") {
1078   if (is_win && current_cpu == "x64") {
1079     cflags = [ "/wd4267" ]
1080   }
1083 # Optimization -----------------------------------------------------------------
1085 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
1086 # which it will assign to the config it implicitly applies to every target. If
1087 # you want to override the optimization level for your target, remove this
1088 # config (which will expand differently for debug or release builds), and then
1089 # add back the one you want to override it with:
1091 #   configs -= default_optimization_config
1092 #   configs += [ "//build/config/compiler/optimize_max" ]
1094 # Shared settings for both "optimize" and "optimize_max" configs.
1095 # IMPORTANT: On Windows "/O1" and "/O2" must go before the common flags.
1096 if (is_win) {
1097   common_optimize_on_cflags = [
1098     "/Ob2",  # Both explicit and auto inlining.
1099     "/Oy-",  # Disable omitting frame pointers, must be after /O2.
1100   ]
1101   if (!is_asan) {
1102     common_optimize_on_cflags += [
1103       # Put data in separate COMDATs. This allows the linker
1104       # to put bit-identical constants at the same address even if
1105       # they're unrelated constants, which saves binary size.
1106       # This optimization can't be used when ASan is enabled because
1107       # it is not compatible with the ASan ODR checker.
1108       "/Gw",
1109     ]
1110   }
1111   common_optimize_on_ldflags = [ "/OPT:REF" ]
1112 } else {
1113   common_optimize_on_cflags = [
1114     # Don't emit the GCC version ident directives, they just end up in the
1115     # .comment section taking up binary size.
1116     "-fno-ident",
1118     # Put data and code in their own sections, so that unused symbols
1119     # can be removed at link time with --gc-sections.
1120     "-fdata-sections",
1121     "-ffunction-sections",
1122   ]
1123   common_optimize_on_ldflags = []
1125   if (is_android) {
1126     if (!using_sanitizer) {
1127       common_optimize_on_cflags += [ "-fomit-frame-pointer" ]
1128     }
1130     # TODO(jdduke) Re-enable on mips after resolving linking
1131     # issues with libc++ (crbug.com/456380).
1132     if (current_cpu != "mipsel" && current_cpu != "mips64el") {
1133       common_optimize_on_ldflags += [
1134         # Warn in case of text relocations.
1135         "-Wl,--warn-shared-textrel",
1136       ]
1137     }
1138   }
1140   if (is_mac) {
1141     if (symbol_level == 2) {
1142       # Mac dead code stripping requires symbols.
1143       common_optimize_on_ldflags += [ "-Wl,-dead_strip" ]
1144     }
1145   } else {
1146     # Non-Mac Posix linker flags.
1147     common_optimize_on_ldflags += [
1148       # Specifically tell the linker to perform optimizations.
1149       # See http://lwn.net/Articles/192624/ .
1150       "-Wl,-O1",
1151       "-Wl,--gc-sections",
1152     ]
1154     if (!using_sanitizer) {
1155       # Functions interposed by the sanitizers can make ld think
1156       # that some libraries aren't needed when they actually are,
1157       # http://crbug.com/234010. As workaround, disable --as-needed.
1158       common_optimize_on_ldflags += [ "-Wl,--as-needed" ]
1159     }
1160   }
1163 # Default "optimization on" config. On Windows, this favors size over speed.
1164 config("optimize") {
1165   if (is_win) {
1166     # Favor size over speed, /O1 must be before the common flags. The GYP
1167     # build also specifies /Os and /GF but these are implied by /O1.
1168     cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ]
1169   } else if (is_android || is_ios) {
1170     cflags = [ "-Os" ] + common_optimize_on_cflags  # Favor size over speed.
1171   } else {
1172     cflags = [ "-O2" ] + common_optimize_on_cflags
1173   }
1174   ldflags = common_optimize_on_ldflags
1177 # Turn off optimizations.
1178 config("no_optimize") {
1179   if (is_win) {
1180     cflags = [
1181       "/Od",  # Disable optimization.
1182       "/Ob0",  # Disable all inlining (on by default).
1183       "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
1184     ]
1185   } else if (is_android && !android_full_debug) {
1186     # On Android we kind of optimize some things that don't affect debugging
1187     # much even when optimization is disabled to get the binary size down.
1188     cflags = [
1189       "-Os",
1190       "-fdata-sections",
1191       "-ffunction-sections",
1192     ]
1193     if (!using_sanitizer) {
1194       cflags += [ "-fomit-frame-pointer" ]
1195     }
1196     ldflags = common_optimize_on_ldflags
1197   } else {
1198     cflags = [ "-O0" ]
1199   }
1202 # Turns up the optimization level. On Windows, this implies whole program
1203 # optimization and link-time code generation which is very expensive and should
1204 # be used sparingly.
1205 config("optimize_max") {
1206   ldflags = common_optimize_on_ldflags
1207   if (is_win) {
1208     # Favor speed over size, /O2 must be before the common flags. The GYP
1209     # build also specifies /Ot, /Oi, and /GF, but these are implied by /O2.
1210     cflags = [ "/O2" ] + common_optimize_on_cflags
1211     if (is_official_build) {
1212       # TODO(GYP): TODO(dpranke): Should these only be on in an official
1213       # build, or on all the time? For now we'll require official build so
1214       # that the compile is clean.
1215       cflags += [
1216         "/GL",  # Whole program optimization.
1218         # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
1219         # Probably anything that this would catch that wouldn't be caught in a
1220         # normal build isn't going to actually be a bug, so the incremental
1221         # value of C4702 for PGO builds is likely very small.
1222         "/wd4702",
1223       ]
1224       ldflags += [ "/LTCG" ]
1225     }
1226   } else {
1227     cflags = [ "-O2" ] + common_optimize_on_cflags
1228   }
1231 # Symbols ----------------------------------------------------------------------
1233 config("symbols") {
1234   if (is_win) {
1235     import("//build/toolchain/goma.gni")
1236     if (use_goma) {
1237       cflags = [ "/Z7" ]  # No PDB file
1238     } else {
1239       cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
1240     }
1241     ldflags = [ "/DEBUG" ]
1242   } else {
1243     cflags = [ "-g2" ]
1244     if (use_debug_fission) {
1245       cflags += [ "-gsplit-dwarf" ]
1246     }
1247   }
1250 config("minimal_symbols") {
1251   if (is_win) {
1252     # Linker symbols for backtraces only.
1253     ldflags = [ "/DEBUG" ]
1254   } else {
1255     cflags = [ "-g1" ]
1256     if (use_debug_fission) {
1257       cflags += [ "-gsplit-dwarf" ]
1258     }
1259   }
1262 config("no_symbols") {
1263   if (!is_win) {
1264     cflags = [ "-g0" ]
1265   }