fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
ba6af156
main
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 13Args: 14triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 15 16Returns: 17struct: 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""" 24if triple in ( "wasm32-wasi" , "wasm32-wasip1" ): 25trip = triple 26if trip == "wasm32-wasi" : 27trip = "wasm32-wasip1" 28return struct ( 29arch = trip . split ( "-" )[ 0 ], 30vendor = trip . split ( "-" )[ 1 ], 31system = trip . split ( "-" )[ 1 ], 32abi = None , 33str = trip , 34) 35elif triple in ( "aarch64-fuchsia" , "x86_64-fuchsia" ): 36return struct ( 37arch = triple . split ( "-" )[ 0 ], 38vendor = "unknown" , 39system = "fuchsia" , 40abi = None , 41str = triple , 42) 43 44component_parts = triple . split ( "-" ) 45if len ( component_parts ) < 3 : 46fail ( "Expected target triple to contain at least three sections separated by '-'" ) 47 48cpu_arch = component_parts [ 0 ] 49vendor = component_parts [ 1 ] 50system = component_parts [ 2 ] 51abi = None 52 53if cpu_arch . startswith (( "thumbv8m" , "thumbv7m" , "thumbv7e" , "thumbv6m" )): 54abi = system 55system = vendor 56vendor = None 57 58if system == "androideabi" : 59system = "android" 60abi = "eabi" 61 62if len ( component_parts ) == 4 : 63abi = component_parts [ 3 ] 64 65return struct ( 66arch = cpu_arch , 67vendor = vendor , 68system = system , 69abi = abi , 70str = triple , 71) 72 73def _validate_cpu_architecture ( arch , expected_archs ): 74"""Validate the host CPU architecture 75 76Args: 77arch (string): a CPU architecture 78expected_archs (list): A list of expected architecture strings 79""" 80if arch not in expected_archs : 81fail ( "{} is not a expected cpu architecture {}" . format ( 82arch , 83expected_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 89Example: 90 91```python 92load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool") 93load("@rules_rust//rust/platform:triple.bzl", "get_host_triple") 94 95def _impl(repository_ctx): 96host_triple = get_host_triple(repository_ctx) 97 98load_arbitrary_tool( 99ctx = repository_ctx, 100tool_name = "cargo", 101tool_subdirectories = ["cargo"], 102target_triple = host_triple.str, 103) 104 105example = repository_rule(implementation = _impl) 106``` 107 108Args: 109repository_ctx (repository_ctx): The repository_rule's context object 110abi (str): Since there's no consistent way to check for ABI, this info 111may be explicitly provided 112 113Returns: 114struct: A triple struct; see the `triple` function in this module 115""" 116 117# Detect the host's cpu architecture 118 119supported_architectures = { 120"linux" : [ "aarch64" , "x86_64" , "s390x" ], 121"macos" : [ "aarch64" , "x86_64" ], 122"windows" : [ "aarch64" , "x86_64" ], 123} 124 125arch = repository_ctx . os . arch 126if arch == "amd64" : 127arch = "x86_64" 128 129if "linux" in repository_ctx . os . name : 130_validate_cpu_architecture ( arch , supported_architectures [ "linux" ]) 131return triple ( "{}-unknown-linux-{}" . format ( 132arch , 133abi or "gnu" , 134)) 135 136if "mac" in repository_ctx . os . name : 137_validate_cpu_architecture ( arch , supported_architectures [ "macos" ]) 138return triple ( "{}-apple-darwin" . format ( arch )) 139 140if "win" in repository_ctx . os . name : 141_validate_cpu_architecture ( arch , supported_architectures [ "windows" ]) 142return triple ( "{}-pc-windows-{}" . format ( 143arch , 144abi or "msvc" , 145)) 146 147fail ( "Unhandled host os: {}" , repository_ctx . os . name )