Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions rust/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _RUST_TOOLCHAIN_VERSIONS = [
]

def _find_modules(module_ctx):
# type: (module_ctx) -> tuple[bazel_module, bazel_module]
root = None
our_module = None
for mod in module_ctx.modules:
Expand All @@ -43,6 +44,7 @@ _empty_repository = repository_rule(
)

def _rust_impl(module_ctx):
# type: (module_ctx) -> extension_metadata
# Toolchain configuration is only allowed in the root module, or in
# rules_rust.
# See https://github.com/bazelbuild/bazel/discussions/22024 for discussion.
Expand Down Expand Up @@ -98,6 +100,10 @@ def _rust_impl(module_ctx):
for toolchain in toolchains:
if toolchain.extra_rustc_flags and toolchain.extra_rustc_flags_triples:
fail("Cannot define both extra_rustc_flags and extra_rustc_flags_triples")

if toolchain.extra_exec_rustc_flags and toolchain.extra_exec_rustc_flags_triples:
fail("Cannot define both extra_exec_rustc_flags and extra_exec_rustc_flags_triples")

if len(toolchain.versions) == 0:
# If the root module has asked for rules_rust to not register default
# toolchains, an empty repository named `rust_toolchains` is created
Expand All @@ -106,13 +112,16 @@ def _rust_impl(module_ctx):
_empty_repository(name = "rust_toolchains")
else:
extra_rustc_flags = toolchain.extra_rustc_flags if toolchain.extra_rustc_flags else toolchain.extra_rustc_flags_triples
extra_exec_rustc_flags = toolchain.extra_exec_rustc_flags if toolchain.extra_exec_rustc_flags else toolchain.extra_exec_rustc_flags_triples

rust_register_toolchains(
hub_name = "rust_toolchains",
dev_components = toolchain.dev_components,
edition = toolchain.edition,
extra_rustc_flags = extra_rustc_flags,
extra_exec_rustc_flags = toolchain.extra_exec_rustc_flags,
extra_rustc_flags_exec = toolchain.extra_rustc_flags_exec_triples,
extra_exec_rustc_flags = extra_exec_rustc_flags,
extra_exec_rustc_flags_exec = toolchain.extra_exec_rustc_flags_exec_triples,
allocator_library = toolchain.allocator_library,
rustfmt_version = toolchain.rustfmt_version,
rust_analyzer_version = toolchain.rust_analyzer_version,
Expand Down Expand Up @@ -216,8 +225,17 @@ _RUST_TOOLCHAIN_TAG = tag_class(
"extra_rustc_flags": attr.string_list(
doc = "Extra flags to pass to rustc in non-exec configuration",
),
"extra_exec_rustc_flags_triples": attr.string_list_dict(
doc = "Extra flags to pass to rustc in exec configuration. Key is the target triple, value is the flag.",
),
"extra_exec_rustc_flags_exec_triples": attr.string_list_dict(
doc = "Extra flags to pass to rustc in exec configuration. Key is the exec triple, value is the flag.",
),
"extra_rustc_flags_triples": attr.string_list_dict(
doc = "Extra flags to pass to rustc in non-exec configuration. Key is the triple, value is the flag.",
doc = "Extra flags to pass to rustc in non-exec configuration. Key is the target triple, value is the flag.",
),
"extra_rustc_flags_exec_triples": attr.string_list_dict(
doc = "Extra flags to pass to rustc in non-exec configuration. Key is the exec triple, value is the flag.",
),
"extra_target_triples": attr.string_list(
default = DEFAULT_EXTRA_TARGET_TRIPLES,
Expand Down
26 changes: 23 additions & 3 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def rust_register_toolchains(
sha256s = None,
extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES,
extra_rustc_flags = None,
extra_rustc_flags_exec = None,
extra_exec_rustc_flags = None,
extra_exec_rustc_flags_exec = None,
strip_level = None,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
versions = _RUST_TOOLCHAIN_VERSIONS,
Expand Down Expand Up @@ -193,7 +195,9 @@ def rust_register_toolchains(
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes.
extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support.
extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
extra_rustc_flags_exec (dict, list, optional): Dictionary of exec triples to list of extra flags to pass to rustc in non-exec configuration.
extra_exec_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in exec configuration.
extra_exec_rustc_flags_exec (dict, list, optional): Dictionary of exec triples to list of extra flags to pass to rustc in exec configuration.
strip_level (dict, dict, optional): Dictionary of target triples to strip config.
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).
versions (list, optional): A list of toolchain versions to download. This parameter only accepts one versions
Expand Down Expand Up @@ -272,7 +276,9 @@ def rust_register_toolchains(
register_toolchain = register_toolchains,
rustfmt_version = rustfmt_version,
extra_rustc_flags = extra_rustc_flags,
extra_rustc_flags_exec = extra_rustc_flags_exec,
extra_exec_rustc_flags = extra_exec_rustc_flags,
extra_exec_rustc_flags_exec = extra_exec_rustc_flags_exec,
strip_level = strip_level,
sha256s = sha256s,
urls = urls,
Expand Down Expand Up @@ -1155,13 +1161,14 @@ def _get_toolchain_repositories(
return toolchain_repos.values()

def _get_flags_for_triple(name, flags, target_triple):
# type: (string, list[string] | dict[string, list[string]] | None, string) -> list[string]
"""Infer toolchain-specific flags depending on the type (list, dict, optional)."""
if flags == None:
return None
return []
elif type(flags) == "list":
return flags
elif type(flags) == "dict":
return flags.get(target_triple)
return flags.get(target_triple, [])
else:
fail(name + " should be a list or a dict")

Expand All @@ -1178,7 +1185,9 @@ def rust_repository_set(
edition = None,
dev_components = False,
extra_rustc_flags = None,
extra_rustc_flags_exec = None,
extra_exec_rustc_flags = None,
extra_exec_rustc_flags_exec = None,
opt_level = None,
strip_level = None,
sha256s = None,
Expand Down Expand Up @@ -1212,7 +1221,9 @@ def rust_repository_set(
dev_components (bool, optional): Whether to download the rustc-dev components.
Requires version to be "nightly".
extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.
extra_rustc_flags_exec (dict, list, optional): Dictionary of exec triples to list of extra flags to pass to rustc in non-exec configuration.
extra_exec_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in exec configuration.
extra_exec_rustc_flags_exec (dict, list, optional): Dictionary of exec triples to list of extra flags to pass to rustc in exec configuration.
opt_level (dict, dict, optional): Dictionary of target triples to optimization config.
strip_level (dict, dict, optional): Dictionary of target triples to strip config.
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
Expand Down Expand Up @@ -1250,11 +1261,20 @@ def rust_repository_set(
"extra_exec_rustc_flags",
extra_exec_rustc_flags,
toolchain.target_triple,
) + _get_flags_for_triple(
"extra_exec_rustc_flags_exec",
extra_exec_rustc_flags_exec,
exec_triple,
)

toolchain_extra_rustc_flags = _get_flags_for_triple(
"extra_rustc_flags",
extra_rustc_flags,
toolchain.target_triple,
) + _get_flags_for_triple(
"extra_rustc_flags_exec",
extra_rustc_flags_exec,
exec_triple,
)

toolchain_info = rust_toolchain_repository(
Expand Down