fork/rules_rust

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

UebelAndreDrop references to `wasm-wasi` (#3027)ba6af156

main
4.5 KiB147 linesraw
1"""Triples are a way to define information about a platform/system. This module provides
2a way to convert a triple string into a well structured object to avoid constant string
3parsing in starlark code, and a way for a repository_rule to extract the target triple
4of the host platform.
5
6Triples can be described at the following link:
7https://clang.llvm.org/docs/CrossCompilation.html#target-triple
8"""
9
10def triple(triple):
11    """Constructs a struct containing each component of the provided triple
12
13    Args:
14        triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
15
16    Returns:
17        struct:
18            - arch (str): The triple's CPU architecture
19            - vendor (str): The vendor of the system
20            - system (str): The name of the system
21            - abi (str, optional): The abi to use or None if abi does not apply.
22            - str (str): Original string representation of the triple
23    """
24    if triple in ("wasm32-wasi", "wasm32-wasip1"):
25        trip = triple
26        if trip == "wasm32-wasi":
27            trip = "wasm32-wasip1"
28        return struct(
29            arch = trip.split("-")[0],
30            vendor = trip.split("-")[1],
31            system = trip.split("-")[1],
32            abi = None,
33            str = trip,
34        )
35    elif triple in ("aarch64-fuchsia", "x86_64-fuchsia"):
36        return struct(
37            arch = triple.split("-")[0],
38            vendor = "unknown",
39            system = "fuchsia",
40            abi = None,
41            str = triple,
42        )
43
44    component_parts = triple.split("-")
45    if len(component_parts) < 3:
46        fail("Expected target triple to contain at least three sections separated by '-'")
47
48    cpu_arch = component_parts[0]
49    vendor = component_parts[1]
50    system = component_parts[2]
51    abi = None
52
53    if cpu_arch.startswith(("thumbv8m", "thumbv7m", "thumbv7e", "thumbv6m")):
54        abi = system
55        system = vendor
56        vendor = None
57
58    if system == "androideabi":
59        system = "android"
60        abi = "eabi"
61
62    if len(component_parts) == 4:
63        abi = component_parts[3]
64
65    return struct(
66        arch = cpu_arch,
67        vendor = vendor,
68        system = system,
69        abi = abi,
70        str = triple,
71    )
72
73def _validate_cpu_architecture(arch, expected_archs):
74    """Validate the host CPU architecture
75
76    Args:
77        arch (string): a CPU architecture
78        expected_archs (list): A list of expected architecture strings
79    """
80    if arch not in expected_archs:
81        fail("{} is not a expected cpu architecture {}".format(
82            arch,
83            expected_archs,
84        ))
85
86def get_host_triple(repository_ctx, abi = None):
87    """Query host information for the appropriate triple to use with load_arbitrary_tool or the crate_universe resolver
88
89    Example:
90
91    ```python
92    load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool")
93    load("@rules_rust//rust/platform:triple.bzl", "get_host_triple")
94
95    def _impl(repository_ctx):
96        host_triple = get_host_triple(repository_ctx)
97
98        load_arbitrary_tool(
99            ctx = repository_ctx,
100            tool_name = "cargo",
101            tool_subdirectories = ["cargo"],
102            target_triple = host_triple.str,
103        )
104
105    example = repository_rule(implementation = _impl)
106    ```
107
108    Args:
109        repository_ctx (repository_ctx): The repository_rule's context object
110        abi (str): Since there's no consistent way to check for ABI, this info
111            may be explicitly provided
112
113    Returns:
114        struct: A triple struct; see the `triple` function in this module
115    """
116
117    # Detect the host's cpu architecture
118
119    supported_architectures = {
120        "linux": ["aarch64", "x86_64", "s390x"],
121        "macos": ["aarch64", "x86_64"],
122        "windows": ["aarch64", "x86_64"],
123    }
124
125    arch = repository_ctx.os.arch
126    if arch == "amd64":
127        arch = "x86_64"
128
129    if "linux" in repository_ctx.os.name:
130        _validate_cpu_architecture(arch, supported_architectures["linux"])
131        return triple("{}-unknown-linux-{}".format(
132            arch,
133            abi or "gnu",
134        ))
135
136    if "mac" in repository_ctx.os.name:
137        _validate_cpu_architecture(arch, supported_architectures["macos"])
138        return triple("{}-apple-darwin".format(arch))
139
140    if "win" in repository_ctx.os.name:
141        _validate_cpu_architecture(arch, supported_architectures["windows"])
142        return triple("{}-pc-windows-{}".format(
143            arch,
144            abi or "msvc",
145        ))
146
147    fail("Unhandled host os: {}", repository_ctx.os.name)