fork/rules_rust

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

UebelAndreRemove deprecated variables from `repositories.bzl` macros (#2795)bef8d2d4

patching
46.6 KiB1072 linesraw
1"""Repository rules for defining Rust dependencies and toolchains"""
2
3load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
5load("//rust/platform:triple.bzl", "get_host_triple", "triple")
6load("//rust/platform:triple_mappings.bzl", "triple_to_constraint_set")
7load("//rust/private:common.bzl", "rust_common")
8load(
9    "//rust/private:repository_utils.bzl",
10    "BUILD_for_rust_analyzer_proc_macro_srv",
11    "BUILD_for_rust_analyzer_toolchain",
12    "BUILD_for_rust_toolchain",
13    "BUILD_for_rustfmt_toolchain",
14    "BUILD_for_toolchain",
15    "DEFAULT_EXTRA_TARGET_TRIPLES",
16    "DEFAULT_NIGHTLY_VERSION",
17    "DEFAULT_STATIC_RUST_URL_TEMPLATES",
18    "TINYJSON_KWARGS",
19    "check_version_valid",
20    "includes_rust_analyzer_proc_macro_srv",
21    "load_cargo",
22    "load_clippy",
23    "load_llvm_tools",
24    "load_rust_compiler",
25    "load_rust_src",
26    "load_rust_stdlib",
27    "load_rustc_dev_nightly",
28    "load_rustfmt",
29    "select_rust_version",
30    "toolchain_repository_hub",
31    _load_arbitrary_tool = "load_arbitrary_tool",
32)
33
34# Re-export `load_arbitrary_tool` as it's historically been used in external repositories.
35load_arbitrary_tool = _load_arbitrary_tool
36
37# 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.
38DEFAULT_TOOLCHAIN_TRIPLES = {
39    "aarch64-apple-darwin": "rust_darwin_aarch64",
40    "aarch64-pc-windows-msvc": "rust_windows_aarch64",
41    "aarch64-unknown-linux-gnu": "rust_linux_aarch64",
42    "x86_64-apple-darwin": "rust_darwin_x86_64",
43    "x86_64-pc-windows-msvc": "rust_windows_x86_64",
44    "x86_64-unknown-freebsd": "rust_freebsd_x86_64",
45    "x86_64-unknown-linux-gnu": "rust_linux_x86_64",
46}
47
48def rules_rust_dependencies():
49    """Dependencies used in the implementation of `rules_rust`."""
50
51    maybe(
52        http_archive,
53        name = "platforms",
54        urls = [
55            "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
56            "https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
57        ],
58        sha256 = "8150406605389ececb6da07cbcb509d5637a3ab9a24bc69b1101531367d89d74",
59    )
60    maybe(
61        http_archive,
62        name = "rules_cc",
63        urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"],
64        sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
65        strip_prefix = "rules_cc-0.0.9",
66    )
67    maybe(
68        http_archive,
69        name = "rules_license",
70        urls = [
71            "https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.8/rules_license-0.0.8.tar.gz",
72            "https://github.com/bazelbuild/rules_license/releases/download/0.0.8/rules_license-0.0.8.tar.gz",
73        ],
74        sha256 = "241b06f3097fd186ff468832150d6cc142247dc42a32aaefb56d0099895fd229",
75    )
76
77    maybe(
78        http_archive,
79        name = "bazel_skylib",
80        sha256 = "cd55a062e763b9349921f0f5db8c3933288dc8ba4f76dd9416aac68acee3cb94",
81        urls = [
82            "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
83            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.5.0/bazel-skylib-1.5.0.tar.gz",
84        ],
85    )
86
87    # Make the iOS simulator constraint available, which is referenced in abi_to_constraints()
88    # rules_rust does not require this dependency; it is just imported as a convenience for users.
89    maybe(
90        http_archive,
91        name = "build_bazel_apple_support",
92        sha256 = "1c4031e72b456a048d8177f59a5581808c07585fa9e255c6f5fefb8752af7e40",
93        url = "https://github.com/bazelbuild/apple_support/releases/download/1.13.0/apple_support.1.13.0.tar.gz",
94    )
95
96    # process_wrapper needs a low-dependency way to process json.
97    maybe(
98        http_archive,
99        **TINYJSON_KWARGS
100    )
101
102_RUST_TOOLCHAIN_VERSIONS = [
103    rust_common.default_version,
104    DEFAULT_NIGHTLY_VERSION,
105]
106
107# buildifier: disable=unnamed-macro
108def rust_register_toolchains(
109        dev_components = False,
110        edition = None,
111        allocator_library = None,
112        global_allocator_library = None,
113        register_toolchains = True,
114        rustfmt_version = None,
115        rust_analyzer_version = None,
116        sha256s = None,
117        extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES,
118        extra_rustc_flags = None,
119        extra_exec_rustc_flags = None,
120        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
121        versions = _RUST_TOOLCHAIN_VERSIONS):
122    """Emits a default set of toolchains for Linux, MacOS, and Freebsd
123
124    Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \
125    other hosts or for additional target triples.
126
127    The `sha256s` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example:
128    ```python
129    {
130        "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5",
131        "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0",
132        "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc",
133    }
134    ```
135    This would match for `exec_triple = "x86_64-unknown-linux-gnu"`.  If not specified, rules_rust pulls from a non-exhaustive \
136    list of known checksums..
137
138    See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details.
139
140    Args:
141        dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
142        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.
143        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary.
144        global_allocator_library (str, optional): Target that provides allocator functions when global allocator is used with cc_common.link.
145        register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets.
146        rustfmt_version (str, optional): The version of rustfmt. If none is supplied and only a single version in `versions` is given, then this defaults to that version, otherwise will default to the default nightly version.
147        rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer.
148        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes.
149        extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support.
150        extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
151        extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
152        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).
153        versions (list, optional): A list of toolchain versions to download. This paramter only accepts one versions
154            per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`.
155    """
156    if not rustfmt_version:
157        if len(versions) == 1:
158            rustfmt_version = versions[0]
159        else:
160            rustfmt_version = DEFAULT_NIGHTLY_VERSION
161
162    if dev_components:
163        has_nightly = False
164        for ver in versions:
165            if ver.startswith("nightly"):
166                has_nightly = True
167                break
168        if not has_nightly:
169            fail("rustc-dev components were requested but no \"nightly\" is being registered. Please update `versions` to include a nightly version.")
170
171    if not rust_analyzer_version:
172        rust_analyzer_version = select_rust_version(versions)
173
174    rust_analyzer_repo_name = "rust_analyzer_{}".format(rust_analyzer_version.replace("/", "-"))
175
176    toolchain_names = []
177    toolchain_labels = {}
178    toolchain_types = {}
179    exec_compatible_with_by_toolchain = {}
180    target_compatible_with_by_toolchain = {}
181
182    maybe(
183        rust_analyzer_toolchain_repository,
184        name = rust_analyzer_repo_name,
185        version = rust_analyzer_version,
186        urls = urls,
187        sha256s = sha256s,
188    )
189
190    toolchain_names.append(rust_analyzer_repo_name)
191    toolchain_labels[rust_analyzer_repo_name] = "@{}_tools//:rust_analyzer_toolchain".format(
192        rust_analyzer_repo_name,
193    )
194    exec_compatible_with_by_toolchain[rust_analyzer_repo_name] = []
195    target_compatible_with_by_toolchain[rust_analyzer_repo_name] = []
196    toolchain_types[rust_analyzer_repo_name] = "@rules_rust//rust/rust_analyzer:toolchain_type"
197
198    if register_toolchains:
199        native.register_toolchains("@{}//:toolchain".format(
200            rust_analyzer_repo_name,
201        ))
202
203    for exec_triple, name in DEFAULT_TOOLCHAIN_TRIPLES.items():
204        maybe(
205            rust_repository_set,
206            name = name,
207            dev_components = dev_components,
208            edition = edition,
209            exec_triple = exec_triple,
210            extra_target_triples = extra_target_triples,
211            allocator_library = allocator_library,
212            global_allocator_library = global_allocator_library,
213            register_toolchain = register_toolchains,
214            rustfmt_version = rustfmt_version,
215            extra_rustc_flags = extra_rustc_flags,
216            extra_exec_rustc_flags = extra_exec_rustc_flags,
217            sha256s = sha256s,
218            urls = urls,
219            versions = versions,
220        )
221
222        rustfmt_repo_name = "rustfmt_{}__{}".format(rustfmt_version.replace("/", "-"), exec_triple)
223
224        maybe(
225            rustfmt_toolchain_repository,
226            name = rustfmt_repo_name,
227            version = rustfmt_version,
228            urls = urls,
229            sha256s = sha256s,
230            exec_triple = exec_triple,
231        )
232
233        if register_toolchains:
234            native.register_toolchains("@{}//:toolchain".format(
235                rustfmt_repo_name,
236            ))
237
238        for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions):
239            toolchain_names.append(toolchain.name)
240            toolchain_labels[toolchain.name] = "@{}//:{}".format(toolchain.name + "_tools", "rust_toolchain")
241            exec_compatible_with_by_toolchain[toolchain.name] = triple_to_constraint_set(exec_triple)
242            target_compatible_with_by_toolchain[toolchain.name] = triple_to_constraint_set(toolchain.target_triple)
243            toolchain_types[toolchain.name] = "@rules_rust//rust:toolchain"
244
245        toolchain_names.append(rustfmt_repo_name)
246        toolchain_labels[rustfmt_repo_name] = "@{}_tools//:rustfmt_toolchain".format(rustfmt_repo_name)
247        exec_compatible_with_by_toolchain[rustfmt_repo_name] = triple_to_constraint_set(exec_triple)
248        target_compatible_with_by_toolchain[rustfmt_repo_name] = []
249        toolchain_types[rustfmt_repo_name] = "@rules_rust//rust/rustfmt:toolchain_type"
250
251    toolchain_repository_hub(
252        name = "rust_toolchains",
253        toolchain_names = toolchain_names,
254        toolchain_labels = toolchain_labels,
255        toolchain_types = toolchain_types,
256        exec_compatible_with = exec_compatible_with_by_toolchain,
257        target_compatible_with = target_compatible_with_by_toolchain,
258    )
259
260# buildifier: disable=unnamed-macro
261def rust_repositories(**kwargs):
262    """**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \
263    and [rust_register_toolchains](#rust_register_toolchains) directly.
264
265    Args:
266        **kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro.
267    """
268    rules_rust_dependencies()
269
270    rust_register_toolchains(**kwargs)
271
272_RUST_TOOLCHAIN_REPOSITORY_ATTRS = {
273    "allocator_library": attr.string(
274        doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
275        default = "@rules_rust//ffi/cc/allocator_library",
276    ),
277    "auth": attr.string_dict(
278        doc = (
279            "Auth object compatible with repository_ctx.download to use when downloading files. " +
280            "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
281        ),
282    ),
283    "auth_patterns": attr.string_list(
284        doc = "A list of patterns to match against urls for which the auth object should be used.",
285    ),
286    "dev_components": attr.bool(
287        doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
288        default = False,
289    ),
290    "edition": attr.string(
291        doc = (
292            "The rust edition to be used by default (2015, 2018, or 2021). " +
293            "If absent, every rule is required to specify its `edition` attribute."
294        ),
295    ),
296    "exec_triple": attr.string(
297        doc = "The Rust-style target that this compiler runs on",
298        mandatory = True,
299    ),
300    "extra_exec_rustc_flags": attr.string_list(
301        doc = "Extra flags to pass to rustc in exec configuration",
302    ),
303    "extra_rustc_flags": attr.string_list(
304        doc = "Extra flags to pass to rustc in non-exec configuration",
305    ),
306    "global_allocator_library": attr.string(
307        doc = "Target that provides allocator functions when a global allocator is used with cc_common.link.",
308        default = "@rules_rust//ffi/cc/global_allocator_library",
309    ),
310    "netrc": attr.string(
311        doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive",
312    ),
313    "opt_level": attr.string_dict(
314        doc = "Rustc optimization levels. For more details see the documentation for `rust_toolchain.opt_level`.",
315    ),
316    "rustfmt_version": attr.string(
317        doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
318    ),
319    "sha256s": attr.string_dict(
320        doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details.",
321    ),
322    "target_triple": attr.string(
323        doc = "The Rust-style target that this compiler builds for.",
324        mandatory = True,
325    ),
326    "urls": attr.string_list(
327        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).",
328        default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
329    ),
330    "version": attr.string(
331        doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
332        mandatory = True,
333    ),
334}
335
336def _rust_toolchain_tools_repository_impl(ctx):
337    """The implementation of the rust toolchain tools repository rule."""
338    sha256s = dict(ctx.attr.sha256s)
339    iso_date = None
340    version = ctx.attr.version
341    version_array = version.split("/")
342    if len(version_array) > 1:
343        version = version_array[0]
344        iso_date = version_array[1]
345
346    check_version_valid(ctx.attr.version, iso_date)
347
348    exec_triple = triple(ctx.attr.exec_triple)
349
350    rustc_content, rustc_sha256 = load_rust_compiler(
351        ctx = ctx,
352        iso_date = iso_date,
353        target_triple = exec_triple,
354        version = version,
355    )
356    clippy_content, clippy_sha256 = load_clippy(
357        ctx = ctx,
358        iso_date = iso_date,
359        target_triple = exec_triple,
360        version = version,
361    )
362    cargo_content, cargo_sha256 = load_cargo(
363        ctx = ctx,
364        iso_date = iso_date,
365        target_triple = exec_triple,
366        version = version,
367    )
368
369    build_components = [
370        rustc_content,
371        clippy_content,
372        cargo_content,
373    ]
374    sha256s.update(rustc_sha256 | clippy_sha256 | cargo_sha256)
375
376    if ctx.attr.rustfmt_version:
377        rustfmt_version = ctx.attr.rustfmt_version
378        rustfmt_iso_date = None
379        if rustfmt_version in ("nightly", "beta"):
380            if iso_date:
381                rustfmt_iso_date = iso_date
382            else:
383                fail("`rustfmt_version` does not include an iso_date. The following reposiotry should either set `iso_date` or update `rustfmt_version` to include an iso_date suffix: {}".format(
384                    ctx.name,
385                ))
386        elif rustfmt_version.startswith(("nightly", "beta")):
387            rustfmt_version, _, rustfmt_iso_date = rustfmt_version.partition("/")
388        rustfmt_content, rustfmt_sha256 = load_rustfmt(
389            ctx = ctx,
390            target_triple = triple(ctx.attr.exec_triple),
391            version = rustfmt_version,
392            iso_date = rustfmt_iso_date,
393        )
394        build_components.append(rustfmt_content)
395        sha256s.update(rustfmt_sha256)
396
397    # Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
398    include_llvm_tools = version >= "1.45.0" or (version == "nightly" and iso_date > "2020-05-22")
399    if include_llvm_tools:
400        llvm_tools_content, llvm_tools_sha256 = load_llvm_tools(
401            ctx = ctx,
402            target_triple = exec_triple,
403            version = version,
404            iso_date = iso_date,
405        )
406        build_components.append(llvm_tools_content)
407        sha256s.update(llvm_tools_sha256)
408
409    target_triple = triple(ctx.attr.target_triple)
410    rust_stdlib_content, rust_stdlib_sha256 = load_rust_stdlib(
411        ctx = ctx,
412        target_triple = target_triple,
413        version = version,
414        iso_date = iso_date,
415    )
416    build_components.append(rust_stdlib_content)
417    sha256s.update(rust_stdlib_sha256)
418
419    stdlib_linkflags = None
420    if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx.os.environ:
421        stdlib_linkflags = ctx.os.environ["BAZEL_RUST_STDLIB_LINKFLAGS"].split(":")
422
423    build_components.append(BUILD_for_rust_toolchain(
424        name = "rust_toolchain",
425        exec_triple = exec_triple,
426        allocator_library = ctx.attr.allocator_library,
427        global_allocator_library = ctx.attr.global_allocator_library,
428        target_triple = target_triple,
429        stdlib_linkflags = stdlib_linkflags,
430        default_edition = ctx.attr.edition,
431        include_rustfmt = not (not ctx.attr.rustfmt_version),
432        include_llvm_tools = include_llvm_tools,
433        extra_rustc_flags = ctx.attr.extra_rustc_flags,
434        extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags,
435        opt_level = ctx.attr.opt_level if ctx.attr.opt_level else None,
436    ))
437
438    # Not all target triples are expected to have dev components
439    if ctx.attr.dev_components:
440        rustc_dev_sha256 = load_rustc_dev_nightly(
441            ctx = ctx,
442            target_triple = target_triple,
443            version = version,
444            iso_date = iso_date,
445        )
446        sha256s.update(rustc_dev_sha256)
447
448    ctx.file("WORKSPACE.bazel", "")
449    ctx.file("BUILD.bazel", "\n".join(build_components))
450
451    repro = {"name": ctx.name}
452    for key in _RUST_TOOLCHAIN_REPOSITORY_ATTRS:
453        repro[key] = getattr(ctx.attr, key)
454    repro["sha256s"] = sha256s
455
456    return repro
457
458rust_toolchain_tools_repository = repository_rule(
459    doc = (
460        "Composes a single workspace containing the toolchain components for compiling on a given " +
461        "platform to a series of target platforms.\n" +
462        "\n" +
463        "A given instance of this rule should be accompanied by a toolchain_repository_proxy " +
464        "invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " +
465        "selection from toolchain fetching."
466    ),
467    attrs = _RUST_TOOLCHAIN_REPOSITORY_ATTRS,
468    implementation = _rust_toolchain_tools_repository_impl,
469)
470
471def _toolchain_repository_proxy_impl(repository_ctx):
472    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
473        repository_ctx.name,
474    ))
475
476    repository_ctx.file("BUILD.bazel", BUILD_for_toolchain(
477        name = "toolchain",
478        toolchain = repository_ctx.attr.toolchain,
479        target_settings = repository_ctx.attr.target_settings,
480        toolchain_type = repository_ctx.attr.toolchain_type,
481        target_compatible_with = repository_ctx.attr.target_compatible_with,
482        exec_compatible_with = repository_ctx.attr.exec_compatible_with,
483    ))
484
485toolchain_repository_proxy = repository_rule(
486    doc = (
487        "Generates a toolchain-bearing repository that declares the toolchains from some other " +
488        "rust_toolchain_repository."
489    ),
490    attrs = {
491        "exec_compatible_with": attr.string_list(
492            doc = "A list of constraints for the execution platform for this toolchain.",
493        ),
494        "target_compatible_with": attr.string_list(
495            doc = "A list of constraints for the target platform for this toolchain.",
496        ),
497        "target_settings": attr.string_list(
498            doc = "A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution.",
499        ),
500        "toolchain": attr.string(
501            doc = "The name of the toolchain implementation target.",
502            mandatory = True,
503        ),
504        "toolchain_type": attr.string(
505            doc = "The toolchain type of the toolchain to declare",
506            mandatory = True,
507        ),
508    },
509    implementation = _toolchain_repository_proxy_impl,
510)
511
512# For legacy support
513rust_toolchain_repository_proxy = toolchain_repository_proxy
514
515# N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints)
516# without actually downloading the toolchain.
517def rust_toolchain_repository(
518        name,
519        version,
520        exec_triple,
521        target_triple,
522        exec_compatible_with = None,
523        target_compatible_with = None,
524        target_settings = [],
525        channel = None,
526        allocator_library = None,
527        global_allocator_library = None,
528        rustfmt_version = None,
529        edition = None,
530        dev_components = False,
531        extra_rustc_flags = None,
532        extra_exec_rustc_flags = None,
533        opt_level = None,
534        sha256s = None,
535        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
536        auth = None,
537        netrc = None,
538        auth_patterns = None):
539    """Assembles a remote repository for the given toolchain params, produces a proxy repository \
540    to contain the toolchain declaration, and registers the toolchains.
541
542    Args:
543        name (str): The name of the generated repository
544        version (str): The version of the tool among "nightly", "beta", or an exact version.
545        exec_triple (str): The Rust-style target that this compiler runs on.
546        target_triple (str): The Rust-style target to build for.
547        channel (str, optional): The channel of the Rust toolchain.
548        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
549        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
550        target_settings (list, optional): A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution.
551        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary.
552        global_allocator_library (str, optional): Target that provides allocator functions when a global allocator is used with cc_common.link.
553        rustfmt_version (str, optional):  The version of rustfmt to be associated with the
554            toolchain.
555        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.
556        dev_components (bool, optional): Whether to download the rustc-dev components.
557            Requires version to be "nightly". Defaults to False.
558        extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration.
559        extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
560        opt_level (dict, optional): Optimization level config for this toolchain.
561        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
562            [rust_register_toolchains](#rust_register_toolchains) for more details.
563        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.xz']
564        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
565            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
566        netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive
567        auth_patterns (list, optional): A list of patterns to match against urls for which the auth object should be used.
568
569    Returns:
570        str: The name of the registerable toolchain created by this rule.
571    """
572    if exec_compatible_with == None:
573        exec_compatible_with = triple_to_constraint_set(exec_triple)
574
575    if target_compatible_with == None:
576        target_compatible_with = triple_to_constraint_set(target_triple)
577
578    tools_repo_name = "{}_tools".format(name)
579
580    rust_toolchain_tools_repository(
581        name = tools_repo_name,
582        exec_triple = exec_triple,
583        allocator_library = allocator_library,
584        global_allocator_library = global_allocator_library,
585        target_triple = target_triple,
586        version = version,
587        rustfmt_version = rustfmt_version,
588        edition = edition,
589        dev_components = dev_components,
590        extra_rustc_flags = extra_rustc_flags,
591        extra_exec_rustc_flags = extra_exec_rustc_flags,
592        opt_level = opt_level,
593        sha256s = sha256s,
594        urls = urls,
595        auth = auth,
596        netrc = netrc,
597        auth_patterns = auth_patterns,
598    )
599
600    channel_target_settings = ["@rules_rust//rust/toolchain/channel:{}".format(channel)] if channel else []
601
602    toolchain_repository_proxy(
603        name = name,
604        toolchain = "@{}//:rust_toolchain".format(tools_repo_name),
605        target_settings = channel_target_settings + target_settings,
606        toolchain_type = "@rules_rust//rust:toolchain",
607        exec_compatible_with = exec_compatible_with,
608        target_compatible_with = target_compatible_with,
609    )
610
611    return "@{name}//:toolchain".format(
612        name = name,
613    )
614
615_RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS = {
616    "auth": attr.string_dict(
617        doc = (
618            "Auth object compatible with repository_ctx.download to use when downloading files. " +
619            "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
620        ),
621    ),
622    "auth_patterns": attr.string_list(
623        doc = "A list of patterns to match against urls for which the auth object should be used.",
624    ),
625    "netrc": attr.string(
626        doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive",
627    ),
628    "sha256s": attr.string_dict(
629        doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details.",
630    ),
631    "urls": attr.string_list(
632        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).",
633        default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
634    ),
635    "version": attr.string(
636        doc = "The version of the tool among \"nightly\", \"beta\" with an iso date, or an exact version.",
637        mandatory = True,
638    ),
639}
640
641def _rust_analyzer_toolchain_tools_repository_impl(repository_ctx):
642    sha256s = dict(repository_ctx.attr.sha256s)
643
644    iso_date = None
645    version = repository_ctx.attr.version
646    version_array = version.split("/")
647    if len(version_array) > 1:
648        version = version_array[0]
649        iso_date = version_array[1]
650
651    sha256s.update(load_rust_src(
652        ctx = repository_ctx,
653        iso_date = iso_date,
654        version = version,
655    ))
656
657    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
658        repository_ctx.name,
659    ))
660
661    host_triple = get_host_triple(repository_ctx)
662    rustc_content, rustc_sha256 = load_rust_compiler(
663        ctx = repository_ctx,
664        iso_date = iso_date,
665        target_triple = host_triple,
666        version = version,
667    )
668    build_contents = [rustc_content]
669    sha256s.update(rustc_sha256)
670    rustc = "//:rustc"
671
672    proc_macro_srv = None
673    if includes_rust_analyzer_proc_macro_srv(version, iso_date):
674        build_contents.append(BUILD_for_rust_analyzer_proc_macro_srv(host_triple))
675        proc_macro_srv = "//:rust_analyzer_proc_macro_srv"
676
677    build_contents.append(BUILD_for_rust_analyzer_toolchain(
678        name = "rust_analyzer_toolchain",
679        rustc = rustc,
680        proc_macro_srv = proc_macro_srv,
681    ))
682
683    repository_ctx.file("BUILD.bazel", "\n".join(build_contents))
684    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
685        repository_ctx.name,
686    ))
687
688    repro = {"name": repository_ctx.name}
689    for key in _RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS:
690        repro[key] = getattr(repository_ctx.attr, key)
691    repro["sha256s"] = sha256s
692
693    return repro
694
695rust_analyzer_toolchain_tools_repository = repository_rule(
696    doc = "A repository rule for defining a rust_analyzer_toolchain with a `rust-src` artifact.",
697    implementation = _rust_analyzer_toolchain_tools_repository_impl,
698    attrs = _RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS,
699)
700
701def rust_analyzer_toolchain_repository(
702        name,
703        version,
704        exec_compatible_with = [],
705        target_compatible_with = [],
706        sha256s = None,
707        urls = None,
708        auth = None,
709        netrc = None,
710        auth_patterns = None):
711    """Assemble a remote rust_analyzer_toolchain target based on the given params.
712
713    Args:
714        name (str): The name of the toolchain proxy repository contianing the registerable toolchain.
715        version (str): The version of the tool among "nightly", "beta', or an exact version.
716        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
717        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
718        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
719            [rust_register_toolchains](#rust_register_toolchains) for more details.
720        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.xz']
721        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
722            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
723        netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive
724        auth_patterns (dict, optional): Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive
725
726    Returns:
727        str: The name of a registerable rust_analyzer_toolchain.
728    """
729    rust_analyzer_toolchain_tools_repository(
730        name = name + "_tools",
731        version = version,
732        sha256s = sha256s,
733        urls = urls,
734        auth = auth,
735        netrc = netrc,
736        auth_patterns = auth_patterns,
737    )
738
739    toolchain_repository_proxy(
740        name = name,
741        toolchain = "@{}//:{}".format(name + "_tools", "rust_analyzer_toolchain"),
742        toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type",
743        exec_compatible_with = exec_compatible_with,
744        target_compatible_with = target_compatible_with,
745    )
746
747    return "@{}//:toolchain".format(
748        name,
749    )
750
751_RUSTFMT_TOOLCHAIN_TOOLS_ATTRS = {
752    "auth": attr.string_dict(
753        doc = (
754            "Auth object compatible with repository_ctx.download to use when downloading files. " +
755            "See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details."
756        ),
757    ),
758    "auth_patterns": attr.string_dict(
759        doc = "Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive",
760    ),
761    "exec_triple": attr.string(
762        doc = "The Rust-style triple Rustfmt is expected to run on.",
763        mandatory = True,
764    ),
765    "netrc": attr.string(
766        doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive",
767    ),
768    "sha256s": attr.string_dict(
769        doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details.",
770    ),
771    "urls": attr.string_list(
772        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).",
773        default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
774    ),
775    "version": attr.string(
776        doc = "The version of the tool among \"nightly\", \"beta\" with an iso date, or an exact version.",
777        mandatory = True,
778    ),
779}
780
781def _rustfmt_toolchain_tools_repository_impl(repository_ctx):
782    sha256s = dict(repository_ctx.attr.sha256s)
783    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
784        repository_ctx.name,
785    ))
786
787    iso_date = None
788    version = repository_ctx.attr.version
789    version_array = version.split("/")
790    if len(version_array) > 1:
791        version = version_array[0]
792        iso_date = version_array[1]
793
794    rustfmt = "//:rustfmt_bin"
795    rustc = "//:rustc"
796    rustc_lib = "//:rustc_lib"
797
798    exec_triple = triple(repository_ctx.attr.exec_triple)
799
800    rustc_content, rustc_sha256 = load_rust_compiler(
801        ctx = repository_ctx,
802        iso_date = iso_date,
803        target_triple = exec_triple,
804        version = version,
805    )
806    rustfmt_content, rustfmt_sha256 = load_rustfmt(
807        ctx = repository_ctx,
808        iso_date = iso_date,
809        target_triple = exec_triple,
810        version = version,
811    )
812
813    build_contents = [
814        rustc_content,
815        rustfmt_content,
816        BUILD_for_rustfmt_toolchain(
817            name = "rustfmt_toolchain",
818            rustfmt = rustfmt,
819            rustc = rustc,
820            rustc_lib = rustc_lib,
821        ),
822    ]
823    sha256s.update(rustc_sha256 | rustfmt_sha256)
824
825    repository_ctx.file("BUILD.bazel", "\n".join(build_contents))
826    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
827        repository_ctx.name,
828    ))
829
830    repro = {"name": repository_ctx.name}
831    for key in _RUSTFMT_TOOLCHAIN_TOOLS_ATTRS:
832        repro[key] = getattr(repository_ctx.attr, key)
833    repro["sha256s"] = sha256s
834
835    return repro
836
837rustfmt_toolchain_tools_repository = repository_rule(
838    doc = "A repository rule for defining a rustfmt_toolchain.",
839    attrs = _RUSTFMT_TOOLCHAIN_TOOLS_ATTRS,
840    implementation = _rustfmt_toolchain_tools_repository_impl,
841)
842
843def rustfmt_toolchain_repository(
844        name,
845        version,
846        exec_triple,
847        exec_compatible_with = None,
848        target_compatible_with = None,
849        channel = None,
850        sha256s = None,
851        urls = None,
852        auth = None,
853        netrc = None,
854        auth_patterns = None):
855    """Assemble a remote rustfmt_toolchain target based on the given params.
856
857    Args:
858        name (str): The name of the toolchain proxy repository contianing the registerable toolchain.
859        version (str): The version of the tool among "nightly", "beta', or an exact version.
860        exec_triple (str): The platform triple Rustfmt is expected to run on.
861        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
862        target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain.
863        channel (str, optional): The channel value to with which to constrain the toolchain.
864        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
865            [rust_register_toolchains](#rust_register_toolchains) for more details.
866        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.xz']
867        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
868            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
869        netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive
870        auth_patterns (dict, optional): Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive
871
872    Returns:
873        str: The name of a registerable rustfmt_toolchain.
874    """
875    if exec_compatible_with == None:
876        exec_compatible_with = triple_to_constraint_set(exec_triple)
877
878    rustfmt_toolchain_tools_repository(
879        name = name + "_tools",
880        version = version,
881        sha256s = sha256s,
882        urls = urls,
883        auth = auth,
884        netrc = netrc,
885        auth_patterns = auth_patterns,
886        exec_triple = exec_triple,
887    )
888
889    toolchain_repository_proxy(
890        name = name,
891        toolchain = "@{}//:{}".format(name + "_tools", "rustfmt_toolchain"),
892        toolchain_type = "@rules_rust//rust/rustfmt:toolchain_type",
893        target_settings = ["@rules_rust//rust/toolchain/channel:{}".format(channel)] if channel else None,
894        exec_compatible_with = exec_compatible_with,
895        target_compatible_with = target_compatible_with,
896    )
897
898    return "@{}//:toolchain".format(
899        name,
900    )
901
902def _rust_toolchain_set_repository_impl(repository_ctx):
903    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
904        repository_ctx.name,
905    ))
906
907    repository_ctx.file("BUILD.bazel", """exports_files(["defs.bzl"])""")
908    repository_ctx.file("defs.bzl", "ALL_TOOLCHAINS = {}\n".format(
909        json.encode_indent(repository_ctx.attr.toolchains, indent = " " * 4),
910    ))
911
912rust_toolchain_set_repository = repository_rule(
913    doc = (
914        "Generates a toolchain-bearing repository that declares the toolchains from some other " +
915        "rust_toolchain_repository."
916    ),
917    attrs = {
918        "toolchains": attr.string_list(
919            doc = "The list of all toolchains created by the current `rust_toolchain_set`",
920            mandatory = True,
921        ),
922    },
923    implementation = _rust_toolchain_set_repository_impl,
924)
925
926def _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions):
927    toolchain_repos = []
928
929    for target_triple in depset([exec_triple] + extra_target_triples).to_list():
930        # Parse all provided versions while checking for duplicates
931        channels = {}
932        for version in versions:
933            if version.startswith(("beta", "nightly")):
934                channel, _, _ = version.partition("/")
935            else:
936                channel = "stable"
937
938            if channel in channels:
939                fail("Duplicate {} channels provided for {}: {}".format(channel, name, versions))
940
941            channels.update({channel: struct(
942                name = channel,
943                version = version,
944            )})
945
946        # Define toolchains for each requested version
947        for channel in channels.values():
948            toolchain_repos.append(struct(
949                name = "{}__{}__{}".format(name, target_triple, channel.name),
950                target_triple = target_triple,
951                channel = channel,
952            ))
953
954    return toolchain_repos
955
956def rust_repository_set(
957        name,
958        versions,
959        exec_triple,
960        target_settings = [],
961        allocator_library = None,
962        global_allocator_library = None,
963        extra_target_triples = {},
964        rustfmt_version = None,
965        edition = None,
966        dev_components = False,
967        extra_rustc_flags = None,
968        extra_exec_rustc_flags = None,
969        opt_level = None,
970        sha256s = None,
971        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
972        auth = None,
973        netrc = None,
974        auth_patterns = None,
975        register_toolchain = True,
976        exec_compatible_with = None,
977        default_target_compatible_with = None):
978    """Assembles a remote repository for the given toolchain params, produces a proxy repository \
979    to contain the toolchain declaration, and registers the toolchains.
980
981    Args:
982        name (str): The name of the generated repository
983        versions (list, optional): A list of toolchain versions to download. This paramter only accepts one versions
984            per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`.
985        exec_triple (str): The Rust-style target that this compiler runs on
986        target_settings (list, optional): A list of config_settings that must be satisfied by the target configuration in order for this set of toolchains to be selected during toolchain resolution.
987        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are
988            embedded in a cc_binary.
989        global_allocator_library (str, optional): Target that provides allocator functions a global allocator is used with cc_common.link.
990        extra_target_triples (list or map, optional): Additional rust-style targets that this set of
991            toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple.
992        rustfmt_version (str, optional):  The version of rustfmt to be associated with the
993            toolchain.
994        edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is
995            required to specify its `edition` attribute.
996        dev_components (bool, optional): Whether to download the rustc-dev components.
997            Requires version to be "nightly".
998        extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
999        extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
1000        opt_level (dict, dict, optional): Dictionary of target triples to optimiztion config.
1001        sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
1002            [rust_register_toolchains](#rust_register_toolchains) for more details.
1003        urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These
1004            must contain the '{}' used to substitute the tool being fetched (using .format).
1005        auth (dict): Auth object compatible with repository_ctx.download to use when downloading files.
1006            See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
1007        netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive
1008        auth_patterns (dict, optional): Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive
1009
1010        register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain.
1011        exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
1012        default_target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform.
1013    """
1014
1015    # extra_target_triples may be a dict or list - make a list we can pass to _get_toolchain_repositories
1016    extra_target_triples_list = []
1017    for extra_target_triple in extra_target_triples:
1018        extra_target_triples_list.append(extra_target_triple)
1019
1020    all_toolchain_names = []
1021    for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples_list, versions):
1022        target_compatible_with = None
1023        if toolchain.target_triple == exec_triple:
1024            # The exec triple implicitly gets a toolchain with itself as a target - use default_target_compatible_with for it
1025            target_compatible_with = default_target_compatible_with
1026        elif type(extra_target_triples) == "dict":
1027            target_compatible_with = extra_target_triples.get(toolchain.target_triple)
1028
1029        # Infer toolchain-specific rustc flags depending on the type (list, dict, optional) of extra_rustc_flags
1030        if extra_rustc_flags == None:
1031            toolchain_extra_rustc_flags = []
1032        elif type(extra_rustc_flags) == "list":
1033            toolchain_extra_rustc_flags = extra_rustc_flags
1034        elif type(extra_rustc_flags) == "dict":
1035            toolchain_extra_rustc_flags = extra_rustc_flags.get(toolchain.target_triple)
1036        else:
1037            fail("extra_rustc_flags should be a list or a dict")
1038
1039        all_toolchain_names.append(rust_toolchain_repository(
1040            name = toolchain.name,
1041            allocator_library = allocator_library,
1042            global_allocator_library = global_allocator_library,
1043            auth = auth,
1044            netrc = netrc,
1045            auth_patterns = auth_patterns,
1046            channel = toolchain.channel.name,
1047            dev_components = dev_components,
1048            edition = edition,
1049            exec_triple = exec_triple,
1050            extra_exec_rustc_flags = extra_exec_rustc_flags,
1051            extra_rustc_flags = toolchain_extra_rustc_flags,
1052            opt_level = opt_level.get(toolchain.target_triple) if opt_level != None else None,
1053            target_settings = target_settings,
1054            rustfmt_version = rustfmt_version,
1055            sha256s = sha256s,
1056            target_triple = toolchain.target_triple,
1057            urls = urls,
1058            version = toolchain.channel.version,
1059            exec_compatible_with = exec_compatible_with,
1060            target_compatible_with = target_compatible_with,
1061        ))
1062
1063    # This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper.
1064    rust_toolchain_set_repository(
1065        name = name,
1066        toolchains = all_toolchain_names,
1067    )
1068
1069    # Register toolchains
1070    if register_toolchain:
1071        native.register_toolchains(*all_toolchain_names)
1072        native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))