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