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 # compiler ---------------------------------------------------------------------
12 # Base compiler configuration.
14 # See also "runtime_library" below for related stuff and a discusison about
15 # where stuff should go. Put warning related stuff in the "warnings" config.
25 include_dirs += [ "//", root_gen_dir ]
27 # In general, Windows is totally different, but all the other builds share
28 # some common GCC configuration. This section sets up Windows and the common
29 # GCC flags, and then we handle the other non-Windows platforms specifically
32 # Windows compiler flags setup.
33 # -----------------------------
35 "/Gy", # Enable function-level linking.
36 "/GS", # Enable buffer security checking.
37 "/EHsc", # Assume C functions can't throw exceptions and don't catch
38 # structured exceptions (only C++ ones).
39 "/FS", # Preserve previous PDB behavior.
42 # Common GCC compiler flags setup.
43 # --------------------------------
45 "-fno-strict-aliasing", # See http://crbug.com/32204
49 "-fno-threadsafe-statics",
50 # Not exporting C++ inline functions can generally be applied anywhere
51 # so we do so here. Normal function visibility is controlled by
52 # //build/config/gcc:symbol_visibility_hidden.
53 "-fvisibility-inlines-hidden",
58 cflags += [ "-fstack-protector-all" ]
59 } else if (is_linux) {
60 cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
64 # Mac-specific compiler flags setup.
65 # ----------------------------------
66 if (is_mac || is_ios) {
67 # These flags are shared between the C compiler and linker.
71 if (cpu_arch == "x64") {
72 common_mac_flags += [ "-arch x86_64" ]
73 } else if (cpu_arch == "x86") {
74 common_mac_flags += [ "-arch i386" ]
77 cflags += common_mac_flags
79 # Without this, the constructors and destructors of a C++ object inside
80 # an Objective C struct won't be called, which is very bad.
81 cflags_objcc = [ "-fobjc-call-cxx-cdtors", ]
83 cflags_c += [ "-std=c99" ]
84 cflags_cc += [ "-std=gnu++11" ]
86 ldflags += common_mac_flags
87 } else if (is_posix) {
88 # Non-Mac Posix compiler flags setup.
89 # -----------------------------------
91 # CPU architecture. We may or may not be doing a cross compile now, so for
92 # simplicity we always explicitly set the architecture.
93 if (cpu_arch == "x64") {
96 } else if (cpu_arch == "x86") {
99 } else if (cpu_arch == "arm") {
100 # Don't set the compiler flags for the WebView build. These will come
101 # from the Android build system.
102 if (!is_android_webview_build) {
106 "-mfloat-abi=$arm_float_abi",
108 if (arm_tune != "") {
109 cflags += [ "-mtune=$arm_tune" ]
112 cflags += [ "-mthumb" ]
113 if (is_android && !is_clang) { # Clang doesn't support this option.
114 cflags += [ "-mthumb-interwork" ]
120 defines += [ "_FILE_OFFSET_BITS=64" ]
122 # Omit unwind support in official builds to save space. We can use breakpad
124 if (is_chrome_branded && is_official_build) {
126 "-fno-unwind-tables",
127 "-fno-asynchronous-unwind-tables",
130 cflags += [ "-funwind-tables" ]
134 # Linux-specific compiler flags setup.
135 # ------------------------------------
139 "-pipe", # Use pipes for communicating between sub-processes. Faster.
142 cflags += [ "-pthread" ]
145 if (cpu_arch == "x64") {
146 # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
147 # address space, and it doesn't support cross-compiling).
148 gold_path = rebase_path("//third_party/binutils/Linux_x64/Release/bin",
153 # There seems to be a conflict of --icf and -pie in gold which can
154 # generate crashy binaries. As a security measure, -pie takes
155 # precendence for now.
156 # TODO(brettw) common.gypi has this only for target toolset.
160 # Experimentation found that using four linking threads
161 # saved ~20% of link time.
162 # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
163 # Only apply this to the target linker, since the host
164 # linker might not be gold, but isn't used much anyway.
165 # TODO(raymes): Disable threading because gold is frequently
166 # crashing on the bots: crbug.com/161942.
168 #"-Wl,--thread-count=4",
175 "-Wl,-z,noexecstack",
181 # Clang-specific compiler flags setup.
182 # ------------------------------------
185 "-fcolor-diagnostics",
192 # Android-specific flags setup.
193 # -----------------------------
196 "-ffunction-sections",
201 # Clang doesn't support this one.
202 cflags += [ "-finline-limit=64" ]
204 if (is_android_webview_build) {
205 # Android predefines this as 1; undefine it here so Chromium can redefine
206 # it later to be 2 for chromium code and unset for third party code. This
207 # works because cflags are added before defines.
208 # TODO(brettw) the above comment seems incorrect. We specify defines
209 # before cflags on our compiler command lines.
210 cflags += [ "-U_FORTIFY_SOURCE" ]
214 # Android build relies on -Wl,--gc-sections removing unreachable code.
215 # ASan instrumentation for globals inhibits this and results in a library
216 # with unresolvable relocations.
217 # TODO(eugenis): find a way to reenable this.
218 cflags += [ "-mllvm -asan-globals=0" ]
221 defines += [ "ANDROID" ]
222 if (!is_android_webview_build) {
223 # The NDK has these things, but doesn't define the constants
224 # to say that it does. Define them here instead.
225 defines += [ "HAVE_SYS_UIO_H" ]
229 "-Wl,--no-undefined",
230 # Don't export symbols from statically linked libraries.
231 "-Wl,--exclude-libs=ALL",
233 if (cpu_arch == "arm") {
235 # Enable identical code folding to reduce size.
241 if (cpu_arch == "arm") {
243 "-target arm-linux-androideabi",
245 ldflags += [ "-target arm-linux-androideabi" ]
246 } else if (cpu_arch == "x86") {
247 cflags += [ "-target x86-linux-androideabi" ]
248 ldflags += [ "-target x86-linux-androideabi" ]
254 # runtime_library -------------------------------------------------------------
256 # Sets the runtime library and associated options.
258 # How do you determine what should go in here vs. "compiler" above? Consider if
259 # a target might choose to use a different runtime library (ignore for a moment
260 # if this is possible or reasonable on your system). If such a target would want
261 # to change or remove your option, put it in the runtime_library config. If a
262 # target wants the option regardless, put it in the compiler config.
264 config("runtime_library") {
271 if (is_component_build) {
272 # Component mode: dynamic CRT.
273 defines += [ "COMPONENT_BUILD" ]
275 # Since the library is shared, it requires exceptions or will give errors
276 # about things not matching, so keep exceptions on.
286 # We don't use exceptions, and when we link statically we can just get
287 # rid of them entirely.
288 defines += [ "_HAS_EXCEPTIONS=0" ]
300 "__STDC_CONSTANT_MACROS",
301 "__STDC_FORMAT_MACROS",
303 "_CRT_SECURE_NO_DEPRECATE",
304 "_SCL_SECURE_NO_DEPRECATE",
308 # Stlport setup. Android uses a different (smaller) version of the STL.
311 # Work around incompatibilities between bionic and clang headers.
313 "__compiler_offsetof=__builtin_offsetof",
320 "_STLP_USE_PTR_SPECIALIZATIONS=1",
321 "__GNU_SOURCE=1", # Necessary for clone().
329 # NOTE: The stlport header include paths below are specified in cflags
330 # rather than include_dirs because they need to come after include_dirs.
331 # Think of them like system headers, but don't use '-isystem' because the
332 # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
333 # strange errors. The include ordering here is important; change with
335 if (use_system_stlport) {
337 # For libstdc++/include, which is used by stlport.
338 "-I$android_src/bionic",
339 "-I$android_src/external/stlport/stlport",
345 android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
347 cflags += [ "-I$android_stlport_root/stlport" ]
348 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
350 if (component_mode == "shared_library") {
351 libs += [ "stlport_shared" ]
353 libs += [ "stlport_static" ]
359 # chromium_code ---------------------------------------------------------------
361 # Toggles between higher and lower warnings for code that is (or isn't)
364 config("chromium_code") {
367 "/W4", # Warning level 4.
368 "/WX", # Treat warnings as errors.
375 # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
376 # so we specify it explicitly.
377 # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
378 # http://code.google.com/p/chromium/issues/detail?id=90453
382 # In Chromium code, we define __STDC_foo_MACROS in order to get the
383 # C99 macros on Mac and Linux.
385 "__STDC_CONSTANT_MACROS",
386 "__STDC_FORMAT_MACROS",
389 # TODO(brettw) this should also be enabled on Linux but some files
392 cflags += [ "-Wextra" ]
396 config("no_chromium_code") {
403 "/W3", # Warning level 3.
404 "/wd4800", # Disable warning when forcing value to bool.
407 "_CRT_NONSTDC_NO_WARNINGS",
408 "_CRT_NONSTDC_NO_DEPRECATE",
413 # Don't warn about ignoring the return value from e.g. close(). This is
414 # off by default in some gccs but on by default in others. BSD systems do
415 # not support this option, since they are usually using gcc 4.2.1, which
416 # does not have this flag yet.
417 cflags += [ "-Wno-unused-result" ]
420 if (is_linux || is_android) {
422 # Don't warn about printf format problems. This is off by default in gcc
423 # but on in Ubuntu's gcc(!).
427 # Don't warn about hash_map in third-party code.
432 if (is_android_webview_build) {
433 # There is a class of warning which:
434 # 1) Android always enables and also treats as errors
435 # 2) Chromium ignores in third party code
436 # So we re-enable those warnings when building Android.
439 "-Wno-format-security",
441 "-Wno-sequence-point",
443 cflags_cc += [ "-Wno-non-virtual-dtor" ]
447 # rtti ------------------------------------------------------------------------
449 # Allows turning Run-Time Type Identification on or off.
453 cflags_cc = [ "/GR" ]
458 cflags_cc = [ "/GR-" ]
460 cflags_cc = [ "-fno-rtti" ]
464 # Warnings ---------------------------------------------------------------------
466 # This is where we disable various warnings that we've decided aren't
467 # worthwhile, and enable special warnings.
469 config("default_warnings") {
471 # Please keep ordered and add names if you add more.
473 "/wd4018", # Comparing signed and unsigned values.
474 "/wd4100", # Unreferenced formal function parameter.
475 "/wd4121", # Alignment of a member was sensitive to packing.
476 "/wd4125", # Decimal digit terminates octal escape sequence.
477 "/wd4127", # Conditional expression is constant.
478 "/wd4130", # Logical operation on address of string constant.
479 "/wd4189", # A variable was declared and initialized but never used.
480 "/wd4201", # Nonstandard extension used: nameless struct/union.
481 "/wd4238", # Nonstandard extension used: class rvalue used as lvalue.
482 "/wd4244", # Conversion: possible loss of data.
483 "/wd4245", # Conversion: signed/unsigned mismatch,
484 "/wd4251", # Class needs to have dll-interface.
485 "/wd4310", # Cast truncates constant value.
486 "/wd4351", # Elements of array will be default initialized.
487 "/wd4355", # 'this' used in base member initializer list.
488 "/wd4396", # Inline friend template thing.
489 "/wd4428", # Universal character name encountered in source.
490 "/wd4481", # Nonstandard extension: override specifier.
491 "/wd4503", # Decorated name length exceeded, name was truncated.
492 "/wd4505", # Unreferenced local function has been removed.
493 "/wd4510", # Default constructor could not be generated.
494 "/wd4512", # Assignment operator could not be generated.
495 "/wd4530", # Exception handler used, but unwind semantics not enabled.
496 "/wd4610", # Class can never be instantiated, constructor required.
497 "/wd4611", # C++ object destruction and 'catch'.
498 "/wd4701", # Potentially uninitialized local variable name used.
499 "/wd4702", # Unreachable code.
500 "/wd4706", # Assignment within conditional expression.
501 "/wd4819", # Character not in the current code page.
504 # Common GCC warning setup.
507 "-Wendif-labels", # Weird old-style text after an #endif.
510 "-Wno-missing-field-initializers", # "struct foo f = {0};"
511 "-Wno-unused-parameter", # Unused function parameters.
522 # This warns on using ints as initializers for floats in
523 # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
524 # which happens in several places in chrome code. Not sure if
525 # this is worth fixing.
526 "-Wno-c++11-narrowing",
528 # Don't die on dtoa code that uses a char as an array index.
529 # This is required solely for base/third_party/dmg_fp/dtoa.cc.
530 # TODO(brettw) move this to that project then!
531 "-Wno-char-subscripts",
533 # Warns on switches on enums that cover all enum values but
534 # also contain a default: branch. Chrome is full of that.
535 "-Wno-covered-switch-default",
537 # Clang considers the `register` keyword as deprecated, but e.g.
538 # code generated by flex (used in angle) contains that keyword.
539 # http://crbug.com/255186
540 "-Wno-deprecated-register",
542 # Clang spots more unused functions.
543 "-Wno-unused-function",
548 # Disable any additional warnings enabled by the Android build system but
549 # which chromium does not build cleanly with (when treating warning as
553 "-Wno-ignored-qualifiers",
557 # Disabling c++0x-compat should be handled in WebKit, but
558 # this currently doesn't work because gcc_version is not set
559 # correctly when building with the Android build system.
560 # TODO(torne): Fix this in WebKit.
561 "-Wno-error=c++0x-compat",
562 # Other things unrelated to -Wextra:
563 "-Wno-non-virtual-dtor",
570 # This will generate warnings when using Clang if code generates exit-time
571 # destructors, which will slow down closing the program.
572 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
573 config("wexit_time_destructors") {
575 cflags = [ "-Wexit-time-destructors" ]
579 # Optimization -----------------------------------------------------------------
581 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
582 # which it will assign to the config it implicitly applies to every target. If
583 # you want to override the optimization level for your target, remove this
584 # config (which will expand differently for debug or release builds), and then
585 # add back the one you want to override it with:
587 # configs -= default_optimization_config
588 # configs += [ "//build/config/compiler/optimize_max" ]
590 # Default "optimization on" config. On Windows, this favors size over speed.
592 # IF YOU CHANGE THIS also consider whether optimize_max should be updated.
597 "/Ob2", # Both explicit and auto inlining.
598 "/Oy-", # Disable omitting frame pointers, must be after /O2.
599 "/Os", # Favor size over speed.
610 # Turn off optimizations.
611 config("no_optimize") {
614 "/Od", # Disable optimization.
615 "/Ob0", # Disable all inlining (on by default).
616 "/RTC1", # Runtime checks for stack frame and uninitialized variables.
623 # On Windows, turns up the optimization level. This implies whole program
624 # optimization and link-time code generation which is very expensive and should
625 # be used sparingly. For non-Windows, this is the same as "optimize".
626 config("optimize_max") {
630 "/Ob2", # Both explicit and auto inlining.
631 "/Oy-", # Disable omitting frame pointers, must be after /O2.
632 "/Ot", # Favor speed over size.
633 "/GL", # Whole program optimization.
644 # Symbols ----------------------------------------------------------------------
646 # TODO(brettw) Since this sets ldflags on Windows which is inherited across
647 # static library boundaries, if you want to remove the default symbol config
648 # and set a different one on a target, you also have to do it for all static
649 # libraries that go into that target, which is messed up. Either we need a
650 # more flexible system for defining linker flags, or we need to separate this
651 # out into a "symbols_linker" config that is only applied to DLLs and EXEs.
654 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
655 ldflags = [ "/DEBUG" ]
661 config("minimal_symbols") {
663 # Linker symbols for backtraces only.
664 ldflags = [ "/DEBUG" ]
670 config("no_symbols") {