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
5 changes: 5 additions & 0 deletions rust/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def _rust_impl(module_ctx):
rustfmt_toolchain_triples = toolchain.rustfmt_toolchain_triples if toolchain.rustfmt_toolchain_triples else DEFAULT_TOOLCHAIN_TRIPLES,
target_settings = [str(v) for v in toolchain.target_settings],
extra_toolchain_infos = extra_toolchain_infos,
use_default_shell_env = use_default_shell_env,
)
metadata_kwargs = {}
if bazel_features.external_deps.extension_metadata_has_reproducible:
Expand Down Expand Up @@ -284,6 +285,10 @@ _RUST_TOOLCHAIN_TAG = tag_class(
),
default = _RUST_TOOLCHAIN_VERSIONS,
),
"use_default_shell_env": attr.bool(
doc = "Controls whether rustc actions inherit the shell environment",
default = False,
),
} | _COMMON_TAG_KWARGS,
)

Expand Down
20 changes: 17 additions & 3 deletions rust/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def rust_register_toolchains(
toolchain_triples = DEFAULT_TOOLCHAIN_TRIPLES,
rustfmt_toolchain_triples = DEFAULT_TOOLCHAIN_TRIPLES,
target_settings = [],
extra_toolchain_infos = None):
extra_toolchain_infos = None,
use_default_shell_env = False):
"""Emits a default set of toolchains for Linux, MacOS, and Freebsd

Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \
Expand Down Expand Up @@ -112,6 +113,7 @@ def rust_register_toolchains(
rustfmt_toolchain_triples (dict[str, str], optional): Like toolchain_triples, but for rustfmt toolchains.
target_settings (list of labels as strings, optional): A list of `config_settings` that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution.
extra_toolchain_infos: (dict[str, dict], optional): Mapping of information about extra toolchains which were created outside of this call, which should be added to the hub repo.
use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False).
"""
if not rustfmt_version:
if len(versions) == 1:
Expand Down Expand Up @@ -185,6 +187,7 @@ def rust_register_toolchains(
urls = urls,
versions = versions,
aliases = dict(aliases),
use_default_shell_env = use_default_shell_env,
)

for toolchain in _get_toolchain_repositories(
Expand Down Expand Up @@ -318,6 +321,10 @@ _RUST_TOOLCHAIN_REPOSITORY_ATTRS = {
doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
mandatory = True,
),
"use_default_shell_env": attr.bool(
doc = "Controls whether rustc actions inherit the shell environment",
default = False,
),
}

def _include_llvm_tools(version, iso_date):
Expand Down Expand Up @@ -480,6 +487,7 @@ def _rust_toolchain_tools_repository_impl(ctx):
version = toolchain_version,
channel = channel,
iso_date = iso_date,
use_default_shell_env = ctx.attr.use_default_shell_env,
))

# Not all target triples are expected to have dev components
Expand Down Expand Up @@ -593,7 +601,8 @@ def rust_toolchain_repository(
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
auth = None,
netrc = None,
auth_patterns = None):
auth_patterns = None,
use_default_shell_env = None):
"""Assembles a remote repository for the given toolchain params, produces a proxy repository \
to contain the toolchain declaration, and registers the toolchains.

Expand Down Expand Up @@ -628,6 +637,7 @@ def rust_toolchain_repository(
See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details.
netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive
auth_patterns (list, optional): A list of patterns to match against urls for which the auth object should be used.
use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False).

Returns:
dict[str, str]: Information about the registerable toolchain created by this rule.
Expand Down Expand Up @@ -659,6 +669,7 @@ def rust_toolchain_repository(
auth = auth,
netrc = netrc,
auth_patterns = auth_patterns,
use_default_shell_env = use_default_shell_env,
)

channel_target_settings = ["@rules_rust//rust/toolchain/channel:{}".format(channel)] if channel else []
Expand Down Expand Up @@ -995,7 +1006,8 @@ def rust_repository_set(
exec_compatible_with = None,
default_target_compatible_with = None,
aliases = {},
compact_windows_names = _COMPACT_WINDOWS_NAMES):
compact_windows_names = _COMPACT_WINDOWS_NAMES,
use_default_shell_env = False):
"""Assembles a remote repository for the given toolchain params, produces a proxy repository \
to contain the toolchain declaration.

Expand Down Expand Up @@ -1033,6 +1045,7 @@ def rust_repository_set(
aliases (dict): Replacement names to use for toolchains created by this macro.
compact_windows_names (bool): Whether or not to produce compact repository names for windows
toolchains. This is to avoid MAX_PATH issues.
use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False).

Returns:
dict[str, dict]: A dict of information about all generated toolchains.
Expand Down Expand Up @@ -1082,6 +1095,7 @@ def rust_repository_set(
auth = auth,
netrc = netrc,
auth_patterns = auth_patterns,
use_default_shell_env = use_default_shell_env,
)

channel_target_settings = ["@rules_rust//rust/toolchain/channel:{}".format(toolchain.channel.name)]
Expand Down
6 changes: 5 additions & 1 deletion rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ rust_toolchain(
channel = "{channel}",
iso_date = {iso_date},
tags = ["rust_version={version}"],
use_default_shell_env = {use_default_shell_env},
)
"""

Expand All @@ -393,7 +394,8 @@ def BUILD_for_rust_toolchain(
extra_rustc_flags = None,
extra_exec_rustc_flags = None,
opt_level = None,
strip_level = None):
strip_level = None,
use_default_shell_env = False):
"""Emits a toolchain declaration to match an existing compiler and stdlib.

Args:
Expand All @@ -419,6 +421,7 @@ def BUILD_for_rust_toolchain(
extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
opt_level (dict, optional): Optimization level config for this toolchain.
strip_level (dict, optional): Strip level config for this toolchain.
use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False).

Returns:
str: A rendered template of a `rust_toolchain` declaration
Expand Down Expand Up @@ -484,6 +487,7 @@ def BUILD_for_rust_toolchain(
version = version,
channel = channel,
iso_date = repr(iso_date),
use_default_shell_env = use_default_shell_env,
)

_build_file_for_toolchain_template = """\
Expand Down
3 changes: 3 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ def rustc_compile_action(
),
toolchain = "@rules_rust//rust:toolchain_type",
resource_set = get_rustc_resource_set(toolchain),
use_default_shell_env = toolchain.use_default_shell_env,
)
if args_metadata:
ctx.actions.run(
Expand All @@ -1613,6 +1614,7 @@ def rustc_compile_action(
"" if len(srcs) == 1 else "s",
),
toolchain = "@rules_rust//rust:toolchain_type",
use_default_shell_env = toolchain.use_default_shell_env,
)
elif hasattr(ctx.executable, "_bootstrap_process_wrapper"):
# Run without process_wrapper
Expand All @@ -1634,6 +1636,7 @@ def rustc_compile_action(
),
toolchain = "@rules_rust//rust:toolchain_type",
resource_set = get_rustc_resource_set(toolchain),
use_default_shell_env = toolchain.use_default_shell_env,
)
else:
fail("No process wrapper was defined for {}".format(ctx.label))
Expand Down
5 changes: 5 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ def _rust_toolchain_impl(ctx):
target_triple = target_triple,
version = ctx.attr.version,
require_explicit_unstable_features = _require_explicit_unstable_features(ctx),
use_default_shell_env = ctx.attr.use_default_shell_env,

# Experimental and incompatible flags
_rename_first_party_crates = rename_first_party_crates,
Expand Down Expand Up @@ -875,6 +876,10 @@ rust_toolchain = rule(
doc = "The version of the Rust compiler (e.g. `1.94.1`).",
default = "",
),
"use_default_shell_env": attr.bool(
doc = "Controls whether rustc actions inherit the shell environment",
default = False,
),
"_codegen_units": attr.label(
default = Label("//rust/settings:codegen_units"),
),
Expand Down