fork/rules_rust

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

Finnfeat: add extra_rustc_flags_triples (#2999)6a663880

cleanup
8.1 KiB216 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                dev_components = toolchain.dev_components,
67                edition = toolchain.edition,
68                extra_rustc_flags = extra_rustc_flags,
69                extra_exec_rustc_flags = toolchain.extra_exec_rustc_flags,
70                allocator_library = toolchain.allocator_library,
71                rustfmt_version = toolchain.rustfmt_version,
72                rust_analyzer_version = toolchain.rust_analyzer_version,
73                sha256s = toolchain.sha256s,
74                extra_target_triples = toolchain.extra_target_triples,
75                urls = toolchain.urls,
76                versions = toolchain.versions,
77                register_toolchains = False,
78                aliases = toolchain.aliases,
79            )
80    metadata_kwargs = {}
81    if bazel_features.external_deps.extension_metadata_has_reproducible:
82        metadata_kwargs["reproducible"] = True
83    return module_ctx.extension_metadata(**metadata_kwargs)
84
85_COMMON_TAG_KWARGS = dict(
86    allocator_library = attr.string(
87        doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
88        default = "@rules_rust//ffi/cc/allocator_library",
89    ),
90    dev_components = attr.bool(
91        doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
92        default = False,
93    ),
94    edition = attr.string(
95        doc = (
96            "The rust edition to be used by default (2015, 2018, or 2021). " +
97            "If absent, every rule is required to specify its `edition` attribute."
98        ),
99    ),
100    rustfmt_version = attr.string(
101        doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
102        default = DEFAULT_NIGHTLY_VERSION,
103    ),
104    sha256s = attr.string_dict(
105        doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
106    ),
107    urls = attr.string_list(
108        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).",
109        default = DEFAULT_STATIC_RUST_URL_TEMPLATES,
110    ),
111)
112
113_RUST_TOOLCHAIN_TAG = tag_class(
114    attrs = dict(
115        extra_target_triples = attr.string_list(
116            default = DEFAULT_EXTRA_TARGET_TRIPLES,
117        ),
118        extra_exec_rustc_flags = attr.string_list(
119            doc = "Extra flags to pass to rustc in exec configuration",
120        ),
121        extra_rustc_flags = attr.string_list(
122            doc = "Extra flags to pass to rustc in non-exec configuration",
123        ),
124        extra_rustc_flags_triples = attr.string_list_dict(
125            doc = "Extra flags to pass to rustc in non-exec configuration. Key is the triple, value is the flag.",
126        ),
127        rust_analyzer_version = attr.string(
128            doc = "The version of Rustc to pair with rust-analyzer.",
129        ),
130        versions = attr.string_list(
131            doc = (
132                "A list of toolchain versions to download. This parameter only accepts one version " +
133                "per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`. " +
134                "May be set to an empty list (`[]`) to inhibit `rules_rust` from registering toolchains."
135            ),
136            default = _RUST_TOOLCHAIN_VERSIONS,
137        ),
138        aliases = attr.string_dict(
139            doc = (
140                "Map of full toolchain repository name to an alias. If any repository is created by this " +
141                "extension matches a key in this dictionary, the name of the created repository will be " +
142                "remapped to the value instead. This may be required to work around path length limits " +
143                "on Windows."
144            ),
145            default = {},
146        ),
147        **_COMMON_TAG_KWARGS
148    ),
149)
150
151_RUST_HOST_TOOLS_TAG = tag_class(
152    attrs = dict(
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)
160
161rust = module_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    implementation = _rust_host_tools_impl,
212    tag_classes = {
213        "host_tools": _RUST_HOST_TOOLS_TAG,
214    },
215    **_conditional_rust_host_tools_args
216)