fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
46b7ea5a
cleanup
1"""Utility functions for the cargo rules""" 2 3load ( "//rust/platform:triple_mappings.bzl" , "system_to_binary_ext" ) 4 5def _resolve_repository_template ( 6template , 7abi = None , 8arch = None , 9channel = None , 10system = None , 11tool = None , 12triple = None , 13vendor = None , 14version = None ): 15"""Render values into a repository template string 16 17Args: 18template (str): The template to use for rendering 19abi (str, optional): The host ABI 20arch (str, optional): The host CPU architecture 21channel (str, optional): The toolchain channel. Eg. `stable`, `nightly`. 22system (str, optional): The host system name 23tool (str, optional): The tool to expect in the particular repository. 24Eg. `cargo`, `rustc`, `stdlib`. 25triple (str, optional): The host triple 26vendor (str, optional): The host vendor name 27version (str, optional): The Rust version used in the toolchain. 28Returns: 29string: The resolved template string based on the given parameters 30""" 31if abi : 32template = template . replace ( "{abi}" , abi ) 33 34if arch : 35template = template . replace ( "{arch}" , arch ) 36 37if system : 38template = template . replace ( "{system}" , system ) 39 40if tool : 41template = template . replace ( "{tool}" , tool ) 42 43if triple : 44template = template . replace ( "{triple}" , triple ) 45 46if vendor : 47template = template . replace ( "{vendor}" , vendor ) 48 49if version : 50template = template . replace ( "{version}" , version ) 51 52if channel : 53template = template . replace ( "{channel}" , channel ) 54 55return template 56 57def get_rust_tools ( cargo_template , rustc_template , host_triple , channel , version ): 58"""Retrieve `cargo` and `rustc` labels based on the host triple. 59 60Args: 61cargo_template (str): A template used to identify the label of the host `cargo` binary. 62rustc_template (str): A template used to identify the label of the host `rustc` binary. 63host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`. 64channel (str): The Rust toolchain channel. 65version (str): The version (or iso date in case of beta or nightly channels) of Cargo+Rustc to use. 66 67Returns: 68struct: A struct containing the labels of expected tools 69""" 70extension = system_to_binary_ext ( host_triple . system ) 71 72cargo_label = Label ( _resolve_repository_template ( 73template = cargo_template , 74version = version , 75channel = channel , 76triple = host_triple . str , 77arch = host_triple . arch , 78vendor = host_triple . vendor , 79system = host_triple . system , 80abi = host_triple . abi , 81tool = "cargo" + extension , 82)) 83 84rustc_label = Label ( _resolve_repository_template ( 85template = rustc_template , 86version = version , 87channel = channel , 88triple = host_triple . str , 89arch = host_triple . arch , 90vendor = host_triple . vendor , 91system = host_triple . system , 92abi = host_triple . abi , 93tool = "rustc" + extension , 94)) 95 96return struct ( 97cargo = cargo_label , 98rustc = rustc_label , 99)