fork/rules_rust

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

UebelAndreAdded bzlmod support to core rules_rust (#3034)69ddb09d

main
8.2 KiB217 linesraw
1"""Module extensions for using rules_rust with bzlmod"""
2
3load("@bazel_features//:features.bzl", "bazel_features")
4load("//rust:defs.bzl", "rust_common")
5load("//rust:repositories.bzl", "rust_register_toolchains", "rust_toolchain_tools_repository")
6load("//rust/platform:triple.bzl", "get_host_triple")
7load(
8    "//rust/private:repository_utils.bzl",
9    "DEFAULT_EXTRA_TARGET_TRIPLES",
10    "DEFAULT_NIGHTLY_VERSION",
11    "DEFAULT_STATIC_RUST_URL_TEMPLATES",
12)
13
14_RUST_TOOLCHAIN_VERSIONS = [
15    rust_common.default_version,
16    DEFAULT_NIGHTLY_VERSION,
17]
18
19def _find_modules(module_ctx):
20    root = None
21    our_module = None
22    for mod in module_ctx.modules:
23        if mod.is_root:
24            root = mod
25        if mod.name == "rules_rust":
26            our_module = mod
27    if root == None:
28        root = our_module
29    if our_module == None:
30        fail("Unable to find rules_rust module")
31
32    return root, our_module
33
34def _empty_repository_impl(repository_ctx):
35    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
36        repository_ctx.name,
37    ))
38    repository_ctx.file("BUILD.bazel", "")
39
40_empty_repository = repository_rule(
41    doc = "Declare an empty repository.",
42    implementation = _empty_repository_impl,
43)
44
45def _rust_impl(module_ctx):
46    # Toolchain configuration is only allowed in the root module, or in
47    # rules_rust.
48    # See https://github.com/bazelbuild/bazel/discussions/22024 for discussion.
49    root, rules_rust = _find_modules(module_ctx)
50
51    toolchains = root.tags.toolchain or rules_rust.tags.toolchain
52
53    for toolchain in toolchains:
54        if toolchain.extra_rustc_flags and toolchain.extra_rustc_flags_triples:
55            fail("Cannot define both extra_rustc_flags and extra_rustc_flags_triples")
56        if len(toolchain.versions) == 0:
57            # If the root module has asked for rules_rust to not register default
58            # toolchains, an empty repository named `rust_toolchains` is created
59            # so that the `register_toolchains()` in MODULES.bazel is still
60            # valid.
61            _empty_repository(name = "rust_toolchains")
62        else:
63            extra_rustc_flags = toolchain.extra_rustc_flags if toolchain.extra_rustc_flags else toolchain.extra_rustc_flags_triples
64
65            rust_register_toolchains(
66                hub_name = "rust_toolchains",
67                dev_components = toolchain.dev_components,
68                edition = toolchain.edition,
69                extra_rustc_flags = extra_rustc_flags,
70                extra_exec_rustc_flags = toolchain.extra_exec_rustc_flags,
71                allocator_library = toolchain.allocator_library,
72                rustfmt_version = toolchain.rustfmt_version,
73                rust_analyzer_version = toolchain.rust_analyzer_version,
74                sha256s = toolchain.sha256s,
75                extra_target_triples = toolchain.extra_target_triples,
76                urls = toolchain.urls,
77                versions = toolchain.versions,
78                register_toolchains = False,
79                aliases = toolchain.aliases,
80            )
81    metadata_kwargs = {}
82    if bazel_features.external_deps.extension_metadata_has_reproducible:
83        metadata_kwargs["reproducible"] = True
84    return module_ctx.extension_metadata(**metadata_kwargs)
85
86_COMMON_TAG_KWARGS = {
87    "allocator_library": attr.string(
88        doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
89        default = "@rules_rust//ffi/cc/allocator_library",
90    ),
91    "dev_components": attr.bool(
92        doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
93        default = False,
94    ),
95    "edition": attr.string(
96        doc = (
97            "The rust edition to be used by default (2015, 2018, or 2021). " +
98            "If absent, every rule is required to specify its `edition` attribute."
99        ),
100    ),
101    "rustfmt_version": attr.string(
102        doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
103        default = DEFAULT_NIGHTLY_VERSION,
104    ),
105    "sha256s": attr.string_dict(
106        doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
107    ),
108    "urls": attr.string_list(
109        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).",
110        default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
111    ),
112}
113
114_RUST_TOOLCHAIN_TAG = tag_class(
115    attrs = {
116        "aliases": attr.string_dict(
117            doc = (
118                "Map of full toolchain repository name to an alias. If any repository is created by this " +
119                "extension matches a key in this dictionary, the name of the created repository will be " +
120                "remapped to the value instead. This may be required to work around path length limits " +
121                "on Windows."
122            ),
123            default = {},
124        ),
125        "extra_exec_rustc_flags": attr.string_list(
126            doc = "Extra flags to pass to rustc in exec configuration",
127        ),
128        "extra_rustc_flags": attr.string_list(
129            doc = "Extra flags to pass to rustc in non-exec configuration",
130        ),
131        "extra_rustc_flags_triples": attr.string_list_dict(
132            doc = "Extra flags to pass to rustc in non-exec configuration. Key is the triple, value is the flag.",
133        ),
134        "extra_target_triples": attr.string_list(
135            default = DEFAULT_EXTRA_TARGET_TRIPLES,
136        ),
137        "rust_analyzer_version": attr.string(
138            doc = "The version of Rustc to pair with rust-analyzer.",
139        ),
140        "versions": attr.string_list(
141            doc = (
142                "A list of toolchain versions to download. This parameter only accepts one version " +
143                "per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`. " +
144                "May be set to an empty list (`[]`) to inhibit `rules_rust` from registering toolchains."
145            ),
146            default = _RUST_TOOLCHAIN_VERSIONS,
147        ),
148    } | _COMMON_TAG_KWARGS,
149)
150
151_RUST_HOST_TOOLS_TAG = tag_class(
152    attrs = {
153        "version": attr.string(
154            default = rust_common.default_version,
155            doc = "The version of Rust to use for tools executed on the Bazel host.",
156        ),
157    } | _COMMON_TAG_KWARGS,
158)
159
160rust = module_extension(
161    doc = "Rust toolchain extension.",
162    implementation = _rust_impl,
163    tag_classes = {
164        "toolchain": _RUST_TOOLCHAIN_TAG,
165    },
166)
167
168# This is a separate module extension so that only the host tools are
169# marked as reproducible and os and arch dependent
170def _rust_host_tools_impl(module_ctx):
171    root, _ = _find_modules(module_ctx)
172
173    if len(root.tags.host_tools) == 1:
174        attrs = root.tags.host_tools[0]
175
176        host_tools = {
177            "allocator_library": attrs.allocator_library,
178            "dev_components": attrs.dev_components,
179            "edition": attrs.edition,
180            "rustfmt_version": attrs.rustfmt_version,
181            "sha256s": attrs.sha256s,
182            "urls": attrs.urls,
183            "version": attrs.version,
184        }
185    elif not root.tags.host_tools:
186        host_tools = {
187            "version": rust_common.default_version,
188        }
189    else:
190        fail("Multiple host_tools were defined in your root MODULE.bazel")
191
192    host_triple = get_host_triple(module_ctx)
193    rust_toolchain_tools_repository(
194        name = "rust_host_tools",
195        exec_triple = host_triple.str,
196        target_triple = host_triple.str,
197        **host_tools
198    )
199
200    metadata_kwargs = {}
201    if bazel_features.external_deps.extension_metadata_has_reproducible:
202        metadata_kwargs["reproducible"] = True
203    return module_ctx.extension_metadata(**metadata_kwargs)
204
205_conditional_rust_host_tools_args = {
206    "arch_dependent": True,
207    "os_dependent": True,
208} if bazel_features.external_deps.module_extension_has_os_arch_dependent else {}
209
210rust_host_tools = module_extension(
211    doc = "An extension which exposes Rust tools compatible with the current host platform.",
212    implementation = _rust_host_tools_impl,
213    tag_classes = {
214        "host_tools": _RUST_HOST_TOOLS_TAG,
215    },
216    **_conditional_rust_host_tools_args
217)