xref: /aosp_15_r20/external/bazelbuild-rules_rust/cargo/private/cargo_utils.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Utility functions for the cargo rules"""
2
3load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext")
4
5def _resolve_repository_template(
6        template,
7        abi = None,
8        arch = None,
9        channel = None,
10        system = None,
11        tool = None,
12        triple = None,
13        vendor = None,
14        version = None):
15    """Render values into a repository template string
16
17    Args:
18        template (str): The template to use for rendering
19        abi (str, optional): The host ABI
20        arch (str, optional): The host CPU architecture
21        channel (str, optional): The toolchain channel. Eg. `stable`, `nightly`.
22        system (str, optional): The host system name
23        tool (str, optional): The tool to expect in the particular repository.
24            Eg. `cargo`, `rustc`, `stdlib`.
25        triple (str, optional): The host triple
26        vendor (str, optional): The host vendor name
27        version (str, optional): The Rust version used in the toolchain.
28    Returns:
29        string: The resolved template string based on the given parameters
30    """
31    if abi:
32        template = template.replace("{abi}", abi)
33
34    if arch:
35        template = template.replace("{arch}", arch)
36
37    if system:
38        template = template.replace("{system}", system)
39
40    if tool:
41        template = template.replace("{tool}", tool)
42
43    if triple:
44        template = template.replace("{triple}", triple)
45
46    if vendor:
47        template = template.replace("{vendor}", vendor)
48
49    if version:
50        template = template.replace("{version}", version)
51
52    if channel:
53        template = template.replace("{channel}", channel)
54
55    return template
56
57def get_rust_tools(cargo_template, rustc_template, host_triple, channel, version):
58    """Retrieve `cargo` and `rustc` labels based on the host triple.
59
60    Args:
61        cargo_template (str): A template used to identify the label of the host `cargo` binary.
62        rustc_template (str): A template used to identify the label of the host `rustc` binary.
63        host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`.
64        channel (str): The Rust toolchain channel.
65        version (str): The version (or iso date in case of beta or nightly channels) of Cargo+Rustc to use.
66
67    Returns:
68        struct: A struct containing the labels of expected tools
69    """
70    extension = system_to_binary_ext(host_triple.system)
71
72    cargo_label = Label(_resolve_repository_template(
73        template = cargo_template,
74        version = version,
75        channel = channel,
76        triple = host_triple.str,
77        arch = host_triple.arch,
78        vendor = host_triple.vendor,
79        system = host_triple.system,
80        abi = host_triple.abi,
81        tool = "cargo" + extension,
82    ))
83
84    rustc_label = Label(_resolve_repository_template(
85        template = rustc_template,
86        version = version,
87        channel = channel,
88        triple = host_triple.str,
89        arch = host_triple.arch,
90        vendor = host_triple.vendor,
91        system = host_triple.system,
92        abi = host_triple.abi,
93        tool = "rustc" + extension,
94    ))
95
96    return struct(
97        cargo = cargo_label,
98        rustc = rustc_label,
99    )
100