fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
8bb25b8b
rust-analyzer
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 == "wasm32-wasi" : 25return struct ( 26arch = "wasm32" , 27system = "wasi" , 28vendor = "wasi" , 29abi = None , 30str = triple , 31) 32 33component_parts = triple . split ( "-" ) 34if len ( component_parts ) < 3 : 35fail ( "Expected target triple to contain at least three sections separated by '-'" ) 36 37cpu_arch = component_parts [ 0 ] 38vendor = component_parts [ 1 ] 39system = component_parts [ 2 ] 40abi = None 41 42if system == "androideabi" : 43system = "android" 44abi = "eabi" 45 46if len ( component_parts ) == 4 : 47abi = component_parts [ 3 ] 48 49return struct ( 50arch = cpu_arch , 51vendor = vendor , 52system = system , 53abi = abi , 54str = triple , 55) 56 57_CPU_ARCH_ERROR_MSG = """\ 58Command failed with exit code '{code}': {args} 59----------stdout: 60{stdout} 61----------stderr: 62{stderr} 63""" 64 65def _query_cpu_architecture ( repository_ctx , expected_archs , is_windows = False ): 66"""Detect the host CPU architecture 67 68Args: 69repository_ctx (repository_ctx): The repository_rule's context object 70expected_archs (list): A list of expected architecture strings 71is_windows (bool, optional): If true, the cpu lookup will use the windows method (`wmic` vs `uname`) 72 73Returns: 74str: The host's CPU architecture 75""" 76if is_windows : 77arguments = [ "wmic" , "os" , "get" , "osarchitecture" ] 78else : 79arguments = [ "uname" , "-m" ] 80 81result = repository_ctx . execute ( arguments ) 82 83if result . return_code : 84fail ( _CPU_ARCH_ERROR_MSG . format ( 85code = result . return_code , 86args = arguments , 87stdout = result . stdout , 88stderr = result . stderr , 89)) 90 91if is_windows : 92# Example output: 93# OSArchitecture 94# 64-bit 95lines = result . stdout . split ( "\n ") 96arch = lines [ 1 ]. strip () 97 98# Translate 64-bit to a compatible rust platform 99# https://doc.rust-lang.org/nightly/rustc/platform-support.html 100if arch . startswith ( "ARM 64-bit" ): 101arch = "aarch64" 102elif arch == "64-bit" : 103arch = "x86_64" 104else : 105arch = result . stdout . strip ( "\n ") 106 107# Correct the arm architecture for macos 108if "mac" in repository_ctx . os . name and arch == "arm64" : 109arch = "aarch64" 110 111if not arch in expected_archs : 112fail ( "{} is not a expected cpu architecture {}\n {} ". format ( 113arch , 114expected_archs , 115result . stdout , 116)) 117 118return arch 119 120def get_host_triple ( repository_ctx , abi = None ): 121"""Query host information for the appropriate triple to use with load_arbitrary_tool or the crate_universe resolver 122 123Example: 124 125```python 126load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool") 127load("@rules_rust//rust/platform:triple.bzl", "get_host_triple") 128 129def _impl(repository_ctx): 130host_triple = get_host_triple(repository_ctx) 131 132load_arbitrary_tool( 133ctx = repository_ctx, 134tool_name = "cargo", 135tool_subdirectories = ["cargo"], 136target_triple = host_triple.str, 137) 138 139example = repository_rule(implementation = _impl) 140``` 141 142Args: 143repository_ctx (repository_ctx): The repository_rule's context object 144abi (str): Since there's no consistent way to check for ABI, this info 145may be explicitly provided 146 147Returns: 148struct: A triple struct; see the `triple` function in this module 149""" 150 151# Detect the host's cpu architecture 152 153supported_architectures = { 154"linux" : [ "aarch64" , "x86_64" ], 155"macos" : [ "aarch64" , "x86_64" ], 156"windows" : [ "aarch64" , "x86_64" ], 157} 158 159if "linux" in repository_ctx . os . name : 160cpu = _query_cpu_architecture ( repository_ctx , supported_architectures [ "linux" ]) 161return triple ( "{}-unknown-linux-{}" . format ( 162cpu , 163abi or "gnu" , 164)) 165 166if "mac" in repository_ctx . os . name : 167cpu = _query_cpu_architecture ( repository_ctx , supported_architectures [ "macos" ]) 168return triple ( "{}-apple-darwin" . format ( cpu )) 169 170if "win" in repository_ctx . os . name : 171cpu = _query_cpu_architecture ( repository_ctx , supported_architectures [ "windows" ], True ) 172return triple ( "{}-pc-windows-{}" . format ( 173cpu , 174abi or "msvc" , 175)) 176 177fail ( "Unhandled host os: {}" , repository_ctx . os . name )