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("../android/config.gni")
6 if (cpu_arch == "arm") {
10 import("../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
20 # compiler ---------------------------------------------------------------------
22 # Base compiler configuration.
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.
35 include_dirs += [ "//", root_gen_dir ]
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
42 # Windows compiler flags setup.
43 # -----------------------------
45 "/Gy", # Enable function-level linking.
46 "/GS", # Enable buffer security checking.
47 "/FS", # Preserve previous PDB behavior.
49 if (is_component_build) {
51 "/EHsc", # Assume C functions can't throw exceptions and don't catch
52 # structured exceptions (only C++ ones).
56 # Common GCC compiler flags setup.
57 # --------------------------------
59 "-fno-strict-aliasing", # See http://crbug.com/32204
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",
71 cflags += [ "-fstack-protector-all" ]
72 } else if (is_linux) {
73 cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
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" ]
83 # Mac-specific compiler flags setup.
84 # ----------------------------------
85 if (is_mac || is_ios) {
86 # These flags are shared between the C compiler and linker.
90 if (cpu_arch == "x64") {
91 common_mac_flags += [ "-arch", "x86_64" ]
92 } else if (cpu_arch == "x86") {
93 common_mac_flags += [ "-arch", "i386" ]
96 cflags += common_mac_flags
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", ]
102 cflags_c += [ "-std=c99" ]
103 cflags_cc += [ "-std=gnu++11" ]
105 ldflags += common_mac_flags
106 } else if (is_posix) {
107 # Non-Mac Posix compiler flags setup.
108 # -----------------------------------
109 if (gcc_version >= 48) {
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") {
119 ldflags += [ "-m64" ]
120 } else if (cpu_arch == "x86") {
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) {
130 "-mfloat-abi=$arm_float_abi",
132 if (arm_tune != "") {
133 cflags += [ "-mtune=$arm_tune" ]
136 cflags += [ "-mthumb" ]
137 if (is_android && !is_clang) { # Clang doesn't support this option.
138 cflags += [ "-mthumb-interwork" ]
144 defines += [ "_FILE_OFFSET_BITS=64" ]
146 # Omit unwind support in official builds to save space. We can use breakpad
148 if (is_chrome_branded && is_official_build) {
150 "-fno-unwind-tables",
151 "-fno-asynchronous-unwind-tables",
154 cflags += [ "-funwind-tables" ]
158 # Linux/Android common flags setup.
159 # ---------------------------------
160 if (is_linux || is_android) {
163 "-pipe", # Use pipes for communicating between sub-processes. Faster.
168 "-Wl,-z,noexecstack",
174 # Linux-specific compiler flags setup.
175 # ------------------------------------
177 cflags += [ "-pthread" ]
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",
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.
192 # TODO(noelallen) Add use_gold_linker flag to enable/disable this.
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.
203 #"-Wl,--thread-count=4",
212 # Clang-specific compiler flags setup.
213 # ------------------------------------
216 "-fcolor-diagnostics",
223 # Android-specific flags setup.
224 # -----------------------------
227 "-ffunction-sections",
232 # Clang doesn't support these flags.
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",
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" ]
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" ]
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" ]
270 # Use gold for Android for most CPU architectures.
271 if (cpu_arch == "x86" || cpu_arch == "x64" || cpu_arch == "arm") {
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.
276 "-B" + rebase_path("//build/android/arm-linux-androideabi-gold",
280 ldflags += [ "-fuse-ld=gold" ]
285 "-Wl,--no-undefined",
286 # Don't export symbols from statically linked libraries.
287 "-Wl,--exclude-libs=ALL",
289 if (cpu_arch == "arm") {
291 # Enable identical code folding to reduce size.
297 if (cpu_arch == "arm") {
299 "-target arm-linux-androideabi",
301 ldflags += [ "-target arm-linux-androideabi" ]
302 } else if (cpu_arch == "x86") {
303 cflags += [ "-target x86-linux-androideabi" ]
304 ldflags += [ "-target x86-linux-androideabi" ]
310 # runtime_library -------------------------------------------------------------
312 # Sets the runtime library and associated options.
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.
320 config("runtime_library") {
327 if (is_component_build) {
328 # Component mode: dynamic CRT.
329 defines += [ "COMPONENT_BUILD" ]
331 # Since the library is shared, it requires exceptions or will give errors
332 # about things not matching, so keep exceptions on.
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" ]
356 "__STDC_CONSTANT_MACROS",
357 "__STDC_FORMAT_MACROS",
359 "_CRT_SECURE_NO_DEPRECATE",
360 "_SCL_SECURE_NO_DEPRECATE",
364 # Stlport setup. Android uses a different (smaller) version of the STL.
367 # Work around incompatibilities between bionic and clang headers.
369 "__compiler_offsetof=__builtin_offsetof",
376 "_STLP_USE_PTR_SPECIALIZATIONS=1",
377 "__GNU_SOURCE=1", # Necessary for clone().
381 "-Wl,--warn-shared-textrel",
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
391 if (use_system_stlport) {
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",
402 android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
405 "-I" + rebase_path("$android_stlport_root/stlport", root_build_dir)
407 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
409 if (component_mode == "shared_library") {
410 libs += [ "stlport_shared" ]
412 libs += [ "stlport_static" ]
416 if (cpu_arch == "mipsel") {
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.
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),
441 # chromium_code ---------------------------------------------------------------
443 # Toggles between higher and lower warnings for code that is (or isn't)
446 config("chromium_code") {
449 "/W4", # Warning level 4.
450 "/WX", # Treat warnings as errors.
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
464 # In Chromium code, we define __STDC_foo_MACROS in order to get the
465 # C99 macros on Mac and Linux.
467 "__STDC_CONSTANT_MACROS",
468 "__STDC_FORMAT_MACROS",
471 # TODO(brettw) this should also be enabled on Linux but some files
474 cflags += [ "-Wextra" ]
478 config("no_chromium_code") {
485 "/W3", # Warning level 3.
486 "/wd4800", # Disable warning when forcing value to bool.
489 "_CRT_NONSTDC_NO_WARNINGS",
490 "_CRT_NONSTDC_NO_DEPRECATE",
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" ]
502 if (is_linux || is_android) {
504 # Don't warn about printf format problems. This is off by default in gcc
505 # but on in Ubuntu's gcc(!).
509 # Don't warn about hash_map in third-party code.
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.
521 "-Wno-format-security",
523 "-Wno-sequence-point",
525 cflags_cc += [ "-Wno-non-virtual-dtor" ]
529 # rtti ------------------------------------------------------------------------
531 # Allows turning Run-Time Type Identification on or off.
535 cflags_cc = [ "/GR" ]
540 cflags_cc = [ "/GR-" ]
542 cflags_cc = [ "-fno-rtti" ]
546 # Warnings ---------------------------------------------------------------------
548 # This is where we disable various warnings that we've decided aren't
549 # worthwhile, and enable special warnings.
551 config("default_warnings") {
553 # Please keep ordered and add names if you add more.
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.
586 # Common GCC warning setup.
589 "-Wendif-labels", # Weird old-style text after an #endif.
592 "-Wno-missing-field-initializers", # "struct foo f = {0};"
593 "-Wno-unused-parameter", # Unused function parameters.
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",
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",
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",
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",
625 # Clang spots more unused functions.
626 "-Wno-unused-function",
629 if (!is_mac && !is_ios) {
631 "-Wno-reserved-user-defined-literal",
635 if (gcc_version >= 48) {
637 # See comment for -Wno-c++11-narrowing.
639 # TODO(thakis): Remove, http://crbug.com/263960
640 "-Wno-literal-suffix",
644 # Suppress warnings about ABI changes on ARM (Clang doesn't give this
646 if (cpu_arch == "arm" && !is_clang) {
647 cflags += [ "-Wno-psabi" ]
651 # Disable any additional warnings enabled by the Android build system but
652 # which chromium does not build cleanly with (when treating warning as
656 "-Wno-ignored-qualifiers",
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",
671 if (gcc_version >= 48) {
672 # Don't warn about the "typedef 'foo' locally defined but not used"
674 # TODO: remove this flag once all builds work. See crbug.com/227506
676 "-Wno-unused-local-typedefs",
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") {
687 cflags = [ "-Wexit-time-destructors" ]
691 # Optimization -----------------------------------------------------------------
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:
699 # configs -= default_optimization_config
700 # configs += [ "//native_client/build/config/compiler/optimize_max" ]
702 # Shared settings for both "optimize" and "optimize_max" configs.
704 common_optimize_on_cflags = [
706 "/Ob2", # both explicit and auto inlining.
707 "/Oy-", # disable omitting frame pointers, must be after /o2.
708 "/Os", # favor size over speed.
710 common_optimize_on_ldflags = []
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.
716 # Put data and code in their own sections, so that unused symbols
717 # can be removed at link time with --gc-sections.
719 "-ffunction-sections",
721 common_optimize_on_ldflags = []
724 common_optimize_on_cflags += [
725 "-fomit-frame-pointer",
727 common_optimize_on_ldflags += [
728 # Warn in case of text relocations.
729 "-Wl,--warn-shared-textrel",
734 if (symbol_level == 2) {
735 # Mac dead code stripping requires symbols.
736 common_optimize_on_ldflags += [
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/ .
752 # Default "optimization on" config. On Windows, this favors size over speed.
754 cflags = common_optimize_on_cflags
755 ldflags = common_optimize_on_ldflags
758 "/Os", # favor size over speed.
760 } else if (is_android || is_ios) {
762 "-Os", # Favor size over speed.
771 # Turn off optimizations.
772 config("no_optimize") {
775 "/Od", # Disable optimization.
776 "/Ob0", # Disable all inlining (on by default).
777 "/RTC1", # Runtime checks for stack frame and uninitialized variables.
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.
784 "-fomit-frame-pointer",
786 "-ffunction-sections",
788 ldflags = common_optimize_on_ldflags
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
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.
817 # Symbols ----------------------------------------------------------------------
821 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
822 ldflags = [ "/DEBUG" ]
828 config("minimal_symbols") {
830 # Linker symbols for backtraces only.
831 ldflags = [ "/DEBUG" ]
837 config("no_symbols") {