blob: 13a59a4cabe02b6e22f754df01899a020dc6e291 [file] [log] [blame]
Brian Silvermancc09f182022-03-09 15:40:20 -08001"""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.
4
5Triples can be described at the following link:
6https://clang.llvm.org/docs/CrossCompilation.html#target-triple
7"""
8
9def triple(triple):
10 """Constructs a struct containing each component of the provided triple
11
12 Args:
13 triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
14
15 Returns:
16 struct:
17 - arch (str): The triple's CPU architecture
18 - vendor (str): The vendor of the system
19 - system (str): The name of the system
20 - abi (str, optional): The abi to use or None if abi does not apply.
21 - triple (str): The original triple
22 """
23 if triple == "wasm32-wasi":
24 return struct(
25 arch = "wasm32",
26 system = "wasi",
27 vendor = "wasi",
28 abi = None,
29 triple = triple,
30 )
31
32 component_parts = triple.split("-")
33 if len(component_parts) < 3:
34 fail("Expected target triple to contain at least three sections separated by '-'")
35
36 cpu_arch = component_parts[0]
37 vendor = component_parts[1]
38 system = component_parts[2]
39 abi = None
40
41 if system == "androideabi":
42 system = "android"
43 abi = "eabi"
44
45 if len(component_parts) == 4:
46 abi = component_parts[3]
47
48 return struct(
49 arch = cpu_arch,
50 vendor = vendor,
51 system = system,
52 abi = abi,
53 triple = triple,
54 )