fork/rules_rust

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

UebelAndreCleanup deprecated code (#1577)352bfeb0

0.11.0
27.2 KiB611 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 = "d94b7a0f49d735f196e1f36d2e6ef79c4e8e8b82132848dd8cd93cd82d9b12a8",
76        url = "https://github.com/bazelbuild/apple_support/releases/download/1.3.0/apple_support.1.3.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        allocator_library = None,
96        iso_date = None,
97        register_toolchains = True,
98        rustfmt_version = None,
99        sha256s = None,
100        extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
101        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
102        version = rust_common.default_version):
103    """Emits a default set of toolchains for Linux, MacOS, and Freebsd
104
105    Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \
106    other hosts or for additional target triples.
107
108    The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example:
109    ```python
110    {
111        "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5",
112        "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0",
113        "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc",
114    }
115    ```
116    This would match for `exec_triple = "x86_64-unknown-linux-gnu"`.  If not specified, rules_rust pulls from a non-exhaustive \
117    list of known checksums..
118
119    See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details.
120
121    Args:
122        dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
123        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.
124        include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
125            See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details
126        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary.
127        iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version).
128        register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets.
129        rustfmt_version (str, optional): The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to `version` if not specified.
130        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes.
131        extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support.
132        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).
133        version (str, optional): The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version.
134    """
135    if dev_components and version != "nightly":
136        fail("Rust version must be set to \"nightly\" to enable rustc-dev components")
137
138    if not rustfmt_version:
139        rustfmt_version = version
140
141    rust_analyzer_repo_name = "rust_analyzer_{}".format(version)
142    if iso_date:
143        rust_analyzer_repo_name = "{}-{}".format(
144            rust_analyzer_repo_name,
145            iso_date,
146        )
147
148    maybe(
149        rust_analyzer_toolchain_repository,
150        name = rust_analyzer_repo_name,
151        version = version,
152        urls = urls,
153        sha256s = sha256s,
154        iso_date = iso_date,
155    )
156
157    if register_toolchains:
158        native.register_toolchains("@{}//:toolchain".format(
159            rust_analyzer_repo_name,
160        ))
161
162    for exec_triple, name in DEFAULT_TOOLCHAIN_TRIPLES.items():
163        maybe(
164            rust_repository_set,
165            name = name,
166            dev_components = dev_components,
167            edition = edition,
168            exec_triple = exec_triple,
169            extra_target_triples = extra_target_triples,
170            include_rustc_srcs = include_rustc_srcs,
171            allocator_library = allocator_library,
172            iso_date = iso_date,
173            register_toolchain = register_toolchains,
174            rustfmt_version = rustfmt_version,
175            sha256s = sha256s,
176            urls = urls,
177            version = version,
178        )
179
180# buildifier: disable=unnamed-macro
181def rust_repositories(**kwargs):
182    """**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \
183    and [rust_register_toolchains](#rust_register_toolchains) directly.
184
185    Args:
186        **kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro.
187    """
188    rules_rust_dependencies()
189
190    rust_register_toolchains(**kwargs)
191
192def _rust_toolchain_tools_repository_impl(ctx):
193    """The implementation of the rust toolchain tools repository rule."""
194
195    check_version_valid(ctx.attr.version, ctx.attr.iso_date)
196
197    # Conditionally download rustc sources. Generally used for `rust-analyzer`
198    if should_include_rustc_srcs(ctx):
199        load_rust_src(ctx)
200
201    build_components = [
202        load_rust_compiler(ctx),
203        load_clippy(ctx),
204        load_cargo(ctx),
205    ]
206
207    if ctx.attr.rustfmt_version:
208        build_components.append(load_rustfmt(ctx))
209
210    # Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
211    include_llvm_tools = ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22")
212    if include_llvm_tools:
213        build_components.append(load_llvm_tools(ctx, ctx.attr.exec_triple))
214
215    build_components.append(load_rust_stdlib(ctx, ctx.attr.target_triple))
216
217    stdlib_linkflags = None
218    if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx.os.environ:
219        stdlib_linkflags = ctx.os.environ["BAZEL_RUST_STDLIB_LINKFLAGS"].split(":")
220
221    build_components.append(BUILD_for_rust_toolchain(
222        name = "rust_toolchain",
223        exec_triple = ctx.attr.exec_triple,
224        include_rustc_srcs = should_include_rustc_srcs(ctx),
225        allocator_library = ctx.attr.allocator_library,
226        target_triple = ctx.attr.target_triple,
227        stdlib_linkflags = stdlib_linkflags,
228        workspace_name = ctx.attr.name,
229        default_edition = ctx.attr.edition,
230        include_rustfmt = not (not ctx.attr.rustfmt_version),
231        include_llvm_tools = include_llvm_tools,
232    ))
233
234    # Not all target triples are expected to have dev components
235    if ctx.attr.dev_components:
236        load_rustc_dev_nightly(ctx, ctx.attr.target_triple)
237
238    ctx.file("WORKSPACE.bazel", "")
239    ctx.file("BUILD.bazel", "\n".join(build_components))
240
241rust_toolchain_tools_repository = repository_rule(
242    doc = (
243        "Composes a single workspace containing the toolchain components for compiling on a given " +
244        "platform to a series of target platforms.\n" +
245        "\n" +
246        "A given instance of this rule should be accompanied by a toolchain_repository_proxy " +
247        "invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " +
248        "selection from toolchain fetching."
249    ),
250    attrs = {
251        "allocator_library": attr.string(
252            doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
253        ),
254        "auth": attr.string_dict(
255            doc = (
256                "Auth object compatible with repository_ctx.download to use when downloading files. " +
257                "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
258            ),
259        ),
260        "dev_components": attr.bool(
261            doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
262            default = False,
263        ),
264        "edition": attr.string(
265            doc = (
266                "The rust edition to be used by default (2015, 2018, or 2021). " +
267                "If absent, every rule is required to specify its `edition` attribute."
268            ),
269        ),
270        "exec_triple": attr.string(
271            doc = "The Rust-style target that this compiler runs on",
272            mandatory = True,
273        ),
274        "include_rustc_srcs": attr.bool(
275            doc = (
276                "Whether to download and unpack the rustc source files. These are very large, and " +
277                "slow to unpack, but are required to support rust analyzer. An environment variable " +
278                "`RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS` can also be used to control this attribute. " +
279                "This variable will take precedence over the hard coded attribute. Setting it to `true` to " +
280                "activates this attribute where all other values deactivate it."
281            ),
282            default = False,
283        ),
284        "iso_date": attr.string(
285            doc = "The date of the tool (or None, if the version is a specific version).",
286        ),
287        "rustfmt_version": attr.string(
288            doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
289        ),
290        "sha256s": attr.string_dict(
291            doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
292        ),
293        "target_triple": attr.string(
294            doc = "The Rust-style target that this compiler builds for",
295            mandatory = True,
296        ),
297        "urls": attr.string_list(
298            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).",
299            default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
300        ),
301        "version": attr.string(
302            doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
303            mandatory = True,
304        ),
305    },
306    implementation = _rust_toolchain_tools_repository_impl,
307    environ = ["RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS"],
308)
309
310def _toolchain_repository_proxy_impl(repository_ctx):
311    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
312        repository_ctx.name,
313    ))
314
315    repository_ctx.file("BUILD.bazel", BUILD_for_toolchain(
316        name = "toolchain",
317        toolchain = repository_ctx.attr.toolchain,
318        toolchain_type = repository_ctx.attr.toolchain_type,
319        target_compatible_with = json.encode(repository_ctx.attr.target_compatible_with),
320        exec_compatible_with = json.encode(repository_ctx.attr.exec_compatible_with),
321    ))
322
323toolchain_repository_proxy = repository_rule(
324    doc = (
325        "Generates a toolchain-bearing repository that declares the toolchains from some other " +
326        "rust_toolchain_repository."
327    ),
328    attrs = {
329        "exec_compatible_with": attr.string_list(
330            doc = "A list of constraints for the execution platform for this toolchain.",
331        ),
332        "target_compatible_with": attr.string_list(
333            doc = "A list of constraints for the target platform for this toolchain.",
334        ),
335        "toolchain": attr.string(
336            doc = "The name of the toolchain implementation target.",
337            mandatory = True,
338        ),
339        "toolchain_type": attr.string(
340            doc = "The toolchain type of the toolchain to declare",
341            mandatory = True,
342        ),
343    },
344    implementation = _toolchain_repository_proxy_impl,
345)
346
347# For legacy support
348rust_toolchain_repository_proxy = toolchain_repository_proxy
349
350def rust_toolchain_repository(
351        name,
352        version,
353        exec_triple,
354        target_triple,
355        exec_compatible_with = None,
356        target_compatible_with = None,
357        include_rustc_srcs = False,
358        allocator_library = None,
359        iso_date = None,
360        rustfmt_version = None,
361        edition = None,
362        dev_components = False,
363        sha256s = None,
364        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
365        auth = None):
366    """Assembles a remote repository for the given toolchain params, produces a proxy repository \
367    to contain the toolchain declaration, and registers the toolchains.
368
369    N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) \
370    without actually downloading the toolchain.
371
372    Args:
373        name (str): The name of the generated repository
374        version (str): The version of the tool among "nightly", "beta', or an exact version.
375        exec_triple (str): The Rust-style target that this compiler runs on.
376        target_triple (str): The Rust-style target to build for.
377        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
378        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
379        include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
380        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary.
381        iso_date (str, optional): The date of the tool.
382        rustfmt_version (str, optional):  The version of rustfmt to be associated with the
383            toolchain.
384        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.
385        dev_components (bool, optional): Whether to download the rustc-dev components.
386            Requires version to be "nightly". Defaults to False.
387        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
388            [rust_repositories](#rust_repositories) for more details.
389        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']
390        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
391            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
392
393    Returns:
394        str: The name of the registerable toolchain created by this rule.
395    """
396
397    if exec_compatible_with == None:
398        exec_compatible_with = triple_to_constraint_set(exec_triple)
399
400    if target_compatible_with == None:
401        target_compatible_with = triple_to_constraint_set(target_triple)
402
403    rust_toolchain_tools_repository(
404        name = name + "_tools",
405        exec_triple = exec_triple,
406        include_rustc_srcs = include_rustc_srcs,
407        allocator_library = allocator_library,
408        target_triple = target_triple,
409        iso_date = iso_date,
410        version = version,
411        rustfmt_version = rustfmt_version,
412        edition = edition,
413        dev_components = dev_components,
414        sha256s = sha256s,
415        urls = urls,
416        auth = auth,
417    )
418
419    toolchain_repository_proxy(
420        name = name,
421        toolchain = "@{}//:{}".format(name + "_tools", "rust_toolchain"),
422        toolchain_type = "@rules_rust//rust:toolchain",
423        exec_compatible_with = exec_compatible_with,
424        target_compatible_with = target_compatible_with,
425    )
426
427    return "@{name}//:toolchain".format(
428        name = name,
429    )
430
431def _rust_analyzer_toolchain_srcs_repository_impl(repository_ctx):
432    load_rust_src(repository_ctx)
433
434    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
435        repository_ctx.name,
436    ))
437
438    repository_ctx.file("BUILD.bazel", BUILD_for_rust_analyzer_toolchain(
439        name = "rust_analyzer_toolchain",
440    ))
441
442rust_analyzer_toolchain_srcs_repository = repository_rule(
443    doc = "A repository rule for defining a rust_analyzer_toolchain with a `rust-src` artifact.",
444    attrs = {
445        "auth": attr.string_dict(
446            doc = (
447                "Auth object compatible with repository_ctx.download to use when downloading files. " +
448                "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
449            ),
450        ),
451        "iso_date": attr.string(
452            doc = "The date of the tool (or None, if the version is a specific version).",
453        ),
454        "sha256s": attr.string_dict(
455            doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
456        ),
457        "urls": attr.string_list(
458            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).",
459            default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
460        ),
461        "version": attr.string(
462            doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
463            mandatory = True,
464        ),
465    },
466    implementation = _rust_analyzer_toolchain_srcs_repository_impl,
467)
468
469def rust_analyzer_toolchain_repository(
470        name,
471        version,
472        exec_compatible_with = [],
473        target_compatible_with = [],
474        iso_date = None,
475        sha256s = None,
476        urls = None,
477        auth = None):
478    """Assemble a remote rust_analyzer_toolchain target based on the given params.
479
480    Args:
481        name (str): The name of the toolchain proxy repository contianing the registerable toolchain.
482        version (str): The version of the tool among "nightly", "beta', or an exact version.
483        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
484        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
485        iso_date (str, optional): The date of the tool.
486        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
487            [rust_repositories](#rust_repositories) for more details.
488        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']
489        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
490            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
491
492    Returns:
493        str: The name of a registerable rust_analyzer_toolchain.
494    """
495    rust_analyzer_toolchain_srcs_repository(
496        name = name + "_srcs",
497        version = version,
498        iso_date = iso_date,
499        sha256s = sha256s,
500        urls = urls,
501        auth = auth,
502    )
503
504    toolchain_repository_proxy(
505        name = name,
506        toolchain = "@{}//:{}".format(name + "_srcs", "rust_analyzer_toolchain"),
507        toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type",
508        exec_compatible_with = exec_compatible_with,
509        target_compatible_with = target_compatible_with,
510    )
511
512    return "@{}//:toolchain".format(
513        name,
514    )
515
516def _rust_toolchain_set_repository_impl(repository_ctx):
517    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
518        repository_ctx.name,
519    ))
520
521    repository_ctx.file("BUILD.bazel", """exports_files(["defs.bzl"])""")
522    repository_ctx.file("defs.bzl", "ALL_TOOLCHAINS = {}\n".format(
523        json.encode_indent(repository_ctx.attr.toolchains, indent = " " * 4),
524    ))
525
526rust_toolchain_set_repository = repository_rule(
527    doc = (
528        "Generates a toolchain-bearing repository that declares the toolchains from some other " +
529        "rust_toolchain_repository."
530    ),
531    attrs = {
532        "toolchains": attr.string_list(
533            doc = "The list of all toolchains created by the current `rust_toolchain_set`",
534            mandatory = True,
535        ),
536    },
537    implementation = _rust_toolchain_set_repository_impl,
538)
539
540def rust_repository_set(
541        name,
542        version,
543        exec_triple,
544        include_rustc_srcs = False,
545        allocator_library = None,
546        extra_target_triples = [],
547        iso_date = None,
548        rustfmt_version = None,
549        edition = None,
550        dev_components = False,
551        sha256s = None,
552        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
553        auth = None,
554        register_toolchain = True):
555    """Assembles a remote repository for the given toolchain params, produces a proxy repository \
556    to contain the toolchain declaration, and registers the toolchains.
557
558    N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) \
559    without actually downloading the toolchain.
560
561    Args:
562        name (str): The name of the generated repository
563        version (str): The version of the tool among "nightly", "beta', or an exact version.
564        exec_triple (str): The Rust-style target that this compiler runs on
565        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.
566        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary.
567        extra_target_triples (list, optional): Additional rust-style targets that this set of
568            toolchains should support. Defaults to [].
569        iso_date (str, optional): The date of the tool. Defaults to None.
570        rustfmt_version (str, optional):  The version of rustfmt to be associated with the
571            toolchain. Defaults to None.
572        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.
573        dev_components (bool, optional): Whether to download the rustc-dev components.
574            Requires version to be "nightly". Defaults to False.
575        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
576            [rust_repositories](#rust_repositories) for more details.
577        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']
578        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
579            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
580        register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain.
581    """
582
583    all_toolchain_names = []
584    for target_triple in [exec_triple] + extra_target_triples:
585        toolchain_name = "{}__{}".format(name, target_triple)
586        all_toolchain_names.append(rust_toolchain_repository(
587            name = toolchain_name,
588            exec_triple = exec_triple,
589            include_rustc_srcs = include_rustc_srcs,
590            allocator_library = allocator_library,
591            target_triple = target_triple,
592            iso_date = iso_date,
593            version = version,
594            rustfmt_version = rustfmt_version,
595            edition = edition,
596            dev_components = dev_components,
597            sha256s = sha256s,
598            urls = urls,
599            auth = auth,
600        ))
601
602    # This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper.
603    rust_toolchain_set_repository(
604        name = name,
605        toolchains = all_toolchain_names,
606    )
607
608    # Register toolchains
609    if register_toolchain:
610        native.register_toolchains(*all_toolchain_names)
611        native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))