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 # compiler ---------------------------------------------------------------------
21 # Base compiler configuration.
23 # See also "runtime_library" below for related stuff and a discusison about
24 # where stuff should go. Put warning related stuff in the "warnings" config.
34 include_dirs += [ "//", root_gen_dir ]
36 # In general, Windows is totally different, but all the other builds share
37 # some common GCC configuration. This section sets up Windows and the common
38 # GCC flags, and then we handle the other non-Windows platforms specifically
41 # Windows compiler flags setup.
42 # -----------------------------
44 "/Gy", # Enable function-level linking.
45 "/GS", # Enable buffer security checking.
46 "/FS", # Preserve previous PDB behavior.
48 if (is_component_build) {
50 "/EHsc", # Assume C functions can't throw exceptions and don't catch
51 # structured exceptions (only C++ ones).
55 # Common GCC compiler flags setup.
56 # --------------------------------
58 "-fno-strict-aliasing", # See http://crbug.com/32204
61 "-fno-threadsafe-statics",
62 # Not exporting C++ inline functions can generally be applied anywhere
63 # so we do so here. Normal function visibility is controlled by
64 # //build/config/gcc:symbol_visibility_hidden.
65 "-fvisibility-inlines-hidden",
70 cflags += [ "-fstack-protector-all" ]
71 } else if (is_linux) {
72 cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
76 if (!(is_chromeos && cpu_arch == "arm") && !is_mac) {
77 # TODO(jochen): Enable this on chromeos on arm. http://crbug.com/356580
78 ldflags += [ "-Wl,--fatal-warnings" ]
82 # Mac-specific compiler flags setup.
83 # ----------------------------------
84 if (is_mac || is_ios) {
85 # These flags are shared between the C compiler and linker.
89 if (cpu_arch == "x64") {
90 common_mac_flags += [ "-arch", "x86_64" ]
91 } else if (cpu_arch == "x86") {
92 common_mac_flags += [ "-arch", "i386" ]
95 cflags += common_mac_flags
97 # Without this, the constructors and destructors of a C++ object inside
98 # an Objective C struct won't be called, which is very bad.
99 cflags_objcc = [ "-fobjc-call-cxx-cdtors", ]
101 cflags_c += [ "-std=c99" ]
102 cflags_cc += [ "-std=gnu++11" ]
104 ldflags += common_mac_flags
105 } else if (is_posix) {
106 # Non-Mac Posix compiler flags setup.
107 # -----------------------------------
108 if (gcc_version >= 48) {
114 # CPU architecture. We may or may not be doing a cross compile now, so for
115 # simplicity we always explicitly set the architecture.
116 if (cpu_arch == "x64") {
118 ldflags += [ "-m64" ]
119 } else if (cpu_arch == "x86") {
121 ldflags += [ "-m32" ]
122 } else if (cpu_arch == "arm") {
123 # Don't set the compiler flags for the WebView build. These will come
124 # from the Android build system.
125 if (!is_android_webview_build) {
129 "-mfloat-abi=$arm_float_abi",
131 if (arm_tune != "") {
132 cflags += [ "-mtune=$arm_tune" ]
135 cflags += [ "-mthumb" ]
136 if (is_android && !is_clang) { # Clang doesn't support this option.
137 cflags += [ "-mthumb-interwork" ]
143 defines += [ "_FILE_OFFSET_BITS=64" ]
145 # Omit unwind support in official builds to save space. We can use breakpad
147 if (is_chrome_branded && is_official_build) {
149 "-fno-unwind-tables",
150 "-fno-asynchronous-unwind-tables",
153 cflags += [ "-funwind-tables" ]
157 # Linux/Android common flags setup.
158 # ---------------------------------
159 if (is_linux || is_android) {
162 "-pipe", # Use pipes for communicating between sub-processes. Faster.
167 "-Wl,-z,noexecstack",
173 # Linux-specific compiler flags setup.
174 # ------------------------------------
176 cflags += [ "-pthread" ]
178 if (cpu_arch == "x64") {
179 # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
180 # address space, and it doesn't support cross-compiling).
181 gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
186 # There seems to be a conflict of --icf and -pie in gold which can
187 # generate crashy binaries. As a security measure, -pie takes
188 # precedence for now.
189 # TODO(brettw) common.gypi has this only for target toolset.
193 # Experimentation found that using four linking threads
194 # saved ~20% of link time.
195 # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
196 # Only apply this to the target linker, since the host
197 # linker might not be gold, but isn't used much anyway.
198 # TODO(raymes): Disable threading because gold is frequently
199 # crashing on the bots: crbug.com/161942.
201 #"-Wl,--thread-count=4",
210 # Clang-specific compiler flags setup.
211 # ------------------------------------
214 "-fcolor-diagnostics",
221 # Android-specific flags setup.
222 # -----------------------------
225 "-ffunction-sections",
230 # Clang doesn't support these flags.
233 # The following 6 options are disabled to save on
234 # binary size in gcc 4.8.
235 # TODO(fdegans) Reevaluate when we upgrade GCC.
236 "-fno-partial-inlining",
237 "-fno-early-inlining",
238 "-fno-tree-copy-prop",
239 "-fno-tree-loop-optimize",
240 "-fno-move-loop-invariants",
244 if (is_android_webview_build) {
245 # Android predefines this as 1; undefine it here so Chromium can redefine
246 # it later to be 2 for chromium code and unset for third party code. This
247 # works because cflags are added before defines.
248 # TODO(brettw) the above comment seems incorrect. We specify defines
249 # before cflags on our compiler command lines.
250 cflags += [ "-U_FORTIFY_SOURCE" ]
254 # Android build relies on -Wl,--gc-sections removing unreachable code.
255 # ASan instrumentation for globals inhibits this and results in a library
256 # with unresolvable relocations.
257 # TODO(eugenis): find a way to reenable this.
258 cflags += [ "-mllvm -asan-globals=0" ]
261 defines += [ "ANDROID" ]
262 if (!is_android_webview_build) {
263 # The NDK has these things, but doesn't define the constants
264 # to say that it does. Define them here instead.
265 defines += [ "HAVE_SYS_UIO_H" ]
268 # Use gold for Android for most CPU architectures.
269 if (cpu_arch == "x86" || cpu_arch == "x64" || cpu_arch == "arm") {
271 # Clang does not support -fuse-ld to invoke the built-in gold linker,
272 # so use the -B option which requires us to specify the path.
274 "-B" + rebase_path("//build/android/arm-linux-androideabi-gold",
278 ldflags += [ "-fuse-ld=gold" ]
283 "-Wl,--no-undefined",
284 # Don't export symbols from statically linked libraries.
285 "-Wl,--exclude-libs=ALL",
287 if (cpu_arch == "arm") {
289 # Enable identical code folding to reduce size.
295 if (cpu_arch == "arm") {
297 "-target arm-linux-androideabi",
299 ldflags += [ "-target arm-linux-androideabi" ]
300 } else if (cpu_arch == "x86") {
301 cflags += [ "-target x86-linux-androideabi" ]
302 ldflags += [ "-target x86-linux-androideabi" ]
308 # runtime_library -------------------------------------------------------------
310 # Sets the runtime library and associated options.
312 # How do you determine what should go in here vs. "compiler" above? Consider if
313 # a target might choose to use a different runtime library (ignore for a moment
314 # if this is possible or reasonable on your system). If such a target would want
315 # to change or remove your option, put it in the runtime_library config. If a
316 # target wants the option regardless, put it in the compiler config.
318 config("runtime_library") {
325 if (is_component_build) {
326 # Component mode: dynamic CRT.
327 defines += [ "COMPONENT_BUILD" ]
329 # Since the library is shared, it requires exceptions or will give errors
330 # about things not matching, so keep exceptions on.
340 # We don't use exceptions, and when we link statically we can just get
341 # rid of them entirely.
342 defines += [ "_HAS_EXCEPTIONS=0" ]
354 "__STDC_CONSTANT_MACROS",
355 "__STDC_FORMAT_MACROS",
357 "_CRT_SECURE_NO_DEPRECATE",
358 "_SCL_SECURE_NO_DEPRECATE",
362 # Stlport setup. Android uses a different (smaller) version of the STL.
365 # Work around incompatibilities between bionic and clang headers.
367 "__compiler_offsetof=__builtin_offsetof",
374 "_STLP_USE_PTR_SPECIALIZATIONS=1",
375 "__GNU_SOURCE=1", # Necessary for clone().
379 "-Wl,--warn-shared-textrel",
383 # NOTE: The stlport header include paths below are specified in cflags
384 # rather than include_dirs because they need to come after include_dirs.
385 # Think of them like system headers, but don't use '-isystem' because the
386 # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
387 # strange errors. The include ordering here is important; change with
389 if (use_system_stlport) {
391 # For libstdc++/include, which is used by stlport.
392 "-I" + rebase_path("$android_src/bionic", root_build_dir),
393 "-I" + rebase_path("$android_src/external/stlport/stlport",
400 android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
403 "-I" + rebase_path("$android_stlport_root/stlport", root_build_dir)
405 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
407 if (component_mode == "shared_library") {
408 libs += [ "stlport_shared" ]
410 libs += [ "stlport_static" ]
414 if (cpu_arch == "mipsel") {
416 # ld linker is used for mips Android, and ld does not accept library
417 # absolute path prefixed by "-l"; Since libgcc does not exist in mips
418 # sysroot the proper library will be linked.
419 # TODO(gordanac): Remove once gold linker is used for mips Android.
424 # Manually link the libgcc.a that the cross compiler uses. This is
425 # absolute because the linker will look inside the sysroot if it's not.
426 rebase_path(android_libgcc_file),
439 # chromium_code ---------------------------------------------------------------
441 # Toggles between higher and lower warnings for code that is (or isn't)
444 config("chromium_code") {
447 "/W4", # Warning level 4.
448 "/WX", # Treat warnings as errors.
455 # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
456 # so we specify it explicitly.
457 # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
458 # http://code.google.com/p/chromium/issues/detail?id=90453
462 # In Chromium code, we define __STDC_foo_MACROS in order to get the
463 # C99 macros on Mac and Linux.
465 "__STDC_CONSTANT_MACROS",
466 "__STDC_FORMAT_MACROS",
469 # TODO(brettw) this should also be enabled on Linux but some files
472 cflags += [ "-Wextra" ]
476 config("no_chromium_code") {
483 "/W3", # Warning level 3.
484 "/wd4800", # Disable warning when forcing value to bool.
487 "_CRT_NONSTDC_NO_WARNINGS",
488 "_CRT_NONSTDC_NO_DEPRECATE",
493 # Don't warn about ignoring the return value from e.g. close(). This is
494 # off by default in some gccs but on by default in others. BSD systems do
495 # not support this option, since they are usually using gcc 4.2.1, which
496 # does not have this flag yet.
497 cflags += [ "-Wno-unused-result" ]
500 if (is_linux || is_android) {
502 # Don't warn about printf format problems. This is off by default in gcc
503 # but on in Ubuntu's gcc(!).
507 # Don't warn about hash_map in third-party code.
512 if (is_android_webview_build) {
513 # There is a class of warning which:
514 # 1) Android always enables and also treats as errors
515 # 2) Chromium ignores in third party code
516 # So we re-enable those warnings when building Android.
519 "-Wno-format-security",
521 "-Wno-sequence-point",
523 cflags_cc += [ "-Wno-non-virtual-dtor" ]
527 # rtti ------------------------------------------------------------------------
529 # Allows turning Run-Time Type Identification on or off.
533 cflags_cc = [ "/GR" ]
538 cflags_cc = [ "/GR-" ]
540 cflags_cc = [ "-fno-rtti" ]
544 # Warnings ---------------------------------------------------------------------
546 # This is where we disable various warnings that we've decided aren't
547 # worthwhile, and enable special warnings.
549 config("default_warnings") {
551 # Please keep ordered and add names if you add more.
553 "/wd4018", # Comparing signed and unsigned values.
554 "/wd4100", # Unreferenced formal function parameter.
555 "/wd4121", # Alignment of a member was sensitive to packing.
556 "/wd4125", # Decimal digit terminates octal escape sequence.
557 "/wd4127", # Conditional expression is constant.
558 "/wd4130", # Logical operation on address of string constant.
559 "/wd4189", # A variable was declared and initialized but never used.
560 "/wd4201", # Nonstandard extension used: nameless struct/union.
561 "/wd4238", # Nonstandard extension used: class rvalue used as lvalue.
562 "/wd4244", # Conversion: possible loss of data.
563 "/wd4245", # Conversion: signed/unsigned mismatch,
564 "/wd4251", # Class needs to have dll-interface.
565 "/wd4310", # Cast truncates constant value.
566 "/wd4351", # Elements of array will be default initialized.
567 "/wd4355", # 'this' used in base member initializer list.
568 "/wd4396", # Inline friend template thing.
569 "/wd4428", # Universal character name encountered in source.
570 "/wd4481", # Nonstandard extension: override specifier.
571 "/wd4503", # Decorated name length exceeded, name was truncated.
572 "/wd4505", # Unreferenced local function has been removed.
573 "/wd4510", # Default constructor could not be generated.
574 "/wd4512", # Assignment operator could not be generated.
575 "/wd4530", # Exception handler used, but unwind semantics not enabled.
576 "/wd4610", # Class can never be instantiated, constructor required.
577 "/wd4611", # C++ object destruction and 'catch'.
578 "/wd4701", # Potentially uninitialized local variable name used.
579 "/wd4702", # Unreachable code.
580 "/wd4706", # Assignment within conditional expression.
581 "/wd4819", # Character not in the current code page.
584 # Common GCC warning setup.
587 "-Wendif-labels", # Weird old-style text after an #endif.
590 "-Wno-missing-field-initializers", # "struct foo f = {0};"
591 "-Wno-unused-parameter", # Unused function parameters.
603 # This warns on using ints as initializers for floats in
604 # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
605 # which happens in several places in chrome code. Not sure if
606 # this is worth fixing.
607 "-Wno-c++11-narrowing",
609 # Don't die on dtoa code that uses a char as an array index.
610 # This is required solely for base/third_party/dmg_fp/dtoa.cc.
611 # TODO(brettw) move this to that project then!
612 "-Wno-char-subscripts",
614 # Warns on switches on enums that cover all enum values but
615 # also contain a default: branch. Chrome is full of that.
616 "-Wno-covered-switch-default",
618 # Clang considers the `register` keyword as deprecated, but e.g.
619 # code generated by flex (used in angle) contains that keyword.
620 # http://crbug.com/255186
621 "-Wno-deprecated-register",
623 # Clang spots more unused functions.
624 "-Wno-unused-function",
627 if (!is_mac && !is_ios) {
629 "-Wno-reserved-user-defined-literal",
633 if (gcc_version >= 48) {
635 # See comment for -Wno-c++11-narrowing.
637 # TODO(thakis): Remove, http://crbug.com/263960
638 "-Wno-literal-suffix",
642 # Suppress warnings about ABI changes on ARM (Clang doesn't give this
644 if (cpu_arch == "arm" && !is_clang) {
645 cflags += [ "-Wno-psabi" ]
649 # Disable any additional warnings enabled by the Android build system but
650 # which chromium does not build cleanly with (when treating warning as
654 "-Wno-ignored-qualifiers",
658 # Disabling c++0x-compat should be handled in WebKit, but
659 # this currently doesn't work because gcc_version is not set
660 # correctly when building with the Android build system.
661 # TODO(torne): Fix this in WebKit.
662 "-Wno-error=c++0x-compat",
663 # Other things unrelated to -Wextra:
664 "-Wno-non-virtual-dtor",
669 if (gcc_version >= 48) {
670 # Don't warn about the "typedef 'foo' locally defined but not used"
672 # TODO: remove this flag once all builds work. See crbug.com/227506
674 "-Wno-unused-local-typedefs",
680 # This will generate warnings when using Clang if code generates exit-time
681 # destructors, which will slow down closing the program.
682 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
683 config("wexit_time_destructors") {
685 cflags = [ "-Wexit-time-destructors" ]
689 # Optimization -----------------------------------------------------------------
691 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
692 # which it will assign to the config it implicitly applies to every target. If
693 # you want to override the optimization level for your target, remove this
694 # config (which will expand differently for debug or release builds), and then
695 # add back the one you want to override it with:
697 # configs -= default_optimization_config
698 # configs += [ "//build/config/compiler/optimize_max" ]
700 # Shared settings for both "optimize" and "optimize_max" configs.
702 common_optimize_on_cflags = [
704 "/Ob2", # both explicit and auto inlining.
705 "/Oy-", # disable omitting frame pointers, must be after /o2.
706 "/Os", # favor size over speed.
708 common_optimize_on_ldflags = []
710 common_optimize_on_cflags = [
711 # Don't emit the GCC version ident directives, they just end up in the
712 # .comment section taking up binary size.
714 # Put data and code in their own sections, so that unused symbols
715 # can be removed at link time with --gc-sections.
717 "-ffunction-sections",
719 common_optimize_on_ldflags = []
722 common_optimize_on_cflags += [
723 "-fomit-frame-pointer",
725 common_optimize_on_ldflags += [
726 # Warn in case of text relocations.
727 "-Wl,--warn-shared-textrel",
732 if (symbol_level == 2) {
733 # Mac dead code stripping requires symbols.
734 common_optimize_on_ldflags += [
739 # Non-Mac Posix linker flags.
740 common_optimize_on_ldflags += [
741 # Specifically tell the linker to perform optimizations.
742 # See http://lwn.net/Articles/192624/ .
750 # Default "optimization on" config. On Windows, this favors size over speed.
752 cflags = common_optimize_on_cflags
753 ldflags = common_optimize_on_ldflags
756 "/Os", # favor size over speed.
758 } else if (is_android || is_ios) {
760 "-Os", # Favor size over speed.
769 # Turn off optimizations.
770 config("no_optimize") {
773 "/Od", # Disable optimization.
774 "/Ob0", # Disable all inlining (on by default).
775 "/RTC1", # Runtime checks for stack frame and uninitialized variables.
777 } else if (is_android && !android_full_debug) {
778 # On Android we kind of optimize some things that don't affect debugging
779 # much even when optimization is disabled to get the binary size down.
782 "-fomit-frame-pointer",
784 "-ffunction-sections",
786 ldflags = common_optimize_on_ldflags
792 # On Windows, turns up the optimization level. This implies whole program
793 # optimization and link-time code generation which is very expensive and should
794 # be used sparingly. For non-Windows, this is the same as "optimize".
795 config("optimize_max") {
796 cflags = common_optimize_on_cflags
797 ldflags = common_optimize_on_ldflags
800 "/Ot", # Favor speed over size.
801 "/GL", # Whole program optimization.
802 # Disable Warning 4702 ("Unreachable code") for the WPO/PGO builds.
803 # Probably anything that this would catch that wouldn't be caught in a
804 # normal build isn't going to actually be a bug, so the incremental value
805 # of C4702 for PGO builds is likely very small.
815 # Symbols ----------------------------------------------------------------------
819 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
820 ldflags = [ "/DEBUG" ]
826 config("minimal_symbols") {
828 # Linker symbols for backtraces only.
829 ldflags = [ "/DEBUG" ]
835 config("no_symbols") {