fork/rules_rust

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

UebelAndreAdded `rust_analyzer_toolchain` and repository rules for creating one (#1455)0e86b9dd

0.8.1
27.1 KiB616 linesraw
1# buildifier: disable=module-docstring
2load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
3load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
4load(
5    "//rust/platform:triple_mappings.bzl",
6    "triple_to_constraint_set",
7)
8load("//rust/private:common.bzl", "rust_common")
9load(
10    "//rust/private:repository_utils.bzl",
11    "BUILD_for_rust_analyzer_toolchain",
12    "BUILD_for_rust_toolchain",
13    "BUILD_for_toolchain",
14    "DEFAULT_STATIC_RUST_URL_TEMPLATES",
15    "check_version_valid",
16    "load_cargo",
17    "load_clippy",
18    "load_llvm_tools",
19    "load_rust_compiler",
20    "load_rust_src",
21    "load_rust_stdlib",
22    "load_rustc_dev_nightly",
23    "load_rustfmt",
24    "should_include_rustc_srcs",
25    _load_arbitrary_tool = "load_arbitrary_tool",
26)
27
28# Reexport `load_arbitrary_tool` as it's currently in use in https://github.com/google/cargo-raze
29load_arbitrary_tool = _load_arbitrary_tool
30
31# Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if you remove it or change its format, you will also need to update that code.
32DEFAULT_TOOLCHAIN_TRIPLES = {
33    "aarch64-apple-darwin": "rust_darwin_aarch64",
34    "aarch64-unknown-linux-gnu": "rust_linux_aarch64",
35    "x86_64-apple-darwin": "rust_darwin_x86_64",
36    "x86_64-pc-windows-msvc": "rust_windows_x86_64",
37    "x86_64-unknown-freebsd": "rust_freebsd_x86_64",
38    "x86_64-unknown-linux-gnu": "rust_linux_x86_64",
39}
40
41def rules_rust_dependencies():
42    """Dependencies used in the implementation of `rules_rust`."""
43
44    maybe(
45        http_archive,
46        name = "platforms",
47        urls = [
48            "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz",
49            "https://github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz",
50        ],
51        sha256 = "379113459b0feaf6bfbb584a91874c065078aa673222846ac765f86661c27407",
52    )
53    maybe(
54        http_archive,
55        name = "rules_cc",
56        urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz"],
57        sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241",
58    )
59
60    maybe(
61        http_archive,
62        name = "bazel_skylib",
63        urls = [
64            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
65            "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
66        ],
67        sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
68    )
69
70    # Make the iOS simulator constraint available, which is referenced in abi_to_constraints()
71    # rules_rust does not require this dependency; it is just imported as a convenience for users.
72    maybe(
73        http_archive,
74        name = "build_bazel_apple_support",
75        sha256 = "5bbce1b2b9a3d4b03c0697687023ef5471578e76f994363c641c5f50ff0c7268",
76        url = "https://github.com/bazelbuild/apple_support/releases/download/0.13.0/apple_support.0.13.0.tar.gz",
77    )
78
79    # process_wrapper needs a low-dependency way to process json.
80    maybe(
81        http_archive,
82        name = "rules_rust_tinyjson",
83        sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16",
84        url = "https://crates.io/api/v1/crates/tinyjson/2.3.0/download",
85        strip_prefix = "tinyjson-2.3.0",
86        type = "tar.gz",
87        build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel",
88    )
89
90# buildifier: disable=unnamed-macro
91def rust_register_toolchains(
92        dev_components = False,
93        edition = None,
94        include_rustc_srcs = False,
95        iso_date = None,
96        register_toolchains = True,
97        rustfmt_version = None,
98        sha256s = None,
99        extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
100        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
101        version = rust_common.default_version):
102    """Emits a default set of toolchains for Linux, MacOS, and Freebsd
103
104    Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \
105    other hosts or for additional target triples.
106
107    The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example:
108    ```python
109    {
110        "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5",
111        "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0",
112        "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc",
113    }
114    ```
115    This would match for `exec_triple = "x86_64-unknown-linux-gnu"`.  If not specified, rules_rust pulls from a non-exhaustive \
116    list of known checksums..
117
118    See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details.
119
120    Args:
121        dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
122        edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every target is required to specify its `edition` attribute.
123        include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
124            See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details
125        iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version).
126        register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets.
127        rustfmt_version (str, optional): The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to `version` if not specified.
128        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes.
129        extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support.
130        urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format).
131        version (str, optional): The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version.
132    """
133    if dev_components and version != "nightly":
134        fail("Rust version must be set to \"nightly\" to enable rustc-dev components")
135
136    if not rustfmt_version:
137        rustfmt_version = version
138
139    rust_analyzer_repo_name = "rust_analyzer_{}".format(version)
140    if iso_date:
141        rust_analyzer_repo_name = "{}-{}".format(
142            rust_analyzer_repo_name,
143            iso_date,
144        )
145
146    maybe(
147        rust_analyzer_toolchain_repository,
148        name = rust_analyzer_repo_name,
149        version = version,
150        urls = urls,
151        sha256s = sha256s,
152        iso_date = iso_date,
153    )
154
155    if register_toolchains:
156        native.register_toolchains("@{}//:toolchain".format(
157            rust_analyzer_repo_name,
158        ))
159
160    for exec_triple, name in DEFAULT_TOOLCHAIN_TRIPLES.items():
161        maybe(
162            rust_repository_set,
163            name = name,
164            dev_components = dev_components,
165            edition = edition,
166            exec_triple = exec_triple,
167            extra_target_triples = extra_target_triples,
168            include_rustc_srcs = include_rustc_srcs,
169            iso_date = iso_date,
170            register_toolchain = register_toolchains,
171            rustfmt_version = rustfmt_version,
172            sha256s = sha256s,
173            urls = urls,
174            version = version,
175        )
176
177# buildifier: disable=unnamed-macro
178def rust_repositories(**kwargs):
179    """**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \
180    and [rust_register_toolchains](#rust_register_toolchains) directly.
181
182    Args:
183        **kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro.
184    """
185    rules_rust_dependencies()
186
187    rust_register_toolchains(**kwargs)
188
189def _rust_toolchain_tools_repository_impl(ctx):
190    """The implementation of the rust toolchain tools repository rule."""
191
192    check_version_valid(ctx.attr.version, ctx.attr.iso_date)
193
194    # Conditionally download rustc sources. Generally used for `rust-analyzer`
195    if should_include_rustc_srcs(ctx):
196        load_rust_src(ctx)
197
198    build_components = [
199        load_rust_compiler(ctx),
200        load_clippy(ctx),
201        load_cargo(ctx),
202    ]
203
204    if ctx.attr.rustfmt_version:
205        build_components.append(load_rustfmt(ctx))
206
207    # Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
208    include_llvm_tools = ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22")
209    if include_llvm_tools:
210        build_components.append(load_llvm_tools(ctx, ctx.attr.exec_triple))
211
212    build_components.append(load_rust_stdlib(ctx, ctx.attr.target_triple))
213
214    stdlib_linkflags = None
215    if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx.os.environ:
216        stdlib_linkflags = ctx.os.environ["BAZEL_RUST_STDLIB_LINKFLAGS"].split(":")
217
218    build_components.append(BUILD_for_rust_toolchain(
219        name = "rust_toolchain",
220        exec_triple = ctx.attr.exec_triple,
221        include_rustc_srcs = should_include_rustc_srcs(ctx),
222        target_triple = ctx.attr.target_triple,
223        stdlib_linkflags = stdlib_linkflags,
224        workspace_name = ctx.attr.name,
225        default_edition = ctx.attr.edition,
226        include_rustfmt = not (not ctx.attr.rustfmt_version),
227        include_llvm_tools = include_llvm_tools,
228    ))
229
230    # Not all target triples are expected to have dev components
231    if ctx.attr.dev_components:
232        load_rustc_dev_nightly(ctx, ctx.attr.target_triple)
233
234    ctx.file("WORKSPACE.bazel", "")
235    ctx.file("BUILD.bazel", "\n".join(build_components))
236
237rust_toolchain_tools_repository = repository_rule(
238    doc = (
239        "Composes a single workspace containing the toolchain components for compiling on a given " +
240        "platform to a series of target platforms.\n" +
241        "\n" +
242        "A given instance of this rule should be accompanied by a toolchain_repository_proxy " +
243        "invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " +
244        "selection from toolchain fetching."
245    ),
246    attrs = {
247        "auth": attr.string_dict(
248            doc = (
249                "Auth object compatible with repository_ctx.download to use when downloading files. " +
250                "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
251            ),
252        ),
253        "dev_components": attr.bool(
254            doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
255            default = False,
256        ),
257        "edition": attr.string(
258            doc = (
259                "The rust edition to be used by default (2015, 2018, or 2021). " +
260                "If absent, every rule is required to specify its `edition` attribute."
261            ),
262        ),
263        "exec_triple": attr.string(
264            doc = "The Rust-style target that this compiler runs on",
265            mandatory = True,
266        ),
267        "include_rustc_srcs": attr.bool(
268            doc = (
269                "Whether to download and unpack the rustc source files. These are very large, and " +
270                "slow to unpack, but are required to support rust analyzer. An environment variable " +
271                "`RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS` can also be used to control this attribute. " +
272                "This variable will take precedence over the hard coded attribute. Setting it to `true` to " +
273                "activates this attribute where all other values deactivate it."
274            ),
275            default = False,
276        ),
277        "iso_date": attr.string(
278            doc = "The date of the tool (or None, if the version is a specific version).",
279        ),
280        "rustfmt_version": attr.string(
281            doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
282        ),
283        "sha256s": attr.string_dict(
284            doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
285        ),
286        "target_triple": attr.string(
287            doc = "The Rust-style target that this compiler builds for",
288            mandatory = True,
289        ),
290        "urls": attr.string_list(
291            doc = "A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format).",
292            default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
293        ),
294        "version": attr.string(
295            doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
296            mandatory = True,
297        ),
298    },
299    implementation = _rust_toolchain_tools_repository_impl,
300    environ = ["RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS"],
301)
302
303def _toolchain_repository_proxy_impl(repository_ctx):
304    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
305        repository_ctx.name,
306    ))
307
308    repository_ctx.file("BUILD.bazel", BUILD_for_toolchain(
309        name = "toolchain",
310        toolchain = repository_ctx.attr.toolchain,
311        toolchain_type = repository_ctx.attr.toolchain_type,
312        target_compatible_with = json.encode(repository_ctx.attr.target_compatible_with),
313        exec_compatible_with = json.encode(repository_ctx.attr.exec_compatible_with),
314    ))
315
316toolchain_repository_proxy = repository_rule(
317    doc = (
318        "Generates a toolchain-bearing repository that declares the toolchains from some other " +
319        "rust_toolchain_repository."
320    ),
321    attrs = {
322        "exec_compatible_with": attr.string_list(
323            doc = "A list of constraints for the execution platform for this toolchain.",
324        ),
325        "target_compatible_with": attr.string_list(
326            doc = "A list of constraints for the target platform for this toolchain.",
327        ),
328        "toolchain": attr.string(
329            doc = "The name of the toolchain implementation target.",
330            mandatory = True,
331        ),
332        "toolchain_type": attr.string(
333            doc = "The toolchain type of the toolchain to declare",
334            mandatory = True,
335        ),
336    },
337    implementation = _toolchain_repository_proxy_impl,
338)
339
340# For legacy support
341rust_toolchain_repository_proxy = toolchain_repository_proxy
342
343def rust_toolchain_repository(
344        name,
345        version,
346        exec_triple,
347        target_triple,
348        exec_compatible_with = None,
349        target_compatible_with = None,
350        include_rustc_srcs = False,
351        iso_date = None,
352        rustfmt_version = None,
353        edition = None,
354        dev_components = False,
355        sha256s = None,
356        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
357        auth = None):
358    """Assembles a remote repository for the given toolchain params, produces a proxy repository \
359    to contain the toolchain declaration, and registers the toolchains.
360
361    N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) \
362    without actually downloading the toolchain.
363
364    Args:
365        name (str): The name of the generated repository
366        version (str): The version of the tool among "nightly", "beta', or an exact version.
367        exec_triple (str): The Rust-style target that this compiler runs on.
368        target_triple (str): The Rust-style target to build for.
369        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
370        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
371        include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
372        iso_date (str, optional): The date of the tool.
373        rustfmt_version (str, optional):  The version of rustfmt to be associated with the
374            toolchain.
375        edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute.
376        dev_components (bool, optional): Whether to download the rustc-dev components.
377            Requires version to be "nightly". Defaults to False.
378        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
379            [rust_repositories](#rust_repositories) for more details.
380        urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz']
381        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
382            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
383
384    Returns:
385        str: The name of the registerable toolchain created by this rule.
386    """
387
388    if exec_compatible_with == None:
389        exec_compatible_with = triple_to_constraint_set(exec_triple)
390
391    if target_compatible_with == None:
392        target_compatible_with = triple_to_constraint_set(target_triple)
393
394    rust_toolchain_tools_repository(
395        name = name + "_tools",
396        exec_triple = exec_triple,
397        include_rustc_srcs = include_rustc_srcs,
398        target_triple = target_triple,
399        iso_date = iso_date,
400        version = version,
401        rustfmt_version = rustfmt_version,
402        edition = edition,
403        dev_components = dev_components,
404        sha256s = sha256s,
405        urls = urls,
406        auth = auth,
407    )
408
409    toolchain_repository_proxy(
410        name = name,
411        toolchain = "@{}//:{}".format(name + "_tools", "rust_toolchain"),
412        toolchain_type = "@rules_rust//rust:toolchain",
413        exec_compatible_with = exec_compatible_with,
414        target_compatible_with = target_compatible_with,
415    )
416
417    return "@{name}//:toolchain".format(
418        name = name,
419    )
420
421def _rust_analyzer_toolchain_srcs_repository_impl(repository_ctx):
422    load_rust_src(repository_ctx)
423
424    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
425        repository_ctx.name,
426    ))
427
428    repository_ctx.file("BUILD.bazel", BUILD_for_rust_analyzer_toolchain(
429        name = "rust_analyzer_toolchain",
430    ))
431
432rust_analyzer_toolchain_srcs_repository = repository_rule(
433    doc = "A repository rule for defining a rust_analyzer_toolchain with a `rust-src` artifact.",
434    attrs = {
435        "auth": attr.string_dict(
436            doc = (
437                "Auth object compatible with repository_ctx.download to use when downloading files. " +
438                "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
439            ),
440        ),
441        "iso_date": attr.string(
442            doc = "The date of the tool (or None, if the version is a specific version).",
443        ),
444        "sha256s": attr.string_dict(
445            doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
446        ),
447        "urls": attr.string_list(
448            doc = "A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format).",
449            default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
450        ),
451        "version": attr.string(
452            doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
453            mandatory = True,
454        ),
455    },
456    implementation = _rust_analyzer_toolchain_srcs_repository_impl,
457)
458
459def rust_analyzer_toolchain_repository(
460        name,
461        version,
462        exec_compatible_with = [],
463        target_compatible_with = [],
464        iso_date = None,
465        sha256s = None,
466        urls = None,
467        auth = None):
468    """Assemble a remote rust_analyzer_toolchain target based on the given params.
469
470    Args:
471        name (str): The name of the toolchain proxy repository contianing the registerable toolchain.
472        version (str): The version of the tool among "nightly", "beta', or an exact version.
473        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
474        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
475        iso_date (str, optional): The date of the tool.
476        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
477            [rust_repositories](#rust_repositories) for more details.
478        urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz']
479        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
480            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
481
482    Returns:
483        str: The name of a registerable rust_analyzer_toolchain.
484    """
485    rust_analyzer_toolchain_srcs_repository(
486        name = name + "_srcs",
487        version = version,
488        iso_date = iso_date,
489        sha256s = sha256s,
490        urls = urls,
491        auth = auth,
492    )
493
494    toolchain_repository_proxy(
495        name = name,
496        toolchain = "@{}//:{}".format(name + "_srcs", "rust_analyzer_toolchain"),
497        toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type",
498        exec_compatible_with = exec_compatible_with,
499        target_compatible_with = target_compatible_with,
500    )
501
502    return "@{}//:toolchain".format(
503        name,
504    )
505
506def _rust_toolchain_set_repository_impl(repository_ctx):
507    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
508        repository_ctx.name,
509    ))
510
511    repository_ctx.file("BUILD.bazel", """exports_files(["defs.bzl"])""")
512    repository_ctx.file("defs.bzl", "ALL_TOOLCHAINS = {}\n".format(
513        json.encode_indent(repository_ctx.attr.toolchains, indent = " " * 4),
514    ))
515
516rust_toolchain_set_repository = repository_rule(
517    doc = (
518        "Generates a toolchain-bearing repository that declares the toolchains from some other " +
519        "rust_toolchain_repository."
520    ),
521    attrs = {
522        "toolchains": attr.string_list(
523            doc = "The list of all toolchains created by the current `rust_toolchain_set`",
524            mandatory = True,
525        ),
526    },
527    implementation = _rust_toolchain_set_repository_impl,
528)
529
530def rust_repository_set(
531        name,
532        version,
533        exec_triple,
534        include_rustc_srcs = False,
535        extra_target_triples = [],
536        iso_date = None,
537        rustfmt_version = None,
538        edition = None,
539        dev_components = False,
540        sha256s = None,
541        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
542        auth = None,
543        register_toolchain = True):
544    """Assembles a remote repository for the given toolchain params, produces a proxy repository \
545    to contain the toolchain declaration, and registers the toolchains.
546
547    N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) \
548    without actually downloading the toolchain.
549
550    Args:
551        name (str): The name of the generated repository
552        version (str): The version of the tool among "nightly", "beta', or an exact version.
553        exec_triple (str): The Rust-style target that this compiler runs on
554        include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.
555        extra_target_triples (list, optional): Additional rust-style targets that this set of
556            toolchains should support. Defaults to [].
557        iso_date (str, optional): The date of the tool. Defaults to None.
558        rustfmt_version (str, optional):  The version of rustfmt to be associated with the
559            toolchain. Defaults to None.
560        edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute.
561        dev_components (bool, optional): Whether to download the rustc-dev components.
562            Requires version to be "nightly". Defaults to False.
563        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
564            [rust_repositories](#rust_repositories) for more details.
565        urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz']
566        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
567            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
568        register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain.
569    """
570
571    all_toolchain_names = []
572    for target_triple in [exec_triple] + extra_target_triples:
573        toolchain_name = "{}__{}".format(name, target_triple)
574        all_toolchain_names.append(rust_toolchain_repository(
575            name = toolchain_name,
576            exec_triple = exec_triple,
577            include_rustc_srcs = include_rustc_srcs,
578            target_triple = target_triple,
579            iso_date = iso_date,
580            version = version,
581            rustfmt_version = rustfmt_version,
582            edition = edition,
583            dev_components = dev_components,
584            sha256s = sha256s,
585            urls = urls,
586            auth = auth,
587        ))
588
589    # This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper.
590    rust_toolchain_set_repository(
591        name = name,
592        toolchains = all_toolchain_names,
593    )
594
595    # Register toolchains
596    if register_toolchain:
597        native.register_toolchains(*all_toolchain_names)
598        native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))
599
600    # Inform users that they should be using the canonical name if it's not detected
601    if "rules_rust" not in native.existing_rules():
602        message = "\n" + ("=" * 79) + "\n"
603        message += (
604            "It appears that you are trying to import rules_rust without using its\n" +
605            "canonical name, \"@rules_rust\" Please change your WORKSPACE file to\n" +
606            "import this repo with `name = \"rules_rust\"` instead."
607        )
608
609        if "io_bazel_rules_rust" in native.existing_rules():
610            message += "\n\n" + (
611                "Note that the previous name of \"@io_bazel_rules_rust\" is deprecated.\n" +
612                "See https://github.com/bazelbuild/rules_rust/issues/499 for context."
613            )
614
615        message += "\n" + ("=" * 79)
616        fail(message)