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