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
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624
  625
  626
  627
  628
  629
  630
  631
  632
  633
  634
  635
  636
  637
  638
  639
  640
  641
  642
  643
  644
  645
  646
  647
  648
  649
  650
  651
  652
  653
  654
  655
  656
  657
  658
  659
  660
  661
  662
  663
  664
  665
  666
  667
  668
  669
  670
  671
  672
  673
  674
  675
  676
  677
  678
  679
  680
  681
  682
  683
  684
  685
  686
  687
  688
  689
  690
  691
  692
  693
  694
  695
  696
  697
  698
  699
  700
  701
  702
  703
  704
  705
  706
  707
  708
  709
  710
  711
  712
  713
  714
  715
  716
  717
  718
  719
  720
  721
  722
  723
  724
  725
  726
  727
  728
  729
  730
  731
  732
  733
  734
  735
  736
  737
  738
  739
  740
  741
  742
  743
  744
  745
  746
  747
  748
  749
  750
  751
  752
  753
  754
  755
  756
  757
  758
  759
  760
  761
  762
  763
  764
  765
  766
  767
  768
  769
  770
  771
  772
  773
  774
  775
  776
  777
  778
  779
  780
  781
  782
  783
  784
  785
  786
  787
  788
  789
  790
  791
  792
  793
  794
  795
  796
  797
  798
  799
  800
  801
  802
  803
  804
  805
  806
  807
  808
  809
  810
  811
  812
  813
  814
  815
  816
  817
  818
  819
  820
  821
  822
  823
  824
  825
  826
  827
  828
  829
  830
  831
  832
  833
  834
  835
  836
  837
  838
  839
  840
  841
  842
  843
  844
  845
  846
  847
  848
  849
  850
  851
  852
  853
  854
  855
  856
  857
  858
  859
  860
  861
  862
  863
  864
  865
  866

build / toolchain / win / toolchain.gni [blame]

# Copyright 2022 The Chromium Authors
# 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/rust.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/config/win/visual_studio_version.gni")
import("//build/toolchain/cc_wrapper.gni")
import("//build/toolchain/rbe.gni")
import("//build/toolchain/toolchain.gni")
import("//build/toolchain/win/win_toolchain_data.gni")

assert(is_win, "Should only be running on Windows")

# This tool will is used as a wrapper for various commands below.
_tool_wrapper_path =
    rebase_path("//build/toolchain/win/tool_wrapper.py", root_build_dir)

if (host_os == "win") {
  _exe = ".exe"
} else {
  _exe = ""
}

_clang_bin_path = rebase_path("$clang_base_path/bin", root_build_dir)

# Makes a single MSVC toolchain.
#
# Parameters:
#   environment: File name of environment file.
#
# You would also define a toolchain_args variable with at least these set:
#   current_cpu: current_cpu to pass as a build arg
#   current_os: current_os to pass as a build arg
template("msvc_toolchain") {
  toolchain(target_name) {
    # 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.
    assert(defined(invoker.toolchain_args))
    toolchain_args = {
      forward_variables_from(invoker.toolchain_args, "*")

      # This value needs to be passed through unchanged.
      host_toolchain = host_toolchain
    }

    if (defined(toolchain_args.is_clang)) {
      toolchain_is_clang = toolchain_args.is_clang
    } else {
      toolchain_is_clang = is_clang
    }

    # When the invoker has explicitly overridden use_reclient or cc_wrapper in
    # the toolchain args, use those values, otherwise default to the global one.
    # This works because the only reasonable override that toolchains might
    # supply for these values are to force-disable them.
    if (defined(toolchain_args.use_reclient)) {
      toolchain_uses_reclient = toolchain_args.use_reclient
    } else {
      toolchain_uses_reclient = use_reclient
    }
    if (defined(toolchain_args.cc_wrapper)) {
      toolchain_cc_wrapper = toolchain_args.cc_wrapper
    } else {
      toolchain_cc_wrapper = cc_wrapper
    }
    assert(!(toolchain_cc_wrapper != "" && toolchain_uses_reclient),
           "re-client and cc_wrapper can't be used together.")

    if (toolchain_uses_reclient) {
      if (toolchain_is_clang) {
        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 "
      } else {
        cl_prefix = ""
      }
    } else if (toolchain_cc_wrapper != "" && toolchain_is_clang) {
      cl_prefix = toolchain_cc_wrapper + " "
    } else {
      cl_prefix = ""
    }

    cl = "${cl_prefix}${invoker.cl}"
    if (host_os == "win") {
      # Flip the slashes so that copy/paste of the command works.
      cl = string_replace(cl, "/", "\\")
    }

    # Make these apply to all tools below.
    lib_switch = ""
    lib_dir_switch = "/LIBPATH:"

    # Object files go in this directory.
    object_subdir = "{{target_out_dir}}/{{label_name}}"

    env = invoker.environment

    if (use_lld) {
      # lld-link includes a replacement for lib.exe that can produce thin
      # archives and understands bitcode (for lto builds).
      link = "${_clang_bin_path}/lld-link${_exe}"
      cc_linkflags = ""
      if (toolchain_has_rust) {
        rust_linkflags = ""
      }
      if (host_os == "win") {
        # Flip the slashes so that copy/paste of the commands works.
        link = string_replace(link, "/", "\\")
      }
      lib = "$link /lib"
      if (host_os != "win") {
        # See comment adding --rsp-quoting to $cl above for more information.
        cc_linkflags += " --rsp-quoting=posix"
        if (toolchain_has_rust) {
          rust_linkflags += " -Clink-arg=--rsp-quoting=posix"
        }
      }
    } else {
      lib = "lib.exe"
      link = "link.exe"
      cc_linkflags = ""
      if (toolchain_has_rust) {
        rust_linkflags = ""
      }
    }

    # If possible, pass system includes as flags to the compiler.  When that's
    # not possible, load a full environment file (containing %INCLUDE% and
    # %PATH%) -- e.g. 32-bit MSVS builds require %PATH% to be set and just
    # passing in a list of include directories isn't enough.
    if (defined(invoker.sys_include_flags)) {
      env_wrapper = ""
      sys_include_flags =
          "${invoker.sys_include_flags} "  # Note trailing space.
    } else {
      # clang-cl doesn't need this env hoop, so omit it there.
      assert(!toolchain_is_clang)
      env_wrapper = "ninja -t msvc -e $env -- "  # Note trailing space.
      sys_include_flags = ""
    }

    if (host_os != "win" || (use_lld && defined(invoker.sys_lib_flags))) {
      linker_wrapper = ""
      sys_lib_flags = "${invoker.sys_lib_flags}"

      # TODO(thakis): Remove once crbug.com/1300005 is fixed
      assert(toolchain_args.current_cpu == "x64" ||
                 toolchain_args.current_cpu == "x86" ||
                 toolchain_args.current_cpu == "arm" ||
                 toolchain_args.current_cpu == "arm64",
             "Only supports x64, x86, arm and arm64 CPUs")
      if (toolchain_args.current_cpu == "x64") {
        sys_lib_flags += " /MACHINE:X64"
      } else if (toolchain_args.current_cpu == "x86") {
        sys_lib_flags += " /MACHINE:X86"
      } else if (toolchain_args.current_cpu == "arm") {
        sys_lib_flags += " /MACHINE:ARM"
      } else if (toolchain_args.current_cpu == "arm64") {
        sys_lib_flags += " /MACHINE:ARM64"
      }

      sys_lib_flags += " "  # Note trailing space.
    } else {
      # link.exe must be run under a wrapper to set up the environment
      # (it needs %LIB% set to find libraries), and to work around its bugs.
      # Note trailing space:
      linker_wrapper =
          "\"$python_path\" $_tool_wrapper_path link-wrapper $env False "
      sys_lib_flags = ""
    }

    if (defined(toolchain_args.use_clang_coverage)) {
      toolchain_use_clang_coverage = toolchain_args.use_clang_coverage
    } else {
      toolchain_use_clang_coverage = use_clang_coverage
    }

    if (toolchain_use_clang_coverage) {
      assert(toolchain_is_clang,
             "use_clang_coverage should only be used with Clang")
      if (defined(toolchain_args.coverage_instrumentation_input_file)) {
        toolchain_coverage_instrumentation_input_file =
            toolchain_args.coverage_instrumentation_input_file
      } else {
        toolchain_coverage_instrumentation_input_file =
            coverage_instrumentation_input_file
      }

      coverage_wrapper =
          rebase_path("//build/toolchain/clang_code_coverage_wrapper.py",
                      root_build_dir)
      coverage_wrapper = coverage_wrapper + " --target-os=" + target_os
      if (toolchain_coverage_instrumentation_input_file != "") {
        coverage_wrapper =
            coverage_wrapper + " --files-to-instrument=" +
            rebase_path(toolchain_coverage_instrumentation_input_file,
                        root_build_dir)
      }
      coverage_wrapper = "\"$python_path\" " + coverage_wrapper + " "
    } else {
      coverage_wrapper = ""
    }

    # Disabled with cc_wrapper because of
    # https://github.com/mozilla/sccache/issues/1013
    if (toolchain_is_clang && toolchain_cc_wrapper == "") {
      # This flag omits system includes from /showIncludes output, to reduce
      # the amount of data to parse and store in .ninja_deps. We do this on
      # non-Windows too, and already make sure rebuilds after winsdk/libc++/
      # clang header updates happen via changing command line flags.
      show_includes = "/showIncludes:user"
    } else {
      show_includes = "/showIncludes"
    }

    tool("cc") {
      precompiled_header_type = "msvc"
      pdbname = "{{target_out_dir}}/{{label_name}}_c.pdb"

      # Label names may have spaces in them so the pdbname must be quoted. The
      # source and output don't need to be quoted because GN knows they're a
      # full file name and will quote automatically when necessary.
      depsformat = "msvc"
      description = "CC {{output}}"
      outputs = [ "$object_subdir/{{source_name_part}}.obj" ]

      # Note that the code coverage wrapper scripts assumes that {{source}}
      # comes immediately after /c.
      command = "$coverage_wrapper$env_wrapper$cl /c {{source}} /nologo $show_includes $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} /Fo{{output}} /Fd\"$pdbname\""
    }

    tool("cxx") {
      precompiled_header_type = "msvc"

      # The PDB name needs to be different between C and C++ compiled files.
      pdbname = "{{target_out_dir}}/{{label_name}}_cc.pdb"

      # See comment in CC tool about quoting.
      depsformat = "msvc"
      description = "CXX {{output}}"
      outputs = [ "$object_subdir/{{source_name_part}}.obj" ]

      # Note that the code coverage wrapper scripts assumes that {{source}}
      # comes immediately after /c.
      command = "$coverage_wrapper$env_wrapper$cl /c {{source}} /Fo{{output}} /nologo $show_includes $sys_include_flags{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} /Fd\"$pdbname\""
    }

    tool("rc") {
      command = "\"$python_path\" $_tool_wrapper_path rc-wrapper $env rc.exe /nologo $sys_include_flags{{defines}} {{include_dirs}} /fo{{output}} {{source}}"
      depsformat = "msvc"
      outputs = [ "$object_subdir/{{source_name_part}}.res" ]
      description = "RC {{output}}"
    }

    tool("asm") {
      is_msvc_assembler = true

      if (toolchain_args.current_cpu == "arm64") {
        if (toolchain_is_clang) {
          ml = "${cl_prefix}${_clang_bin_path}/clang-cl${_exe} --target=aarch64-pc-windows"
          if (host_os == "win") {
            # Flip the slashes so that copy/paste of the command works.
            ml = string_replace(ml, "/", "\\")
          }
          ml += " -c -o{{output}} $show_includes"
          is_msvc_assembler = false
          depsformat = "msvc"
        } else {
          # Only affects Arm builds with is_clang = false, implemented for
          # building V8 for Windows on Arm systems with the MSVC toolchain.
          ml = "armasm64.exe"
        }
      } else {
        if (toolchain_is_clang && !disable_llvm_ml) {
          prefix = rebase_path("$clang_base_path/bin", root_build_dir)
          ml = "$prefix/llvm-ml${_exe}"
          if (toolchain_args.current_cpu == "x64") {
            ml += " -m64"
          } else {
            ml += " -m32"
          }
        } else {
          if (toolchain_args.current_cpu == "x64") {
            ml = "ml64.exe"
          } else {
            ml = "ml.exe"
          }
        }
      }

      if (is_msvc_assembler) {
        ml += " /nologo /Fo{{output}}"

        # Suppress final-stage linking on x64/x86 builds. (Armasm64 does not
        # require /c because it doesn't support linking.)
        if (toolchain_args.current_cpu != "arm64") {
          ml += " /c"
        }
        if (use_lld && (!toolchain_is_clang || disable_llvm_ml)) {
          # Wrap ml(64).exe with a script that makes its output deterministic.
          # It's lld only because the script zaps obj Timestamp which
          # link.exe /incremental looks at.
          ml_py = rebase_path("//build/toolchain/win/ml.py", root_build_dir)
          ml = "\"$python_path\" $ml_py $ml"
        }

        if (toolchain_args.current_cpu == "arm64") {
          # armasm64.exe does not support definitions passed via the command
          # line. (Fortunately, they're not needed for compiling the V8
          # snapshot, which is the only time this assembler is required.)
          command = "\"$python_path\" $_tool_wrapper_path asm-wrapper $env $ml {{include_dirs}} {{asmflags}} {{source}}"
        } else {
          command = "\"$python_path\" $_tool_wrapper_path asm-wrapper $env $ml {{defines}} {{include_dirs}} {{asmflags}} {{source}}"
        }
      } else {
        command = "$ml {{defines}} {{include_dirs}} {{asmflags}} {{source}}"
      }

      description = "ASM {{output}}"
      outputs = [ "$object_subdir/{{source_name_part}}.obj" ]
    }

    if (toolchain_has_rust) {
      rust_sysroot_relative = rebase_path(rust_sysroot, root_build_dir)
      rustc = "$rust_sysroot_relative/bin/rustc"
      rustc_wrapper =
          rebase_path("//build/rust/rustc_wrapper.py", root_build_dir)
      rustc_windows_args = " -Clinker=$link$rust_linkflags $rustc_common_args"

      tool("rust_staticlib") {
        libname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
        rspfile = "$libname.rsp"
        depfile = "$libname.d"

        default_output_extension = ".lib"
        output_prefix = "lib"

        # Static libraries go in the target out directory by default so we can
        # generate different targets with the same name and not have them
        # collide.
        default_output_dir = "{{target_out_dir}}"
        description = "RUST(STATICLIB) {{output}}"
        outputs = [ libname ]

        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
        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}}"
        rust_sysroot = rust_sysroot_relative
      }

      tool("rust_rlib") {
        # We must always prefix with `lib` even if the library already starts
        # with that prefix or else our stdlib is unable to find libc.rlib (or
        # actually liblibc.rlib).
        rlibname =
            "{{output_dir}}/lib{{target_output_name}}{{output_extension}}"
        rspfile = "$rlibname.rsp"
        depfile = "$rlibname.d"

        default_output_extension = ".rlib"

        # This is prefixed unconditionally in `rlibname`.
        # output_prefix = "lib"

        # Static libraries go in the target out directory by default so we can
        # generate different targets with the same name and not have them
        # collide.
        default_output_dir = "{{target_out_dir}}"
        description = "RUST {{output}}"
        outputs = [ rlibname ]

        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
        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}}"
        rust_sysroot = rust_sysroot_relative
      }

      tool("rust_bin") {
        exename = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
        pdbname = "$exename.pdb"
        rspfile = "$exename.rsp"
        depfile = "$exename.d"
        pool = "//build/toolchain:link_pool($default_toolchain)"

        default_output_extension = ".exe"
        default_output_dir = "{{root_out_dir}}"
        description = "RUST(BIN) {{output}}"
        outputs = [
          # The first entry here is used for dependency tracking.
          exename,
          pdbname,
        ]
        runtime_outputs = outputs

        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
        dynamic_link_switch = ""
        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}}"
        rust_sysroot = rust_sysroot_relative
      }

      tool("rust_cdylib") {
        # E.g. "foo.dll":
        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
        libname = "$dllname.lib"  # e.g. foo.dll.lib
        pdbname = "$dllname.pdb"
        rspfile = "$dllname.rsp"
        depfile = "$dllname.d"
        pool = "//build/toolchain:link_pool($default_toolchain)"

        default_output_extension = ".dll"
        default_output_dir = "{{root_out_dir}}"
        description = "RUST(CDYLIB) {{output}}"
        outputs = [
          # The first entry here is used for dependency tracking. Dylibs are
          # linked into other targets and that linking must be done through
          # the .lib file, not the .dll file. So the .lib file is the primary
          # output here.
          libname,
          dllname,
          pdbname,
        ]
        runtime_outputs = [
          dllname,
          pdbname,
        ]

        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
        dynamic_link_switch = ""
        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}}"
        rust_sysroot = rust_sysroot_relative

        # Since the above commands only updates the .lib file when it changes,
        # ask Ninja to check if the timestamp actually changed to know if
        # downstream dependencies should be recompiled.
        restat = true
      }

      tool("rust_macro") {
        # E.g. "foo.dll":
        dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
        pdbname = "$dllname.pdb"
        rspfile = "$dllname.rsp"
        depfile = "$dllname.d"
        pool = "//build/toolchain:link_pool($default_toolchain)"

        default_output_extension = ".dll"
        default_output_dir = "{{root_out_dir}}"
        description = "RUST(MACRO) {{output}}"
        outputs = [
          # The first entry here is used for dependency tracking. Proc macros
          # are consumed as dlls directly, loaded a runtime, so the dll is the
          # primary output here. If we make a .lib file the primary output, we
          # end up trying to load the .lib file as a procmacro which fails.
          #
          # Since depending on a macro target for linking would fail (it would
          # try to link primary .dll target) we omit the .lib here entirely.
          dllname,
          pdbname,
        ]
        runtime_outputs = outputs

        rspfile_content = "{{rustdeps}} {{externs}} SOURCES {{sources}}"
        dynamic_link_switch = ""
        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}}"
        rust_sysroot = rust_sysroot_relative

        # Since the above commands only updates the .lib file when it changes,
        # ask Ninja to check if the timestamp actually changed to know if
        # downstream dependencies should be recompiled.
        restat = true
      }
    }

    tool("alink") {
      rspfile = "{{output}}.rsp"
      command = "$linker_wrapper$lib \"/OUT:{{output}}\" /nologo {{arflags}} \"@$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.
        "{{output_dir}}/{{target_output_name}}.lib",
      ]
      default_output_extension = ".lib"
      default_output_dir = "{{target_out_dir}}"

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

    tool("solink") {
      # E.g. "foo.dll":
      dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
      libname = "${dllname}.lib"  # e.g. foo.dll.lib
      pdbname = "${dllname}.pdb"
      rspfile = "${dllname}.rsp"
      pool = "//build/toolchain:link_pool($default_toolchain)"

      command = "$linker_wrapper$link$cc_linkflags \"/OUT:$dllname\" /nologo ${sys_lib_flags} \"/IMPLIB:$libname\" /DLL \"/PDB:$pdbname\" \"@$rspfile\""

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

      # Since the above commands only updates the .lib file when it changes,
      # ask Ninja to check if the timestamp actually changed to know if
      # downstream dependencies should be recompiled.
      restat = true

      # 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}} {{rlibs}}"
    }

    tool("solink_module") {
      # E.g. "foo.dll":
      dllname = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
      pdbname = "${dllname}.pdb"
      rspfile = "${dllname}.rsp"
      pool = "//build/toolchain:link_pool($default_toolchain)"

      command = "$linker_wrapper$link$cc_linkflags \"/OUT:$dllname\" /nologo ${sys_lib_flags} /DLL \"/PDB:$pdbname\" \"@$rspfile\""

      default_output_extension = ".dll"
      default_output_dir = "{{root_out_dir}}"
      description = "LINK_MODULE(DLL) {{output}}"
      outputs = [
        dllname,
        pdbname,
      ]
      runtime_outputs = outputs

      # 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}} {{rlibs}}"
    }

    tool("link") {
      exename = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
      pdbname = "$exename.pdb"
      rspfile = "$exename.rsp"
      pool = "//build/toolchain:link_pool($default_toolchain)"

      command = "$linker_wrapper$link$cc_linkflags \"/OUT:$exename\" /nologo ${sys_lib_flags} \"/PDB:$pdbname\" \"@$rspfile\""

      default_output_extension = ".exe"
      default_output_dir = "{{root_out_dir}}"
      description = "LINK {{output}}"
      outputs = [
        exename,
        pdbname,
      ]
      runtime_outputs = outputs

      # 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}} {{rlibs}}"
    }

    # These two are really entirely generic, but have to be repeated in
    # each toolchain because GN doesn't allow a template to be used here.
    # See //build/toolchain/toolchain.gni for details.
    tool("stamp") {
      command = stamp_command
      description = stamp_description
      pool = "//build/toolchain:action_pool($default_toolchain)"
    }
    tool("copy") {
      command = copy_command
      description = copy_description
      pool = "//build/toolchain:action_pool($default_toolchain)"
    }

    tool("action") {
      pool = "//build/toolchain:action_pool($default_toolchain)"
    }
  }
}

# Make an additional toolchain which is used for making tools that are run
# on the host machine as part of the build process (such as proc macros
# and Cargo build scripts). This toolchain uses the prebuilt stdlib that
# comes with the compiler, so it doesn't have to wait for the stdlib to be
# built before building other stuff. And this ensures its proc macro
# outputs have the right ABI to be loaded by the compiler, and it can be
# used to compile build scripts that are part of the stdlib that is built
# for the default toolchain.
template("msvc_rust_host_build_tools_toolchain") {
  msvc_toolchain(target_name) {
    assert(defined(invoker.toolchain_args))
    forward_variables_from(invoker,
                           "*",
                           [
                             "toolchain_args",
                             "visibility",
                             "testonly",
                           ])
    toolchain_args = {
      # Populate toolchain args from the invoker.
      forward_variables_from(invoker.toolchain_args, "*")
      toolchain_for_rust_host_build_tools = true

      # The host build tools are static release builds to make the Chromium
      # build faster. They do not need PGO etc, so no official builds.
      is_debug = false
      is_component_build = false
      is_official_build = false
      use_clang_coverage = false
      use_sanitizer_coverage = false
      generate_linker_map = false
      use_thin_lto = false
    }
  }
}

# If PartitionAlloc is part of the build (even as a transitive dependency), then
# it replaces the system allocator. If this toolchain is used, that will be
# overridden and the system allocator will be used regardless. This is important
# in some third-party binaries outside of Chrome.
template("msvc_system_allocator_toolchain") {
  msvc_toolchain(target_name) {
    assert(defined(invoker.toolchain_args))
    forward_variables_from(invoker,
                           "*",
                           [
                             "toolchain_args",
                             "visibility",
                             "testonly",
                           ])
    toolchain_args = {
      # Populate toolchain args from the invoker.
      forward_variables_from(invoker.toolchain_args, "*")
      toolchain_allows_use_partition_alloc_as_malloc = false

      # Disable component build so that we can copy the exes to the
      # root_build_dir and support the default_toolchain redirection on Windows.
      # See also the comment in //build/symlink.gni.
      is_component_build = false

      # Only one toolchain can be configured with MSAN support with our current
      # GN setup, or they all try to make the instrumented libraries and
      # collide.
      is_msan = false
    }
  }
}

template("win_toolchains") {
  # On Windows, cross-compile for x86 changes the `host_toolchain`
  # into x86 too so as to avoid compiling things twice (see
  # //build/config/BUILDCONFIG.gn). But the prebuilt stdlib does not
  # exist for Windows x86 and it's exceedingly difficult to get it
  # built from a single build_rust.py invocation. So we just don't follow
  # along in the `build_tools_toolchain` toolchains, and always use the host
  # cpu type (which will be x64 in that case). Things built with these
  # toolchains are never built for the target_cpu anyhow, so the optimization
  # there does not benefit them.
  #
  # Thus, in build_tools_toolchain, for the host machine:
  # * Use `rust_host_toolchain_arch` instead of `toolchain_arch`.
  # * Use `rust_host_win_toolchain_data` instead of `win_toolchain_data`.

  assert(defined(invoker.toolchain_arch))
  toolchain_arch = invoker.toolchain_arch
  rust_host_toolchain_arch = host_cpu

  # The toolchain data for `msvc_toolchain()`.
  if (toolchain_arch == "x86") {
    win_toolchain_data = win_toolchain_data_x86
  } else if (toolchain_arch == "x64") {
    win_toolchain_data = win_toolchain_data_x64
  } else if (toolchain_arch == "arm64") {
    win_toolchain_data = win_toolchain_data_arm64
  } else {
    error("Unsupported toolchain_arch, add it to win_toolchain_data.gni")
  }

  # The toolchain data for `build_tools_toolchain` for the host machine.
  if (rust_host_toolchain_arch == "x86") {
    rust_host_win_toolchain_data = win_toolchain_data_x86
  } else if (rust_host_toolchain_arch == "x64") {
    rust_host_win_toolchain_data = win_toolchain_data_x64
  } else if (rust_host_toolchain_arch == "arm64") {
    rust_host_win_toolchain_data = win_toolchain_data_arm64
  } else {
    error(
        "Unsupported rust_host_toolchain_arch, add it to win_toolchain_data.gni")
  }

  # The toolchain using MSVC only makes sense when not doing cross builds.
  # Chromium exclusively uses the win_clang_ toolchain below, but V8 and
  # WebRTC still use this MSVC toolchain in some cases.
  if (host_os == "win") {
    if (defined(invoker.cl_toolchain_prefix)) {
      cl_toolchain_prefix = invoker.cl_toolchain_prefix
    } else {
      cl_toolchain_prefix = ""
    }
    msvc_toolchain(cl_toolchain_prefix + target_name) {
      environment = "environment." + toolchain_arch
      cl = "\"${win_toolchain_data.vc_bin_dir}/cl.exe\""

      toolchain_args = {
        if (defined(invoker.toolchain_args)) {
          forward_variables_from(invoker.toolchain_args, "*")
        }
        is_clang = false
        use_clang_coverage = false
        current_os = "win"
        current_cpu = toolchain_arch
      }
    }
    msvc_system_allocator_toolchain(
        cl_toolchain_prefix + target_name + "_host_with_system_allocator") {
      environment = "environment." + rust_host_toolchain_arch
      cl = "\"${rust_host_win_toolchain_data.vc_bin_dir}/cl.exe\""

      toolchain_args = {
        if (defined(invoker.toolchain_args)) {
          forward_variables_from(invoker.toolchain_args, "*")
        }
        is_clang = false
        use_clang_coverage = false
        current_os = "win"
        current_cpu = rust_host_toolchain_arch
      }
    }
    msvc_system_allocator_toolchain(
        cl_toolchain_prefix + target_name + "_with_system_allocator") {
      environment = "environment." + toolchain_arch
      cl = "\"${win_toolchain_data.vc_bin_dir}/cl.exe\""

      toolchain_args = {
        if (defined(invoker.toolchain_args)) {
          forward_variables_from(invoker.toolchain_args, "*")
        }
        is_clang = false
        use_clang_coverage = false
        current_os = "win"
        current_cpu = toolchain_arch
      }
    }
    msvc_rust_host_build_tools_toolchain(
        cl_toolchain_prefix + target_name + "_for_rust_host_build_tools") {
      environment = "environment." + rust_host_toolchain_arch
      cl = "\"${rust_host_win_toolchain_data.vc_bin_dir}/cl.exe\""

      toolchain_args = {
        if (defined(invoker.toolchain_args)) {
          forward_variables_from(invoker.toolchain_args, "*")
        }
        is_clang = false
        use_clang_coverage = false
        current_os = "win"
        current_cpu = rust_host_toolchain_arch
      }
    }
  }

  if (defined(invoker.clang_toolchain_prefix)) {
    clang_toolchain_prefix = invoker.clang_toolchain_prefix
  } else {
    clang_toolchain_prefix = "win_clang_"
  }

  _clang_lib_dir =
      rebase_path("$clang_base_path/lib/clang/$clang_version/lib/windows",
                  root_build_dir)
  if (host_os == "win") {
    # And to match the other -libpath flags.
    _clang_lib_dir = string_replace(_clang_lib_dir, "/", "\\")
  }

  msvc_toolchain(clang_toolchain_prefix + target_name) {
    environment = "environment." + toolchain_arch
    cl = "${_clang_bin_path}/clang-cl${_exe}"

    sys_include_flags = "${win_toolchain_data.include_flags_imsvc}"
    if (use_lld) {
      sys_lib_flags = "-libpath:$_clang_lib_dir " +
                      "${win_toolchain_data.libpath_lldlink_flags}"
    }

    toolchain_args = {
      if (defined(invoker.toolchain_args)) {
        forward_variables_from(invoker.toolchain_args, "*")
      }
      is_clang = true
      current_os = "win"
      current_cpu = toolchain_arch
    }
  }
  msvc_system_allocator_toolchain(
      clang_toolchain_prefix + target_name + "_host_with_system_allocator") {
    environment = "environment." + rust_host_toolchain_arch
    cl = "${_clang_bin_path}/clang-cl${_exe}"

    sys_include_flags = "${rust_host_win_toolchain_data.include_flags_imsvc}"
    if (use_lld) {
      sys_lib_flags = "-libpath:$_clang_lib_dir " +
                      "${rust_host_win_toolchain_data.libpath_lldlink_flags}"
    }

    toolchain_args = {
      if (defined(invoker.toolchain_args)) {
        forward_variables_from(invoker.toolchain_args, "*")
      }
      is_clang = true
      current_os = "win"
      current_cpu = rust_host_toolchain_arch
    }
  }
  msvc_system_allocator_toolchain(
      clang_toolchain_prefix + target_name + "_with_system_allocator") {
    environment = "environment." + toolchain_arch
    cl = "${_clang_bin_path}/clang-cl${_exe}"

    sys_include_flags = "${win_toolchain_data.include_flags_imsvc}"
    if (use_lld) {
      sys_lib_flags = "-libpath:$_clang_lib_dir " +
                      "${win_toolchain_data.libpath_lldlink_flags}"
    }

    toolchain_args = {
      if (defined(invoker.toolchain_args)) {
        forward_variables_from(invoker.toolchain_args, "*")
      }
      is_clang = true
      current_os = "win"
      current_cpu = toolchain_arch
    }
  }
  msvc_rust_host_build_tools_toolchain(
      clang_toolchain_prefix + target_name + "_for_rust_host_build_tools") {
    environment = "environment." + rust_host_toolchain_arch
    cl = "${_clang_bin_path}/clang-cl${_exe}"

    sys_include_flags = "${rust_host_win_toolchain_data.include_flags_imsvc}"
    if (use_lld) {
      sys_lib_flags = "-libpath:$_clang_lib_dir " +
                      "${rust_host_win_toolchain_data.libpath_lldlink_flags}"
    }

    toolchain_args = {
      if (defined(invoker.toolchain_args)) {
        forward_variables_from(invoker.toolchain_args, "*")
      }
      is_clang = true
      current_os = "win"
      current_cpu = rust_host_toolchain_arch
    }
  }
}