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
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"
25 use_gold = is_linux && cpu_arch == "x64"
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
36 # default_include_dirs ---------------------------------------------------------
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") {
48 # TODO(GYP): is_ubsan, is_ubsan_vptr
50 using_sanitizer = is_asan || is_lsan || is_tsan || is_msan
53 # compiler ---------------------------------------------------------------------
55 # Base compiler configuration.
57 # See also "runtime_library" below for related stuff and a discusison about
58 # where stuff should go. Put warning related stuff in the "warnings" config.
67 # In general, Windows is totally different, but all the other builds share
68 # some common GCC configuration. This section sets up Windows and the common
69 # GCC flags, and then we handle the other non-Windows platforms specifically
72 # Windows compiler flags setup.
73 # -----------------------------
75 "/Gy", # Enable function-level linking.
76 "/GS", # Enable buffer security checking.
77 "/FS", # Preserve previous PDB behavior.
79 if (is_component_build) {
81 "/EHsc", # Assume C functions can't throw exceptions and don't catch
82 # structured exceptions (only C++ ones).
86 # Common GCC compiler flags setup.
87 # --------------------------------
89 "-fno-strict-aliasing", # See http://crbug.com/32204
92 "-fno-threadsafe-statics",
93 # Not exporting C++ inline functions can generally be applied anywhere
94 # so we do so here. Normal function visibility is controlled by
95 # //build/config/gcc:symbol_visibility_hidden.
96 "-fvisibility-inlines-hidden",
101 cflags += [ "-fstack-protector-all" ]
102 } else if (is_linux) {
103 cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
107 if (!(is_chromeos && cpu_arch == "arm") && !is_mac) {
108 # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
109 ldflags += [ "-Wl,--fatal-warnings" ]
112 # Common options for AddressSanitizer, LeakSanitizer, ThreadSanitizer and
114 if (using_sanitizer) {
116 "-fno-omit-frame-pointer",
117 "-gline-tables-only",
121 cflags += [ "-fsanitize=address" ]
123 cflags += [ "-mllvm -asan-globals=0" ] # http://crbug.com/352073
124 # TODO(GYP): deal with mac_bundles.
129 if (is_clang && is_debug) {
130 # Allow comparing the address of references and 'this' against 0
131 # in debug builds. Technically, these can never be null in
132 # well-defined C/C++ and Clang can optimize such checks away in
133 # release builds, but they may be used in asserts in debug builds.
135 "-Wno-undefined-bool-conversion",
136 "-Wno-tautological-undefined-compare",
140 if (is_clang && !is_win) {
141 # This is here so that all files get recompiled after a clang roll and
142 # when turning clang on or off. (defines are passed via the command line,
143 # and build system rebuild things when their commandline changes). Nothing
144 # should ever read this define.
146 "CR_CLANG_REVISION=" +
148 "//tools/clang/scripts/posix-print-revision.py", [], "value")
152 # Mac-specific compiler flags setup.
153 # ----------------------------------
154 if (is_mac || is_ios) {
155 # These flags are shared between the C compiler and linker.
156 common_mac_flags = []
159 if (cpu_arch == "x64") {
160 common_mac_flags += [ "-arch", "x86_64" ]
161 } else if (cpu_arch == "x86") {
162 common_mac_flags += [ "-arch", "i386" ]
165 cflags += common_mac_flags
167 # Without this, the constructors and destructors of a C++ object inside
168 # an Objective C struct won't be called, which is very bad.
169 cflags_objcc = [ "-fobjc-call-cxx-cdtors", ]
171 cflags_c += [ "-std=c99" ]
172 cflags_cc += [ "-std=gnu++11" ]
174 ldflags += common_mac_flags
175 } else if (is_posix) {
176 # Non-Mac Posix compiler flags setup.
177 # -----------------------------------
178 if (gcc_version >= 48) {
184 # CPU architecture. We may or may not be doing a cross compile now, so for
185 # simplicity we always explicitly set the architecture.
186 if (cpu_arch == "x64") {
187 cflags += [ "-m64", "-march=x86-64", ]
188 ldflags += [ "-m64" ]
189 } else if (cpu_arch == "x86") {
191 ldflags += [ "-m32" ]
194 # Else building libyuv gives clang's register allocator issues,
195 # see llvm.org/PR15798 / crbug.com/233709
196 "-momit-leaf-frame-pointer",
197 # Align the stack on 16-byte boundaries, http://crbug.com/418554.
198 "-mstack-alignment=16",
202 } else if (cpu_arch == "arm") {
203 # Don't set the compiler flags for the WebView build. These will come
204 # from the Android build system.
205 if (!is_android_webview_build) {
208 "-mfloat-abi=$arm_float_abi",
210 if (arm_tune != "") {
211 cflags += [ "-mtune=$arm_tune" ]
214 cflags += [ "-mthumb" ]
215 if (is_android && !is_clang) { # Clang doesn't support this option.
216 cflags += [ "-mthumb-interwork" ]
220 # Clang doesn't support these flags.
222 # The tree-sra optimization (scalar replacement for
223 # aggregates enabling subsequent optimizations) leads to
224 # invalid code generation when using the Android NDK's
225 # compiler (r5-r7). This can be verified using
226 # webkit_unit_tests' WTF.Checked_int8_t test.
228 # The following option is disabled to improve binary
229 # size and performance in gcc 4.9.
236 defines += [ "_FILE_OFFSET_BITS=64" ]
238 # Omit unwind support in official builds to save space. We can use breakpad
240 if (is_chrome_branded && is_official_build) {
242 "-fno-unwind-tables",
243 "-fno-asynchronous-unwind-tables",
246 cflags += [ "-funwind-tables" ]
250 # Linux/Android common flags setup.
251 # ---------------------------------
252 if (is_linux || is_android) {
255 "-pipe", # Use pipes for communicating between sub-processes. Faster.
260 "-Wl,-z,noexecstack",
266 # Linux-specific compiler flags setup.
267 # ------------------------------------
269 cflags += [ "-pthread" ]
275 # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
276 # address space, and it doesn't support cross-compiling).
277 gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
282 # Newer gccs and clangs support -fuse-ld, use the flag to force gold
284 # gcc -- http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Optimize-Options.html
287 # TODO(brettw) common.gypi has this only for target toolset.
290 # Experimentation found that using four linking threads
291 # saved ~20% of link time.
292 # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
293 # Only apply this to the target linker, since the host
294 # linker might not be gold, but isn't used much anyway.
295 # TODO(raymes): Disable threading because gold is frequently
296 # crashing on the bots: crbug.com/161942.
298 #"-Wl,--thread-count=4",
302 if (linux_use_bundled_binutils) {
303 binutils_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
305 cflags += [ "-B$binutils_path" ]
308 # Clang-specific compiler flags setup.
309 # ------------------------------------
312 "-fcolor-diagnostics",
319 # Android-specific flags setup.
320 # -----------------------------
323 "-ffunction-sections",
328 # Clang doesn't support these flags.
333 if (is_android_webview_build) {
334 # Android predefines this as 1; undefine it here so Chromium can redefine
335 # it later to be 2 for chromium code and unset for third party code. This
336 # works because cflags are added before defines.
337 # TODO(brettw) the above comment seems incorrect. We specify defines
338 # before cflags on our compiler command lines.
339 cflags += [ "-U_FORTIFY_SOURCE" ]
343 # Android build relies on -Wl,--gc-sections removing unreachable code.
344 # ASan instrumentation for globals inhibits this and results in a library
345 # with unresolvable relocations.
346 # TODO(eugenis): find a way to reenable this.
347 cflags += [ "-mllvm -asan-globals=0" ]
350 defines += [ "ANDROID" ]
351 if (!is_android_webview_build) {
352 # The NDK has these things, but doesn't define the constants
353 # to say that it does. Define them here instead.
354 defines += [ "HAVE_SYS_UIO_H" ]
357 # Use gold for Android for most CPU architectures.
358 if (cpu_arch == "x86" || cpu_arch == "x64" || cpu_arch == "arm") {
359 ldflags += [ "-fuse-ld=gold" ]
361 # Let clang find the ld.gold in the NDK.
362 ldflags += [ "--gcc-toolchain=" + rebase_path(android_toolchain_root,
368 "-Wl,--no-undefined",
369 # Don't export symbols from statically linked libraries.
370 "-Wl,--exclude-libs=ALL",
372 if (cpu_arch == "arm") {
374 # Enable identical code folding to reduce size.
380 if (cpu_arch == "arm") {
382 "-target arm-linux-androideabi",
384 ldflags += [ "-target arm-linux-androideabi" ]
385 } else if (cpu_arch == "x86") {
386 cflags += [ "-target x86-linux-androideabi" ]
387 ldflags += [ "-target x86-linux-androideabi" ]
393 config("compiler_arm_fpu") {
394 if (cpu_arch == "arm" && !is_android_webview_build) {
401 # runtime_library -------------------------------------------------------------
403 # Sets the runtime library and associated options.
405 # How do you determine what should go in here vs. "compiler" above? Consider if
406 # a target might choose to use a different runtime library (ignore for a moment
407 # if this is possible or reasonable on your system). If such a target would want
408 # to change or remove your option, put it in the runtime_library config. If a
409 # target wants the option regardless, put it in the compiler config.
411 config("runtime_library") {
418 if (is_component_build) {
419 # Component mode: dynamic CRT.
420 defines += [ "COMPONENT_BUILD" ]
422 # Since the library is shared, it requires exceptions or will give errors
423 # about things not matching, so keep exceptions on.
433 # We don't use exceptions, and when we link statically we can just get
434 # rid of them entirely.
435 defines += [ "_HAS_EXCEPTIONS=0" ]
447 "__STDC_CONSTANT_MACROS",
448 "__STDC_FORMAT_MACROS",
450 "_CRT_SECURE_NO_DEPRECATE",
451 "_SCL_SECURE_NO_DEPRECATE",
455 # Stlport setup. Android uses a different (smaller) version of the STL.
458 # Work around incompatibilities between bionic and clang headers.
460 "__compiler_offsetof=__builtin_offsetof",
467 "_STLP_USE_PTR_SPECIALIZATIONS=1",
468 "__GNU_SOURCE=1", # Necessary for clone().
472 "-Wl,--warn-shared-textrel",
476 # NOTE: The stlport header include paths below are specified in cflags
477 # rather than include_dirs because they need to come after include_dirs.
478 # Think of them like system headers, but don't use '-isystem' because the
479 # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
480 # strange errors. The include ordering here is important; change with
482 android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
485 "-isystem" + rebase_path("$android_stlport_root/stlport",
489 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi/thumb" ]
491 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
494 if (component_mode == "shared_library") {
495 libs += [ "stlport_shared" ]
497 libs += [ "stlport_static" ]
500 if (cpu_arch == "mipsel") {
502 # ld linker is used for mips Android, and ld does not accept library
503 # absolute path prefixed by "-l"; Since libgcc does not exist in mips
504 # sysroot the proper library will be linked.
505 # TODO(gordanac): Remove once gold linker is used for mips Android.
510 # Manually link the libgcc.a that the cross compiler uses. This is
511 # absolute because the linker will look inside the sysroot if it's not.
512 rebase_path(android_libgcc_file),
525 # chromium_code ---------------------------------------------------------------
527 # Toggles between higher and lower warnings for code that is (or isn't)
530 config("chromium_code") {
533 "/W4", # Warning level 4.
539 # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
540 # so we specify it explicitly.
541 # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
542 # http://code.google.com/p/chromium/issues/detail?id=90453
546 # In Chromium code, we define __STDC_foo_MACROS in order to get the
547 # C99 macros on Mac and Linux.
549 "__STDC_CONSTANT_MACROS",
550 "__STDC_FORMAT_MACROS",
554 config("no_chromium_code") {
561 "/W3", # Warning level 3.
562 "/wd4800", # Disable warning when forcing value to bool.
563 "/wd4267", # TODO(jschuh): size_t to int.
566 "_CRT_NONSTDC_NO_WARNINGS",
567 "_CRT_NONSTDC_NO_DEPRECATE",
572 # Don't warn about ignoring the return value from e.g. close(). This is
573 # off by default in some gccs but on by default in others. BSD systems do
574 # not support this option, since they are usually using gcc 4.2.1, which
575 # does not have this flag yet.
576 cflags += [ "-Wno-unused-result" ]
579 if (is_linux || is_android) {
581 # Don't warn about printf format problems. This is off by default in gcc
582 # but on in Ubuntu's gcc(!).
586 # Don't warn about hash_map in third-party code.
591 if (is_android_webview_build) {
592 # There is a class of warning which:
593 # 1) Android always enables and also treats as errors
594 # 2) Chromium ignores in third party code
595 # So we re-enable those warnings when building Android.
598 "-Wno-format-security",
600 "-Wno-sequence-point",
602 cflags_cc += [ "-Wno-non-virtual-dtor" ]
606 # rtti ------------------------------------------------------------------------
608 # Allows turning Run-Time Type Identification on or off.
612 cflags_cc = [ "/GR" ]
617 cflags_cc = [ "/GR-" ]
619 cflags_cc = [ "-fno-rtti" ]
623 # Warnings ---------------------------------------------------------------------
625 # This is where we disable various warnings that we've decided aren't
626 # worthwhile, and enable special warnings.
628 config("default_warnings") {
631 "/WX", # Treat warnings as errors.
633 # Warnings permanently disabled:
635 # TODO(GYP) The GYP build doesn't have this globally enabled but disabled
636 # for a bunch of individual targets. Re-enable this globally when those
638 "/wd4018", # Comparing signed and unsigned values.
640 # C4127: conditional expression is constant
641 # This warning can in theory catch dead code and other problems, but
642 # triggers in far too many desirable cases where the conditional
643 # expression is either set by macros or corresponds some legitimate
644 # compile-time constant expression (due to constant template args,
645 # conditionals comparing the sizes of different types, etc.). Some of
646 # these can be worked around, but it's not worth it.
649 # C4251: 'identifier' : class 'type' needs to have dll-interface to be
650 # used by clients of class 'type2'
651 # This is necessary for the shared library build.
654 # C4351: new behavior: elements of array 'array' will be default
656 # This is a silly "warning" that basically just alerts you that the
657 # compiler is going to actually follow the language spec like it's
658 # supposed to, instead of not following it like old buggy versions did.
659 # There's absolutely no reason to turn this on.
662 # C4355: 'this': used in base member initializer list
663 # It's commonly useful to pass |this| to objects in a class' initializer
664 # list. While this warning can catch real bugs, most of the time the
665 # constructors in question don't attempt to call methods on the passed-in
666 # pointer (until later), and annotating every legit usage of this is
667 # simply more hassle than the warning is worth.
670 # C4503: 'identifier': decorated name length exceeded, name was
672 # This only means that some long error messages might have truncated
673 # identifiers in the presence of lots of templates. It has no effect on
674 # program correctness and there's no real reason to waste time trying to
678 # C4611: interaction between 'function' and C++ object destruction is
680 # This warning is unavoidable when using e.g. setjmp/longjmp. MSDN
681 # suggests using exceptions instead of setjmp/longjmp for C++, but
682 # Chromium code compiles without exception support. We therefore have to
683 # use setjmp/longjmp for e.g. JPEG decode error handling, which means we
684 # have to turn off this warning (and be careful about how object
685 # destruction happens in such cases).
689 # Warnings to evaluate and possibly fix/reenable later:
691 "/wd4100", # Unreferenced formal function parameter.
692 "/wd4189", # A variable was declared and initialized but never used.
693 "/wd4244", # Conversion: possible loss of data.
694 "/wd4481", # Nonstandard extension: override specifier.
695 "/wd4505", # Unreferenced local function has been removed.
696 "/wd4510", # Default constructor could not be generated.
697 "/wd4512", # Assignment operator could not be generated.
698 "/wd4610", # Class can never be instantiated, constructor required.
701 # Common GCC warning setup.
704 "-Wendif-labels", # Weird old-style text after an #endif.
705 "-Werror", # Warnings as errors.
708 "-Wno-missing-field-initializers", # "struct foo f = {0};"
709 "-Wno-unused-parameter", # Unused function parameters.
721 # This warns on using ints as initializers for floats in
722 # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
723 # which happens in several places in chrome code. Not sure if
724 # this is worth fixing.
725 "-Wno-c++11-narrowing",
727 # Don't die on dtoa code that uses a char as an array index.
728 # This is required solely for base/third_party/dmg_fp/dtoa.cc.
729 # TODO(brettw) move this to that project then!
730 "-Wno-char-subscripts",
732 # Warns on switches on enums that cover all enum values but
733 # also contain a default: branch. Chrome is full of that.
734 "-Wno-covered-switch-default",
736 # Clang considers the `register` keyword as deprecated, but e.g.
737 # code generated by flex (used in angle) contains that keyword.
738 # http://crbug.com/255186
739 "-Wno-deprecated-register",
741 # TODO(thakis): This used to be implied by -Wno-unused-function,
742 # which we no longer use. Check if it makes sense to remove
743 # this as well. http://crbug.com/316352
744 "-Wno-unneeded-internal-declaration",
746 # TODO(thakis): Remove, http://crbug.com/263960
747 "-Wno-reserved-user-defined-literal",
749 # TODO(hans): Clean this up. Or disable with finer granularity.
750 "-Wno-unused-local-typedef",
753 if (gcc_version >= 48) {
755 # See comment for -Wno-c++11-narrowing.
757 # TODO(thakis): Remove, http://crbug.com/263960
758 "-Wno-literal-suffix",
762 # Suppress warnings about ABI changes on ARM (Clang doesn't give this
764 if (cpu_arch == "arm" && !is_clang) {
765 cflags += [ "-Wno-psabi" ]
769 # Disable any additional warnings enabled by the Android build system but
770 # which chromium does not build cleanly with (when treating warning as
774 "-Wno-ignored-qualifiers",
778 # Disabling c++0x-compat should be handled in WebKit, but
779 # this currently doesn't work because gcc_version is not set
780 # correctly when building with the Android build system.
781 # TODO(torne): Fix this in WebKit.
782 "-Wno-error=c++0x-compat",
783 # Other things unrelated to -Wextra:
784 "-Wno-non-virtual-dtor",
789 if (gcc_version >= 48) {
790 # Don't warn about the "typedef 'foo' locally defined but not used"
792 # TODO: remove this flag once all builds work. See crbug.com/227506
794 "-Wno-unused-local-typedefs",
800 # This will generate warnings when using Clang if code generates exit-time
801 # destructors, which will slow down closing the program.
802 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
803 config("wexit_time_destructors") {
805 cflags = [ "-Wexit-time-destructors" ]
809 # Optimization -----------------------------------------------------------------
811 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
812 # which it will assign to the config it implicitly applies to every target. If
813 # you want to override the optimization level for your target, remove this
814 # config (which will expand differently for debug or release builds), and then
815 # add back the one you want to override it with:
817 # configs -= default_optimization_config
818 # configs += [ "//build/config/compiler/optimize_max" ]
820 # Shared settings for both "optimize" and "optimize_max" configs.
822 common_optimize_on_cflags = [
824 "/Ob2", # both explicit and auto inlining.
825 "/Oy-", # disable omitting frame pointers, must be after /o2.
826 "/Os", # favor size over speed.
828 common_optimize_on_ldflags = []
830 common_optimize_on_cflags = [
831 # Don't emit the GCC version ident directives, they just end up in the
832 # .comment section taking up binary size.
834 # Put data and code in their own sections, so that unused symbols
835 # can be removed at link time with --gc-sections.
837 "-ffunction-sections",
839 common_optimize_on_ldflags = []
842 if (!using_sanitizer) {
843 common_optimize_on_cflags += [ "-fomit-frame-pointer" ]
845 common_optimize_on_ldflags += [
846 # Warn in case of text relocations.
847 "-Wl,--warn-shared-textrel",
852 if (symbol_level == 2) {
853 # Mac dead code stripping requires symbols.
854 common_optimize_on_ldflags += [
859 # Non-Mac Posix linker flags.
860 common_optimize_on_ldflags += [
861 # Specifically tell the linker to perform optimizations.
862 # See http://lwn.net/Articles/192624/ .
870 # Default "optimization on" config. On Windows, this favors size over speed.
872 cflags = common_optimize_on_cflags
873 ldflags = common_optimize_on_ldflags
876 "/Os", # favor size over speed.
878 } else if (is_android || is_ios) {
880 "-Os", # Favor size over speed.
889 # Turn off optimizations.
890 config("no_optimize") {
893 "/Od", # Disable optimization.
894 "/Ob0", # Disable all inlining (on by default).
895 "/RTC1", # Runtime checks for stack frame and uninitialized variables.
897 } else if (is_android && !android_full_debug) {
898 # On Android we kind of optimize some things that don't affect debugging
899 # much even when optimization is disabled to get the binary size down.
903 "-ffunction-sections",
905 if (!using_sanitizer) {
906 cflags += [ "-fomit-frame-pointer" ]
908 ldflags = common_optimize_on_ldflags
914 # Turns up the optimization level. On Windows, this implies whole program
915 # optimization and link-time code generation which is very expensive and should
917 config("optimize_max") {
918 cflags = common_optimize_on_cflags
919 ldflags = common_optimize_on_ldflags
922 "/Ot", # Favor speed over size.
923 "/GL", # Whole program optimization.
924 # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
925 # Probably anything that this would catch that wouldn't be caught in a
926 # normal build isn't going to actually be a bug, so the incremental value
927 # of C4702 for PGO builds is likely very small.
937 # Symbols ----------------------------------------------------------------------
941 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
942 ldflags = [ "/DEBUG" ]
945 if (use_debug_fission) {
946 cflags += [ "-gsplit-dwarf" ]
951 config("minimal_symbols") {
953 # Linker symbols for backtraces only.
954 ldflags = [ "/DEBUG" ]
957 if (use_debug_fission) {
958 cflags += [ "-gsplit-dwarf" ]
963 config("no_symbols") {