xref: /aosp_15_r20/external/angle/build/toolchain/win/toolchain.gni (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# Copyright 2022 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/clang/clang.gni")
6import("//build/config/compiler/compiler.gni")
7import("//build/config/rust.gni")
8import("//build/config/sanitizers/sanitizers.gni")
9import("//build/config/win/visual_studio_version.gni")
10import("//build/toolchain/cc_wrapper.gni")
11import("//build/toolchain/rbe.gni")
12import("//build/toolchain/toolchain.gni")
13import("//build/toolchain/win/win_toolchain_data.gni")
14
15assert(is_win, "Should only be running on Windows")
16
17# This tool will is used as a wrapper for various commands below.
18_tool_wrapper_path =
19    rebase_path("//build/toolchain/win/tool_wrapper.py", root_build_dir)
20
21if (host_os == "win") {
22  _exe = ".exe"
23} else {
24  _exe = ""
25}
26
27_clang_bin_path = rebase_path("$clang_base_path/bin", root_build_dir)
28
29# Makes a single MSVC toolchain.
30#
31# Parameters:
32#   environment: File name of environment file.
33#
34# You would also define a toolchain_args variable with at least these set:
35#   current_cpu: current_cpu to pass as a build arg
36#   current_os: current_os to pass as a build arg
37template("msvc_toolchain") {
38  toolchain(target_name) {
39    # When invoking this toolchain not as the default one, these args will be
40    # passed to the build. They are ignored when this is the default toolchain.
41    assert(defined(invoker.toolchain_args))
42    toolchain_args = {
43      forward_variables_from(invoker.toolchain_args, "*")
44
45      # This value needs to be passed through unchanged.
46      host_toolchain = host_toolchain
47    }
48
49    if (defined(toolchain_args.is_clang)) {
50      toolchain_is_clang = toolchain_args.is_clang
51    } else {
52      toolchain_is_clang = is_clang
53    }
54
55    # When the invoker has explicitly overridden use_reclient or cc_wrapper in
56    # the toolchain args, use those values, otherwise default to the global one.
57    # This works because the only reasonable override that toolchains might
58    # supply for these values are to force-disable them.
59    if (defined(toolchain_args.use_reclient)) {
60      toolchain_uses_reclient = toolchain_args.use_reclient
61    } else {
62      toolchain_uses_reclient = use_reclient
63    }
64    if (defined(toolchain_args.cc_wrapper)) {
65      toolchain_cc_wrapper = toolchain_args.cc_wrapper
66    } else {
67      toolchain_cc_wrapper = cc_wrapper
68    }
69    assert(!(toolchain_cc_wrapper != "" && toolchain_uses_reclient),
70           "re-client and cc_wrapper can't be used together.")
71
72    if (toolchain_uses_reclient) {
73      if (toolchain_is_clang) {
74        cl_prefix = "${reclient_bin_dir}/rewrapper -cfg=${reclient_cc_cfg_file}${rbe_bug_326584510_missing_inputs} -exec_root=${rbe_exec_root} -labels=type=compile,compiler=clang-cl,lang=cpp "
75      } else {
76        cl_prefix = ""
77      }
78    } else if (toolchain_cc_wrapper != "" && toolchain_is_clang) {
79      cl_prefix = toolchain_cc_wrapper + " "
80    } else {
81      cl_prefix = ""
82    }
83
84    cl = "${cl_prefix}${invoker.cl}"
85    if (host_os == "win") {
86      # Flip the slashes so that copy/paste of the command works.
87      cl = string_replace(cl, "/", "\\")
88    }
89
90    # Make these apply to all tools below.
91    lib_switch = ""
92    lib_dir_switch = "/LIBPATH:"
93
94    # Object files go in this directory.
95    object_subdir = "{{target_out_dir}}/{{label_name}}"
96
97    env = invoker.environment
98
99    if (use_lld) {
100      # lld-link includes a replacement for lib.exe that can produce thin
101      # archives and understands bitcode (for lto builds).
102      link = "${_clang_bin_path}/lld-link${_exe}"
103      cc_linkflags = ""
104      if (toolchain_has_rust) {
105        rust_linkflags = ""
106      }
107      if (host_os == "win") {
108        # Flip the slashes so that copy/paste of the commands works.
109        link = string_replace(link, "/", "\\")
110      }
111      lib = "$link /lib"
112      if (host_os != "win") {
113        # See comment adding --rsp-quoting to $cl above for more information.
114        cc_linkflags += " --rsp-quoting=posix"
115        if (toolchain_has_rust) {
116          rust_linkflags += " -Clink-arg=--rsp-quoting=posix"
117        }
118      }
119    } else {
120      lib = "lib.exe"
121      link = "link.exe"
122      cc_linkflags = ""
123      if (toolchain_has_rust) {
124        rust_linkflags = ""
125      }
126    }
127
128    # If possible, pass system includes as flags to the compiler.  When that's
129    # not possible, load a full environment file (containing %INCLUDE% and
130    # %PATH%) -- e.g. 32-bit MSVS builds require %PATH% to be set and just
131    # passing in a list of include directories isn't enough.
132    if (defined(invoker.sys_include_flags)) {
133      env_wrapper = ""
134      sys_include_flags =
135          "${invoker.sys_include_flags} "  # Note trailing space.
136    } else {
137      # clang-cl doesn't need this env hoop, so omit it there.
138      assert(!toolchain_is_clang)
139      env_wrapper = "ninja -t msvc -e $env -- "  # Note trailing space.
140      sys_include_flags = ""
141    }
142
143    if (host_os != "win" || (use_lld && defined(invoker.sys_lib_flags))) {
144      linker_wrapper = ""
145      sys_lib_flags = "${invoker.sys_lib_flags}"
146
147      # TODO(thakis): Remove once crbug.com/1300005 is fixed
148      assert(toolchain_args.current_cpu == "x64" ||
149                 toolchain_args.current_cpu == "x86" ||
150                 toolchain_args.current_cpu == "arm" ||
151                 toolchain_args.current_cpu == "arm64",
152             "Only supports x64, x86, arm and arm64 CPUs")
153      if (toolchain_args.current_cpu == "x64") {
154        sys_lib_flags += " /MACHINE:X64"
155      } else if (toolchain_args.current_cpu == "x86") {
156        sys_lib_flags += " /MACHINE:X86"
157      } else if (toolchain_args.current_cpu == "arm") {
158        sys_lib_flags += " /MACHINE:ARM"
159      } else if (toolchain_args.current_cpu == "arm64") {
160        sys_lib_flags += " /MACHINE:ARM64"
161      }
162
163      sys_lib_flags += " "  # Note trailing space.
164    } else {
165      # link.exe must be run under a wrapper to set up the environment
166      # (it needs %LIB% set to find libraries), and to work around its bugs.
167      # Note trailing space:
168      linker_wrapper =
169          "\"$python_path\" $_tool_wrapper_path link-wrapper $env False "
170      sys_lib_flags = ""
171    }
172
173    if (defined(toolchain_args.use_clang_coverage)) {
174      toolchain_use_clang_coverage = toolchain_args.use_clang_coverage
175    } else {
176      toolchain_use_clang_coverage = use_clang_coverage
177    }
178
179    if (toolchain_use_clang_coverage) {
180      assert(toolchain_is_clang,
181             "use_clang_coverage should only be used with Clang")
182      if (defined(toolchain_args.coverage_instrumentation_input_file)) {
183        toolchain_coverage_instrumentation_input_file =
184            toolchain_args.coverage_instrumentation_input_file
185      } else {
186        toolchain_coverage_instrumentation_input_file =
187            coverage_instrumentation_input_file
188      }
189
190      coverage_wrapper =
191          rebase_path("//build/toolchain/clang_code_coverage_wrapper.py",
192                      root_build_dir)
193      coverage_wrapper = coverage_wrapper + " --target-os=" + target_os
194      if (toolchain_coverage_instrumentation_input_file != "") {
195        coverage_wrapper =
196            coverage_wrapper + " --files-to-instrument=" +
197            rebase_path(toolchain_coverage_instrumentation_input_file,
198                        root_build_dir)
199      }
200      coverage_wrapper = "\"$python_path\" " + coverage_wrapper + " "
201    } else {
202      coverage_wrapper = ""
203    }
204
205    # Disabled with cc_wrapper because of
206    # https://github.com/mozilla/sccache/issues/1013
207    if (toolchain_is_clang && toolchain_cc_wrapper == "") {
208      # This flag omits system includes from /showIncludes output, to reduce
209      # the amount of data to parse and store in .ninja_deps. We do this on
210      # non-Windows too, and already make sure rebuilds after winsdk/libc++/
211      # clang header updates happen via changing command line flags.
212      show_includes = "/showIncludes:user"
213    } else {
214      show_includes = "/showIncludes"
215    }
216
217    tool("cc") {
218      precompiled_header_type = "msvc"
219      pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"
220
221      # Label names may have spaces in them so the pdbname must be quoted. The
222      # source and output don't need to be quoted because GN knows they're a
223      # full file name and will quote automatically when necessary.
224      depsformat = "msvc"
225      description = "CC {{output}}"
226      outputs = [ "$object_subdir/{{source_name_part}}.obj" ]
227
228      # Note that the code coverage wrapper scripts assumes that {{source}}
229      # comes immediately after /c.
230      command = "$coverage_wrapper$env_wrapper$cl /c {{source}} /nologo $show_includes $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} /Fo{{output}} /Fd\"$pdbname\""
231    }
232
233    tool("cxx") {
234      precompiled_header_type = "msvc"
235
236      # The PDB name needs to be different between C and C++ compiled files.
237      pdbname = "{{target_out_dir}}/{{label_name}}_cc.pdb"
238
239      # See comment in CC tool about quoting.
240      depsformat = "msvc"
241      description = "CXX {{output}}"
242      outputs = [ "$object_subdir/{{source_name_part}}.obj" ]
243
244      # Note that the code coverage wrapper scripts assumes that {{source}}
245      # comes immediately after /c.
246      command = "$coverage_wrapper$env_wrapper$cl /c {{source}} /Fo{{output}} /nologo $show_includes $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} /Fd\"$pdbname\""
247    }
248
249    tool("rc") {
250      command = "\"$python_path\" $_tool_wrapper_path rc-wrapper $env rc.exe /nologo $sys_include_flags{{defines}} {{include_dirs}} /fo{{output}} {{source}}"
251      depsformat = "msvc"
252      outputs = [ "$object_subdir/{{source_name_part}}.res" ]
253      description = "RC {{output}}"
254    }
255
256    tool("asm") {
257      is_msvc_assembler = true
258
259      if (toolchain_args.current_cpu == "arm64") {
260        if (toolchain_is_clang) {
261          ml = "${cl_prefix}${_clang_bin_path}/clang-cl${_exe} --target=aarch64-pc-windows"
262          if (host_os == "win") {
263            # Flip the slashes so that copy/paste of the command works.
264            ml = string_replace(ml, "/", "\\")
265          }
266          ml += " -c -o{{output}} $show_includes"
267          is_msvc_assembler = false
268          depsformat = "msvc"
269        } else {
270          # Only affects Arm builds with is_clang = false, implemented for
271          # building V8 for Windows on Arm systems with the MSVC toolchain.
272          ml = "armasm64.exe"
273        }
274      } else {
275        if (toolchain_is_clang && !disable_llvm_ml) {
276          prefix = rebase_path("$clang_base_path/bin", root_build_dir)
277          ml = "$prefix/llvm-ml${_exe}"
278          if (toolchain_args.current_cpu == "x64") {
279            ml += " -m64"
280          } else {
281            ml += " -m32"
282          }
283        } else {
284          if (toolchain_args.current_cpu == "x64") {
285            ml = "ml64.exe"
286          } else {
287            ml = "ml.exe"
288          }
289        }
290      }
291
292      if (is_msvc_assembler) {
293        ml += " /nologo /Fo{{output}}"
294
295        # Suppress final-stage linking on x64/x86 builds. (Armasm64 does not
296        # require /c because it doesn't support linking.)
297        if (toolchain_args.current_cpu != "arm64") {
298          ml += " /c"
299        }
300        if (use_lld && (!toolchain_is_clang || disable_llvm_ml)) {
301          # Wrap ml(64).exe with a script that makes its output deterministic.
302          # It's lld only because the script zaps obj Timestamp which
303          # link.exe /incremental looks at.
304          ml_py = rebase_path("//build/toolchain/win/ml.py", root_build_dir)
305          ml = "\"$python_path\" $ml_py $ml"
306        }
307
308        if (toolchain_args.current_cpu == "arm64") {
309          # armasm64.exe does not support definitions passed via the command
310          # line. (Fortunately, they're not needed for compiling the V8
311          # snapshot, which is the only time this assembler is required.)
312          command = "\"$python_path\" $_tool_wrapper_path asm-wrapper $env $ml {{include_dirs}} {{asmflags}} {{source}}"
313        } else {
314          command = "\"$python_path\" $_tool_wrapper_path asm-wrapper $env $ml {{defines}} {{include_dirs}} {{asmflags}} {{source}}"
315        }
316      } else {
317        command = "$ml {{defines}} {{include_dirs}} {{asmflags}} {{source}}"
318      }
319
320      description = "ASM {{output}}"
321      outputs = [ "$object_subdir/{{source_name_part}}.obj" ]
322    }
323
324    if (toolchain_has_rust) {
325      rust_sysroot_relative = rebase_path(rust_sysroot, root_build_dir)
326      rustc = "$rust_sysroot_relative/bin/rustc"
327      rustc_wrapper =
328          rebase_path("//build/rust/rustc_wrapper.py", root_build_dir)
329      rustc_windows_args = " -Clinker=$link$rust_linkflags $rustc_common_args"
330
331      tool("rust_staticlib") {
332        libname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
333        rspfile = "$libname.rsp"
334        depfile = "$libname.d"
335
336        default_output_extension = ".lib"
337        output_prefix = "lib"
338
339        # Static libraries go in the target out directory by default so we can
340        # generate different targets with the same name and not have them
341        # collide.
342        default_output_dir = "{{target_out_dir}}"
343        description = "RUST(STATICLIB) {{output}}"
344        outputs = [ libname ]
345
346        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
347        command = "\"$python_path\" \"$rustc_wrapper\" --target-windows --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- $rustc_windows_args --emit=dep-info=$depfile,link -o $libname LDFLAGS RUSTENV {{rustenv}}"
348        rust_sysroot = rust_sysroot_relative
349      }
350
351      tool("rust_rlib") {
352        # We must always prefix with `lib` even if the library already starts
353        # with that prefix or else our stdlib is unable to find libc.rlib (or
354        # actually liblibc.rlib).
355        rlibname =
356            "{{output_dir}}/lib{{target_output_name}}{{output_extension}}"
357        rspfile = "$rlibname.rsp"
358        depfile = "$rlibname.d"
359
360        default_output_extension = ".rlib"
361
362        # This is prefixed unconditionally in `rlibname`.
363        # output_prefix = "lib"
364
365        # Static libraries go in the target out directory by default so we can
366        # generate different targets with the same name and not have them
367        # collide.
368        default_output_dir = "{{target_out_dir}}"
369        description = "RUST {{output}}"
370        outputs = [ rlibname ]
371
372        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
373        command = "\"$python_path\" \"$rustc_wrapper\" --target-windows --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- $rustc_windows_args --emit=dep-info=$depfile,link -o $rlibname {{rustdeps}} {{externs}} LDFLAGS RUSTENV {{rustenv}}"
374        rust_sysroot = rust_sysroot_relative
375      }
376
377      tool("rust_bin") {
378        exename = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
379        pdbname = "$exename.pdb"
380        rspfile = "$exename.rsp"
381        depfile = "$exename.d"
382        pool = "//build/toolchain:link_pool($default_toolchain)"
383
384        default_output_extension = ".exe"
385        default_output_dir = "{{root_out_dir}}"
386        description = "RUST(BIN) {{output}}"
387        outputs = [
388          # The first entry here is used for dependency tracking.
389          exename,
390          pdbname,
391        ]
392        runtime_outputs = outputs
393
394        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
395        dynamic_link_switch = ""
396        command = "\"$python_path\" \"$rustc_wrapper\" --target-windows --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- $rustc_windows_args --emit=dep-info=$depfile,link -o $exename LDFLAGS {{ldflags}} $sys_lib_flags /PDB:$pdbname RUSTENV {{rustenv}}"
397        rust_sysroot = rust_sysroot_relative
398      }
399
400      tool("rust_cdylib") {
401        # E.g. "foo.dll":
402        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
403        libname = "$dllname.lib"  # e.g. foo.dll.lib
404        pdbname = "$dllname.pdb"
405        rspfile = "$dllname.rsp"
406        depfile = "$dllname.d"
407        pool = "//build/toolchain:link_pool($default_toolchain)"
408
409        default_output_extension = ".dll"
410        default_output_dir = "{{root_out_dir}}"
411        description = "RUST(CDYLIB) {{output}}"
412        outputs = [
413          # The first entry here is used for dependency tracking. Dylibs are
414          # linked into other targets and that linking must be done through
415          # the .lib file, not the .dll file. So the .lib file is the primary
416          # output here.
417          libname,
418          dllname,
419          pdbname,
420        ]
421        runtime_outputs = [
422          dllname,
423          pdbname,
424        ]
425
426        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
427        dynamic_link_switch = ""
428        command = "\"$python_path\" \"$rustc_wrapper\" --target-windows --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- $rustc_windows_args --emit=dep-info=$depfile,link -o $dllname LDFLAGS {{ldflags}} $sys_lib_flags /PDB:$pdbname /IMPLIB:$libname RUSTENV {{rustenv}}"
429        rust_sysroot = rust_sysroot_relative
430
431        # Since the above commands only updates the .lib file when it changes,
432        # ask Ninja to check if the timestamp actually changed to know if
433        # downstream dependencies should be recompiled.
434        restat = true
435      }
436
437      tool("rust_macro") {
438        # E.g. "foo.dll":
439        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
440        pdbname = "$dllname.pdb"
441        rspfile = "$dllname.rsp"
442        depfile = "$dllname.d"
443        pool = "//build/toolchain:link_pool($default_toolchain)"
444
445        default_output_extension = ".dll"
446        default_output_dir = "{{root_out_dir}}"
447        description = "RUST(MACRO) {{output}}"
448        outputs = [
449          # The first entry here is used for dependency tracking. Proc macros
450          # are consumed as dlls directly, loaded a runtime, so the dll is the
451          # primary output here. If we make a .lib file the primary output, we
452          # end up trying to load the .lib file as a procmacro which fails.
453          #
454          # Since depending on a macro target for linking would fail (it would
455          # try to link primary .dll target) we omit the .lib here entirely.
456          dllname,
457          pdbname,
458        ]
459        runtime_outputs = outputs
460
461        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
462        dynamic_link_switch = ""
463        command = "\"$python_path\" \"$rustc_wrapper\" --target-windows --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- $rustc_windows_args --emit=dep-info=$depfile,link -o $dllname LDFLAGS {{ldflags}} $sys_lib_flags /PDB:$pdbname RUSTENV {{rustenv}}"
464        rust_sysroot = rust_sysroot_relative
465
466        # Since the above commands only updates the .lib file when it changes,
467        # ask Ninja to check if the timestamp actually changed to know if
468        # downstream dependencies should be recompiled.
469        restat = true
470      }
471    }
472
473    tool("alink") {
474      rspfile = "{{output}}.rsp"
475      command = "$linker_wrapper$lib \"/OUT:{{output}}\" /nologo {{arflags}} \"@$rspfile\""
476      description = "LIB {{output}}"
477      outputs = [
478        # Ignore {{output_extension}} and always use .lib, there's no reason to
479        # allow targets to override this extension on Windows.
480        "{{output_dir}}/{{target_output_name}}.lib",
481      ]
482      default_output_extension = ".lib"
483      default_output_dir = "{{target_out_dir}}"
484
485      # The use of inputs_newline is to work around a fixed per-line buffer
486      # size in the linker.
487      rspfile_content = "{{inputs_newline}}"
488    }
489
490    tool("solink") {
491      # E.g. "foo.dll":
492      dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
493      libname = "${dllname}.lib"  # e.g. foo.dll.lib
494      pdbname = "${dllname}.pdb"
495      rspfile = "${dllname}.rsp"
496      pool = "//build/toolchain:link_pool($default_toolchain)"
497
498      command = "$linker_wrapper$link$cc_linkflags \"/OUT:$dllname\" /nologo ${sys_lib_flags} \"/IMPLIB:$libname\" /DLL \"/PDB:$pdbname\" \"@$rspfile\""
499
500      default_output_extension = ".dll"
501      default_output_dir = "{{root_out_dir}}"
502      description = "LINK(DLL) {{output}}"
503      outputs = [
504        dllname,
505        libname,
506        pdbname,
507      ]
508      link_output = libname
509      depend_output = libname
510      runtime_outputs = [
511        dllname,
512        pdbname,
513      ]
514
515      # Since the above commands only updates the .lib file when it changes,
516      # ask Ninja to check if the timestamp actually changed to know if
517      # downstream dependencies should be recompiled.
518      restat = true
519
520      # The use of inputs_newline is to work around a fixed per-line buffer
521      # size in the linker.
522      rspfile_content =
523          "{{libs}} {{solibs}} {{inputs_newline}} {{ldflags}} {{rlibs}}"
524    }
525
526    tool("solink_module") {
527      # E.g. "foo.dll":
528      dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
529      pdbname = "${dllname}.pdb"
530      rspfile = "${dllname}.rsp"
531      pool = "//build/toolchain:link_pool($default_toolchain)"
532
533      command = "$linker_wrapper$link$cc_linkflags \"/OUT:$dllname\" /nologo ${sys_lib_flags} /DLL \"/PDB:$pdbname\" \"@$rspfile\""
534
535      default_output_extension = ".dll"
536      default_output_dir = "{{root_out_dir}}"
537      description = "LINK_MODULE(DLL) {{output}}"
538      outputs = [
539        dllname,
540        pdbname,
541      ]
542      runtime_outputs = outputs
543
544      # The use of inputs_newline is to work around a fixed per-line buffer
545      # size in the linker.
546      rspfile_content =
547          "{{libs}} {{solibs}} {{inputs_newline}} {{ldflags}} {{rlibs}}"
548    }
549
550    tool("link") {
551      exename = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
552      pdbname = "$exename.pdb"
553      rspfile = "$exename.rsp"
554      pool = "//build/toolchain:link_pool($default_toolchain)"
555
556      command = "$linker_wrapper$link$cc_linkflags \"/OUT:$exename\" /nologo ${sys_lib_flags} \"/PDB:$pdbname\" \"@$rspfile\""
557
558      default_output_extension = ".exe"
559      default_output_dir = "{{root_out_dir}}"
560      description = "LINK {{output}}"
561      outputs = [
562        exename,
563        pdbname,
564      ]
565      runtime_outputs = outputs
566
567      # The use of inputs_newline is to work around a fixed per-line buffer
568      # size in the linker.
569      rspfile_content =
570          "{{inputs_newline}} {{libs}} {{solibs}} {{ldflags}} {{rlibs}}"
571    }
572
573    # These two are really entirely generic, but have to be repeated in
574    # each toolchain because GN doesn't allow a template to be used here.
575    # See //build/toolchain/toolchain.gni for details.
576    tool("stamp") {
577      command = stamp_command
578      description = stamp_description
579      pool = "//build/toolchain:action_pool($default_toolchain)"
580    }
581    tool("copy") {
582      command = copy_command
583      description = copy_description
584      pool = "//build/toolchain:action_pool($default_toolchain)"
585    }
586
587    tool("action") {
588      pool = "//build/toolchain:action_pool($default_toolchain)"
589    }
590  }
591}
592
593# Make an additional toolchain which is used for making tools that are run
594# on the host machine as part of the build process (such as proc macros
595# and Cargo build scripts). This toolchain uses the prebuilt stdlib that
596# comes with the compiler, so it doesn't have to wait for the stdlib to be
597# built before building other stuff. And this ensures its proc macro
598# outputs have the right ABI to be loaded by the compiler, and it can be
599# used to compile build scripts that are part of the stdlib that is built
600# for the default toolchain.
601template("msvc_rust_host_build_tools_toolchain") {
602  msvc_toolchain(target_name) {
603    assert(defined(invoker.toolchain_args))
604    forward_variables_from(invoker,
605                           "*",
606                           [
607                             "toolchain_args",
608                             "visibility",
609                             "testonly",
610                           ])
611    toolchain_args = {
612      # Populate toolchain args from the invoker.
613      forward_variables_from(invoker.toolchain_args, "*")
614      toolchain_for_rust_host_build_tools = true
615
616      # The host build tools are static release builds to make the Chromium
617      # build faster. They do not need PGO etc, so no official builds.
618      is_debug = false
619      is_component_build = false
620      is_official_build = false
621      use_clang_coverage = false
622      use_sanitizer_coverage = false
623      generate_linker_map = false
624      use_thin_lto = false
625    }
626  }
627}
628
629# If PartitionAlloc is part of the build (even as a transitive dependency), then
630# it replaces the system allocator. If this toolchain is used, that will be
631# overridden and the system allocator will be used regardless. This is important
632# in some third-party binaries outside of Chrome.
633template("msvc_system_allocator_toolchain") {
634  msvc_toolchain(target_name) {
635    assert(defined(invoker.toolchain_args))
636    forward_variables_from(invoker,
637                           "*",
638                           [
639                             "toolchain_args",
640                             "visibility",
641                             "testonly",
642                           ])
643    toolchain_args = {
644      # Populate toolchain args from the invoker.
645      forward_variables_from(invoker.toolchain_args, "*")
646      toolchain_allows_use_partition_alloc_as_malloc = false
647
648      # Disable component build so that we can copy the exes to the
649      # root_build_dir and support the default_toolchain redirection on Windows.
650      # See also the comment in //build/symlink.gni.
651      is_component_build = false
652
653      # Only one toolchain can be configured with MSAN support with our current
654      # GN setup, or they all try to make the instrumented libraries and
655      # collide.
656      is_msan = false
657    }
658  }
659}
660
661template("win_toolchains") {
662  # On Windows, cross-compile for x86 changes the `host_toolchain`
663  # into x86 too so as to avoid compiling things twice (see
664  # //build/config/BUILDCONFIG.gn). But the prebuilt stdlib does not
665  # exist for Windows x86 and it's exceedingly difficult to get it
666  # built from a single build_rust.py invocation. So we just don't follow
667  # along in the `build_tools_toolchain` toolchains, and always use the host
668  # cpu type (which will be x64 in that case). Things built with these
669  # toolchains are never built for the target_cpu anyhow, so the optimization
670  # there does not benefit them.
671  #
672  # Thus, in build_tools_toolchain, for the host machine:
673  # * Use `rust_host_toolchain_arch` instead of `toolchain_arch`.
674  # * Use `rust_host_win_toolchain_data` instead of `win_toolchain_data`.
675
676  assert(defined(invoker.toolchain_arch))
677  toolchain_arch = invoker.toolchain_arch
678  rust_host_toolchain_arch = host_cpu
679
680  # The toolchain data for `msvc_toolchain()`.
681  if (toolchain_arch == "x86") {
682    win_toolchain_data = win_toolchain_data_x86
683  } else if (toolchain_arch == "x64") {
684    win_toolchain_data = win_toolchain_data_x64
685  } else if (toolchain_arch == "arm64") {
686    win_toolchain_data = win_toolchain_data_arm64
687  } else {
688    error("Unsupported toolchain_arch, add it to win_toolchain_data.gni")
689  }
690
691  # The toolchain data for `build_tools_toolchain` for the host machine.
692  if (rust_host_toolchain_arch == "x86") {
693    rust_host_win_toolchain_data = win_toolchain_data_x86
694  } else if (rust_host_toolchain_arch == "x64") {
695    rust_host_win_toolchain_data = win_toolchain_data_x64
696  } else if (rust_host_toolchain_arch == "arm64") {
697    rust_host_win_toolchain_data = win_toolchain_data_arm64
698  } else {
699    error(
700        "Unsupported rust_host_toolchain_arch, add it to win_toolchain_data.gni")
701  }
702
703  # The toolchain using MSVC only makes sense when not doing cross builds.
704  # Chromium exclusively uses the win_clang_ toolchain below, but V8 and
705  # WebRTC still use this MSVC toolchain in some cases.
706  if (host_os == "win") {
707    if (defined(invoker.cl_toolchain_prefix)) {
708      cl_toolchain_prefix = invoker.cl_toolchain_prefix
709    } else {
710      cl_toolchain_prefix = ""
711    }
712    msvc_toolchain(cl_toolchain_prefix + target_name) {
713      environment = "environment." + toolchain_arch
714      cl = "\"${win_toolchain_data.vc_bin_dir}/cl.exe\""
715
716      toolchain_args = {
717        if (defined(invoker.toolchain_args)) {
718          forward_variables_from(invoker.toolchain_args, "*")
719        }
720        is_clang = false
721        use_clang_coverage = false
722        current_os = "win"
723        current_cpu = toolchain_arch
724      }
725    }
726    msvc_system_allocator_toolchain(
727        cl_toolchain_prefix + target_name + "_host_with_system_allocator") {
728      environment = "environment." + rust_host_toolchain_arch
729      cl = "\"${rust_host_win_toolchain_data.vc_bin_dir}/cl.exe\""
730
731      toolchain_args = {
732        if (defined(invoker.toolchain_args)) {
733          forward_variables_from(invoker.toolchain_args, "*")
734        }
735        is_clang = false
736        use_clang_coverage = false
737        current_os = "win"
738        current_cpu = rust_host_toolchain_arch
739      }
740    }
741    msvc_system_allocator_toolchain(
742        cl_toolchain_prefix + target_name + "_with_system_allocator") {
743      environment = "environment." + toolchain_arch
744      cl = "\"${win_toolchain_data.vc_bin_dir}/cl.exe\""
745
746      toolchain_args = {
747        if (defined(invoker.toolchain_args)) {
748          forward_variables_from(invoker.toolchain_args, "*")
749        }
750        is_clang = false
751        use_clang_coverage = false
752        current_os = "win"
753        current_cpu = toolchain_arch
754      }
755    }
756    msvc_rust_host_build_tools_toolchain(
757        cl_toolchain_prefix + target_name + "_for_rust_host_build_tools") {
758      environment = "environment." + rust_host_toolchain_arch
759      cl = "\"${rust_host_win_toolchain_data.vc_bin_dir}/cl.exe\""
760
761      toolchain_args = {
762        if (defined(invoker.toolchain_args)) {
763          forward_variables_from(invoker.toolchain_args, "*")
764        }
765        is_clang = false
766        use_clang_coverage = false
767        current_os = "win"
768        current_cpu = rust_host_toolchain_arch
769      }
770    }
771  }
772
773  if (defined(invoker.clang_toolchain_prefix)) {
774    clang_toolchain_prefix = invoker.clang_toolchain_prefix
775  } else {
776    clang_toolchain_prefix = "win_clang_"
777  }
778
779  _clang_lib_dir =
780      rebase_path("$clang_base_path/lib/clang/$clang_version/lib/windows",
781                  root_build_dir)
782  if (host_os == "win") {
783    # And to match the other -libpath flags.
784    _clang_lib_dir = string_replace(_clang_lib_dir, "/", "\\")
785  }
786
787  msvc_toolchain(clang_toolchain_prefix + target_name) {
788    environment = "environment." + toolchain_arch
789    cl = "${_clang_bin_path}/clang-cl${_exe}"
790
791    sys_include_flags = "${win_toolchain_data.include_flags_imsvc}"
792    if (use_lld) {
793      sys_lib_flags = "-libpath:$_clang_lib_dir " +
794                      "${win_toolchain_data.libpath_lldlink_flags}"
795    }
796
797    toolchain_args = {
798      if (defined(invoker.toolchain_args)) {
799        forward_variables_from(invoker.toolchain_args, "*")
800      }
801      is_clang = true
802      current_os = "win"
803      current_cpu = toolchain_arch
804    }
805  }
806  msvc_system_allocator_toolchain(
807      clang_toolchain_prefix + target_name + "_host_with_system_allocator") {
808    environment = "environment." + rust_host_toolchain_arch
809    cl = "${_clang_bin_path}/clang-cl${_exe}"
810
811    sys_include_flags = "${rust_host_win_toolchain_data.include_flags_imsvc}"
812    if (use_lld) {
813      sys_lib_flags = "-libpath:$_clang_lib_dir " +
814                      "${rust_host_win_toolchain_data.libpath_lldlink_flags}"
815    }
816
817    toolchain_args = {
818      if (defined(invoker.toolchain_args)) {
819        forward_variables_from(invoker.toolchain_args, "*")
820      }
821      is_clang = true
822      current_os = "win"
823      current_cpu = rust_host_toolchain_arch
824    }
825  }
826  msvc_system_allocator_toolchain(
827      clang_toolchain_prefix + target_name + "_with_system_allocator") {
828    environment = "environment." + toolchain_arch
829    cl = "${_clang_bin_path}/clang-cl${_exe}"
830
831    sys_include_flags = "${win_toolchain_data.include_flags_imsvc}"
832    if (use_lld) {
833      sys_lib_flags = "-libpath:$_clang_lib_dir " +
834                      "${win_toolchain_data.libpath_lldlink_flags}"
835    }
836
837    toolchain_args = {
838      if (defined(invoker.toolchain_args)) {
839        forward_variables_from(invoker.toolchain_args, "*")
840      }
841      is_clang = true
842      current_os = "win"
843      current_cpu = toolchain_arch
844    }
845  }
846  msvc_rust_host_build_tools_toolchain(
847      clang_toolchain_prefix + target_name + "_for_rust_host_build_tools") {
848    environment = "environment." + rust_host_toolchain_arch
849    cl = "${_clang_bin_path}/clang-cl${_exe}"
850
851    sys_include_flags = "${rust_host_win_toolchain_data.include_flags_imsvc}"
852    if (use_lld) {
853      sys_lib_flags = "-libpath:$_clang_lib_dir " +
854                      "${rust_host_win_toolchain_data.libpath_lldlink_flags}"
855    }
856
857    toolchain_args = {
858      if (defined(invoker.toolchain_args)) {
859        forward_variables_from(invoker.toolchain_args, "*")
860      }
861      is_clang = true
862      current_os = "win"
863      current_cpu = rust_host_toolchain_arch
864    }
865  }
866}
867