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).
41 # Common GCC compiler flags setup.
42 # --------------------------------
44 "-fno-strict-aliasing", # See http://crbug.com/32204
45 "-fvisibility=hidden",
49 "-fno-threadsafe-statics",
50 "-fvisibility-inlines-hidden",
55 cflags += [ "-fstack-protector-all" ]
56 } else if (is_linux) {
57 cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
61 # Mac-specific compiler flags setup.
62 # ----------------------------------
63 if (is_mac || is_ios) {
64 # These flags are shared between the C compiler and linker.
68 if (cpu_arch == "x64") {
69 common_mac_flags += [ "-arch x86_64" ]
70 } else if (cpu_arch == "x86") {
71 common_mac_flags += [ "-arch i386" ]
74 cflags += common_mac_flags
76 # Without this, the constructors and destructors of a C++ object inside
77 # an Objective C struct won't be called, which is very bad.
78 cflags_objcc = [ "-fobjc-call-cxx-cdtors", ]
80 cflags_c += [ "-std=c99" ]
81 cflags_cc += [ "-std=gnu++11" ]
83 ldflags += common_mac_flags + [
86 # TODO(brettW) I don't understand these options.
87 "-Wl,-rpath,@loader_path/.",
88 "-Wl,-rpath,@loader_path/../../..",
90 } else if (is_posix) {
91 # Non-Mac Posix compiler flags setup.
92 # -----------------------------------
94 # CPU architecture. We may or may not be doing a cross compile now, so for
95 # simplicity we always explicitly set the architecture.
96 if (cpu_arch == "x64") {
99 } else if (cpu_arch == "x86") {
101 ldflags += [ "-m32" ]
102 } else if (cpu_arch == "arm") {
103 # Don't set the compiler flags for the WebView build. These will come
104 # from the Android build system.
105 if (!is_android_webview_build) {
109 "-mfloat-abi=$arm_float_abi",
111 if (arm_tune != "") {
112 cflags += [ "-mtune=$arm_tune" ]
115 cflags += [ "-mthumb" ]
116 if (is_android && !is_clang) { # Clang doesn't support this option.
117 cflags += [ "-mthumb-interwork" ]
123 defines += [ "_FILE_OFFSET_BITS=64" ]
125 # Omit unwind support in official builds to save space. We can use breakpad
127 if (is_chrome_branded && is_official_build) {
129 "-fno-unwind-tables",
130 "-fno-asynchronous-unwind-tables",
133 cflags += [ "-funwind-tables" ]
137 # Linux-specific compiler flags setup.
138 # ------------------------------------
142 "-pipe", # Use pipes for communicating between sub-processes. Faster.
145 cflags += [ "-pthread" ]
148 if (cpu_arch == "x64") {
149 # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
150 # address space, and it doesn't support cross-compiling).
151 gold_path = rebase_path("//third_party/gold", ".", root_build_dir)
155 # There seems to be a conflict of --icf and -pie in gold which can
156 # generate crashy binaries. As a security measure, -pie takes
157 # precendence for now.
158 # TODO(brettw) common.gypi has this only for target toolset.
162 # Experimentation found that using four linking threads
163 # saved ~20% of link time.
164 # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
165 # Only apply this to the target linker, since the host
166 # linker might not be gold, but isn't used much anyway.
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",
244 "-mllvm -arm-enable-ehabi",
246 ldflags += [ "-target arm-linux-androideabi" ]
247 } else if (cpu_arch == "x86") {
248 cflags += [ "-target x86-linux-androideabi" ]
249 ldflags += [ "-target x86-linux-androideabi" ]
255 # runtime_library -------------------------------------------------------------
257 # Sets the runtime library and associated options.
259 # How do you determine what should go in here vs. "compiler" above? Consider if
260 # a target might choose to use a different runtime library (ignore for a moment
261 # if this is possible or reasonable on your system). If such a target would want
262 # to change or remove your option, put it in the runtime_library config. If a
263 # target wants the option regardless, put it in the compiler config.
265 config("runtime_library") {
272 if (is_component_build) {
273 # Component mode: dynamic CRT.
274 defines += [ "COMPONENT_BUILD" ]
276 # Since the library is shared, it requires exceptions or will give errors
277 # about things not matching, so keep exceptions on.
287 # We don't use exceptions, and when we link statically we can just get
288 # rid of them entirely.
289 defines += [ "_HAS_EXCEPTIONS=0" ]
301 "__STDC_CONSTANT_MACROS",
302 "__STDC_FORMAT_MACROS",
304 "_CRT_SECURE_NO_DEPRECATE",
305 "_SCL_SECURE_NO_DEPRECATE",
311 # Stlport setup. Android uses a different (smaller) version of the STL.
314 # Work around incompatibilities between bionic and clang headers.
316 "__compiler_offsetof=__builtin_offsetof",
323 "_STLP_USE_PTR_SPECIALIZATIONS=1",
324 "__GNU_SOURCE=1", # Necessary for clone().
332 # NOTE: The stlport header include paths below are specified in cflags
333 # rather than include_dirs because they need to come after include_dirs.
334 # Think of them like system headers, but don't use '-isystem' because the
335 # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
336 # strange errors. The include ordering here is important; change with
338 if (use_system_stlport) {
340 # For libstdc++/include, which is used by stlport.
341 "-I$android_src/bionic",
342 "-I$android_src/external/stlport/stlport",
348 android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
350 cflags += [ "-I$android_stlport_root/stlport" ]
351 lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
353 if (component_mode == "shared_library") {
354 libs += [ "stlport_shared" ]
356 libs += [ "stlport_static" ]
362 # chromium_code ---------------------------------------------------------------
364 # Toggles between higher and lower warnings for code that is (or isn't)
367 config("chromium_code") {
370 "/W4", # Warning level 4.
371 "/WX", # Treat warnings as errors.
378 # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
379 # so we specify it explicitly.
380 # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
381 # http://code.google.com/p/chromium/issues/detail?id=90453
385 # In Chromium code, we define __STDC_foo_MACROS in order to get the
386 # C99 macros on Mac and Linux.
388 "__STDC_CONSTANT_MACROS",
389 "__STDC_FORMAT_MACROS",
392 # TODO(brettw) this should also be enabled on Linux but some files
395 cflags += [ "-Wextra" ]
399 config("no_chromium_code") {
402 "/W3", # Warning level 3.
403 "/wd4800", # Disable warning when forcing value to bool.
406 "_CRT_NONSTDC_NO_WARNINGS",
407 "_CRT_NONSTDC_NO_DEPRECATE",
411 if (is_android_webview_build) {
412 # There is a class of warning which:
413 # 1) Android always enables and also treats as errors
414 # 2) Chromium ignores in third party code
415 # So we re-enable those warnings when building Android.
418 "-Wno-format-security",
420 "-Wno-sequence-point",
422 cflags_cc = [ "-Wno-non-virtual-dtor" ]
426 # rtti ------------------------------------------------------------------------
428 # Allows turning Run-Time Type Identification on or off.
432 cflags_cc = [ "/GR" ]
437 cflags_cc = [ "/GR-" ]
439 cflags_cc = [ "-fno-rtti" ]
443 # Warnings ---------------------------------------------------------------------
445 # This is where we disable various warnings that we've decided aren't
446 # worthwhile, and enable special warnings.
448 config("default_warnings") {
450 # Please keep ordered and add names if you add more.
452 "/wd4018", # Comparing signed and unsigned values.
453 "/wd4100", # Unreferenced formal function parameter.
454 "/wd4121", # Alignment of a member was sensitive to packing.
455 "/wd4125", # Decimal digit terminates octal escape sequence.
456 "/wd4127", # Conditional expression is constant.
457 "/wd4130", # Logical operation on address of string constant.
458 "/wd4189", # A variable was declared and initialized but never used.
459 "/wd4201", # Nonstandard extension used: nameless struct/union.
460 "/wd4238", # Nonstandard extension used: class rvalue used as lvalue.
461 "/wd4244", # Conversion: possible loss of data.
462 "/wd4245", # Conversion: signed/unsigned mismatch,
463 "/wd4251", # Class needs to have dll-interface.
464 "/wd4310", # Cast truncates constant value.
465 "/wd4351", # Elements of array will be default initialized.
466 "/wd4355", # 'this' used in base member initializer list.
467 "/wd4396", # Inline friend template thing.
468 "/wd4428", # Universal character name encountered in source.
469 "/wd4481", # Nonstandard extension: override specifier.
470 "/wd4503", # Decorated name length exceeded, name was truncated.
471 "/wd4505", # Unreferenced local function has been removed.
472 "/wd4510", # Default constructor could not be generated.
473 "/wd4512", # Assignment operator could not be generated.
474 "/wd4530", # Exception handler used, but unwind semantics not enabled.
475 "/wd4610", # Class can never be instantiated, constructor required.
476 "/wd4611", # C++ object destruction and 'catch'.
477 "/wd4701", # Potentially uninitialized local variable name used.
478 "/wd4702", # Unreachable code.
479 "/wd4706", # Assignment within conditional expression.
480 "/wd4819", # Character not in the current code page.
483 # Common GCC warning setup.
486 "-Wendif-labels", # Weird old-style text after an #endif.
489 "-Wno-missing-field-initializers", # "struct foo f = {0};"
490 "-Wno-unused-parameter", # Unused function parameters.
491 "-Wno-write-strings",
500 # TODO(brettw) Ones below here should be clang-only when we have a flag
506 # This warns on using ints as initializers for floats in
507 # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
508 # which happens in several places in chrome code. Not sure if
509 # this is worth fixing.
510 "-Wno-c++11-narrowing",
512 # Don't die on dtoa code that uses a char as an array index.
513 # This is required solely for base/third_party/dmg_fp/dtoa.cc.
514 # TODO(brettw) move this to that project then!
515 "-Wno-char-subscripts",
517 # Warns on switches on enums that cover all enum values but
518 # also contain a default: branch. Chrome is full of that.
519 "-Wno-covered-switch-default",
521 # Clang considers the `register` keyword as deprecated, but e.g.
522 # code generated by flex (used in angle) contains that keyword.
523 # http://crbug.com/255186
524 "-Wno-deprecated-register",
526 # Clang spots more unused functions.
527 "-Wno-unused-function",
529 # Warns when a const char[] is converted to bool.
530 "-Wstring-conversion",
535 # Disable any additional warnings enabled by the Android build system but
536 # which chromium does not build cleanly with (when treating warning as
540 "-Wno-ignored-qualifiers",
544 # Disabling c++0x-compat should be handled in WebKit, but
545 # this currently doesn't work because gcc_version is not set
546 # correctly when building with the Android build system.
547 # TODO(torne): Fix this in WebKit.
548 "-Wno-error=c++0x-compat",
549 # Other things unrelated to -Wextra:
550 "-Wno-non-virtual-dtor",
557 # This will generate warnings when using Clang if code generates exit-time
558 # destructors, which will slow down closing the program.
559 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
560 config("wexit_time_destructors") {
562 cflags = [ "-Wexit-time-destructors" ]
566 # Optimization -----------------------------------------------------------------
568 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
569 # which it will assign to the config it implicitly applies to every target. If
570 # you want to override the optimization level for your target, remove this
571 # config (which will expand differently for debug or release builds), and then
572 # add back the one you want to override it with:
574 # configs -= default_optimization_config
575 # configs += [ "//build/config/compiler/optimize_max" ]
577 # Default "optimization on" config. On Windows, this favors size over speed.
579 # IF YOU CHANGE THIS also consider whether optimize_max should be updated.
584 "/Ob2", # Both explicit and auto inlining.
585 "/Oy-", # Disable omitting frame pointers, must be after /O2.
586 "/Os", # Favor size over speed.
597 # Turn off optimizations.
598 config("no_optimize") {
601 "/Od", # Disable optimization.
602 "/Ob0", # Disable all inlining (on by default).
603 "/RTC1", # Runtime checks for stack frame and uninitialized variables.
610 # On Windows, turns up the optimization level. This implies whole program
611 # optimization and link-time code generation which is very expensive and should
612 # be used sparingly. For non-Windows, this is the same as "optimize".
613 config("optimize_max") {
617 "/Ob2", # Both explicit and auto inlining.
618 "/Oy-", # Disable omitting frame pointers, must be after /O2.
619 "/Ot", # Favor speed over size.
620 "/GL", # Whole program optimization.
631 # Symbols ----------------------------------------------------------------------
633 # TODO(brettw) Since this sets ldflags on Windows which is inherited across
634 # static library boundaries, if you want to remove the default symbol config
635 # and set a different one on a target, you also have to do it for all static
636 # libraries that go into that target, which is messed up. Either we need a
637 # more flexible system for defining linker flags, or we need to separate this
638 # out into a "symbols_linker" config that is only applied to DLLs and EXEs.
641 cflags = [ "/Zi" ] # Produce PDB file, no edit and continue.
642 ldflags = [ "/DEBUG" ]
648 config("minimal_symbols") {
650 # Linker symbols for backtraces only.
651 ldflags = [ "/DEBUG" ]
657 config("no_symbols") {