fork/rules_rust

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

David TolnayRemove 'triple' field from triple struct in favor of 'str' (#1297)97de47df

0.8.1
2.8 KiB91 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        template,
7        abi = None,
8        arch = None,
9        system = None,
10        tool = None,
11        triple = None,
12        vendor = None,
13        version = None):
14    """Render values into a repository template string
15
16    Args:
17        template (str): The template to use for rendering
18        abi (str, optional): The host ABI
19        arch (str, optional): The host CPU architecture
20        system (str, optional): The host system name
21        tool (str, optional): The tool to expect in the particular repository.
22            Eg. `cargo`, `rustc`, `stdlib`.
23        triple (str, optional): The host triple
24        vendor (str, optional): The host vendor name
25        version (str, optional): The Rust version used in the toolchain.
26    Returns:
27        string: The resolved template string based on the given parameters
28    """
29    if abi:
30        template = template.replace("{abi}", abi)
31
32    if arch:
33        template = template.replace("{arch}", arch)
34
35    if system:
36        template = template.replace("{system}", system)
37
38    if tool:
39        template = template.replace("{tool}", tool)
40
41    if triple:
42        template = template.replace("{triple}", triple)
43
44    if vendor:
45        template = template.replace("{vendor}", vendor)
46
47    if version:
48        template = template.replace("{version}", version)
49
50    return template
51
52def get_rust_tools(cargo_template, rustc_template, host_triple, version):
53    """Retrieve `cargo` and `rustc` labels based on the host triple.
54
55    Args:
56        cargo_template (str): A template used to identify the label of the host `cargo` binary.
57        rustc_template (str): A template used to identify the label of the host `rustc` binary.
58        host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`.
59        version (str): The version of Cargo+Rustc to use.
60
61    Returns:
62        struct: A struct containing the labels of expected tools
63    """
64    extension = system_to_binary_ext(host_triple.system)
65
66    cargo_label = Label(_resolve_repository_template(
67        template = cargo_template,
68        version = version,
69        triple = host_triple.str,
70        arch = host_triple.arch,
71        vendor = host_triple.vendor,
72        system = host_triple.system,
73        abi = host_triple.abi,
74        tool = "cargo" + extension,
75    ))
76
77    rustc_label = Label(_resolve_repository_template(
78        template = rustc_template,
79        version = version,
80        triple = host_triple.str,
81        arch = host_triple.arch,
82        vendor = host_triple.vendor,
83        system = host_triple.system,
84        abi = host_triple.abi,
85        tool = "rustc" + extension,
86    ))
87
88    return struct(
89        cargo = cargo_label,
90        rustc = rustc_label,
91    )