Skip to content

Commit fabd0f9

Browse files
authored
Expose opt_level in rust extension (bazelbuild#4061)
1 parent 20dbf36 commit fabd0f9

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

rust/extensions.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def _rust_impl(module_ctx):
139139
rust_analyzer_version = toolchain.rust_analyzer_version,
140140
sha256s = toolchain.sha256s,
141141
extra_target_triples = toolchain.extra_target_triples,
142+
opt_level = toolchain.opt_level if toolchain.opt_level else None,
142143
strip_level = toolchain.strip_level if toolchain.strip_level else None,
143144
urls = toolchain.urls,
144145
versions = toolchain.versions,
@@ -270,6 +271,9 @@ _RUST_TOOLCHAIN_TAG = tag_class(
270271
"extra_target_triples": attr.string_list(
271272
default = DEFAULT_EXTRA_TARGET_TRIPLES,
272273
),
274+
"opt_level": attr.string_dict(
275+
doc = "Rustc optimization levels. For more details see the documentation for `rust_toolchain.opt_level`.",
276+
),
273277
"rust_analyzer_version": attr.string(
274278
doc = "The version of Rustc to pair with rust-analyzer.",
275279
),

rust/private/repositories.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def rust_register_toolchains(
6363
extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES,
6464
extra_rustc_flags = None,
6565
extra_exec_rustc_flags = None,
66+
opt_level = None,
6667
strip_level = None,
6768
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
6869
versions = _RUST_TOOLCHAIN_VERSIONS,
@@ -97,9 +98,11 @@ def rust_register_toolchains(
9798
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.
9899
rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer.
99100
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes.
100-
extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support.
101+
extra_target_triples (list or map, optional): Additional rust-style targets that this set of
102+
toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple.
101103
extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
102104
extra_exec_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in exec configuration.
105+
opt_level (dict, optional): Rustc optimization levels. For more details see the documentation for `rust_toolchain.opt_level`.
103106
strip_level (dict, dict, optional): Dictionary of target triples to strip config.
104107
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).
105108
versions (list, optional): A list of toolchain versions to download. This parameter only accepts one versions
@@ -168,6 +171,9 @@ def rust_register_toolchains(
168171

169172
# Rust toolchains per exec triple
170173
for exec_triple, name in toolchain_triples.items():
174+
extra = extra_target_triples.keys() if type(extra_target_triples) == "dict" else extra_target_triples
175+
opt_level_by_triple = {t: opt_level for t in depset([exec_triple] + list(extra)).to_list()} if opt_level else None
176+
171177
maybe(
172178
rust_repository_set,
173179
name = name,
@@ -180,6 +186,7 @@ def rust_register_toolchains(
180186
rustfmt_version = rustfmt_version,
181187
extra_rustc_flags = extra_rustc_flags,
182188
extra_exec_rustc_flags = extra_exec_rustc_flags,
189+
opt_level = opt_level_by_triple,
183190
strip_level = strip_level,
184191
sha256s = sha256s,
185192
urls = urls,

0 commit comments

Comments
 (0)