summaryrefslogtreecommitdiff
path: root/chromium/build/config/win/BUILD.gn
blob: c121d2cf1b6b99678f7f0ce2b5dd33b3d6912330 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# 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.

import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/config/win/visual_studio_version.gni")
import("//build/toolchain/toolchain.gni")

assert(is_win)

declare_args() {
  # Set this to true to enable static analysis through Visual Studio's
  # /analyze. This dramatically slows compiles and reports thousands of
  # warnings, so normally this is done on a build machine and only the new
  # warnings are examined.
  use_vs_code_analysis = false

  # Turn this on to have the linker output extra timing information.
  win_linker_timing = false
}

# This is included by reference in the //build/config/compiler config that
# is applied to all targets. It is here to separate out the logic that is
# Windows-only.
config("compiler") {
  if (current_cpu == "x86") {
    asmflags = [
      # When /safeseh is specified, the linker will only produce an image if it
      # can also produce a table of the image's safe exception handlers. This
      # table specifies for the operating system which exception handlers are
      # valid for the image. Note that /SAFESEH isn't accepted on the command
      # line, only /safeseh. This is only accepted by ml.exe, not ml64.exe.
      "/safeseh",
    ]
  }

  cflags = [
    "/Gy",  # Enable function-level linking.
    "/FS",  # Preserve previous PDB behavior.
    "/bigobj",  # Some of our files are bigger than the regular limits.
  ]

  # Force C/C++ mode for the given GN detected file type. This is necessary
  # for precompiled headers where the same source file is compiled in both
  # modes.
  cflags_c = [ "/TC" ]
  cflags_cc = [ "/TP" ]

  cflags += [
    # Tell the compiler to crash on failures. This is undocumented
    # and unsupported but very handy.
    "/d2FastFail",

    # Work around crbug.com/526851, bug in VS 2015 RTM compiler.
    "/Zc:sizedDealloc-",
  ]

  # Building with Clang on Windows is a work in progress and very
  # experimental. See crbug.com/82385.
  if (is_clang) {
    cflags += [ "-fmsc-version=1911" ]

    if (current_cpu == "x86") {
      cflags += [ "-m32" ]
    } else {
      cflags += [ "-m64" ]
    }

    if (exec_script("//build/win/use_ansi_codes.py", [], "trim string") ==
        "True") {
      cflags += [
        # cmd.exe doesn't understand ANSI escape codes by default,
        # so only enable them if something emulating them is around.
        "-fansi-escape-codes",
      ]
    }

    # Clang runtime libraries, such as the sanitizer runtimes, live here.
    lib_dirs = [ "$clang_base_path/lib/clang/$clang_version/lib/windows" ]
  }

  # /PROFILE ensures that the PDB file contains FIXUP information (growing the
  # PDB file by about 5%) but does not otherwise alter the output binary. This
  # information is used by the Syzygy optimization tool when decomposing the
  # release image. It is enabled for syzyasan builds and opportunistically for
  # other builds where it is not prohibited (not supported when incrementally
  # linking, or using /debug:fastlink).
  if (is_syzyasan) {
    assert(!is_win_fastlink)
    ldflags = [ "/PROFILE" ]
  } else {
    if (!is_debug && !is_component_build) {
      if (is_win_fastlink) {
        # /PROFILE implies the following linker flags. Therefore if we are
        # skipping /PROFILE because it is incompatible with /DEBUG:FASTLINK
        # we should explicitly add these flags in order to avoid unintended
        # consequences such as larger binaries.
        ldflags = [
          "/OPT:REF",
          "/OPT:ICF",
          "/INCREMENTAL:NO",
          "/FIXED:NO",
        ]
      } else {
        ldflags = [ "/PROFILE" ]
      }
    }
  }

  # arflags apply only to static_libraries. The normal linker configs are only
  # set for executable and shared library targets so arflags must be set
  # elsewhere. Since this is relatively contained, we just apply them in this
  # more general config and they will only have an effect on static libraries.
  arflags = [
    # "No public symbols found; archive member will be inaccessible." This
    # means that one or more object files in the library can never be
    # pulled in to targets that link to this library. It's just a warning that
    # the source file is a no-op.
    "/ignore:4221",
  ]
}

config("vs_code_analysis") {
  if (use_vs_code_analysis && !is_clang) {
    # When use_vs_code_analysis is specified add the /analyze switch to enable
    # static analysis. Specifying /analyze:WX- says that /analyze warnings
    # should not be treated as errors.
    cflags = [ "/analyze:WX-" ]

    # Also, disable various noisy warnings that have low value.
    cflags += [
      "/wd6011",  # Dereferencing NULL pointer

      # C6285 is ~16% of raw warnings and has low value
      "/wd6285",  # non-zero constant || non-zero constant
      "/wd6308",  # realloc might return null pointer

      # Possible infinite loop: use of the constant
      # EXCEPTION_CONTINUE_EXECUTION in the exception-filter
      "/wd6312",

      "/wd6322",  # Empty _except block
      "/wd6330",  # 'char' used instead of 'unsigned char' for istype() call

      # C6334 is ~80% of raw warnings and has low value
      "/wd6334",  # sizeof applied to an expression with an operator
      "/wd6326",  # Potential comparison of constant with constant
      "/wd6340",  # Sign mismatch in function parameter
      "/wd28159",  # Consider using 'GetTickCount64'
      "/wd28196",  # The precondition is not satisfied
      "/wd28204",  # Inconsistent SAL annotations
      "/wd28251",  # Inconsistent SAL annotations
      "/wd28252",  # Inconsistent SAL annotations
      "/wd28253",  # Inconsistent SAL annotations
      "/wd28278",  # Function appears with no prototype in scope
      "/wd28285",  # syntax error in SAL annotation (in algorithm)
      "/wd28301",  # Inconsistent SAL annotations
      "/wd28182",  # Dereferencing NULL pointer
    ]
  }
}

# This is included by reference in the //build/config/compiler:runtime_library
# config that is applied to all targets. It is here to separate out the logic
# that is Windows-only. Please see that target for advice on what should go in
# :runtime_library vs. :compiler.
config("runtime_library") {
  cflags = []

  # Defines that set up the CRT.
  defines = [
    "__STD_C",
    "_CRT_RAND_S",
    "_CRT_SECURE_NO_DEPRECATE",
    "_HAS_EXCEPTIONS=0",
    "_SCL_SECURE_NO_DEPRECATE",
  ]

  # Defines that set up the Windows SDK.
  defines += [
    "_ATL_NO_OPENGL",
    "_WINDOWS",
    "CERT_CHAIN_PARA_HAS_EXTRA_FIELDS",
    "PSAPI_VERSION=1",
    "WIN32",
    "_SECURE_ATL",
  ]

  if (!use_vs_code_analysis) {
    # This is required for ATL to use XP-safe versions of its functions.
    # However it is prohibited when using /analyze
    defines += [ "_USING_V110_SDK71_" ]
  }
}

# Sets the default Windows build version. This is separated because some
# targets need to manually override it for their compiles.
config("winver") {
  defines = [
    "NTDDI_VERSION=0x0A000000",
    "_WIN32_WINNT=0x0A00",
    "WINVER=0x0A00",
  ]
}

# Linker flags for Windows SDK setup, this is applied only to EXEs and DLLs.
config("sdk_link") {
  if (current_cpu == "x64") {
    ldflags = [ "/MACHINE:X64" ]
    lib_dirs = [
      "$windows_sdk_path\Lib\winv6.3\um\x64",
      "$visual_studio_path\VC\lib\amd64",
      "$visual_studio_path\VC\atlmfc\lib\amd64",
    ]
  } else {
    ldflags = [
      "/MACHINE:X86",
      "/SAFESEH",  # Not compatible with x64 so use only for x86.
      "/largeaddressaware",
    ]
    lib_dirs = [
      "$windows_sdk_path\Lib\winv6.3\um\x86",
      "$visual_studio_path\VC\lib",
      "$visual_studio_path\VC\atlmfc\lib",
    ]
  }
}

# This default linker setup is provided separately from the SDK setup so
# targets who want different library configurations can remove this and specify
# their own.
config("common_linker_setup") {
  ldflags = [
    "/fastfail",
    "/FIXED:NO",
    "/ignore:4199",
    "/ignore:4221",
    "/NXCOMPAT",
  ]

  # ASLR makes debugging with windbg difficult because Chrome.exe and
  # Chrome.dll share the same base name. As result, windbg will name the
  # Chrome.dll module like chrome_<base address>, where <base address>
  # typically changes with each launch. This in turn means that breakpoints in
  # Chrome.dll don't stick from one launch to the next. For this reason, we
  # turn ASLR off in debug builds.
  if (is_debug) {
    ldflags += [ "/DYNAMICBASE:NO" ]
  } else {
    ldflags += [ "/DYNAMICBASE" ]
  }

  if (win_linker_timing) {
    ldflags += [
      "/time",
      "/verbose:incr",
    ]
  }
}

config("cfi_linker") {
  # Control Flow Guard (CFG)
  # https://msdn.microsoft.com/en-us/library/windows/desktop/mt637065.aspx
  # /DYNAMICBASE (ASLR) is turned off in debug builds, therefore CFG can’t be
  # turned on either.
  # TODO(thakis): Turn this on with lld once supported, https://crbug.com/693709
  if (!is_debug && !use_lld) {
    # Turn on CFG in msvc linker, regardless of compiler used. Turn off CFG for
    # longjmp (new in VS 2017) because it relies on compiler support which we do
    # not have enabled.
    ldflags = [ "/guard:cf,nolongjmp" ]
  }
}

# CRT --------------------------------------------------------------------------

# Configures how the runtime library (CRT) is going to be used.
# See https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx for a reference of
# what each value does.
config("default_crt") {
  if (is_component_build) {
    # Component mode: dynamic CRT. Since the library is shared, it requires
    # exceptions or will give errors about things not matching, so keep
    # exceptions on.
    configs = [ ":dynamic_crt" ]
  } else {
    if (current_os != "win") {
      # WindowsRT: use the dynamic CRT.
      configs = [ ":dynamic_crt" ]
    } else {
      # Desktop Windows: static CRT.
      configs = [ ":static_crt" ]
    }
  }
}

# Use this to force the debug CRT for when building perf-critical build tools
# that need to be fully optimized even in debug builds, for those times when the
# debug CRT is part of the bottleneck. This also avoids *implicitly* defining
# _DEBUG.
config("release_crt") {
  if (is_component_build) {
    cflags = [ "/MD" ]
  } else {
    cflags = [ "/MT" ]
  }
}

config("dynamic_crt") {
  if (is_debug) {
    # This pulls in the DLL debug CRT and defines _DEBUG
    cflags = [ "/MDd" ]
  } else {
    cflags = [ "/MD" ]
  }
}

config("static_crt") {
  if (is_debug) {
    # This pulls in the static debug CRT and defines _DEBUG
    cflags = [ "/MTd" ]
  } else {
    cflags = [ "/MT" ]
  }
}

# Subsystem --------------------------------------------------------------------

# This is appended to the subsystem to specify a minimum version.
if (current_cpu == "x64") {
  # The number after the comma is the minimum required OS version.
  # 5.02 = Windows Server 2003.
  subsystem_version_suffix = ",5.02"
} else {
  # 5.01 = Windows XP.
  subsystem_version_suffix = ",5.01"
}

config("console") {
  ldflags = [ "/SUBSYSTEM:CONSOLE$subsystem_version_suffix" ]
}
config("windowed") {
  ldflags = [ "/SUBSYSTEM:WINDOWS$subsystem_version_suffix" ]
}

# Incremental linking ----------------------------------------------------------

incremental_linking_on_switch = [ "/INCREMENTAL" ]
incremental_linking_off_switch = [ "/INCREMENTAL:NO" ]

# Disable incremental linking for syzyasan, enable for debug builds and all
# component builds - any builds where performance is not job one.
if ((is_debug || is_component_build) && !is_syzyasan) {
  default_incremental_linking_switch = incremental_linking_on_switch
} else {
  default_incremental_linking_switch = incremental_linking_off_switch
}

# Applies incremental linking or not depending on the current configuration.
config("default_incremental_linking") {
  ldflags = default_incremental_linking_switch
}

# Explicitly on or off incremental linking
config("incremental_linking") {
  ldflags = incremental_linking_on_switch
}
config("no_incremental_linking") {
  ldflags = incremental_linking_off_switch
}

# Some large modules can't handle incremental linking in some situations. This
# config should be applied to large modules to turn off incremental linking
# when it won't work.
config("default_large_module_incremental_linking") {
  if (symbol_level == 0 || (current_cpu == "x86" && is_component_build)) {
    # In these configurations, ilk file sizes stay low enough that we can
    # link incrementally.
    ldflags = default_incremental_linking_switch
  } else {
    ldflags = incremental_linking_off_switch
  }
}

# Character set ----------------------------------------------------------------

# Not including this config means "ansi" (8-bit system codepage).
config("unicode") {
  defines = [
    "_UNICODE",
    "UNICODE",
  ]
}

# Lean and mean ----------------------------------------------------------------

# Some third party code might not compile with WIN32_LEAN_AND_MEAN so we have
# to have a separate config for it. Remove this config from your target to
# get the "bloaty and accommodating" version of windows.h.
config("lean_and_mean") {
  defines = [ "WIN32_LEAN_AND_MEAN" ]
}

# Nominmax --------------------------------------------------------------------

# Some third party code defines NOMINMAX before including windows.h, which
# then causes warnings when it's been previously defined on the command line.
# For such targets, this config can be removed.

config("nominmax") {
  defines = [ "NOMINMAX" ]
}

# Target WinRT ----------------------------------------------------------------

# When targeting Windows Runtime, certain compiler/linker flags are necessary.

config("target_winrt") {
  defines = [
    "WINRT",
    "WINAPI_FAMILY=WINAPI_FAMILY_PC_APP",
  ]
  cflags_cc = [
    "/ZW",
    "/EHsc",
  ]
}