summaryrefslogtreecommitdiffstats
path: root/build/config/compiler/BUILD.gn
blob: 20ffbb54c2ae677689961ff87a3bb29a8d967934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Base compiler configuration.
config("compiler") {
  include_dirs = [ "//", root_gen_dir ]
  if (is_win) {
    cflags = [
      "/Gy",  # Enable function-level linking.
      "/GS",  # Enable buffer security checking.
      "/EHsc",  # Assume C functions can't throw exceptions and don't catch
                # structured exceptions (only C++ ones).
    ]
  } else {
    # Common GCC compiler flags setup.
    # --------------------------------
    cflags = [
      "-fno-strict-aliasing",  # See http://crbug.com/32204
      "-fvisibility=hidden",
    ]
    cflags_c = [
    ]
    cflags_cc = [
      "-fno-exceptions",
      "-fno-threadsafe-statics",
      "-fvisibility-inlines-hidden",
    ]
    ldflags = [
    ]

    # Stack protection.
    # TODO(brettw) why do we have different values for all of these cases?
    if (is_mac) {
      cflags += "-fstack-protector-all"
    } else if (is_chromeos) {
      cflags += "-fstack-protector-strong"
    } else if (is_linux) {
      cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
    }

    # Mac-specific compiler flags setup.
    # ----------------------------------
    if (is_mac) {
      # These flags are shared between the C compiler and linker.
      common_mac_flags = [
        # TODO(brettw) obviously this arch flag needs to be parameterized.
        "-arch i386",

        # Set which SDK to use.
        # TODO(brettw) this needs to be configurable somehow.
        "-isysroot", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk",

        "-mmacosx-version-min=10.6",
      ]

      cflags += common_mac_flags + [
        # Without this, the constructors and destructors of a C++ object inside
        # an Objective C struct won't be called, which is very bad.
        "-fobjc-call-cxx-cdtors",
      ]

      cflags_c += [ "-std=c99" ]
      cflags_cc += [ "-std=gnu++11" ]

      ldflags += common_mac_flags + [
        "-L.",

        # TODO(brettW) I don't understand these options.
        "-Wl,-rpath,@loader_path/.",
        "-Wl,-rpath,@loader_path/../../..",
      ]
    }

    # Linux-specific compiler flags setup.
    # ------------------------------------
    if (is_linux) {
      cflags += [
        "-fPIC",
        "-pthread",
        "-pipe",  # Use pipes for communicating between sub-processes. Faster.
      ]

      # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
      # address space, and it doesn't support cross-compiling).
      if (cpu_arch == "ia64") {
        gold_path = rebase_path("//third_party/gold", ".", root_build_dir)
        ldflags += [
          "-B$gold_path",

          # There seems to be a conflict of --icf and -pie in gold which can
          # generate crashy binaries. As a security measure, -pie takes
          # precendence for now.
          # TODO(brettw) common.gypi has this only for target toolset.
          #"-Wl,--icf=safe",
          "-Wl,--icf=none",

          # Experimentation found that using four linking threads
          # saved ~20% of link time.
          # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
          # Only apply this to the target linker, since the host
          # linker might not be gold, but isn't used much anyway.
          "-Wl,--threads",
          "-Wl,--thread-count=4",
        ]
      }

      ldflags += [
        "-fPIC",
        "-pthread",
        "-Wl,-z,noexecstack",
        "-Wl,-z,now",
        "-Wl,-z,relro",
      ]
    }

    # Clang-specific compiler flags setup.
    # ------------------------------------
    if (is_clang) {
      cflags += [
        "-fcolor-diagnostics",
      ]
    }
  }
}

# runtime_library -------------------------------------------------------------
#
# Sets the runtime library and associated options.
#
# We don't bother making multiple versions that are toggle-able since there
# is more than one axis of control (which makes it complicated) and there's
# no practical reason for anybody to change this since the CRT must agree.

config("runtime_library") {
  if (is_component_build) {
    # Component mode: dynamic CRT.
    defines = [ "COMPONENT_BUILD" ]
    if (is_win) {
      # Since the library is shared, it requires exceptions or will give errors
      # about things not matching, so keep exceptions on.
      if (is_debug) {
        cflags = [ "/MDd" ]
      } else {
        cflags = [ "/MD" ]
      }
    }
  } else {
    # Static CRT.
    if (is_win) {
      # We don't use exceptions, and when we link statically we can just get
      # rid of them entirely.
      defines = [ "_HAS_EXCEPTIONS=0" ]
      if (is_debug) {
        cflags = [ "/MTd" ]
      } else {
        cflags = [ "/MT" ]
      }
    }
  }

  if (is_win) {
    defines += [
      "__STD_C",
      "__STDC_CONSTANT_MACROS",
      "__STDC_FORMAT_MACROS",
      "_CRT_RAND_S",
      "_CRT_SECURE_NO_DEPRECATE",
      "_SCL_SECURE_NO_DEPRECATE",
      "_UNICODE",
      "UNICODE",
    ]
  }
}

# chromium_code ---------------------------------------------------------------
#
# Toggles between higher and lower warnings for code that is (or isn't)
# part of Chromium.

config("chromium_code") {
  if (is_win) {
    cflags = [
      "/W4",  # Warning level 4.
      "/WX",  # Treat warnings as errors.
    ]
  } else {
    cflags = [
      "-Wall",
      "-Werror",

      # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
      # so we specify it explicitly.
      # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
      # http://code.google.com/p/chromium/issues/detail?id=90453
      "-Wsign-compare",
    ]

    # In Chromium code, we define __STDC_foo_MACROS in order to get the
    # C99 macros on Mac and Linux.
    defines = [
      "__STDC_CONSTANT_MACROS",
      "__STDC_FORMAT_MACROS",
    ]

    # TODO(brettw) this should also be enabled on Linux but some files
    # currently fail.
    if (is_mac) {
      cflags += "-Wextra"
    }
  }
}
config("no_chromium_code") {
  if (is_win) {
    cflags = [
      "/W3",  # Warning level 3.
      "/wd4800",  # Disable warning when forcing value to bool.
    ]
    defines = [
      "_CRT_NONSTDC_NO_WARNINGS",
      "_CRT_NONSTDC_NO_DEPRECATE",
    ]
  }
}

# rtti ------------------------------------------------------------------------
#
# Allows turning Run-Time Type Identification on or off.

config("rtti") {
  if (is_win) {
    cflags_cc = [ "/GR" ]
  }
}
config("no_rtti") {
  if (is_win) {
    cflags_cc = [ "/GR-" ]
  } else {
    cflags_cc = [ "-fno-rtti" ]
  }
}

# Warnings ---------------------------------------------------------------------

config("default_warnings") {
  if (is_win) {
    # Please keep ordered and add names if you add more.
    cflags = [
      "/wd4018",  # Comparing signed and unsigned values.
      "/wd4100",  # Unreferenced formal function parameter.
      "/wd4121",  # Alignment of a member was sensitive to packing.
      "/wd4125",  # Decimal digit terminates octal escape sequence.
      "/wd4127",  # Conditional expression is constant.
      "/wd4130",  # Logical operation on address of string constant.
      # TODO(brettw) is this necessary? If so, it should probably be on whoever
      # is silly enough to be doing this rather than globally.
      #"/wd4131",  # Function uses old-style declarator.
      "/wd4189",  # A variable was declared and initialized but never used.
      "/wd4201",  # Nonstandard extension used: nameless struct/union.
      "/wd4238",  # Nonstandard extension used: class rvalue used as lvalue.
      "/wd4244",  # Conversion: possible loss of data.
      "/wd4245",  # Conversion: signed/unsigned mismatch,
      "/wd4251",  # Class needs to have dll-interface.
      "/wd4310",  # Cast truncates constant value.
      "/wd4351",  # Elements of array will be default initialized.
      "/wd4355",  # 'this' used in base member initializer list.
      "/wd4396",  # Inline friend template thing.
      "/wd4428",  # Universal character name encountered in source.
      "/wd4481",  # Nonstandard extension: override specifier.
      "/wd4503",  # Decorated name length exceeded, name was truncated.
      "/wd4505",  # Unreferenced local function has been removed.
      "/wd4510",  # Default constructor could not be generated.
      "/wd4512",  # Assignment operator could not be generated.
      "/wd4530",  # Exception handler used, but unwind semantics not enabled.
      "/wd4610",  # Class can never be instantiated, constructor required.
      "/wd4611",  # C++ object destruction and 'catch'.
      "/wd4701",  # Potentially uninitialized local variable name used.
      "/wd4702",  # Unreachable code.
      "/wd4706",  # Assignment within conditional expression.
      "/wd4819",  # Character not in the current code page.
    ]
  } else {
    # Common GCC warning setup.
    cflags = [
      # Enables.
      "-Wendif-labels",  # Weird old-style text after an #endif.

      # Disables.
      "-Wno-missing-field-initializers",  # "struct foo f = {0};"
      "-Wno-unused-parameter",  # Unused function parameters.
      "-Wno-write-strings",
    ]

    if (is_mac) {
      cflags += [
        "-Wnewline-eof",
      ]
    }

    # TODO(brettw) Ones below here should be clang-only when we have a flag
    # for it.
    if (is_clang) {
      cflags += [
        "-Wheader-hygiene",

        # This warns on using ints as initializers for floats in
        # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
        # which happens in several places in chrome code. Not sure if
        # this is worth fixing.
        "-Wno-c++11-narrowing",

        # Don't die on dtoa code that uses a char as an array index.
        # This is required solely for base/third_party/dmg_fp/dtoa.cc.
        # TODO(brettw) move this to that project then!
        "-Wno-char-subscripts",

        # Warns on switches on enums that cover all enum values but
        # also contain a default: branch. Chrome is full of that.
        "-Wno-covered-switch-default",

        # Clang considers the `register` keyword as deprecated, but e.g.
        # code generated by flex (used in angle) contains that keyword.
        # http://crbug.com/255186
        "-Wno-deprecated-register",

        # Clang spots more unused functions.
        "-Wno-unused-function",

        # Warns when a const char[] is converted to bool.
        "-Wstring-conversion",
      ]
    }
  }
}

# Optimization -----------------------------------------------------------------

config("optimize") {
  if (is_win) {
    cflags = [
      "/O2",
      "/Ob2",  # Both explicit and auto inlining.
      "/Oy-",  # Disable omitting frame pointers, must be after /O2.
    ]
  } else {
    if (is_ios) {
      cflags = [ "-Os" ]
    } else {
      cflags = [ "-O2" ]
    }
  }
}

config("no_optimize") {
  if (is_win) {
    cflags = [
      "/Od",  # Disable optimization.
      "/Ob0",  # Disable all inlining (on by default).
      "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
    ]
  } else {
    cflags = [ "-O0" ]
  }
}

# Symbols ----------------------------------------------------------------------

# TODO(brettw) Since this sets ldflags on Windows which is inherited across
# static library boundaries, if you want to remove the default symbol config
# and set a different one on a target, you also have to do it for all static
# libraries that go into that target, which is messed up. Either we need a
# more flexible system for defining linker flags, or we need to separate this
# out into a "symbols_linker" config that is only applied to DLLs and EXEs.
config("symbols") {
  if (is_win) {
    cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
    ldflags = [ "/DEBUG" ]
  } else {
    cflags = [ "-g2" ]
  }
}

config("minimal_symbols") {
  if (is_win) {
    # Linker symbols for backtraces only.
    ldflags = [ "/DEBUG" ]
  } else {
    cflags = [ "-g1" ]
  }
}

config("no_symbols") {
  if (!is_win) {
    cflags = [ "-g0" ]
  }
}