aboutsummaryrefslogtreecommitdiff
path: root/rust/toolchain.bzl
blob: 1d268ec0a858e56ab3a16c2df536dbe15362439d (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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
"""The rust_toolchain rule definition and implementation."""

load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//rust/platform:triple.bzl", "triple")
load("//rust/private:common.bzl", "rust_common")
load("//rust/private:rust_analyzer.bzl", _rust_analyzer_toolchain = "rust_analyzer_toolchain")
load(
    "//rust/private:rustfmt.bzl",
    _current_rustfmt_toolchain = "current_rustfmt_toolchain",
    _rustfmt_toolchain = "rustfmt_toolchain",
)
load(
    "//rust/private:utils.bzl",
    "dedent",
    "dedup_expand_location",
    "find_cc_toolchain",
    "is_exec_configuration",
    "is_std_dylib",
    "make_static_lib_symlink",
)
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")

rust_analyzer_toolchain = _rust_analyzer_toolchain
rustfmt_toolchain = _rustfmt_toolchain
current_rustfmt_toolchain = _current_rustfmt_toolchain

def _rust_stdlib_filegroup_impl(ctx):
    rust_std = ctx.files.srcs
    dot_a_files = []
    between_alloc_and_core_files = []
    core_files = []
    between_core_and_std_files = []
    std_files = []
    test_files = []
    memchr_files = []
    alloc_files = []
    self_contained_files = [
        file
        for file in rust_std
        if file.basename.endswith(".o") and "self-contained" in file.path
    ]
    panic_files = []

    std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")]
    if std_rlibs:
        # test depends on std
        # std depends on everything except test
        #
        # core only depends on alloc, but we poke adler in there
        # because that needs to be before miniz_oxide
        #
        # panic_unwind depends on unwind, alloc, cfg_if, compiler_builtins, core, libc
        # panic_abort depends on alloc, cfg_if, compiler_builtins, core, libc
        #
        # alloc depends on the allocator_library if it's configured, but we
        # do that later.
        dot_a_files = [make_static_lib_symlink(ctx.actions, f) for f in std_rlibs]

        alloc_files = [f for f in dot_a_files if "alloc" in f.basename and "std" not in f.basename]
        between_alloc_and_core_files = [f for f in dot_a_files if "compiler_builtins" in f.basename]
        core_files = [f for f in dot_a_files if ("core" in f.basename or "adler" in f.basename) and "std" not in f.basename]
        panic_files = [f for f in dot_a_files if f.basename in ["cfg_if", "libc", "panic_abort", "panic_unwind", "unwind"]]
        between_core_and_std_files = [
            f
            for f in dot_a_files
            if "alloc" not in f.basename and "compiler_builtins" not in f.basename and "core" not in f.basename and "adler" not in f.basename and "std" not in f.basename and "memchr" not in f.basename and "test" not in f.basename
        ]
        memchr_files = [f for f in dot_a_files if "memchr" in f.basename]
        std_files = [f for f in dot_a_files if "std" in f.basename]
        test_files = [f for f in dot_a_files if "test" in f.basename]

        partitioned_files_len = len(alloc_files) + len(between_alloc_and_core_files) + len(core_files) + len(between_core_and_std_files) + len(memchr_files) + len(std_files) + len(test_files)
        if partitioned_files_len != len(dot_a_files):
            partitioned = alloc_files + between_alloc_and_core_files + core_files + between_core_and_std_files + memchr_files + std_files + test_files
            for f in sorted(partitioned):
                # buildifier: disable=print
                print("File partitioned: {}".format(f.basename))
            fail("rust_toolchain couldn't properly partition rlibs in rust_std. Partitioned {} out of {} files. This is probably a bug in the rule implementation.".format(partitioned_files_len, len(dot_a_files)))

    std_dylib = None

    for file in rust_std:
        if is_std_dylib(file):
            std_dylib = file
            break

    return [
        DefaultInfo(
            files = depset(ctx.files.srcs),
            runfiles = ctx.runfiles(ctx.files.srcs),
        ),
        rust_common.stdlib_info(
            std_rlibs = std_rlibs,
            dot_a_files = dot_a_files,
            between_alloc_and_core_files = between_alloc_and_core_files,
            core_files = core_files,
            between_core_and_std_files = between_core_and_std_files,
            std_files = std_files,
            std_dylib = std_dylib,
            test_files = test_files,
            memchr_files = memchr_files,
            alloc_files = alloc_files,
            self_contained_files = self_contained_files,
            panic_files = panic_files,
            srcs = ctx.attr.srcs,
        ),
    ]

rust_stdlib_filegroup = rule(
    doc = "A dedicated filegroup-like rule for Rust stdlib artifacts.",
    implementation = _rust_stdlib_filegroup_impl,
    attrs = {
        "srcs": attr.label_list(
            allow_files = True,
            doc = "The list of targets/files that are components of the rust-stdlib file group",
            mandatory = True,
        ),
    },
)

def _ltl(library, ctx, cc_toolchain, feature_configuration):
    """A helper to generate `LibraryToLink` objects

    Args:
        library (File): A rust library file to link.
        ctx (ctx): The rule's context object.
        cc_toolchain (CcToolchainInfo): A cc toolchain provider to be used.
        feature_configuration (feature_configuration): feature_configuration to be queried.

    Returns:
        LibraryToLink: A provider containing information about libraries to link.
    """
    return cc_common.create_library_to_link(
        actions = ctx.actions,
        feature_configuration = feature_configuration,
        cc_toolchain = cc_toolchain,
        static_library = library,
        pic_static_library = library,
    )

def _make_libstd_and_allocator_ccinfo(ctx, rust_std, allocator_library, std = "std"):
    """Make the CcInfo (if possible) for libstd and allocator libraries.

    Args:
        ctx (ctx): The rule's context object.
        rust_std: The Rust standard library.
        allocator_library: The target to use for providing allocator functions.
        std: Standard library flavor. Currently only "std" and "no_std_with_alloc" are supported,
             accompanied with the default panic behavior.


    Returns:
        A CcInfo object for the required libraries, or None if no such libraries are available.
    """
    cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
    cc_infos = []

    if not rust_common.stdlib_info in rust_std:
        fail(dedent("""\
            {} --
            The `rust_lib` ({}) must be a target providing `rust_common.stdlib_info`
            (typically `rust_stdlib_filegroup` rule from @rules_rust//rust:defs.bzl).
            See https://github.com/bazelbuild/rules_rust/pull/802 for more information.
        """).format(ctx.label, rust_std))
    rust_stdlib_info = rust_std[rust_common.stdlib_info]

    if rust_stdlib_info.self_contained_files:
        compilation_outputs = cc_common.create_compilation_outputs(
            objects = depset(rust_stdlib_info.self_contained_files),
        )

        linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
            name = ctx.label.name,
            actions = ctx.actions,
            feature_configuration = feature_configuration,
            cc_toolchain = cc_toolchain,
            compilation_outputs = compilation_outputs,
        )

        cc_infos.append(CcInfo(
            linking_context = linking_context,
        ))

    if rust_stdlib_info.std_rlibs:
        alloc_inputs = depset(
            [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files],
        )
        between_alloc_and_core_inputs = depset(
            [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.between_alloc_and_core_files],
            transitive = [alloc_inputs],
            order = "topological",
        )
        core_inputs = depset(
            [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.core_files],
            transitive = [between_alloc_and_core_inputs],
            order = "topological",
        )

        # The libraries panic_abort and panic_unwind are alternatives.
        # The std by default requires panic_unwind.
        # Exclude panic_abort if panic_unwind is present.
        # TODO: Provide a setting to choose between panic_abort and panic_unwind.
        filtered_between_core_and_std_files = rust_stdlib_info.between_core_and_std_files
        has_panic_unwind = [
            f
            for f in filtered_between_core_and_std_files
            if "panic_unwind" in f.basename
        ]
        if has_panic_unwind:
            filtered_between_core_and_std_files = [
                f
                for f in filtered_between_core_and_std_files
                if "abort" not in f.basename
            ]
            core_alloc_and_panic_inputs = depset(
                [
                    _ltl(f, ctx, cc_toolchain, feature_configuration)
                    for f in rust_stdlib_info.panic_files
                    if "unwind" not in f.basename
                ],
                transitive = [core_inputs],
                order = "topological",
            )
        else:
            core_alloc_and_panic_inputs = depset(
                [
                    _ltl(f, ctx, cc_toolchain, feature_configuration)
                    for f in rust_stdlib_info.panic_files
                    if "unwind" not in f.basename
                ],
                transitive = [core_inputs],
                order = "topological",
            )
        memchr_inputs = depset(
            [
                _ltl(f, ctx, cc_toolchain, feature_configuration)
                for f in rust_stdlib_info.memchr_files
            ],
            transitive = [core_inputs],
            order = "topological",
        )
        between_core_and_std_inputs = depset(
            [
                _ltl(f, ctx, cc_toolchain, feature_configuration)
                for f in filtered_between_core_and_std_files
            ],
            transitive = [memchr_inputs],
            order = "topological",
        )

        if _experimental_link_std_dylib(ctx):
            # std dylib has everything so that we do not need to include all std_files
            std_inputs = depset(
                [cc_common.create_library_to_link(
                    actions = ctx.actions,
                    feature_configuration = feature_configuration,
                    cc_toolchain = cc_toolchain,
                    dynamic_library = rust_stdlib_info.std_dylib,
                )],
            )
        else:
            std_inputs = depset(
                [
                    _ltl(f, ctx, cc_toolchain, feature_configuration)
                    for f in rust_stdlib_info.std_files
                ],
                transitive = [between_core_and_std_inputs],
                order = "topological",
            )

        test_inputs = depset(
            [
                _ltl(f, ctx, cc_toolchain, feature_configuration)
                for f in rust_stdlib_info.test_files
            ],
            transitive = [std_inputs],
            order = "topological",
        )

        if std == "std":
            link_inputs = cc_common.create_linker_input(
                owner = rust_std.label,
                libraries = test_inputs,
            )
        elif std == "no_std_with_alloc":
            link_inputs = cc_common.create_linker_input(
                owner = rust_std.label,
                libraries = core_alloc_and_panic_inputs,
            )
        else:
            fail("Requested '{}' std mode is currently not supported.".format(std))

        allocator_inputs = None
        if allocator_library:
            allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs]

        cc_infos.append(CcInfo(
            linking_context = cc_common.create_linking_context(
                linker_inputs = depset(
                    [link_inputs],
                    transitive = allocator_inputs,
                    order = "topological",
                ),
            ),
        ))

    if cc_infos:
        return cc_common.merge_cc_infos(
            direct_cc_infos = cc_infos,
        )
    return None

def _symlink_sysroot_tree(ctx, name, target):
    """Generate a set of symlinks to files from another target

    Args:
        ctx (ctx): The toolchain's context object
        name (str): The name of the sysroot directory (typically `ctx.label.name`)
        target (Target): A target owning files to symlink

    Returns:
        depset[File]: A depset of the generated symlink files
    """
    tree_files = []
    for file in target.files.to_list():
        # Parse the path to the file relative to the workspace root so a
        # symlink matching this path can be created within the sysroot.

        # The code blow attempts to parse any workspace names out of the
        # path. For local targets, this code is a noop.
        if target.label.workspace_root:
            file_path = file.path.split(target.label.workspace_root, 1)[-1]
        else:
            file_path = file.path

        symlink = ctx.actions.declare_file("{}/{}".format(name, file_path.lstrip("/")))

        ctx.actions.symlink(
            output = symlink,
            target_file = file,
        )

        tree_files.append(symlink)

    return depset(tree_files)

def _symlink_sysroot_bin(ctx, name, directory, target):
    """Crete a symlink to a target file.

    Args:
        ctx (ctx): The rule's context object
        name (str): A common name for the output directory
        directory (str): The directory under `name` to put the file in
        target (File): A File object to symlink to

    Returns:
        File: A newly generated symlink file
    """
    symlink = ctx.actions.declare_file("{}/{}/{}".format(
        name,
        directory,
        target.basename,
    ))

    ctx.actions.symlink(
        output = symlink,
        target_file = target,
        is_executable = True,
    )

    return symlink

def _generate_sysroot(
        ctx,
        rustc,
        rustdoc,
        rustc_lib,
        cargo = None,
        clippy = None,
        llvm_tools = None,
        rust_std = None,
        rustfmt = None):
    """Generate a rust sysroot from collection of toolchain components

    Args:
        ctx (ctx): A context object from a `rust_toolchain` rule.
        rustc (File): The path to a `rustc` executable.
        rustdoc (File): The path to a `rustdoc` executable.
        rustc_lib (Target): A collection of Files containing dependencies of `rustc`.
        cargo (File, optional): The path to a `cargo` executable.
        clippy (File, optional): The path to a `clippy-driver` executable.
        llvm_tools (Target, optional): A collection of llvm tools used by `rustc`.
        rust_std (Target, optional): A collection of Files containing Rust standard library components.
        rustfmt (File, optional): The path to a `rustfmt` executable.

    Returns:
        struct: A struct of generated files representing the new sysroot
    """
    name = ctx.label.name

    # Define runfiles
    direct_files = []
    transitive_file_sets = []

    # Rustc
    sysroot_rustc = _symlink_sysroot_bin(ctx, name, "bin", rustc)
    direct_files.extend([sysroot_rustc])

    # Rustc dependencies
    sysroot_rustc_lib = None
    if rustc_lib:
        sysroot_rustc_lib = _symlink_sysroot_tree(ctx, name, rustc_lib)
        transitive_file_sets.extend([sysroot_rustc_lib])

    # Rustdoc
    sysroot_rustdoc = _symlink_sysroot_bin(ctx, name, "bin", rustdoc)
    direct_files.extend([sysroot_rustdoc])

    # Clippy
    sysroot_clippy = None
    if clippy:
        sysroot_clippy = _symlink_sysroot_bin(ctx, name, "bin", clippy)
        direct_files.extend([sysroot_clippy])

    # Cargo
    sysroot_cargo = None
    if cargo:
        sysroot_cargo = _symlink_sysroot_bin(ctx, name, "bin", cargo)
        direct_files.extend([sysroot_cargo])

    # Rustfmt
    sysroot_rustfmt = None
    if rustfmt:
        sysroot_rustfmt = _symlink_sysroot_bin(ctx, name, "bin", rustfmt)
        direct_files.extend([sysroot_rustfmt])

    # Llvm tools
    sysroot_llvm_tools = None
    if llvm_tools:
        sysroot_llvm_tools = _symlink_sysroot_tree(ctx, name, llvm_tools)
        transitive_file_sets.extend([sysroot_llvm_tools])

    # Rust standard library
    sysroot_rust_std = None
    if rust_std:
        sysroot_rust_std = _symlink_sysroot_tree(ctx, name, rust_std)
        transitive_file_sets.extend([sysroot_rust_std])

    # Declare a file in the root of the sysroot to make locating the sysroot easy
    sysroot_anchor = ctx.actions.declare_file("{}/rust.sysroot".format(name))
    ctx.actions.write(
        output = sysroot_anchor,
        content = "\n".join([
            "cargo: {}".format(cargo),
            "clippy: {}".format(clippy),
            "llvm_tools: {}".format(llvm_tools),
            "rust_std: {}".format(rust_std),
            "rustc_lib: {}".format(rustc_lib),
            "rustc: {}".format(rustc),
            "rustdoc: {}".format(rustdoc),
            "rustfmt: {}".format(rustfmt),
        ]),
    )

    # Create a depset of all sysroot files (symlinks and their real paths)
    all_files = depset(direct_files, transitive = transitive_file_sets)

    return struct(
        all_files = all_files,
        cargo = sysroot_cargo,
        clippy = sysroot_clippy,
        rust_std = sysroot_rust_std,
        rustc = sysroot_rustc,
        rustc_lib = sysroot_rustc_lib,
        rustdoc = sysroot_rustdoc,
        rustfmt = sysroot_rustfmt,
        sysroot_anchor = sysroot_anchor,
    )

def _experimental_use_cc_common_link(ctx):
    return ctx.attr.experimental_use_cc_common_link[BuildSettingInfo].value

def _rust_toolchain_impl(ctx):
    """The rust_toolchain implementation

    Args:
        ctx (ctx): The rule's context object

    Returns:
        list: A list containing the target's toolchain Provider info
    """
    compilation_mode_opts = {}
    for k, v in ctx.attr.opt_level.items():
        if not k in ctx.attr.debug_info:
            fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k))
        compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v)
    for k, v in ctx.attr.debug_info.items():
        if not k in ctx.attr.opt_level:
            fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k))

    rename_first_party_crates = ctx.attr._rename_first_party_crates[BuildSettingInfo].value
    third_party_dir = ctx.attr._third_party_dir[BuildSettingInfo].value
    pipelined_compilation = ctx.attr._pipelined_compilation[BuildSettingInfo].value
    no_std = ctx.attr._no_std[BuildSettingInfo].value

    experimental_use_global_allocator = ctx.attr._experimental_use_global_allocator[BuildSettingInfo].value
    if _experimental_use_cc_common_link(ctx):
        if experimental_use_global_allocator and not ctx.attr.global_allocator_library:
            fail("rust_toolchain.experimental_use_cc_common_link with --@rules_rust//rust/settings:experimental_use_global_allocator " +
                 "requires rust_toolchain.global_allocator_library to be set")
        if not ctx.attr.allocator_library:
            fail("rust_toolchain.experimental_use_cc_common_link requires rust_toolchain.allocator_library to be set")
    if experimental_use_global_allocator and not _experimental_use_cc_common_link(ctx):
        fail(
            "Using @rules_rust//rust/settings:experimental_use_global_allocator requires" +
            "--@rules_rust//rust/settings:experimental_use_cc_common_link to be set",
        )

    rust_std = ctx.attr.rust_std

    sysroot = _generate_sysroot(
        ctx = ctx,
        rustc = ctx.file.rustc,
        rustdoc = ctx.file.rust_doc,
        rustc_lib = ctx.attr.rustc_lib,
        rust_std = rust_std,
        rustfmt = ctx.file.rustfmt,
        clippy = ctx.file.clippy_driver,
        cargo = ctx.file.cargo,
        llvm_tools = ctx.attr.llvm_tools,
    )

    expanded_stdlib_linkflags = []
    for flag in ctx.attr.stdlib_linkflags:
        expanded_stdlib_linkflags.append(
            dedup_expand_location(
                ctx,
                flag,
                targets = rust_std[rust_common.stdlib_info].srcs,
            ),
        )

    linking_context = cc_common.create_linking_context(
        linker_inputs = depset([
            cc_common.create_linker_input(
                owner = ctx.label,
                user_link_flags = depset(expanded_stdlib_linkflags),
            ),
        ]),
    )

    # Contains linker flags needed to link Rust standard library.
    # These need to be added to linker command lines when the linker is not rustc
    # (rustc does this automatically). Linker flags wrapped in an otherwise empty
    # `CcInfo` to provide the flags in a way that doesn't duplicate them per target
    # providing a `CcInfo`.
    stdlib_linkflags_cc_info = CcInfo(
        compilation_context = cc_common.create_compilation_context(),
        linking_context = linking_context,
    )

    # Determine the path and short_path of the sysroot
    sysroot_path = sysroot.sysroot_anchor.dirname
    sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/")

    # Variables for make variable expansion
    make_variables = {
        "RUSTC": sysroot.rustc.path,
        "RUSTDOC": sysroot.rustdoc.path,
        "RUST_DEFAULT_EDITION": ctx.attr.default_edition or "",
        "RUST_SYSROOT": sysroot_path,
    }

    if sysroot.cargo:
        make_variables.update({
            "CARGO": sysroot.cargo.path,
        })

    if sysroot.rustfmt:
        make_variables.update({
            "RUSTFMT": sysroot.rustfmt.path,
        })

    make_variable_info = platform_common.TemplateVariableInfo(make_variables)

    exec_triple = triple(ctx.attr.exec_triple)

    if not exec_triple.system:
        fail("No system was provided for the execution platform. Please update {}".format(
            ctx.label,
        ))

    if ctx.attr.target_triple and ctx.attr.target_json:
        fail("Do not specify both target_triple and target_json, either use a builtin triple or provide a custom specification file. Please update {}".format(
            ctx.label,
        ))

    target_triple = None
    target_json = None
    target_arch = None
    target_os = None

    if ctx.attr.target_triple:
        target_triple = triple(ctx.attr.target_triple)
        target_arch = target_triple.arch
        target_os = target_triple.system

    elif ctx.attr.target_json:
        # Ensure the data provided is valid json
        target_json_content = json.decode(ctx.attr.target_json)
        target_json = ctx.actions.declare_file("{}.target.json".format(ctx.label.name))

        ctx.actions.write(
            output = target_json,
            content = json.encode_indent(target_json_content, indent = " " * 4),
        )

        if "arch" in target_json_content:
            target_arch = target_json_content["arch"]
        if "os" in target_json_content:
            target_os = target_json_content["os"]
    else:
        fail("Either `target_triple` or `target_json` must be provided. Please update {}".format(
            ctx.label,
        ))

    toolchain = platform_common.ToolchainInfo(
        all_files = sysroot.all_files,
        binary_ext = ctx.attr.binary_ext,
        cargo = sysroot.cargo,
        clippy_driver = sysroot.clippy,
        compilation_mode_opts = compilation_mode_opts,
        crosstool_files = ctx.files._cc_toolchain,
        default_edition = ctx.attr.default_edition,
        dylib_ext = ctx.attr.dylib_ext,
        env = ctx.attr.env,
        exec_triple = exec_triple,
        libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.allocator_library, "std"),
        libstd_and_global_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.global_allocator_library, "std"),
        nostd_and_global_allocator_cc_info = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.global_allocator_library, "no_std_with_alloc"),
        llvm_cov = ctx.file.llvm_cov,
        llvm_profdata = ctx.file.llvm_profdata,
        make_variables = make_variable_info,
        rust_doc = sysroot.rustdoc,
        rust_std = sysroot.rust_std,
        rust_std_paths = depset([file.dirname for file in sysroot.rust_std.to_list()]),
        rustc = sysroot.rustc,
        rustc_lib = sysroot.rustc_lib,
        rustfmt = sysroot.rustfmt,
        staticlib_ext = ctx.attr.staticlib_ext,
        stdlib_linkflags = stdlib_linkflags_cc_info,
        extra_rustc_flags = ctx.attr.extra_rustc_flags,
        extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
        per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
        sysroot = sysroot_path,
        sysroot_short_path = sysroot_short_path,
        target_arch = target_arch,
        target_flag_value = target_json.path if target_json else target_triple.str,
        target_json = target_json,
        target_os = target_os,
        target_triple = target_triple,

        # Experimental and incompatible flags
        _rename_first_party_crates = rename_first_party_crates,
        _third_party_dir = third_party_dir,
        _pipelined_compilation = pipelined_compilation,
        _experimental_link_std_dylib = _experimental_link_std_dylib(ctx),
        _experimental_use_cc_common_link = _experimental_use_cc_common_link(ctx),
        _experimental_use_global_allocator = experimental_use_global_allocator,
        _experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value,
        _experimental_toolchain_generated_sysroot = ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled,
        _incompatible_no_rustc_sysroot_env = ctx.attr._incompatible_no_rustc_sysroot_env[IncompatibleFlagInfo].enabled,
        _incompatible_test_attr_crate_and_srcs_mutually_exclusive = ctx.attr._incompatible_test_attr_crate_and_srcs_mutually_exclusive[IncompatibleFlagInfo].enabled,
        _no_std = no_std,
    )
    return [
        toolchain,
        make_variable_info,
    ]

def _experimental_link_std_dylib(ctx):
    return not is_exec_configuration(ctx) and \
           ctx.attr.experimental_link_std_dylib[BuildSettingInfo].value and \
           ctx.attr.rust_std[rust_common.stdlib_info].std_dylib != None

rust_toolchain = rule(
    implementation = _rust_toolchain_impl,
    fragments = ["cpp"],
    attrs = {
        "allocator_library": attr.label(
            doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
            default = "@rules_rust//ffi/cc/allocator_library",
        ),
        "binary_ext": attr.string(
            doc = "The extension for binaries created from rustc.",
            mandatory = True,
        ),
        "cargo": attr.label(
            doc = "The location of the `cargo` binary. Can be a direct source or a filegroup containing one item.",
            allow_single_file = True,
            cfg = "exec",
        ),
        "clippy_driver": attr.label(
            doc = "The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item.",
            allow_single_file = True,
            cfg = "exec",
        ),
        "debug_info": attr.string_dict(
            doc = "Rustc debug info levels per opt level",
            default = {
                "dbg": "2",
                "fastbuild": "0",
                "opt": "0",
            },
        ),
        "default_edition": attr.string(
            doc = (
                "The edition to use for rust_* rules that don't specify an edition. " +
                "If absent, every rule is required to specify its `edition` attribute."
            ),
        ),
        "dylib_ext": attr.string(
            doc = "The extension for dynamic libraries created from rustc.",
            mandatory = True,
        ),
        "env": attr.string_dict(
            doc = "Environment variables to set in actions.",
        ),
        "exec_triple": attr.string(
            doc = (
                "The platform triple for the toolchains execution environment. " +
                "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations"
            ),
            mandatory = True,
        ),
        "experimental_link_std_dylib": attr.label(
            default = Label("@rules_rust//rust/settings:experimental_link_std_dylib"),
            doc = "Label to a boolean build setting that controls whether whether to link libstd dynamically.",
        ),
        "experimental_use_cc_common_link": attr.label(
            default = Label("//rust/settings:experimental_use_cc_common_link"),
            doc = "Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries.",
        ),
        "extra_exec_rustc_flags": attr.string_list(
            doc = "Extra flags to pass to rustc in exec configuration",
        ),
        "extra_rustc_flags": attr.string_list(
            doc = "Extra flags to pass to rustc in non-exec configuration",
        ),
        "global_allocator_library": attr.label(
            doc = "Target that provides allocator functions for when a global allocator is present.",
            default = "@rules_rust//ffi/cc/global_allocator_library",
        ),
        "llvm_cov": attr.label(
            doc = "The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage.",
            allow_single_file = True,
            cfg = "exec",
        ),
        "llvm_profdata": attr.label(
            doc = "The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item. If `llvm_cov` is None, this can be None as well and rust code is not instrumented for coverage.",
            allow_single_file = True,
            cfg = "exec",
        ),
        "llvm_tools": attr.label(
            doc = "LLVM tools that are shipped with the Rust toolchain.",
            allow_files = True,
        ),
        "opt_level": attr.string_dict(
            doc = "Rustc optimization levels.",
            default = {
                "dbg": "0",
                "fastbuild": "0",
                "opt": "3",
            },
        ),
        "per_crate_rustc_flags": attr.string_list(
            doc = "Extra flags to pass to rustc in non-exec configuration",
        ),
        "rust_doc": attr.label(
            doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item.",
            allow_single_file = True,
            cfg = "exec",
            mandatory = True,
        ),
        "rust_std": attr.label(
            doc = "The Rust standard library.",
            mandatory = True,
        ),
        "rustc": attr.label(
            doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.",
            allow_single_file = True,
            cfg = "exec",
            mandatory = True,
        ),
        "rustc_lib": attr.label(
            doc = "The libraries used by rustc during compilation.",
            cfg = "exec",
        ),
        "rustfmt": attr.label(
            doc = "**Deprecated**: Instead see [rustfmt_toolchain](#rustfmt_toolchain)",
            allow_single_file = True,
            cfg = "exec",
        ),
        "staticlib_ext": attr.string(
            doc = "The extension for static libraries created from rustc.",
            mandatory = True,
        ),
        "stdlib_linkflags": attr.string_list(
            doc = (
                "Additional linker flags to use when Rust standard library is linked by a C++ linker " +
                "(rustc will deal with these automatically). Subject to location expansion with respect " +
                "to the srcs of the `rust_std` attribute."
            ),
            mandatory = True,
        ),
        "target_json": attr.string(
            doc = ("Override the target_triple with a custom target specification. " +
                   "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"),
        ),
        "target_triple": attr.string(
            doc = (
                "The platform triple for the toolchains target environment. " +
                "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations"
            ),
        ),
        "_cc_toolchain": attr.label(
            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
        ),
        "_experimental_toolchain_generated_sysroot": attr.label(
            default = Label("//rust/settings:experimental_toolchain_generated_sysroot"),
            doc = (
                "Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" +
                "This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot."
            ),
        ),
        "_experimental_use_coverage_metadata_files": attr.label(
            default = Label("//rust/settings:experimental_use_coverage_metadata_files"),
        ),
        "_experimental_use_global_allocator": attr.label(
            default = Label("//rust/settings:experimental_use_global_allocator"),
            doc = (
                "Label to a boolean build setting that informs the target build whether a global allocator is being used." +
                "This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_use_global_allocator."
            ),
        ),
        "_incompatible_no_rustc_sysroot_env": attr.label(
            default = Label("//rust/settings:incompatible_no_rustc_sysroot_env"),
        ),
        "_incompatible_test_attr_crate_and_srcs_mutually_exclusive": attr.label(
            default = Label("//rust/settings:incompatible_test_attr_crate_and_srcs_mutually_exclusive"),
        ),
        "_no_std": attr.label(
            default = Label("//:no_std"),
        ),
        "_pipelined_compilation": attr.label(
            default = Label("//rust/settings:pipelined_compilation"),
        ),
        "_rename_first_party_crates": attr.label(
            default = Label("//rust/settings:rename_first_party_crates"),
        ),
        "_third_party_dir": attr.label(
            default = Label("//rust/settings:third_party_dir"),
        ),
    },
    toolchains = [
        "@bazel_tools//tools/cpp:toolchain_type",
    ],
    doc = """Declares a Rust toolchain for use.

This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform.

Example:

Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This \
support can be used in Bazel by defining a new toolchain definition and declaration:

```python
load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain')

rust_toolchain(
    name = "rust_cpuX_impl",
    binary_ext = "",
    dylib_ext = ".so",
    exec_triple = "cpuX-unknown-linux-gnu",
    rust_doc = "@rust_cpuX//:rustdoc",
    rust_std = "@rust_cpuX//:rust_std",
    rustc = "@rust_cpuX//:rustc",
    rustc_lib = "@rust_cpuX//:rustc_lib",
    staticlib_ext = ".a",
    stdlib_linkflags = ["-lpthread", "-ldl"],
    target_triple = "cpuX-unknown-linux-gnu",
)

toolchain(
    name = "rust_cpuX",
    exec_compatible_with = [
        "@platforms//cpu:cpuX",
        "@platforms//os:linux",
    ],
    target_compatible_with = [
        "@platforms//cpu:cpuX",
        "@platforms//os:linux",
    ],
    toolchain = ":rust_cpuX_impl",
)
```

Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass \
it to the `"--extra_toolchains"` flag for Bazel, and it will be used.

See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository \
with the actual binaries and libraries.
""",
)