fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Roberto Bampirules_rust: enable pipelined compilation. (#1275)867fc37c

0.9.0
27.0 KiB735 linesraw
1"""The rust_toolchain rule definition and implementation."""
2
3load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
4load("//rust/private:common.bzl", "rust_common")
5load("//rust/private:rust_analyzer.bzl", _rust_analyzer_toolchain = "rust_analyzer_toolchain")
6load("//rust/private:utils.bzl", "dedent", "find_cc_toolchain", "make_static_lib_symlink")
7
8rust_analyzer_toolchain = _rust_analyzer_toolchain
9
10def _rust_stdlib_filegroup_impl(ctx):
11    rust_std = ctx.files.srcs
12    dot_a_files = []
13    between_alloc_and_core_files = []
14    core_files = []
15    between_core_and_std_files = []
16    std_files = []
17    test_files = []
18    memchr_files = []
19    alloc_files = []
20    self_contained_files = [
21        file
22        for file in rust_std
23        if file.basename.endswith(".o") and "self-contained" in file.path
24    ]
25
26    std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")]
27    if std_rlibs:
28        # test depends on std
29        # std depends on everything except test
30        #
31        # core only depends on alloc, but we poke adler in there
32        # because that needs to be before miniz_oxide
33        #
34        # alloc depends on the allocator_library if it's configured, but we
35        # do that later.
36        dot_a_files = [make_static_lib_symlink(ctx.actions, f) for f in std_rlibs]
37
38        alloc_files = [f for f in dot_a_files if "alloc" in f.basename and "std" not in f.basename]
39        between_alloc_and_core_files = [f for f in dot_a_files if "compiler_builtins" in f.basename]
40        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]
41        between_core_and_std_files = [
42            f
43            for f in dot_a_files
44            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
45        ]
46        memchr_files = [f for f in dot_a_files if "memchr" in f.basename]
47        std_files = [f for f in dot_a_files if "std" in f.basename]
48        test_files = [f for f in dot_a_files if "test" in f.basename]
49
50        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)
51        if partitioned_files_len != len(dot_a_files):
52            partitioned = alloc_files + between_alloc_and_core_files + core_files + between_core_and_std_files + memchr_files + std_files + test_files
53            for f in sorted(partitioned):
54                # buildifier: disable=print
55                print("File partitioned: {}".format(f.basename))
56            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)))
57
58    return [
59        DefaultInfo(
60            files = depset(ctx.files.srcs),
61        ),
62        rust_common.stdlib_info(
63            std_rlibs = std_rlibs,
64            dot_a_files = dot_a_files,
65            between_alloc_and_core_files = between_alloc_and_core_files,
66            core_files = core_files,
67            between_core_and_std_files = between_core_and_std_files,
68            std_files = std_files,
69            test_files = test_files,
70            memchr_files = memchr_files,
71            alloc_files = alloc_files,
72            self_contained_files = self_contained_files,
73            srcs = ctx.attr.srcs,
74        ),
75    ]
76
77rust_stdlib_filegroup = rule(
78    doc = "A dedicated filegroup-like rule for Rust stdlib artifacts.",
79    implementation = _rust_stdlib_filegroup_impl,
80    attrs = {
81        "srcs": attr.label_list(
82            allow_files = True,
83            doc = "The list of targets/files that are components of the rust-stdlib file group",
84            mandatory = True,
85        ),
86    },
87)
88
89def _ltl(library, ctx, cc_toolchain, feature_configuration):
90    """A helper to generate `LibraryToLink` objects
91
92    Args:
93        library (File): A rust library file to link.
94        ctx (ctx): The rule's context object.
95        cc_toolchain (CcToolchainInfo): A cc toolchain provider to be used.
96        feature_configuration (feature_configuration): feature_configuration to be queried.
97
98    Returns:
99        LibraryToLink: A provider containing information about libraries to link.
100    """
101    return cc_common.create_library_to_link(
102        actions = ctx.actions,
103        feature_configuration = feature_configuration,
104        cc_toolchain = cc_toolchain,
105        static_library = library,
106        pic_static_library = library,
107    )
108
109def _make_libstd_and_allocator_ccinfo(ctx, rust_std, allocator_library):
110    """Make the CcInfo (if possible) for libstd and allocator libraries.
111
112    Args:
113        ctx (ctx): The rule's context object.
114        rust_std: The Rust standard library.
115        allocator_library: The target to use for providing allocator functions.
116
117
118    Returns:
119        A CcInfo object for the required libraries, or None if no such libraries are available.
120    """
121    cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
122    cc_infos = []
123
124    if not rust_common.stdlib_info in rust_std:
125        fail(dedent("""\
126            {} --
127            The `rust_lib` ({}) must be a target providing `rust_common.stdlib_info`
128            (typically `rust_stdlib_filegroup` rule from @rules_rust//rust:defs.bzl).
129            See https://github.com/bazelbuild/rules_rust/pull/802 for more information.
130        """).format(ctx.label, rust_std))
131    rust_stdlib_info = rust_std[rust_common.stdlib_info]
132
133    if rust_stdlib_info.self_contained_files:
134        compilation_outputs = cc_common.create_compilation_outputs(
135            objects = depset(rust_stdlib_info.self_contained_files),
136        )
137
138        linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
139            name = ctx.label.name,
140            actions = ctx.actions,
141            feature_configuration = feature_configuration,
142            cc_toolchain = cc_toolchain,
143            compilation_outputs = compilation_outputs,
144        )
145
146        cc_infos.append(CcInfo(
147            linking_context = linking_context,
148        ))
149
150    if rust_stdlib_info.std_rlibs:
151        alloc_inputs = depset(
152            [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files],
153        )
154        between_alloc_and_core_inputs = depset(
155            [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.between_alloc_and_core_files],
156            transitive = [alloc_inputs],
157            order = "topological",
158        )
159        core_inputs = depset(
160            [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.core_files],
161            transitive = [between_alloc_and_core_inputs],
162            order = "topological",
163        )
164
165        # The libraries panic_abort and panic_unwind are alternatives.
166        # The std by default requires panic_unwind.
167        # Exclude panic_abort if panic_unwind is present.
168        # TODO: Provide a setting to choose between panic_abort and panic_unwind.
169        filtered_between_core_and_std_files = rust_stdlib_info.between_core_and_std_files
170        has_panic_unwind = [
171            f
172            for f in filtered_between_core_and_std_files
173            if "panic_unwind" in f.basename
174        ]
175        if has_panic_unwind:
176            filtered_between_core_and_std_files = [
177                f
178                for f in filtered_between_core_and_std_files
179                if "panic_abort" not in f.basename
180            ]
181        memchr_inputs = depset(
182            [
183                _ltl(f, ctx, cc_toolchain, feature_configuration)
184                for f in rust_stdlib_info.memchr_files
185            ],
186            transitive = [core_inputs],
187            order = "topological",
188        )
189        between_core_and_std_inputs = depset(
190            [
191                _ltl(f, ctx, cc_toolchain, feature_configuration)
192                for f in filtered_between_core_and_std_files
193            ],
194            transitive = [memchr_inputs],
195            order = "topological",
196        )
197        std_inputs = depset(
198            [
199                _ltl(f, ctx, cc_toolchain, feature_configuration)
200                for f in rust_stdlib_info.std_files
201            ],
202            transitive = [between_core_and_std_inputs],
203            order = "topological",
204        )
205        test_inputs = depset(
206            [
207                _ltl(f, ctx, cc_toolchain, feature_configuration)
208                for f in rust_stdlib_info.test_files
209            ],
210            transitive = [std_inputs],
211            order = "topological",
212        )
213
214        link_inputs = cc_common.create_linker_input(
215            owner = rust_std.label,
216            libraries = test_inputs,
217        )
218
219        allocator_inputs = None
220        if allocator_library:
221            allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs]
222
223        cc_infos.append(CcInfo(
224            linking_context = cc_common.create_linking_context(
225                linker_inputs = depset(
226                    [link_inputs],
227                    transitive = allocator_inputs,
228                    order = "topological",
229                ),
230            ),
231        ))
232
233    if cc_infos:
234        return cc_common.merge_cc_infos(
235            direct_cc_infos = cc_infos,
236        )
237    return None
238
239def _symlink_sysroot_tree(ctx, name, target):
240    """Generate a set of symlinks to files from another target
241
242    Args:
243        ctx (ctx): The toolchain's context object
244        name (str): The name of the sysroot directory (typically `ctx.label.name`)
245        target (Target): A target owning files to symlink
246
247    Returns:
248        depset[File]: A depset of the generated symlink files
249    """
250    tree_files = []
251    for file in target.files.to_list():
252        # Parse the path to the file relative to the workspace root so a
253        # symlink matching this path can be created within the sysroot.
254
255        # The code blow attempts to parse any workspace names out of the
256        # path. For local targets, this code is a noop.
257        if target.label.workspace_root:
258            file_path = file.path.split(target.label.workspace_root, 1)[-1]
259        else:
260            file_path = file.path
261
262        symlink = ctx.actions.declare_file("{}/{}".format(name, file_path.lstrip("/")))
263
264        ctx.actions.symlink(
265            output = symlink,
266            target_file = file,
267        )
268
269        tree_files.append(symlink)
270
271    return depset(tree_files)
272
273def _symlink_sysroot_bin(ctx, name, directory, target):
274    """Crete a symlink to a target file.
275
276    Args:
277        ctx (ctx): The rule's context object
278        name (str): A common name for the output directory
279        directory (str): The directory under `name` to put the file in
280        target (File): A File object to symlink to
281
282    Returns:
283        File: A newly generated symlink file
284    """
285    symlink = ctx.actions.declare_file("{}/{}/{}".format(
286        name,
287        directory,
288        target.basename,
289    ))
290
291    ctx.actions.symlink(
292        output = symlink,
293        target_file = target,
294        is_executable = True,
295    )
296
297    return symlink
298
299def _generate_sysroot(
300        ctx,
301        rustc,
302        rustdoc,
303        rustc_lib,
304        cargo = None,
305        clippy = None,
306        llvm_tools = None,
307        rust_std = None,
308        rustfmt = None):
309    """Generate a rust sysroot from collection of toolchain components
310
311    Args:
312        ctx (ctx): A context object from a `rust_toolchain` rule.
313        rustc (File): The path to a `rustc` executable.
314        rustdoc (File): The path to a `rustdoc` executable.
315        rustc_lib (Target): A collection of Files containing dependencies of `rustc`.
316        cargo (File, optional): The path to a `cargo` executable.
317        clippy (File, optional): The path to a `clippy-driver` executable.
318        llvm_tools (Target, optional): A collection of llvm tools used by `rustc`.
319        rust_std (Target, optional): A collection of Files containing Rust standard library components.
320        rustfmt (File, optional): The path to a `rustfmt` executable.
321
322    Returns:
323        struct: A struct of generated files representing the new sysroot
324    """
325    name = ctx.label.name
326
327    # Define runfiles
328    direct_files = []
329    transitive_file_sets = []
330
331    # Rustc
332    sysroot_rustc = _symlink_sysroot_bin(ctx, name, "bin", rustc)
333    direct_files.extend([sysroot_rustc])
334
335    # Rustc dependencies
336    sysroot_rustc_lib = None
337    if rustc_lib:
338        sysroot_rustc_lib = _symlink_sysroot_tree(ctx, name, rustc_lib)
339        transitive_file_sets.extend([sysroot_rustc_lib])
340
341    # Rustdoc
342    sysroot_rustdoc = _symlink_sysroot_bin(ctx, name, "bin", rustdoc)
343    direct_files.extend([sysroot_rustdoc])
344
345    # Clippy
346    sysroot_clippy = None
347    if clippy:
348        sysroot_clippy = _symlink_sysroot_bin(ctx, name, "bin", clippy)
349        direct_files.extend([sysroot_clippy])
350
351    # Cargo
352    sysroot_cargo = None
353    if cargo:
354        sysroot_cargo = _symlink_sysroot_bin(ctx, name, "bin", cargo)
355        direct_files.extend([sysroot_cargo])
356
357    # Rustfmt
358    sysroot_rustfmt = None
359    if rustfmt:
360        sysroot_rustfmt = _symlink_sysroot_bin(ctx, name, "bin", rustfmt)
361        direct_files.extend([sysroot_rustfmt])
362
363    # Llvm tools
364    sysroot_llvm_tools = None
365    if llvm_tools:
366        sysroot_llvm_tools = _symlink_sysroot_tree(ctx, name, llvm_tools)
367        transitive_file_sets.extend([sysroot_llvm_tools])
368
369    # Rust standard library
370    sysroot_rust_std = None
371    if rust_std:
372        sysroot_rust_std = _symlink_sysroot_tree(ctx, name, rust_std)
373        transitive_file_sets.extend([sysroot_rust_std])
374
375    # Declare a file in the root of the sysroot to make locating the sysroot easy
376    sysroot_anchor = ctx.actions.declare_file("{}/rust.sysroot".format(name))
377    ctx.actions.write(
378        output = sysroot_anchor,
379        content = "\n".join([
380            "cargo: {}".format(cargo),
381            "clippy: {}".format(clippy),
382            "llvm_tools: {}".format(llvm_tools),
383            "rust_std: {}".format(rust_std),
384            "rustc_lib: {}".format(rustc_lib),
385            "rustc: {}".format(rustc),
386            "rustdoc: {}".format(rustdoc),
387            "rustfmt: {}".format(rustfmt),
388        ]),
389    )
390
391    # Create a depset of all sysroot files (symlinks and their real paths)
392    all_files = depset(direct_files, transitive = transitive_file_sets)
393
394    return struct(
395        all_files = all_files,
396        cargo = sysroot_cargo,
397        clippy = sysroot_clippy,
398        rust_std = sysroot_rust_std,
399        rustc = sysroot_rustc,
400        rustc_lib = sysroot_rustc_lib,
401        rustdoc = sysroot_rustdoc,
402        rustfmt = sysroot_rustfmt,
403        sysroot_anchor = sysroot_anchor,
404    )
405
406def _rust_toolchain_impl(ctx):
407    """The rust_toolchain implementation
408
409    Args:
410        ctx (ctx): The rule's context object
411
412    Returns:
413        list: A list containing the target's toolchain Provider info
414    """
415    compilation_mode_opts = {}
416    for k, v in ctx.attr.opt_level.items():
417        if not k in ctx.attr.debug_info:
418            fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k))
419        compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v)
420    for k, v in ctx.attr.debug_info.items():
421        if not k in ctx.attr.opt_level:
422            fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k))
423
424    if ctx.attr.target_triple and ctx.file.target_json:
425        fail("Do not specify both target_triple and target_json, either use a builtin triple or provide a custom specification file.")
426
427    rename_first_party_crates = ctx.attr._rename_first_party_crates[BuildSettingInfo].value
428    third_party_dir = ctx.attr._third_party_dir[BuildSettingInfo].value
429    pipelined_compilation = ctx.attr._pipelined_compilation[BuildSettingInfo].value
430
431    if ctx.attr.rust_lib:
432        # buildifier: disable=print
433        print("`rust_toolchain.rust_lib` is deprecated. Please update {} to use `rust_toolchain.rust_std`".format(
434            ctx.label,
435        ))
436        rust_std = ctx.attr.rust_lib
437    else:
438        rust_std = ctx.attr.rust_std
439
440    sysroot = _generate_sysroot(
441        ctx = ctx,
442        rustc = ctx.file.rustc,
443        rustdoc = ctx.file.rust_doc,
444        rustc_lib = ctx.attr.rustc_lib,
445        rust_std = rust_std,
446        rustfmt = ctx.file.rustfmt,
447        clippy = ctx.file.clippy_driver,
448        cargo = ctx.file.cargo,
449        llvm_tools = ctx.attr.llvm_tools,
450    )
451
452    expanded_stdlib_linkflags = []
453    for flag in ctx.attr.stdlib_linkflags:
454        expanded_stdlib_linkflags.append(
455            ctx.expand_location(
456                flag,
457                targets = rust_std[rust_common.stdlib_info].srcs,
458            ),
459        )
460
461    linking_context = cc_common.create_linking_context(
462        linker_inputs = depset([
463            cc_common.create_linker_input(
464                owner = ctx.label,
465                user_link_flags = depset(expanded_stdlib_linkflags),
466            ),
467        ]),
468    )
469
470    # Contains linker flags needed to link Rust standard library.
471    # These need to be added to linker command lines when the linker is not rustc
472    # (rustc does this automatically). Linker flags wrapped in an otherwise empty
473    # `CcInfo` to provide the flags in a way that doesn't duplicate them per target
474    # providing a `CcInfo`.
475    stdlib_linkflags_cc_info = CcInfo(
476        compilation_context = cc_common.create_compilation_context(),
477        linking_context = linking_context,
478    )
479
480    # Determine the path and short_path of the sysroot
481    sysroot_path = sysroot.sysroot_anchor.dirname
482    sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/")
483
484    # Variables for make variable expansion
485    make_variables = {
486        "RUSTC": sysroot.rustc.path,
487        "RUSTDOC": sysroot.rustdoc.path,
488        "RUST_DEFAULT_EDITION": ctx.attr.default_edition or "",
489        "RUST_SYSROOT": sysroot_path,
490    }
491
492    if sysroot.cargo:
493        make_variables.update({
494            "CARGO": sysroot.cargo.path,
495        })
496
497    if sysroot.rustfmt:
498        make_variables.update({
499            "RUSTFMT": sysroot.rustfmt.path,
500        })
501
502    make_variable_info = platform_common.TemplateVariableInfo(make_variables)
503
504    toolchain = platform_common.ToolchainInfo(
505        all_files = sysroot.all_files,
506        binary_ext = ctx.attr.binary_ext,
507        cargo = sysroot.cargo,
508        clippy_driver = sysroot.clippy,
509        compilation_mode_opts = compilation_mode_opts,
510        crosstool_files = ctx.files._cc_toolchain,
511        default_edition = ctx.attr.default_edition,
512        dylib_ext = ctx.attr.dylib_ext,
513        env = ctx.attr.env,
514        exec_triple = ctx.attr.exec_triple,
515        libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.allocator_library),
516        llvm_cov = ctx.file.llvm_cov,
517        llvm_profdata = ctx.file.llvm_profdata,
518        make_variables = make_variable_info,
519        os = ctx.attr.os,
520        rust_doc = sysroot.rustdoc,
521        rust_lib = sysroot.rust_std,  # `rust_lib` is deprecated and only exists for legacy support.
522        rust_std = sysroot.rust_std,
523        rust_std_paths = depset([file.dirname for file in sysroot.rust_std.to_list()]),
524        rustc = sysroot.rustc,
525        rustc_lib = sysroot.rustc_lib,
526        rustc_srcs = ctx.attr.rustc_srcs,
527        rustfmt = sysroot.rustfmt,
528        staticlib_ext = ctx.attr.staticlib_ext,
529        stdlib_linkflags = stdlib_linkflags_cc_info,
530        sysroot = sysroot_path,
531        sysroot_short_path = sysroot_short_path,
532        target_arch = ctx.attr.target_triple.split("-")[0],
533        target_flag_value = ctx.file.target_json.path if ctx.file.target_json else ctx.attr.target_triple,
534        target_json = ctx.file.target_json,
535        target_triple = ctx.attr.target_triple,
536
537        # Experimental and incompatible flags
538        _rename_first_party_crates = rename_first_party_crates,
539        _third_party_dir = third_party_dir,
540        _pipelined_compilation = pipelined_compilation,
541    )
542    return [
543        toolchain,
544        make_variable_info,
545    ]
546
547rust_toolchain = rule(
548    implementation = _rust_toolchain_impl,
549    fragments = ["cpp"],
550    attrs = {
551        "allocator_library": attr.label(
552            doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
553        ),
554        "binary_ext": attr.string(
555            doc = "The extension for binaries created from rustc.",
556            mandatory = True,
557        ),
558        "cargo": attr.label(
559            doc = "The location of the `cargo` binary. Can be a direct source or a filegroup containing one item.",
560            allow_single_file = True,
561            cfg = "exec",
562        ),
563        "clippy_driver": attr.label(
564            doc = "The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item.",
565            allow_single_file = True,
566            cfg = "exec",
567        ),
568        "debug_info": attr.string_dict(
569            doc = "Rustc debug info levels per opt level",
570            default = {
571                "dbg": "2",
572                "fastbuild": "0",
573                "opt": "0",
574            },
575        ),
576        "default_edition": attr.string(
577            doc = (
578                "The edition to use for rust_* rules that don't specify an edition. " +
579                "If absent, every rule is required to specify its `edition` attribute."
580            ),
581        ),
582        "dylib_ext": attr.string(
583            doc = "The extension for dynamic libraries created from rustc.",
584            mandatory = True,
585        ),
586        "env": attr.string_dict(
587            doc = "Environment variables to set in actions.",
588        ),
589        "exec_triple": attr.string(
590            doc = (
591                "The platform triple for the toolchains execution environment. " +
592                "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations"
593            ),
594            mandatory = True,
595        ),
596        "llvm_cov": attr.label(
597            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.",
598            allow_single_file = True,
599            cfg = "exec",
600        ),
601        "llvm_profdata": attr.label(
602            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.",
603            allow_single_file = True,
604            cfg = "exec",
605        ),
606        "llvm_tools": attr.label(
607            doc = "LLVM tools that are shipped with the Rust toolchain.",
608            allow_files = True,
609        ),
610        "opt_level": attr.string_dict(
611            doc = "Rustc optimization levels.",
612            default = {
613                "dbg": "0",
614                "fastbuild": "0",
615                "opt": "3",
616            },
617        ),
618        "os": attr.string(
619            doc = "The operating system for the current toolchain",
620            mandatory = True,
621        ),
622        "rust_doc": attr.label(
623            doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item.",
624            allow_single_file = True,
625            cfg = "exec",
626            mandatory = True,
627        ),
628        "rust_lib": attr.label(
629            doc = "**Deprecated**: Use `rust_std`",
630        ),
631        "rust_std": attr.label(
632            doc = "The Rust standard library.",
633        ),
634        "rustc": attr.label(
635            doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.",
636            allow_single_file = True,
637            cfg = "exec",
638            mandatory = True,
639        ),
640        "rustc_lib": attr.label(
641            doc = "The libraries used by rustc during compilation.",
642            cfg = "exec",
643        ),
644        "rustc_srcs": attr.label(
645            doc = "The source code of rustc.",
646        ),
647        "rustfmt": attr.label(
648            doc = "The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item.",
649            allow_single_file = True,
650            cfg = "exec",
651        ),
652        "staticlib_ext": attr.string(
653            doc = "The extension for static libraries created from rustc.",
654            mandatory = True,
655        ),
656        "stdlib_linkflags": attr.string_list(
657            doc = (
658                "Additional linker flags to use when Rust standard library is linked by a C++ linker " +
659                "(rustc will deal with these automatically). Subject to location expansion with respect " +
660                "to the srcs of the `rust_std` attribute."
661            ),
662            mandatory = True,
663        ),
664        "target_json": attr.label(
665            doc = ("Override the target_triple with a custom target specification. " +
666                   "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"),
667            allow_single_file = True,
668        ),
669        "target_triple": attr.string(
670            doc = (
671                "The platform triple for the toolchains target environment. " +
672                "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations"
673            ),
674        ),
675        "_cc_toolchain": attr.label(
676            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
677        ),
678        "_pipelined_compilation": attr.label(
679            default = "@rules_rust//rust/settings:pipelined_compilation",
680        ),
681        "_rename_first_party_crates": attr.label(
682            default = Label("//rust/settings:rename_first_party_crates"),
683        ),
684        "_third_party_dir": attr.label(
685            default = Label("//rust/settings:third_party_dir"),
686        ),
687    },
688    toolchains = [
689        "@bazel_tools//tools/cpp:toolchain_type",
690    ],
691    incompatible_use_toolchain_transition = True,
692    doc = """Declares a Rust toolchain for use.
693
694This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform.
695
696Example:
697
698Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This \
699support can be used in Bazel by defining a new toolchain definition and declaration:
700
701```python
702load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain')
703
704rust_toolchain(
705    name = "rust_cpuX_impl",
706    rustc = "@rust_cpuX//:rustc",
707    rustc_lib = "@rust_cpuX//:rustc_lib",
708    rust_std = "@rust_cpuX//:rust_std",
709    rust_doc = "@rust_cpuX//:rustdoc",
710    binary_ext = "",
711    staticlib_ext = ".a",
712    dylib_ext = ".so",
713    stdlib_linkflags = ["-lpthread", "-ldl"],
714    os = "linux",
715)
716
717toolchain(
718    name = "rust_cpuX",
719    exec_compatible_with = [
720        "@platforms//cpu:cpuX",
721    ],
722    target_compatible_with = [
723        "@platforms//cpu:cpuX",
724    ],
725    toolchain = ":rust_cpuX_impl",
726)
727```
728
729Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass \
730it to the `"--extra_toolchains"` flag for Bazel, and it will be used.
731
732See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository \
733with the actual binaries and libraries.
734""",
735)