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