fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
97de47df
0.5.0
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 == "64-bit" : 101arch = "x86_64" 102else : 103arch = result . stdout . strip ( "\n ") 104 105# Correct the arm architecture for macos 106if "mac" in repository_ctx . os . name and arch == "arm64" : 107arch = "aarch64" 108 109if not arch in expected_archs : 110fail ( "{} is not a expected cpu architecture {}\n {} ". format ( 111arch , 112expected_archs , 113result . stdout , 114)) 115 116return arch 117 118def get_host_triple ( repository_ctx , abi = None ): 119"""Query host information for the appropriate triple to use with load_arbitrary_tool or the crate_universe resolver 120 121Example: 122 123```python 124load("@rules_rust//rust:repositories.bzl", "load_arbitrary_tool") 125load("@rules_rust//rust/platform:triple.bzl", "get_host_triple") 126 127def _impl(repository_ctx): 128host_triple = get_host_triple(repository_ctx) 129 130load_arbitrary_tool( 131ctx = repository_ctx, 132tool_name = "cargo", 133tool_subdirectories = ["cargo"], 134target_triple = host_triple.str, 135) 136 137example = repository_rule(implementation = _impl) 138``` 139 140Args: 141repository_ctx (repository_ctx): The repository_rule's context object 142abi (str): Since there's no consistent way to check for ABI, this info 143may be explicitly provided 144 145Returns: 146struct: A triple struct; see the `triple` function in this module 147""" 148 149# Detect the host's cpu architecture 150 151supported_architectures = { 152"linux" : [ "aarch64" , "x86_64" ], 153"macos" : [ "aarch64" , "x86_64" ], 154"windows" : [ "x86_64" ], 155} 156 157if "linux" in repository_ctx . os . name : 158cpu = _query_cpu_architecture ( repository_ctx , supported_architectures [ "linux" ]) 159return triple ( "{}-unknown-linux-{}" . format ( 160cpu , 161abi or "gnu" , 162)) 163 164if "mac" in repository_ctx . os . name : 165cpu = _query_cpu_architecture ( repository_ctx , supported_architectures [ "macos" ]) 166return triple ( "{}-apple-darwin" . format ( cpu )) 167 168if "win" in repository_ctx . os . name : 169cpu = _query_cpu_architecture ( repository_ctx , supported_architectures [ "windows" ], True ) 170return triple ( "{}-pc-windows-{}" . format ( 171cpu , 172abi or "msvc" , 173)) 174 175fail ( "Unhandled host os: {}" , repository_ctx . os . name )