fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
8e962a16
main
1"""Utility functions for the cargo rules""" 2 3load ( "//rust/platform:triple_mappings.bzl" , "system_to_binary_ext" ) 4 5def _resolve_repository_template ( 6* , 7template , 8abi = None , 9arch = None , 10channel = None , 11system = None , 12tool = None , 13triple = None , 14vendor = None , 15version = None , 16compress_windows_names = False ): 17"""Render values into a repository template string 18 19Args: 20template (str): The template to use for rendering 21abi (str, optional): The host ABI 22arch (str, optional): The host CPU architecture 23channel (str, optional): The toolchain channel. Eg. `stable`, `nightly`. 24system (str, optional): The host system name 25tool (str, optional): The tool to expect in the particular repository. 26Eg. `cargo`, `rustc`, `stdlib`. 27triple (str, optional): The host triple 28vendor (str, optional): The host vendor name 29version (str, optional): The Rust version used in the toolchain. 30compress_windows_names (bool): Whether or not the windows repositories are 31to be in the compressed form. 32Returns: 33string: The resolved template string based on the given parameters 34""" 35if abi : 36template = template . replace ( "{abi}" , abi ) 37 38if arch : 39template = template . replace ( "{arch}" , arch ) 40 41if system : 42template = template . replace ( "{system}" , system ) 43 44if tool : 45template = template . replace ( "{tool}" , tool ) 46 47if triple : 48template = template . replace ( "{triple}" , triple ) 49 50if vendor : 51template = template . replace ( "{vendor}" , vendor ) 52 53if version : 54template = template . replace ( "{version}" , version ) 55 56if channel : 57template = template . replace ( "{channel}" , channel ) 58 59if compress_windows_names and system == "windows" : 60repo , _ , target = template . partition ( "//" ) 61prefix = "" 62if repo . startswith ( "@" ): 63repo = repo [ 1 :] 64prefix = "@" 65elif repo . startswith ( "@@" ): 66repo = repo [ 2 :] 67prefix = "@@" 68 69suffix = "" 70if repo . endswith ( "_tools" ): 71repo = repo [: - len ( "_tools" )] 72suffix = "_tools" 73 74return "{}rw-{}{}//{}" . format ( prefix , abs ( hash ( repo )), suffix , target ) 75 76return template 77 78def get_rust_tools ( 79* , 80cargo_template , 81rustc_template , 82host_triple , 83channel , 84version , 85compress_windows_names ): 86"""Retrieve `cargo` and `rustc` labels based on the host triple. 87 88Args: 89cargo_template (str): A template used to identify the label of the host `cargo` binary. 90rustc_template (str): A template used to identify the label of the host `rustc` binary. 91host_triple (struct): The host's triple. See `@rules_rust//rust/platform:triple.bzl`. 92channel (str): The Rust toolchain channel. 93version (str): The version (or iso date in case of beta or nightly channels) of Cargo+Rustc to use. 94compress_windows_names (bool): Whether or not the windows repositories are 95to be in the compressed form. 96 97Returns: 98struct: A struct containing the labels of expected tools 99""" 100extension = system_to_binary_ext ( host_triple . system ) 101 102cargo_label = Label ( _resolve_repository_template ( 103template = cargo_template , 104version = version , 105channel = channel , 106triple = host_triple . str , 107arch = host_triple . arch , 108vendor = host_triple . vendor , 109system = host_triple . system , 110abi = host_triple . abi , 111tool = "cargo" + extension , 112compress_windows_names = compress_windows_names , 113)) 114 115rustc_label = Label ( _resolve_repository_template ( 116template = rustc_template , 117version = version , 118channel = channel , 119triple = host_triple . str , 120arch = host_triple . arch , 121vendor = host_triple . vendor , 122system = host_triple . system , 123abi = host_triple . abi , 124tool = "rustc" + extension , 125compress_windows_names = compress_windows_names , 126)) 127 128return struct ( 129cargo = cargo_label , 130rustc = rustc_label , 131)