1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import("//build/config/android/config.gni")
6 if (cpu_arch == "arm") {
7 import("//build/config/arm.gni")
10 import("//build/config/gcc/gcc_version.gni")
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
19 # default_include_dirs ---------------------------------------------------------
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") {
31 # compiler ---------------------------------------------------------------------
33 # Base compiler configuration.
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.
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
50 # Windows compiler flags setup.
51 # -----------------------------
53 "/Gy", # Enable function-level linking.
54 "/GS", # Enable buffer security checking.
55 "/FS", # Preserve previous PDB behavior.
57 if (is_component_build) {
59 "/EHsc", # Assume C functions can't throw exceptions and don't catch
60 # structured exceptions (only C++ ones).
64 # Common GCC compiler flags setup.
65 # --------------------------------
67 "-fno-strict-aliasing", # See http://crbug.com/32204
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",
79 cflags += [ "-fstack-protector-all" ]
80 } else if (is_linux) {
81 cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
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" ]
91 # Mac-specific compiler flags setup.
92 # ----------------------------------
93 if (is_mac || is_ios) {
94 # These flags are shared between the C compiler and linker.
98 if (cpu_arch == "x64") {
99 common_mac_flags += [ "-arch", "x86_64" ]
100 } else if (cpu_arch == "x86") {
101 common_mac_flags += [ "-arch", "i386" ]
104 cflags += common_mac_flags
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", ]
110 cflags_c += [ "-std=c99" ]
111 cflags_cc += [ "-std=gnu++11" ]
113 ldflags += common_mac_flags
114 } else if (is_posix) {
115 # Non-Mac Posix compiler flags setup.
116 # -----------------------------------
117 if (gcc_version >= 48) {
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") {
127 ldflags += [ "-m64" ]
128 } else if (cpu_arch == "x86") {
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) {
137 "-mfloat-abi=$arm_float_abi",
139 if (arm_tune != "") {
140 cflags += [ "-mtune=$arm_tune" ]
143 cflags += [ "-mthumb" ]
144 if (is_android && !is_clang) { # Clang doesn't support this option.
145 cflags += [ "-mthumb-interwork" ]
151 defines += [ "_FILE_OFFSET_BITS=64" ]
153 # Omit unwind support in official builds to save space. We can use breakpad
155 if (is_chrome_branded && is_official_build) {
157 "-fno-unwind-tables",
158 "-fno-asynchronous-unwind-tables",
161 cflags += [ "-funwind-tables" ]
165 # Linux/Android common flags setup.
166 # ---------------------------------
167 if (is_linux || is_android) {
170 "-pipe", # Use pipes for communicating between sub-processes. Faster.
175 "-Wl,-z,noexecstack",
181 # Linux-specific compiler flags setup.
182 # ------------------------------------
184 cflags += [ "-pthread" ]
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",
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.
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.
209 #"-Wl,--thread-count=4",
218 # Clang-specific compiler flags setup.
219 # ------------------------------------
222 "-fcolor-diagnostics",
229 # Android-specific flags setup.
230 # -----------------------------
233 "-ffunction-sections",
238 # Clang doesn't support these flags.
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",
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" ]
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" ]
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" ]
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" ]
280 # Let clang find the ld.gold in the NDK.
281 ldflags += [ "--gcc-toolchain=" + rebase_path(android_toolchain_root,
287 "-Wl,--no-undefined",
288 # Don't export symbols from statically linked libraries.
289 "-Wl,--exclude-libs=ALL",
291 if (cpu_arch == "arm") {
293 # Enable identical code folding to reduce size.
299 if (cpu_arch == "arm") {
301 "-target arm-linux-androideabi",
303 ldflags += [ "-target arm-linux-androideabi" ]
304 } else if (cpu_arch == "x86") {
305 cflags += [ "-target x86-linux-androideabi" ]
306 ldflags += [ "-target x86-linux-androideabi" ]
312 config("compiler_arm_fpu") {
313 if (cpu_arch == "arm" && !is_android_webview_build) {
320 # runtime_library -------------------------------------------------------------
322 # Sets the runtime library and associated options.
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.
330 config("runtime_library") {
337 if (is_component_build) {
338 # Component mode: dynamic CRT.
339 defines += [ "COMPONENT_BUILD" ]
341 # Since the library is shared, it requires exceptions or will give errors
342 # about things not matching, so keep exceptions on.
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" ]
366 "__STDC_CONSTANT_MACROS",
367 "__STDC_FORMAT_MACROS",
369 "_CRT_SECURE_NO_DEPRECATE",
370 "_SCL_SECURE_NO_DEPRECATE",
374 # Stlport setup. Android uses a different (smaller) version of the STL.
377 # Work around incompatibilities between bionic and clang headers.
379 "__compiler_offsetof=__builtin_offsetof",
386 "_STLP_USE_PTR_SPECIALIZATIONS=1",
387 "__GNU_SOURCE=1", # Necessary for clone().
391 "-Wl,--warn-shared-textrel",
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
401 if (use_system_stlport) {
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",
412 android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
415 "-isystem" + rebase_path("$android_stlport_root/stlport",
418 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
420 if (component_mode == "shared_library") {
421 libs += [ "stlport_shared" ]
423 libs += [ "stlport_static" ]
427 if (cpu_arch == "mipsel") {
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.
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),
452 # chromium_code ---------------------------------------------------------------
454 # Toggles between higher and lower warnings for code that is (or isn't)
457 # TODO: -Werror should always be on, independent of chromium_code
458 # http://crbug.com/393046
459 config("chromium_code") {
462 "/W4", # Warning level 4.
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
475 # TODO: Add this unconditionally once linux builds without warnings with
476 # clang in the gn build.
477 cflags += [ "-Werror" ]
480 # In Chromium code, we define __STDC_foo_MACROS in order to get the
481 # C99 macros on Mac and Linux.
483 "__STDC_CONSTANT_MACROS",
484 "__STDC_FORMAT_MACROS",
487 # TODO(brettw) this should also be enabled on Linux but some files
490 cflags += [ "-Wextra" ]
494 config("no_chromium_code") {
501 "/W3", # Warning level 3.
502 "/wd4800", # Disable warning when forcing value to bool.
505 "_CRT_NONSTDC_NO_WARNINGS",
506 "_CRT_NONSTDC_NO_DEPRECATE",
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" ]
518 if (is_linux || is_android) {
520 # Don't warn about printf format problems. This is off by default in gcc
521 # but on in Ubuntu's gcc(!).
525 # Don't warn about hash_map in third-party code.
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.
537 "-Wno-format-security",
539 "-Wno-sequence-point",
541 cflags_cc += [ "-Wno-non-virtual-dtor" ]
545 # rtti ------------------------------------------------------------------------
547 # Allows turning Run-Time Type Identification on or off.
551 cflags_cc = [ "/GR" ]
556 cflags_cc = [ "/GR-" ]
558 cflags_cc = [ "-fno-rtti" ]
562 # Warnings ---------------------------------------------------------------------
564 # This is where we disable various warnings that we've decided aren't
565 # worthwhile, and enable special warnings.
567 config("default_warnings") {
570 "/WX", # Treat warnings as errors.
572 # Warnings permanently disabled:
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
577 "/wd4018", # Comparing signed and unsigned values.
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.
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.
593 # C4351: new behavior: elements of array 'array' will be default
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.
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.
609 # C4503: 'identifier': decorated name length exceeded, name was
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
617 # C4611: interaction between 'function' and C++ object destruction is
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).
628 # Warnings to evaluate and possibly fix/reenable later:
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.
640 # Common GCC warning setup.
643 "-Wendif-labels", # Weird old-style text after an #endif.
646 "-Wno-missing-field-initializers", # "struct foo f = {0};"
647 "-Wno-unused-parameter", # Unused function parameters.
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",
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",
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",
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",
679 # Clang spots more unused functions.
680 "-Wno-unused-function",
683 if (!is_mac && !is_ios) {
685 "-Wno-reserved-user-defined-literal",
689 if (gcc_version >= 48) {
691 # See comment for -Wno-c++11-narrowing.
693 # TODO(thakis): Remove, http://crbug.com/263960
694 "-Wno-literal-suffix",
698 # Suppress warnings about ABI changes on ARM (Clang doesn't give this
700 if (cpu_arch == "arm" && !is_clang) {
701 cflags += [ "-Wno-psabi" ]
705 # Disable any additional warnings enabled by the Android build system but
706 # which chromium does not build cleanly with (when treating warning as
710 "-Wno-ignored-qualifiers",
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",
725 if (gcc_version >= 48) {
726 # Don't warn about the "typedef 'foo' locally defined but not used"
728 # TODO: remove this flag once all builds work. See crbug.com/227506
730 "-Wno-unused-local-typedefs",
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") {
741 cflags = [ "-Wexit-time-destructors" ]
745 # Optimization -----------------------------------------------------------------
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:
753 # configs -= default_optimization_config
754 # configs += [ "//build/config/compiler/optimize_max" ]
756 # Shared settings for both "optimize" and "optimize_max" configs.
758 common_optimize_on_cflags = [
760 "/Ob2", # both explicit and auto inlining.
761 "/Oy-", # disable omitting frame pointers, must be after /o2.
762 "/Os", # favor size over speed.
764 common_optimize_on_ldflags = []
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.
770 # Put data and code in their own sections, so that unused symbols
771 # can be removed at link time with --gc-sections.
773 "-ffunction-sections",
775 common_optimize_on_ldflags = []
778 common_optimize_on_cflags += [
779 "-fomit-frame-pointer",
781 common_optimize_on_ldflags += [
782 # Warn in case of text relocations.
783 "-Wl,--warn-shared-textrel",
788 if (symbol_level == 2) {
789 # Mac dead code stripping requires symbols.
790 common_optimize_on_ldflags += [
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/ .
806 # Default "optimization on" config. On Windows, this favors size over speed.
808 cflags = common_optimize_on_cflags
809 ldflags = common_optimize_on_ldflags
812 "/Os", # favor size over speed.
814 } else if (is_android || is_ios) {
816 "-Os", # Favor size over speed.
825 # Turn off optimizations.
826 config("no_optimize") {
829 "/Od", # Disable optimization.
830 "/Ob0", # Disable all inlining (on by default).
831 "/RTC1", # Runtime checks for stack frame and uninitialized variables.
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.
838 "-fomit-frame-pointer",
840 "-ffunction-sections",
842 ldflags = common_optimize_on_ldflags
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
851 config("optimize_max") {
852 cflags = common_optimize_on_cflags
853 ldflags = common_optimize_on_ldflags
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.
871 # Symbols ----------------------------------------------------------------------
875 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
876 ldflags = [ "/DEBUG" ]
882 config("minimal_symbols") {
884 # Linker symbols for backtraces only.
885 ldflags = [ "/DEBUG" ]
891 config("no_symbols") {