diff --git a/rust/extensions.bzl b/rust/extensions.bzl index 749740032e..54ae6d5fd0 100644 --- a/rust/extensions.bzl +++ b/rust/extensions.bzl @@ -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: @@ -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. @@ -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 @@ -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, @@ -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, diff --git a/rust/repositories.bzl b/rust/repositories.bzl index d3609c2d07..91656782c2 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -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, @@ -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 @@ -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, @@ -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") @@ -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, @@ -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 @@ -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(