Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / build / config / compiler / BUILD.gn
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.
4
5 import("//build/config/android/config.gni")
6 if (cpu_arch == "arm") {
7   import("//build/config/arm.gni")
8 }
9 if (is_posix) {
10   import("//build/config/gcc/gcc_version.gni")
11 }
12
13 declare_args() {
14   # Normally, Android builds are lightly optimized, even for debug builds, to
15   # keep binary size down. Setting this flag to true disables such optimization
16   android_full_debug = false
17
18   # Whether to use the binary binutils checked into third_party/binutils.
19   # These are not multi-arch so cannot be used except on x86 and x86-64 (the
20   # only two architectures that are currently checked in). Turn this off when
21   # you are using a custom toolchain and need to control -B in cflags.
22   linux_use_bundled_binutils = is_linux && cpu_arch == "x64"
23 }
24
25 use_gold = is_linux && cpu_arch == "x64"
26
27 if (!is_win) {
28   # linux_use_debug_fission: whether to use split DWARF debug info
29   # files. This can reduce link time significantly, but is incompatible
30   # with some utilities such as icecc and ccache. Requires gold and
31   # gcc >= 4.8 or clang.
32   # http://gcc.gnu.org/wiki/DebugFission
33   use_debug_fission = use_gold && linux_use_bundled_binutils
34 }
35
36 # default_include_dirs ---------------------------------------------------------
37 #
38 # This is a separate config so that third_party code (which would not use the
39 # source root and might have conflicting versions of some headers) can remove
40 # this and specify their own include paths.
41 config("default_include_dirs") {
42   include_dirs = [
43     "//",
44     root_gen_dir,
45   ]
46 }
47
48 # compiler ---------------------------------------------------------------------
49 #
50 # Base compiler configuration.
51 #
52 # See also "runtime_library" below for related stuff and a discusison about
53 # where stuff should go. Put warning related stuff in the "warnings" config.
54
55 config("compiler") {
56   cflags = []
57   cflags_c = []
58   cflags_cc = []
59   ldflags = []
60   defines = []
61
62   # In general, Windows is totally different, but all the other builds share
63   # some common GCC configuration. This section sets up Windows and the common
64   # GCC flags, and then we handle the other non-Windows platforms specifically
65   # below.
66   if (is_win) {
67     # Windows compiler flags setup.
68     # -----------------------------
69     cflags += [
70       "/Gy",  # Enable function-level linking.
71       "/GS",  # Enable buffer security checking.
72       "/FS",  # Preserve previous PDB behavior.
73     ]
74     if (is_component_build) {
75       cflags += [
76         "/EHsc",  # Assume C functions can't throw exceptions and don't catch
77                   # structured exceptions (only C++ ones).
78       ]
79     }
80   } else {
81     # Common GCC compiler flags setup.
82     # --------------------------------
83     cflags += [
84       "-fno-strict-aliasing",  # See http://crbug.com/32204
85     ]
86     cflags_cc += [
87       "-fno-threadsafe-statics",
88       # Not exporting C++ inline functions can generally be applied anywhere
89       # so we do so here. Normal function visibility is controlled by
90       # //build/config/gcc:symbol_visibility_hidden.
91       "-fvisibility-inlines-hidden",
92     ]
93
94     # Stack protection.
95     if (is_mac) {
96       cflags += [ "-fstack-protector-all" ]
97     } else if (is_linux) {
98       cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
99     }
100
101     # Linker warnings.
102     if (!(is_chromeos && cpu_arch == "arm") && !is_mac) {
103       # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
104       ldflags += [ "-Wl,--fatal-warnings" ]
105     }
106   }
107
108   if (is_clang && is_debug) {
109     # Allow comparing the address of references and 'this' against 0
110     # in debug builds. Technically, these can never be null in
111     # well-defined C/C++ and Clang can optimize such checks away in
112     # release builds, but they may be used in asserts in debug builds.
113     cflags_cc += [
114       "-Wno-undefined-bool-conversion",
115       "-Wno-tautological-undefined-compare",
116     ]
117   }
118
119   if (is_clang && !is_win) {
120     # This is here so that all files get recompiled after a clang roll and
121     # when turning clang on or off. (defines are passed via the command line,
122     # and build system rebuild things when their commandline changes). Nothing
123     # should ever read this define.
124     defines += [
125       "CR_CLANG_REVISION=" +
126           exec_script(
127               "//tools/clang/scripts/posix-print-revision.py", [], "value")
128     ]
129   }
130
131   # Mac-specific compiler flags setup.
132   # ----------------------------------
133   if (is_mac || is_ios) {
134     # These flags are shared between the C compiler and linker.
135     common_mac_flags = []
136
137     # CPU architecture.
138     if (cpu_arch == "x64") {
139       common_mac_flags += [ "-arch", "x86_64" ]
140     } else if (cpu_arch == "x86") {
141       common_mac_flags += [ "-arch", "i386" ]
142     }
143
144     cflags += common_mac_flags
145
146     # Without this, the constructors and destructors of a C++ object inside
147     # an Objective C struct won't be called, which is very bad.
148     cflags_objcc = [ "-fobjc-call-cxx-cdtors", ]
149
150     cflags_c += [ "-std=c99" ]
151     cflags_cc += [ "-std=gnu++11" ]
152
153     ldflags += common_mac_flags
154   } else if (is_posix) {
155     # Non-Mac Posix compiler flags setup.
156     # -----------------------------------
157     if (gcc_version >= 48) {
158       cflags_cc += [
159         "-std=gnu++11",
160       ]
161     }
162
163     # CPU architecture. We may or may not be doing a cross compile now, so for
164     # simplicity we always explicitly set the architecture.
165     if (cpu_arch == "x64") {
166       cflags += [ "-m64", "-march=x86-64", ]
167       ldflags += [ "-m64" ]
168     } else if (cpu_arch == "x86") {
169       cflags += [ "-m32" ]
170       ldflags += [ "-m32" ]
171     } else if (cpu_arch == "arm") {
172       # Don't set the compiler flags for the WebView build. These will come
173       # from the Android build system.
174       if (!is_android_webview_build) {
175         cflags += [
176           "-march=$arm_arch",
177           "-mfloat-abi=$arm_float_abi",
178         ]
179         if (arm_tune != "") {
180           cflags += [ "-mtune=$arm_tune" ]
181         }
182         if (arm_use_thumb) {
183           cflags += [ "-mthumb" ]
184           if (is_android && !is_clang) {  # Clang doesn't support this option.
185             cflags += [ "-mthumb-interwork" ]
186           }
187         }
188         if (!is_clang) {
189           # Clang doesn't support these flags.
190           cflags += [
191             # The tree-sra optimization (scalar replacement for
192             # aggregates enabling subsequent optimizations) leads to
193             # invalid code generation when using the Android NDK's
194             # compiler (r5-r7). This can be verified using
195             # webkit_unit_tests' WTF.Checked_int8_t test.
196             "-fno-tree-sra",
197             # The following option is disabled to improve binary
198             # size and performance in gcc 4.9.
199             "-fno-caller-saves",
200           ]
201         }
202       }
203     }
204
205     defines += [ "_FILE_OFFSET_BITS=64" ]
206
207     # Omit unwind support in official builds to save space. We can use breakpad
208     # for these builds.
209     if (is_chrome_branded && is_official_build) {
210       cflags += [
211         "-fno-unwind-tables",
212         "-fno-asynchronous-unwind-tables",
213       ]
214     } else {
215       cflags += [ "-funwind-tables" ]
216     }
217   }
218
219   # Linux/Android common flags setup.
220   # ---------------------------------
221   if (is_linux || is_android) {
222     cflags += [
223       "-fPIC",
224       "-pipe",  # Use pipes for communicating between sub-processes. Faster.
225     ]
226
227     ldflags += [
228       "-fPIC",
229       "-Wl,-z,noexecstack",
230       "-Wl,-z,now",
231       "-Wl,-z,relro",
232     ]
233   }
234
235   # Linux-specific compiler flags setup.
236   # ------------------------------------
237   if (is_linux) {
238     cflags += [ "-pthread" ]
239     ldflags += [
240       "-pthread",
241     ]
242   }
243   if (use_gold) {
244     # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
245     # address space, and it doesn't support cross-compiling).
246     gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
247                             root_build_dir)
248     ldflags += [
249       "-B$gold_path",
250
251       # Newer gccs and clangs support -fuse-ld, use the flag to force gold
252       # selection.
253       # gcc -- http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Optimize-Options.html
254       "-fuse-ld=gold",
255
256       # There seems to be a conflict of --icf and -pie in gold which can
257       # generate crashy binaries. As a security measure, -pie takes
258       # precedence for now.
259       # TODO(brettw) common.gypi has this only for target toolset.
260       #"-Wl,--icf=safe",
261       "-Wl,--icf=none",
262
263       # Experimentation found that using four linking threads
264       # saved ~20% of link time.
265       # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
266       # Only apply this to the target linker, since the host
267       # linker might not be gold, but isn't used much anyway.
268       # TODO(raymes): Disable threading because gold is frequently
269       # crashing on the bots: crbug.com/161942.
270       #"-Wl,--threads",
271       #"-Wl,--thread-count=4",
272     ]
273   }
274
275   if (linux_use_bundled_binutils) {
276     binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
277                                 root_build_dir)
278     cflags += [ "-B$binutils_path" ]
279   }
280
281   # Clang-specific compiler flags setup.
282   # ------------------------------------
283   if (is_clang) {
284     cflags += [
285       "-fcolor-diagnostics",
286     ]
287     cflags_cc += [
288       "-std=gnu++11",
289     ]
290   }
291
292   # Android-specific flags setup.
293   # -----------------------------
294   if (is_android) {
295     cflags += [
296       "-ffunction-sections",
297       "-funwind-tables",
298       "-fno-short-enums",
299     ]
300     if (!is_clang) {
301       # Clang doesn't support these flags.
302       cflags += [
303         "-finline-limit=64",
304       ]
305     }
306     if (is_android_webview_build) {
307       # Android predefines this as 1; undefine it here so Chromium can redefine
308       # it later to be 2 for chromium code and unset for third party code. This
309       # works because cflags are added before defines.
310       # TODO(brettw) the above comment seems incorrect. We specify defines
311       # before cflags on our compiler command lines.
312       cflags += [ "-U_FORTIFY_SOURCE" ]
313     }
314
315     if (is_asan) {
316       # Android build relies on -Wl,--gc-sections removing unreachable code.
317       # ASan instrumentation for globals inhibits this and results in a library
318       # with unresolvable relocations.
319       # TODO(eugenis): find a way to reenable this.
320       cflags += [ "-mllvm -asan-globals=0" ]
321     }
322
323     defines += [ "ANDROID" ]
324     if (!is_android_webview_build) {
325       # The NDK has these things, but doesn't define the constants
326       # to say that it does. Define them here instead.
327       defines += [ "HAVE_SYS_UIO_H" ]
328     }
329
330     # Use gold for Android for most CPU architectures.
331     if (cpu_arch == "x86" || cpu_arch == "x64" || cpu_arch == "arm") {
332       ldflags += [ "-fuse-ld=gold" ]
333       if (is_clang) {
334         # Let clang find the ld.gold in the NDK.
335         ldflags += [ "--gcc-toolchain=" + rebase_path(android_toolchain_root,
336                                                       root_build_dir) ]
337       }
338     }
339
340     ldflags += [
341       "-Wl,--no-undefined",
342       # Don't export symbols from statically linked libraries.
343       "-Wl,--exclude-libs=ALL",
344     ]
345     if (cpu_arch == "arm") {
346       ldflags += [
347         # Enable identical code folding to reduce size.
348         "-Wl,--icf=safe",
349       ]
350     }
351
352     if (is_clang) {
353       if (cpu_arch == "arm") {
354         cflags += [
355           "-target arm-linux-androideabi",
356         ]
357         ldflags += [ "-target arm-linux-androideabi" ]
358       } else if (cpu_arch == "x86") {
359         cflags += [ "-target x86-linux-androideabi" ]
360         ldflags += [ "-target x86-linux-androideabi" ]
361       }
362     }
363   }
364 }
365
366 config("compiler_arm_fpu") {
367   if (cpu_arch == "arm" && !is_android_webview_build) {
368     cflags = [
369       "-mfpu=$arm_fpu",
370     ]
371   }
372 }
373
374 # runtime_library -------------------------------------------------------------
375 #
376 # Sets the runtime library and associated options.
377 #
378 # How do you determine what should go in here vs. "compiler" above? Consider if
379 # a target might choose to use a different runtime library (ignore for a moment
380 # if this is possible or reasonable on your system). If such a target would want
381 # to change or remove your option, put it in the runtime_library config. If a
382 # target wants the option regardless, put it in the compiler config.
383
384 config("runtime_library") {
385   cflags = []
386   defines = []
387   ldflags = []
388   lib_dirs = []
389   libs = []
390
391   if (is_component_build) {
392     # Component mode: dynamic CRT.
393     defines += [ "COMPONENT_BUILD" ]
394     if (is_win) {
395       # Since the library is shared, it requires exceptions or will give errors
396       # about things not matching, so keep exceptions on.
397       if (is_debug) {
398         cflags += [ "/MDd" ]
399       } else {
400         cflags += [ "/MD" ]
401       }
402     }
403   } else {
404     # Static CRT.
405     if (is_win) {
406       # We don't use exceptions, and when we link statically we can just get
407       # rid of them entirely.
408       defines += [ "_HAS_EXCEPTIONS=0" ]
409       if (is_debug) {
410         cflags += [ "/MTd" ]
411       } else {
412         cflags += [ "/MT" ]
413       }
414     }
415   }
416
417   if (is_win) {
418     defines += [
419       "__STD_C",
420       "__STDC_CONSTANT_MACROS",
421       "__STDC_FORMAT_MACROS",
422       "_CRT_RAND_S",
423       "_CRT_SECURE_NO_DEPRECATE",
424       "_SCL_SECURE_NO_DEPRECATE",
425     ]
426   }
427
428   # Stlport setup. Android uses a different (smaller) version of the STL.
429   if (is_android) {
430     if (is_clang) {
431       # Work around incompatibilities between bionic and clang headers.
432       defines += [
433         "__compiler_offsetof=__builtin_offsetof",
434         "nan=__builtin_nan",
435       ]
436     }
437
438     defines += [
439       "USE_STLPORT=1",
440       "_STLP_USE_PTR_SPECIALIZATIONS=1",
441       "__GNU_SOURCE=1",  # Necessary for clone().
442     ]
443
444     ldflags += [
445       "-Wl,--warn-shared-textrel",
446       "-nostdlib",
447     ]
448
449     # NOTE: The stlport header include paths below are specified in cflags
450     # rather than include_dirs because they need to come after include_dirs.
451     # Think of them like system headers, but don't use '-isystem' because the
452     # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
453     # strange errors. The include ordering here is important; change with
454     # caution.
455     if (use_system_stlport) {
456       cflags += [
457         # For libstdc++/include, which is used by stlport.
458         "-I" + rebase_path("$android_src/bionic", root_build_dir),
459         "-I" + rebase_path("$android_src/external/stlport/stlport",
460                            root_build_dir),
461       ]
462       libs += [
463         "stlport",
464       ]
465     } else {
466       android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
467
468       cflags += [
469         "-isystem" + rebase_path("$android_stlport_root/stlport",
470                                  root_build_dir)
471       ]
472       lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
473
474       if (component_mode == "shared_library") {
475         libs += [ "stlport_shared" ]
476       } else {
477         libs += [ "stlport_static" ]
478       }
479     }
480
481     if (cpu_arch == "mipsel") {
482       libs += [
483         # ld linker is used for mips Android, and ld does not accept library
484         # absolute path prefixed by "-l"; Since libgcc does not exist in mips
485         # sysroot the proper library will be linked.
486         # TODO(gordanac): Remove once gold linker is used for mips Android.
487         "gcc",
488       ]
489     } else {
490       libs += [
491         # Manually link the libgcc.a that the cross compiler uses. This is
492         # absolute because the linker will look inside the sysroot if it's not.
493         rebase_path(android_libgcc_file),
494       ]
495     }
496
497     libs += [
498       "c",
499       "dl",
500       "m",
501     ]
502
503   }
504 }
505
506 # chromium_code ---------------------------------------------------------------
507 #
508 # Toggles between higher and lower warnings for code that is (or isn't)
509 # part of Chromium.
510
511 config("chromium_code") {
512   if (is_win) {
513     cflags = [
514       "/W4",  # Warning level 4.
515     ]
516   } else {
517     cflags = [
518       "-Wall",
519       "-Wextra",
520
521       # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
522       # so we specify it explicitly.
523       # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
524       # http://code.google.com/p/chromium/issues/detail?id=90453
525       "-Wsign-compare",
526     ]
527
528     # In Chromium code, we define __STDC_foo_MACROS in order to get the
529     # C99 macros on Mac and Linux.
530     defines = [
531       "__STDC_CONSTANT_MACROS",
532       "__STDC_FORMAT_MACROS",
533     ]
534   }
535 }
536 config("no_chromium_code") {
537   cflags = []
538   cflags_cc = []
539   defines = []
540
541   if (is_win) {
542     cflags += [
543       "/W3",  # Warning level 3.
544       "/wd4800",  # Disable warning when forcing value to bool.
545     ]
546     defines += [
547       "_CRT_NONSTDC_NO_WARNINGS",
548       "_CRT_NONSTDC_NO_DEPRECATE",
549     ]
550   }
551
552   if (is_linux) {
553     # Don't warn about ignoring the return value from e.g. close(). This is
554     # off by default in some gccs but on by default in others. BSD systems do
555     # not support this option, since they are usually using gcc 4.2.1, which
556     # does not have this flag yet.
557     cflags += [ "-Wno-unused-result" ]
558   }
559
560   if (is_linux || is_android) {
561     cflags += [
562       # Don't warn about printf format problems. This is off by default in gcc
563       # but on in Ubuntu's gcc(!).
564       "-Wno-format",
565     ]
566     cflags_cc += [
567       # Don't warn about hash_map in third-party code.
568       "-Wno-deprecated",
569     ]
570   }
571
572   if (is_android_webview_build) {
573     # There is a class of warning which:
574     #  1) Android always enables and also treats as errors
575     #  2) Chromium ignores in third party code
576     # So we re-enable those warnings when building Android.
577     cflags += [
578       "-Wno-address",
579       "-Wno-format-security",
580       "-Wno-return-type",
581       "-Wno-sequence-point",
582     ]
583     cflags_cc += [ "-Wno-non-virtual-dtor" ]
584   }
585 }
586
587 # rtti ------------------------------------------------------------------------
588 #
589 # Allows turning Run-Time Type Identification on or off.
590
591 config("rtti") {
592   if (is_win) {
593     cflags_cc = [ "/GR" ]
594   }
595 }
596 config("no_rtti") {
597   if (is_win) {
598     cflags_cc = [ "/GR-" ]
599   } else {
600     cflags_cc = [ "-fno-rtti" ]
601   }
602 }
603
604 # Warnings ---------------------------------------------------------------------
605 #
606 # This is where we disable various warnings that we've decided aren't
607 # worthwhile, and enable special warnings.
608
609 config("default_warnings") {
610   if (is_win) {
611     cflags = [
612       "/WX",      # Treat warnings as errors.
613
614       # Warnings permanently disabled:
615
616       # TODO(GYP) The GYP build doesn't have this globally enabled but disabled
617       # for a bunch of individual targets. Re-enable this globally when those
618       # targets are fixed.
619       "/wd4018",  # Comparing signed and unsigned values.
620
621       # C4127: conditional expression is constant
622       # This warning can in theory catch dead code and other problems, but
623       # triggers in far too many desirable cases where the conditional
624       # expression is either set by macros or corresponds some legitimate
625       # compile-time constant expression (due to constant template args,
626       # conditionals comparing the sizes of different types, etc.).  Some of
627       # these can be worked around, but it's not worth it.
628       "/wd4127",
629
630       # C4251: 'identifier' : class 'type' needs to have dll-interface to be
631       #        used by clients of class 'type2'
632       # This is necessary for the shared library build.
633       "/wd4251",
634
635       # C4351: new behavior: elements of array 'array' will be default
636       #        initialized
637       # This is a silly "warning" that basically just alerts you that the
638       # compiler is going to actually follow the language spec like it's
639       # supposed to, instead of not following it like old buggy versions did.
640       # There's absolutely no reason to turn this on.
641       "/wd4351",
642
643       # C4355: 'this': used in base member initializer list
644       # It's commonly useful to pass |this| to objects in a class' initializer
645       # list.  While this warning can catch real bugs, most of the time the
646       # constructors in question don't attempt to call methods on the passed-in
647       # pointer (until later), and annotating every legit usage of this is
648       # simply more hassle than the warning is worth.
649       "/wd4355",
650
651       # C4503: 'identifier': decorated name length exceeded, name was
652       #        truncated
653       # This only means that some long error messages might have truncated
654       # identifiers in the presence of lots of templates.  It has no effect on
655       # program correctness and there's no real reason to waste time trying to
656       # prevent it.
657       "/wd4503",
658
659       # C4611: interaction between 'function' and C++ object destruction is
660       #        non-portable
661       # This warning is unavoidable when using e.g. setjmp/longjmp.  MSDN
662       # suggests using exceptions instead of setjmp/longjmp for C++, but
663       # Chromium code compiles without exception support.  We therefore have to
664       # use setjmp/longjmp for e.g. JPEG decode error handling, which means we
665       # have to turn off this warning (and be careful about how object
666       # destruction happens in such cases).
667       "/wd4611",
668
669
670       # Warnings to evaluate and possibly fix/reenable later:
671
672       "/wd4100",  # Unreferenced formal function parameter.
673       "/wd4189",  # A variable was declared and initialized but never used.
674       "/wd4244",  # Conversion: possible loss of data.
675       "/wd4481",  # Nonstandard extension: override specifier.
676       "/wd4505",  # Unreferenced local function has been removed.
677       "/wd4510",  # Default constructor could not be generated.
678       "/wd4512",  # Assignment operator could not be generated.
679       "/wd4610",  # Class can never be instantiated, constructor required.
680     ]
681   } else {
682     # Common GCC warning setup.
683     cflags = [
684       # Enables.
685       "-Wendif-labels",  # Weird old-style text after an #endif.
686       "-Werror",  # Warnings as errors.
687
688       # Disables.
689       "-Wno-missing-field-initializers",  # "struct foo f = {0};"
690       "-Wno-unused-parameter",  # Unused function parameters.
691     ]
692     cflags_cc = []
693
694     if (is_mac) {
695       cflags += [
696         "-Wnewline-eof",
697       ]
698     }
699
700     if (is_clang) {
701       cflags += [
702         # This warns on using ints as initializers for floats in
703         # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
704         # which happens in several places in chrome code. Not sure if
705         # this is worth fixing.
706         "-Wno-c++11-narrowing",
707
708         # Don't die on dtoa code that uses a char as an array index.
709         # This is required solely for base/third_party/dmg_fp/dtoa.cc.
710         # TODO(brettw) move this to that project then!
711         "-Wno-char-subscripts",
712
713         # Warns on switches on enums that cover all enum values but
714         # also contain a default: branch. Chrome is full of that.
715         "-Wno-covered-switch-default",
716
717         # Clang considers the `register` keyword as deprecated, but e.g.
718         # code generated by flex (used in angle) contains that keyword.
719         # http://crbug.com/255186
720         "-Wno-deprecated-register",
721
722         # TODO(thakis): This used to be implied by -Wno-unused-function,
723         # which we no longer use. Check if it makes sense to remove
724         # this as well. http://crbug.com/316352
725         "-Wno-unneeded-internal-declaration",
726
727         # TODO(thakis): Remove, http://crbug.com/263960
728         "-Wno-reserved-user-defined-literal",
729
730         # TODO(hans): Clean this up. Or disable with finer granularity.
731         "-Wno-unused-local-typedef",
732       ]
733     }
734     if (gcc_version >= 48) {
735       cflags_cc += [
736         # See comment for -Wno-c++11-narrowing.
737         "-Wno-narrowing",
738         # TODO(thakis): Remove, http://crbug.com/263960
739         "-Wno-literal-suffix",
740       ]
741     }
742
743     # Suppress warnings about ABI changes on ARM (Clang doesn't give this
744     # warning).
745     if (cpu_arch == "arm" && !is_clang) {
746       cflags += [ "-Wno-psabi" ]
747     }
748
749     if (is_android) {
750       # Disable any additional warnings enabled by the Android build system but
751       # which chromium does not build cleanly with (when treating warning as
752       # errors).
753       cflags += [
754         "-Wno-extra",
755         "-Wno-ignored-qualifiers",
756         "-Wno-type-limits",
757       ]
758       cflags_cc += [
759         # Disabling c++0x-compat should be handled in WebKit, but
760         # this currently doesn't work because gcc_version is not set
761         # correctly when building with the Android build system.
762         # TODO(torne): Fix this in WebKit.
763         "-Wno-error=c++0x-compat",
764         # Other things unrelated to -Wextra:
765         "-Wno-non-virtual-dtor",
766         "-Wno-sign-promo",
767       ]
768     }
769
770     if (gcc_version >= 48) {
771       # Don't warn about the "typedef 'foo' locally defined but not used"
772       # for gcc 4.8.
773       # TODO: remove this flag once all builds work. See crbug.com/227506
774       cflags += [
775         "-Wno-unused-local-typedefs",
776       ]
777     }
778   }
779 }
780
781 # This will generate warnings when using Clang if code generates exit-time
782 # destructors, which will slow down closing the program.
783 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
784 config("wexit_time_destructors") {
785   if (is_clang) {
786     cflags = [ "-Wexit-time-destructors" ]
787   }
788 }
789
790 # Optimization -----------------------------------------------------------------
791 #
792 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
793 # which it will assign to the config it implicitly applies to every target. If
794 # you want to override the optimization level for your target, remove this
795 # config (which will expand differently for debug or release builds), and then
796 # add back the one you want to override it with:
797 #
798 #   configs -= default_optimization_config
799 #   configs += [ "//build/config/compiler/optimize_max" ]
800
801 # Shared settings for both "optimize" and "optimize_max" configs.
802 if (is_win) {
803   common_optimize_on_cflags = [
804     "/O2",
805     "/Ob2",  # both explicit and auto inlining.
806     "/Oy-",  # disable omitting frame pointers, must be after /o2.
807     "/Os",   # favor size over speed.
808   ]
809   common_optimize_on_ldflags = []
810 } else {
811   common_optimize_on_cflags = [
812     # Don't emit the GCC version ident directives, they just end up in the
813     # .comment section taking up binary size.
814     "-fno-ident",
815     # Put data and code in their own sections, so that unused symbols
816     # can be removed at link time with --gc-sections.
817     "-fdata-sections",
818     "-ffunction-sections",
819   ]
820   common_optimize_on_ldflags = []
821
822   if (is_android) {
823     common_optimize_on_cflags += [
824       "-fomit-frame-pointer",
825     ]
826     common_optimize_on_ldflags += [
827       # Warn in case of text relocations.
828       "-Wl,--warn-shared-textrel",
829     ]
830   }
831
832   if (is_mac) {
833     if (symbol_level == 2) {
834       # Mac dead code stripping requires symbols.
835       common_optimize_on_ldflags += [
836         "-Wl,-dead_strip",
837       ]
838     }
839   } else {
840     # Non-Mac Posix linker flags.
841     common_optimize_on_ldflags += [
842       # Specifically tell the linker to perform optimizations.
843       # See http://lwn.net/Articles/192624/ .
844       "-Wl,-O1",
845       "-Wl,--as-needed",
846       "-Wl,--gc-sections",
847     ]
848   }
849 }
850
851 # Default "optimization on" config. On Windows, this favors size over speed.
852 config("optimize") {
853   cflags = common_optimize_on_cflags
854   ldflags = common_optimize_on_ldflags
855   if (is_win) {
856     cflags += [
857       "/Os",   # favor size over speed.
858     ]
859   } else if (is_android || is_ios) {
860     cflags += [
861       "-Os",  # Favor size over speed.
862     ]
863   } else {
864     cflags += [
865       "-O2",
866     ]
867   }
868 }
869
870 # Turn off optimizations.
871 config("no_optimize") {
872   if (is_win) {
873     cflags = [
874       "/Od",  # Disable optimization.
875       "/Ob0",  # Disable all inlining (on by default).
876       "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
877     ]
878   } else if (is_android && !android_full_debug) {
879     # On Android we kind of optimize some things that don't affect debugging
880     # much even when optimization is disabled to get the binary size down.
881     cflags = [
882       "-Os",
883       "-fomit-frame-pointer",
884       "-fdata-sections",
885       "-ffunction-sections",
886     ]
887     ldflags = common_optimize_on_ldflags
888   } else {
889     cflags = [ "-O0" ]
890   }
891 }
892
893 # Turns up the optimization level. On Windows, this implies whole program
894 # optimization and link-time code generation which is very expensive and should
895 # be used sparingly.
896 config("optimize_max") {
897   cflags = common_optimize_on_cflags
898   ldflags = common_optimize_on_ldflags
899   if (is_win) {
900     cflags += [
901       "/Ot",   # Favor speed over size.
902       "/GL",   # Whole program optimization.
903       # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
904       # Probably anything that this would catch that wouldn't be caught in a
905       # normal build isn't going to actually be a bug, so the incremental value
906       # of C4702 for PGO builds is likely very small.
907       "/wd4702",
908     ]
909   } else {
910     cflags += [
911       "-O2",
912     ]
913   }
914 }
915
916 # Symbols ----------------------------------------------------------------------
917
918 config("symbols") {
919   if (is_win) {
920     cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
921     ldflags = [ "/DEBUG" ]
922   } else {
923     cflags = [ "-g2" ]
924     if (use_debug_fission) {
925       cflags += [ "-gsplit-dwarf" ]
926     }
927   }
928 }
929
930 config("minimal_symbols") {
931   if (is_win) {
932     # Linker symbols for backtraces only.
933     ldflags = [ "/DEBUG" ]
934   } else {
935     cflags = [ "-g1" ]
936     if (use_debug_fission) {
937       cflags += [ "-gsplit-dwarf" ]
938     }
939   }
940 }
941
942 config("no_symbols") {
943   if (!is_win) {
944     cflags = [ "-g0" ]
945   }
946 }