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