summaryrefslogtreecommitdiffstats
path: root/build/toolchain/win/BUILD.gn
blob: 1bd67303cf4a7ccb29f521c3c90ae0e0d2f811de (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
# 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/win/visual_studio_version.gni")
import("//build/toolchain/goma.gni")

# Should only be running on Windows.
assert(is_win)

# Setup the Visual Studio state.
#
# Its arguments are the VS path and the compiler wrapper tool. It will write
# "environment.x86" and "environment.x64" to the build directory and return a
# list to us.
gyp_win_tool_path =
    rebase_path("//tools/gyp/pylib/gyp/win_tool.py", root_build_dir)
exec_script("setup_toolchain.py",
            [
              visual_studio_path,
              gyp_win_tool_path,
              windows_sdk_path,
              visual_studio_runtime_dirs,
            ])

# This value will be inherited in the toolchain below.
concurrent_links = exec_script("../get_concurrent_links.py", [], "value")

# Parameters:
#  cpu_arch: cpu_arch to pass as a build arg
#  environment: File name of environment file.
template("msvc_toolchain") {
  if (defined(invoker.concurrent_links)) {
    concurrent_links = invoker.concurrent_links
  }

  env = invoker.environment

  if (is_debug) {
    configuration = "Debug"
  } else {
    configuration = "Release"
  }
  exec_script("../../vs_toolchain.py",
              [
                "copy_dlls",
                rebase_path(root_build_dir),
                configuration,
                invoker.cpu_arch,
              ])

  toolchain(target_name) {
    # Make these apply to all tools below.
    lib_switch = ""
    lib_dir_switch = "/LIBPATH:"

    tool("cc") {
      rspfile = "{{output}}.rsp"
      pdbname = "{{target_out_dir}}/{{target_output_name}}_c.pdb"
      command =
          "ninja -t msvc -e $env -- cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd$pdbname"
      depsformat = "msvc"
      description = "CC {{output}}"
      outputs =
          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj" ]
      rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
    }

    tool("cxx") {
      rspfile = "{{output}}.rsp"

      # The PDB name needs to be different between C and C++ compiled files.
      pdbname = "{{target_out_dir}}/{{target_output_name}}_cc.pdb"
      command =
          "ninja -t msvc -e $env -- cl.exe /nologo /showIncludes /FC @$rspfile /c {{source}} /Fo{{output}} /Fd$pdbname"
      depsformat = "msvc"
      description = "CXX {{output}}"
      outputs =
          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj" ]
      rspfile_content = "{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}"
    }

    tool("rc") {
      command =
          "$python_path gyp-win-tool rc-wrapper $env rc.exe {{defines}} {{include_dirs}} /fo{{output}} {{source}}"
      outputs =
          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.res" ]
      description = "RC {{output}}"
    }

    tool("asm") {
      # TODO(brettw): "/safeseh" assembler argument is hardcoded here. Extract
      # assembler flags to a variable like cflags. crbug.com/418613
      command =
          "$python_path gyp-win-tool asm-wrapper $env ml.exe {{defines}} {{include_dirs}} /safeseh /c /Fo {{output}} {{source}}"
      description = "ASM {{output}}"
      outputs =
          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.obj" ]
    }

    tool("alink") {
      rspfile = "{{output}}.rsp"
      command =
          "$python_path gyp-win-tool link-wrapper $env False lib.exe /nologo /ignore:4221 /OUT:{{output}} @$rspfile"
      description = "LIB {{output}}"
      outputs = [
        # Ignore {{output_extension}} and always use .lib, there's no reason to
        # allow targets to override this extension on Windows.
        "{{target_out_dir}}/{{target_output_name}}.lib",
      ]
      default_output_extension = ".lib"

      # The use of inputs_newline is to work around a fixed per-line buffer
      # size in the linker.
      rspfile_content = "{{inputs_newline}}"
    }

    tool("solink") {
      dllname = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"  # e.g. foo.dll
      libname =
          "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.lib"  # e.g. foo.dll.lib
      rspfile = "${dllname}.rsp"

      link_command =
          "$python_path gyp-win-tool link-wrapper $env False link.exe /nologo /IMPLIB:$libname /DLL /OUT:$dllname /PDB:${dllname}.pdb @$rspfile"

      # TODO(brettw) support manifests
      #manifest_command = "$python_path gyp-win-tool manifest-wrapper $env mt.exe -nologo -manifest $manifests -out:${dllname}.manifest"
      #command = "cmd /c $link_command && $manifest_command"
      command = link_command

      default_output_extension = ".dll"
      description = "LINK(DLL) {{output}}"
      outputs = [
        dllname,
        libname,
      ]
      link_output = libname
      depend_output = libname

      # The use of inputs_newline is to work around a fixed per-line buffer
      # size in the linker.
      rspfile_content = "{{libs}} {{solibs}} {{inputs_newline}} {{ldflags}}"
    }

    tool("link") {
      rspfile = "{{output}}.rsp"

      link_command =
          "$python_path gyp-win-tool link-wrapper $env False link.exe /nologo /OUT:{{output}} /PDB:{{output}}.pdb @$rspfile"

      # TODO(brettw) support manifests
      #manifest_command = "$python_path gyp-win-tool manifest-wrapper $env mt.exe -nologo -manifest $manifests -out:{{output}}.manifest"
      #command = "cmd /c $link_command && $manifest_command"
      command = link_command

      default_output_extension = ".exe"
      description = "LINK {{output}}"
      outputs =
          [ "{{root_out_dir}}/{{target_output_name}}{{output_extension}}" ]

      # The use of inputs_newline is to work around a fixed per-line buffer
      # size in the linker.
      rspfile_content = "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}}"
    }

    tool("stamp") {
      command = "$python_path gyp-win-tool stamp {{output}}"
      description = "STAMP {{output}}"
    }

    tool("copy") {
      command =
          "$python_path gyp-win-tool recursive-mirror {{source}} {{output}}"
      description = "COPY {{source}} {{output}}"
    }

    # When invoking this toolchain not as the default one, these args will be
    # passed to the build. They are ignored when this is the default toolchain.
    toolchain_args() {
      cpu_arch = invoker.cpu_arch
    }
  }
}

# TODO(dpranke): Declare both toolchains all of the time when we
# get it sorted out how we want to support them both in a single build.
# Right now only one of these can be enabled at a time because the
# runtime libraries get copied to root_build_dir and would collide.
if (cpu_arch == "x86") {
  msvc_toolchain("32") {
    environment = "environment.x86"
    cpu_arch = "x86"
  }
}

if (cpu_arch == "x64") {
  msvc_toolchain("64") {
    environment = "environment.x64"
    cpu_arch = "x64"
  }
}