fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAdded support for compact windows repository names, avoiding MAX_PATH (#3023)8e962a16

main
4.1 KiB131 linesraw
1"""Utility functions for the cargo rules"""
2
3load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext")
4
5def _resolve_repository_template(
6        *,
7        template,
8        abi = None,
9        arch = None,
10        channel = None,
11        system = None,
12        tool = None,
13        triple = None,
14        vendor = None,
15        version = None,
16        compress_windows_names = False):
17    """Render values into a repository template string
18
19    Args:
20        template (str): The template to use for rendering
21        abi (str, optional): The host ABI
22        arch (str, optional): The host CPU architecture
23        channel (str, optional): The toolchain channel. Eg. `stable`, `nightly`.
24        system (str, optional): The host system name
25        tool (str, optional): The tool to expect in the particular repository.
26            Eg. `cargo`, `rustc`, `stdlib`.
27        triple (str, optional): The host triple
28        vendor (str, optional): The host vendor name
29        version (str, optional): The Rust version used in the toolchain.
30        compress_windows_names (bool): Whether or not the windows repositories are
31            to be in the compressed form.
32    Returns:
33        string: The resolved template string based on the given parameters
34    """
35    if abi:
36        template = template.replace("{abi}", abi)
37
38    if arch:
39        template = template.replace("{arch}", arch)
40
41    if system:
42        template = template.replace("{system}", system)
43
44    if tool:
45        template = template.replace("{tool}", tool)
46
47    if triple:
48        template = template.replace("{triple}", triple)
49
50    if vendor:
51        template = template.replace("{vendor}", vendor)
52
53    if version:
54        template = template.replace("{version}", version)
55
56    if channel:
57        template = template.replace("{channel}", channel)
58
59    if compress_windows_names and system == "windows":
60        repo, _, target = template.partition("//")
61        prefix = ""
62        if repo.startswith("@"):
63            repo = repo[1:]
64            prefix = "@"
65        elif repo.startswith("@@"):
66            repo = repo[2:]
67            prefix = "@@"
68
69        suffix = ""
70        if repo.endswith("_tools"):
71            repo = repo[:-len("_tools")]
72            suffix = "_tools"
73
74        return "{}rw-{}{}//{}".format(prefix, abs(hash(repo)), suffix, target)
75
76    return template
77
78def get_rust_tools(
79        *,
80        cargo_template,
81        rustc_template,
82        host_triple,
83        channel,
84        version,
85        compress_windows_names):
86    """Retrieve `cargo` and `rustc` labels based on the host triple.
87
88    Args:
89        cargo_template (str): A template used to identify the label of the host `cargo` binary.
90        rustc_template (str): A template used to identify the label of the host `rustc` binary.
91        host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`.
92        channel (str): The Rust toolchain channel.
93        version (str): The version (or iso date in case of beta or nightly channels) of Cargo+Rustc to use.
94        compress_windows_names (bool): Whether or not the windows repositories are
95            to be in the compressed form.
96
97    Returns:
98        struct: A struct containing the labels of expected tools
99    """
100    extension = system_to_binary_ext(host_triple.system)
101
102    cargo_label = Label(_resolve_repository_template(
103        template = cargo_template,
104        version = version,
105        channel = channel,
106        triple = host_triple.str,
107        arch = host_triple.arch,
108        vendor = host_triple.vendor,
109        system = host_triple.system,
110        abi = host_triple.abi,
111        tool = "cargo" + extension,
112        compress_windows_names = compress_windows_names,
113    ))
114
115    rustc_label = Label(_resolve_repository_template(
116        template = rustc_template,
117        version = version,
118        channel = channel,
119        triple = host_triple.str,
120        arch = host_triple.arch,
121        vendor = host_triple.vendor,
122        system = host_triple.system,
123        abi = host_triple.abi,
124        tool = "rustc" + extension,
125        compress_windows_names = compress_windows_names,
126    ))
127
128    return struct(
129        cargo = cargo_label,
130        rustc = rustc_label,
131    )