fork/rules_rust

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

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

patching
5.7 KiB168 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 _rust_impl(module_ctx):
35    # Toolchain configuration is only allowed in the root module, or in
36    # rules_rust.
37    # See https://github.com/bazelbuild/bazel/discussions/22024 for discussion.
38    root, rules_rust = _find_modules(module_ctx)
39
40    toolchains = root.tags.toolchain or rules_rust.tags.toolchain
41
42    for toolchain in toolchains:
43        rust_register_toolchains(
44            dev_components = toolchain.dev_components,
45            edition = toolchain.edition,
46            allocator_library = toolchain.allocator_library,
47            rustfmt_version = toolchain.rustfmt_version,
48            rust_analyzer_version = toolchain.rust_analyzer_version,
49            sha256s = toolchain.sha256s,
50            extra_target_triples = toolchain.extra_target_triples,
51            urls = toolchain.urls,
52            versions = toolchain.versions,
53            register_toolchains = False,
54        )
55
56_COMMON_TAG_KWARGS = dict(
57    allocator_library = attr.string(
58        doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
59        default = "@rules_rust//ffi/cc/allocator_library",
60    ),
61    dev_components = attr.bool(
62        doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
63        default = False,
64    ),
65    edition = attr.string(
66        doc = (
67            "The rust edition to be used by default (2015, 2018, or 2021). " +
68            "If absent, every rule is required to specify its `edition` attribute."
69        ),
70    ),
71    rustfmt_version = attr.string(
72        doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
73        default = DEFAULT_NIGHTLY_VERSION,
74    ),
75    sha256s = attr.string_dict(
76        doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
77    ),
78    urls = attr.string_list(
79        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).",
80        default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
81    ),
82)
83
84_RUST_TOOLCHAIN_TAG = tag_class(
85    attrs = dict(
86        extra_target_triples = attr.string_list(
87            default = DEFAULT_EXTRA_TARGET_TRIPLES,
88        ),
89        rust_analyzer_version = attr.string(
90            doc = "The version of Rustc to pair with rust-analyzer.",
91        ),
92        versions = attr.string_list(
93            doc = (
94                "A list of toolchain versions to download. This paramter only accepts one versions " +
95                "per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`."
96            ),
97            default = _RUST_TOOLCHAIN_VERSIONS,
98        ),
99        **_COMMON_TAG_KWARGS
100    ),
101)
102
103_RUST_HOST_TOOLS_TAG = tag_class(
104    attrs = dict(
105        version = attr.string(
106            default = rust_common.default_version,
107            doc = "The version of Rust to use for tools executed on the Bazel host.",
108        ),
109        **_COMMON_TAG_KWARGS
110    ),
111)
112
113rust = module_extension(
114    implementation = _rust_impl,
115    tag_classes = {
116        "toolchain": _RUST_TOOLCHAIN_TAG,
117    },
118)
119
120# This is a separate module extension so that only the host tools are
121# marked as reproducible and os and arch dependent
122def _rust_host_tools_impl(module_ctx):
123    root, _ = _find_modules(module_ctx)
124
125    if len(root.tags.host_tools) == 1:
126        attrs = root.tags.host_tools[0]
127
128        host_tools = {
129            "allocator_library": attrs.allocator_library,
130            "dev_components": attrs.dev_components,
131            "edition": attrs.edition,
132            "rustfmt_version": attrs.rustfmt_version,
133            "sha256s": attrs.sha256s,
134            "urls": attrs.urls,
135            "version": attrs.version,
136        }
137    elif not root.tags.host_tools:
138        host_tools = {
139            "version": rust_common.default_version,
140        }
141    else:
142        fail("Multiple host_tools were defined in your root MODULE.bazel")
143
144    host_triple = get_host_triple(module_ctx)
145    rust_toolchain_tools_repository(
146        name = "rust_host_tools",
147        exec_triple = host_triple.str,
148        target_triple = host_triple.str,
149        **host_tools
150    )
151
152    metadata_kwargs = {}
153    if bazel_features.external_deps.extension_metadata_has_reproducible:
154        metadata_kwargs["reproducible"] = True
155    return module_ctx.extension_metadata(**metadata_kwargs)
156
157_conditional_rust_host_tools_args = {
158    "arch_dependent": True,
159    "os_dependent": True,
160} if bazel_features.external_deps.module_extension_has_os_arch_dependent else {}
161
162rust_host_tools = module_extension(
163    implementation = _rust_host_tools_impl,
164    tag_classes = {
165        "host_tools": _RUST_HOST_TOOLS_TAG,
166    },
167    **_conditional_rust_host_tools_args
168)