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