Home | History | Annotate | Download | only in compiler
      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.
      4 
      5 import("//build/config/linux/sysroot.gni")
      6 
      7 # Base compiler configuration.
      8 config("compiler") {
      9   include_dirs = [ "//", root_gen_dir ]
     10   if (is_win) {
     11     cflags = [
     12       "/Gy",  # Enable function-level linking.
     13       "/GS",  # Enable buffer security checking.
     14       "/EHsc",  # Assume C functions can't throw exceptions and don't catch
     15                 # structured exceptions (only C++ ones).
     16     ]
     17   } else {
     18     # Common GCC compiler flags setup.
     19     # --------------------------------
     20     cflags = [
     21       "-fno-strict-aliasing",  # See http://crbug.com/32204
     22       "-fvisibility=hidden",
     23     ]
     24     cflags_c = [
     25     ]
     26     cflags_cc = [
     27       "-fno-exceptions",
     28       "-fno-threadsafe-statics",
     29       "-fvisibility-inlines-hidden",
     30     ]
     31     ldflags = [
     32     ]
     33 
     34     # Stack protection.
     35     # TODO(brettw) why do we have different values for all of these cases?
     36     if (is_mac) {
     37       cflags += "-fstack-protector-all"
     38     } else if (is_chromeos) {
     39       cflags += "-fstack-protector-strong"
     40     } else if (is_linux) {
     41       cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
     42     }
     43 
     44     if (is_mac) {
     45       # Mac-specific compiler flags setup.
     46       # ----------------------------------
     47 
     48       # These flags are shared between the C compiler and linker.
     49       common_mac_flags = [
     50         "-isysroot", sysroot,
     51         "-mmacosx-version-min=10.6",
     52       ]
     53 
     54       # CPU architecture.
     55       if (cpu_arch == "x64") {
     56         common_mac_flags += "-arch x86_64"
     57       } else if (cpu_arch == "x32") {
     58         common_mac_flags += "-arch i386"
     59       }
     60 
     61       cflags += common_mac_flags + [
     62         # Without this, the constructors and destructors of a C++ object inside
     63         # an Objective C struct won't be called, which is very bad.
     64         "-fobjc-call-cxx-cdtors",
     65       ]
     66 
     67       cflags_c += [ "-std=c99" ]
     68       cflags_cc += [ "-std=gnu++11" ]
     69 
     70       ldflags += common_mac_flags + [
     71         "-L.",
     72 
     73         # TODO(brettW) I don't understand these options.
     74         "-Wl,-rpath,@loader_path/.",
     75         "-Wl,-rpath,@loader_path/../../..",
     76       ]
     77     } else {
     78       # Non-Mac Posix compiler flags setup.
     79       # -----------------------------------
     80 
     81       # CPU architecture. We may or may not be doing a cross compile now, so for
     82       # simplicity we always explicitly set the architecture.
     83       if (cpu_arch == "x64") {
     84         cflags += "-m64"
     85         ldflags += "-m64"
     86       } else if (cpu_arch == "x32") {
     87         cflags += "-m32"
     88         ldflags += "-m32"
     89       }
     90     }
     91 
     92     # Linux-specific compiler flags setup.
     93     # ------------------------------------
     94     if (is_linux) {
     95       cflags += [
     96         "-fPIC",
     97         "-pthread",
     98         "-pipe",  # Use pipes for communicating between sub-processes. Faster.
     99       ]
    100 
    101       # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
    102       # address space, and it doesn't support cross-compiling).
    103       if (cpu_arch == "x64") {
    104         gold_path = rebase_path("//third_party/gold", ".", root_build_dir)
    105         ldflags += [
    106           "-B$gold_path",
    107 
    108           # There seems to be a conflict of --icf and -pie in gold which can
    109           # generate crashy binaries. As a security measure, -pie takes
    110           # precendence for now.
    111           # TODO(brettw) common.gypi has this only for target toolset.
    112           #"-Wl,--icf=safe",
    113           "-Wl,--icf=none",
    114 
    115           # Experimentation found that using four linking threads
    116           # saved ~20% of link time.
    117           # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
    118           # Only apply this to the target linker, since the host
    119           # linker might not be gold, but isn't used much anyway.
    120           "-Wl,--threads",
    121           "-Wl,--thread-count=4",
    122         ]
    123       }
    124 
    125       if (sysroot != "") {
    126         cflags += "--sysroot=" + sysroot
    127         ldflags += "--sysroot=" + sysroot
    128       }
    129 
    130       ldflags += [
    131         "-fPIC",
    132         "-pthread",
    133         "-Wl,-z,noexecstack",
    134         "-Wl,-z,now",
    135         "-Wl,-z,relro",
    136       ]
    137     }
    138 
    139     # Clang-specific compiler flags setup.
    140     # ------------------------------------
    141     if (is_clang) {
    142       cflags += [
    143         "-fcolor-diagnostics",
    144       ]
    145     }
    146   }
    147 }
    148 
    149 # runtime_library -------------------------------------------------------------
    150 #
    151 # Sets the runtime library and associated options.
    152 #
    153 # We don't bother making multiple versions that are toggle-able since there
    154 # is more than one axis of control (which makes it complicated) and there's
    155 # no practical reason for anybody to change this since the CRT must agree.
    156 
    157 config("runtime_library") {
    158   if (is_component_build) {
    159     # Component mode: dynamic CRT.
    160     defines = [ "COMPONENT_BUILD" ]
    161     if (is_win) {
    162       # Since the library is shared, it requires exceptions or will give errors
    163       # about things not matching, so keep exceptions on.
    164       if (is_debug) {
    165         cflags = [ "/MDd" ]
    166       } else {
    167         cflags = [ "/MD" ]
    168       }
    169     }
    170   } else {
    171     # Static CRT.
    172     if (is_win) {
    173       # We don't use exceptions, and when we link statically we can just get
    174       # rid of them entirely.
    175       defines = [ "_HAS_EXCEPTIONS=0" ]
    176       if (is_debug) {
    177         cflags = [ "/MTd" ]
    178       } else {
    179         cflags = [ "/MT" ]
    180       }
    181     }
    182   }
    183 
    184   if (is_win) {
    185     defines += [
    186       "__STD_C",
    187       "__STDC_CONSTANT_MACROS",
    188       "__STDC_FORMAT_MACROS",
    189       "_CRT_RAND_S",
    190       "_CRT_SECURE_NO_DEPRECATE",
    191       "_SCL_SECURE_NO_DEPRECATE",
    192       "_UNICODE",
    193       "UNICODE",
    194     ]
    195   }
    196 }
    197 
    198 # chromium_code ---------------------------------------------------------------
    199 #
    200 # Toggles between higher and lower warnings for code that is (or isn't)
    201 # part of Chromium.
    202 
    203 config("chromium_code") {
    204   if (is_win) {
    205     cflags = [
    206       "/W4",  # Warning level 4.
    207       "/WX",  # Treat warnings as errors.
    208     ]
    209   } else {
    210     cflags = [
    211       "-Wall",
    212       "-Werror",
    213 
    214       # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
    215       # so we specify it explicitly.
    216       # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
    217       # http://code.google.com/p/chromium/issues/detail?id=90453
    218       "-Wsign-compare",
    219     ]
    220 
    221     # In Chromium code, we define __STDC_foo_MACROS in order to get the
    222     # C99 macros on Mac and Linux.
    223     defines = [
    224       "__STDC_CONSTANT_MACROS",
    225       "__STDC_FORMAT_MACROS",
    226     ]
    227 
    228     # TODO(brettw) this should also be enabled on Linux but some files
    229     # currently fail.
    230     if (is_mac) {
    231       cflags += "-Wextra"
    232     }
    233   }
    234 }
    235 config("no_chromium_code") {
    236   if (is_win) {
    237     cflags = [
    238       "/W3",  # Warning level 3.
    239       "/wd4800",  # Disable warning when forcing value to bool.
    240     ]
    241     defines = [
    242       "_CRT_NONSTDC_NO_WARNINGS",
    243       "_CRT_NONSTDC_NO_DEPRECATE",
    244     ]
    245   }
    246 }
    247 
    248 # rtti ------------------------------------------------------------------------
    249 #
    250 # Allows turning Run-Time Type Identification on or off.
    251 
    252 config("rtti") {
    253   if (is_win) {
    254     cflags_cc = [ "/GR" ]
    255   }
    256 }
    257 config("no_rtti") {
    258   if (is_win) {
    259     cflags_cc = [ "/GR-" ]
    260   } else {
    261     cflags_cc = [ "-fno-rtti" ]
    262   }
    263 }
    264 
    265 # Warnings ---------------------------------------------------------------------
    266 
    267 config("default_warnings") {
    268   if (is_win) {
    269     # Please keep ordered and add names if you add more.
    270     cflags = [
    271       "/wd4018",  # Comparing signed and unsigned values.
    272       "/wd4100",  # Unreferenced formal function parameter.
    273       "/wd4121",  # Alignment of a member was sensitive to packing.
    274       "/wd4125",  # Decimal digit terminates octal escape sequence.
    275       "/wd4127",  # Conditional expression is constant.
    276       "/wd4130",  # Logical operation on address of string constant.
    277       # TODO(brettw) is this necessary? If so, it should probably be on whoever
    278       # is silly enough to be doing this rather than globally.
    279       #"/wd4131",  # Function uses old-style declarator.
    280       "/wd4189",  # A variable was declared and initialized but never used.
    281       "/wd4201",  # Nonstandard extension used: nameless struct/union.
    282       "/wd4238",  # Nonstandard extension used: class rvalue used as lvalue.
    283       "/wd4244",  # Conversion: possible loss of data.
    284       "/wd4245",  # Conversion: signed/unsigned mismatch,
    285       "/wd4251",  # Class needs to have dll-interface.
    286       "/wd4310",  # Cast truncates constant value.
    287       "/wd4351",  # Elements of array will be default initialized.
    288       "/wd4355",  # 'this' used in base member initializer list.
    289       "/wd4396",  # Inline friend template thing.
    290       "/wd4428",  # Universal character name encountered in source.
    291       "/wd4481",  # Nonstandard extension: override specifier.
    292       "/wd4503",  # Decorated name length exceeded, name was truncated.
    293       "/wd4505",  # Unreferenced local function has been removed.
    294       "/wd4510",  # Default constructor could not be generated.
    295       "/wd4512",  # Assignment operator could not be generated.
    296       "/wd4530",  # Exception handler used, but unwind semantics not enabled.
    297       "/wd4610",  # Class can never be instantiated, constructor required.
    298       "/wd4611",  # C++ object destruction and 'catch'.
    299       "/wd4701",  # Potentially uninitialized local variable name used.
    300       "/wd4702",  # Unreachable code.
    301       "/wd4706",  # Assignment within conditional expression.
    302       "/wd4819",  # Character not in the current code page.
    303     ]
    304   } else {
    305     # Common GCC warning setup.
    306     cflags = [
    307       # Enables.
    308       "-Wendif-labels",  # Weird old-style text after an #endif.
    309 
    310       # Disables.
    311       "-Wno-missing-field-initializers",  # "struct foo f = {0};"
    312       "-Wno-unused-parameter",  # Unused function parameters.
    313       "-Wno-write-strings",
    314     ]
    315 
    316     if (is_mac) {
    317       cflags += [
    318         "-Wnewline-eof",
    319       ]
    320     }
    321 
    322     # TODO(brettw) Ones below here should be clang-only when we have a flag
    323     # for it.
    324     if (is_clang) {
    325       cflags += [
    326         "-Wheader-hygiene",
    327 
    328         # This warns on using ints as initializers for floats in
    329         # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
    330         # which happens in several places in chrome code. Not sure if
    331         # this is worth fixing.
    332         "-Wno-c++11-narrowing",
    333 
    334         # Don't die on dtoa code that uses a char as an array index.
    335         # This is required solely for base/third_party/dmg_fp/dtoa.cc.
    336         # TODO(brettw) move this to that project then!
    337         "-Wno-char-subscripts",
    338 
    339         # Warns on switches on enums that cover all enum values but
    340         # also contain a default: branch. Chrome is full of that.
    341         "-Wno-covered-switch-default",
    342 
    343         # Clang considers the `register` keyword as deprecated, but e.g.
    344         # code generated by flex (used in angle) contains that keyword.
    345         # http://crbug.com/255186
    346         "-Wno-deprecated-register",
    347 
    348         # Clang spots more unused functions.
    349         "-Wno-unused-function",
    350 
    351         # Warns when a const char[] is converted to bool.
    352         "-Wstring-conversion",
    353       ]
    354     }
    355   }
    356 }
    357 
    358 # Optimization -----------------------------------------------------------------
    359 
    360 config("optimize") {
    361   if (is_win) {
    362     cflags = [
    363       "/O2",
    364       "/Ob2",  # Both explicit and auto inlining.
    365       "/Oy-",  # Disable omitting frame pointers, must be after /O2.
    366     ]
    367   } else {
    368     if (is_ios) {
    369       cflags = [ "-Os" ]
    370     } else {
    371       cflags = [ "-O2" ]
    372     }
    373   }
    374 }
    375 
    376 config("no_optimize") {
    377   if (is_win) {
    378     cflags = [
    379       "/Od",  # Disable optimization.
    380       "/Ob0",  # Disable all inlining (on by default).
    381       "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
    382     ]
    383   } else {
    384     cflags = [ "-O0" ]
    385   }
    386 }
    387 
    388 # Symbols ----------------------------------------------------------------------
    389 
    390 # TODO(brettw) Since this sets ldflags on Windows which is inherited across
    391 # static library boundaries, if you want to remove the default symbol config
    392 # and set a different one on a target, you also have to do it for all static
    393 # libraries that go into that target, which is messed up. Either we need a
    394 # more flexible system for defining linker flags, or we need to separate this
    395 # out into a "symbols_linker" config that is only applied to DLLs and EXEs.
    396 config("symbols") {
    397   if (is_win) {
    398     cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
    399     ldflags = [ "/DEBUG" ]
    400   } else {
    401     cflags = [ "-g2" ]
    402   }
    403 }
    404 
    405 config("minimal_symbols") {
    406   if (is_win) {
    407     # Linker symbols for backtraces only.
    408     ldflags = [ "/DEBUG" ]
    409   } else {
    410     cflags = [ "-g1" ]
    411   }
    412 }
    413 
    414 config("no_symbols") {
    415   if (!is_win) {
    416     cflags = [ "-g0" ]
    417   }
    418 }
    419