Skip to content

Commit

Permalink
Added support for compact windows repository names, avoiding MAX_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Nov 27, 2024
1 parent 43e5279 commit 5cfb55c
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ DEFAULT_TOOLCHAIN_TRIPLES = {
"x86_64-unknown-linux-gnu": "rust_linux_x86_64",
}

_COMPACT_WINDOWS_NAMES = True

def rules_rust_dependencies():
"""Dependencies used in the implementation of `rules_rust`."""

Expand Down Expand Up @@ -123,6 +125,7 @@ _RUST_TOOLCHAIN_VERSIONS = [

# buildifier: disable=unnamed-macro
def rust_register_toolchains(
*,
dev_components = False,
edition = None,
allocator_library = None,
Expand All @@ -136,7 +139,8 @@ def rust_register_toolchains(
extra_exec_rustc_flags = None,
urls = DEFAULT_STATIC_RUST_URL_TEMPLATES,
versions = _RUST_TOOLCHAIN_VERSIONS,
aliases = {}):
aliases = {},
compact_windows_names = _COMPACT_WINDOWS_NAMES):
"""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 @@ -171,6 +175,8 @@ def rust_register_toolchains(
versions (list, optional): A list of toolchain versions to download. This parameter only accepts one versions
per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`.
aliases (dict, optional): A mapping of "full" repository name to another name to use instead.
compact_windows_names (bool): Whether or not to produce compact repository names for windows
toolchains. This is to avoid MAX_PATH issues.
"""
if not rustfmt_version:
if len(versions) == 1:
Expand Down Expand Up @@ -261,7 +267,15 @@ def rust_register_toolchains(
rustfmt_repo_name,
))

for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions, fallback_target_compatible_with = None, aliases = aliases):
for toolchain in _get_toolchain_repositories(
name = name,
exec_triple = exec_triple,
extra_target_triples = extra_target_triples,
versions = versions,
fallback_target_compatible_with = None,
aliases = aliases,
compact_windows_names = compact_windows_names,
):
toolchain_names.append(toolchain.name)
toolchain_labels[toolchain.name] = "@{}//:{}".format(toolchain.name + "_tools", "rust_toolchain")
exec_compatible_with_by_toolchain[toolchain.name] = triple_to_constraint_set(exec_triple)
Expand Down Expand Up @@ -952,7 +966,35 @@ rust_toolchain_set_repository = repository_rule(
implementation = _rust_toolchain_set_repository_impl,
)

def _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions, fallback_target_compatible_with, aliases = {}):
def _get_toolchain_repositories(
*,
name,
exec_triple,
extra_target_triples,
versions,
fallback_target_compatible_with,
compact_windows_names,
aliases):
"""Collect structs represent toolchain repositories matching the given parameters.
Args:
name (str): The base name to use for toolchains.
exec_triple (triple): The execution triple associated with the toolchain.
extra_target_triples (list[triple]): Additional target triples to get toolchains for.
the `exec_triple` is a default `target_triple`.
versions (str): The version of rustc to use.
fallback_target_compatible_with (list): _description_
compact_windows_names (bool): Whether or not to produce compact repository names for windows
toolchains. This is to avoid MAX_PATH issues.
aliases (dict): Replacement names to use for toolchains created by this macro.
Returns:
list[struct]: A list of toolchain structs
- name: The name of the toolchain repository.
- target_triple: The target triple of the toolchain.
- channel: The toolchain channel (nightly/stable).
- target_constraints: Bazel constrants assicated with the toolchain.
"""
extra_target_triples_list = extra_target_triples.keys() if type(extra_target_triples) == "dict" else extra_target_triples

toolchain_repos = []
Expand Down Expand Up @@ -987,6 +1029,9 @@ def _get_toolchain_repositories(name, exec_triple, extra_target_triples, version
full_name = "{}__{}__{}".format(name, target_triple, channel.name)
if full_name in aliases:
full_name = aliases.pop(full_name)
elif compact_windows_names and "windows" in exec_triple:
full_name = "rw-{}".format(abs(hash(full_name)))

toolchain_repos.append(struct(
name = full_name,
target_triple = target_triple,
Expand All @@ -997,6 +1042,7 @@ def _get_toolchain_repositories(name, exec_triple, extra_target_triples, version
return toolchain_repos

def rust_repository_set(
*,
name,
versions,
exec_triple,
Expand All @@ -1018,7 +1064,8 @@ def rust_repository_set(
register_toolchain = True,
exec_compatible_with = None,
default_target_compatible_with = None,
aliases = {}):
aliases = {},
compact_windows_names = _COMPACT_WINDOWS_NAMES):
"""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 @@ -1055,10 +1102,20 @@ def rust_repository_set(
exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain.
default_target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform.
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.
"""

all_toolchain_names = []
for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions, default_target_compatible_with, aliases):
for toolchain in _get_toolchain_repositories(
name = name,
exec_triple = exec_triple,
extra_target_triples = extra_target_triples,
versions = versions,
default_target_compatible_with = default_target_compatible_with,
aliases = aliases,
compact_windows_names = compact_windows_names,
):
# Infer toolchain-specific rustc flags depending on the type (list, dict, optional) of extra_rustc_flags
if extra_rustc_flags == None:
toolchain_extra_rustc_flags = []
Expand Down

0 comments on commit 5cfb55c

Please sign in to comment.