fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
aca4ec1a
0.5.0
1"""Utilities directly related to the `splicing` step of `cargo-bazel`.""" 2 3load ( ":common_utils.bzl" , "cargo_environ" , "execute" ) 4 5CARGO_BAZEL_DEBUG = "CARGO_BAZEL_DEBUG" 6 7def splicing_config ( resolver_version = "1" ): 8"""Various settings used to configure Cargo manifest splicing behavior. 9 10[rv]: https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions 11 12Args: 13resolver_version (str, optional): The [resolver version][rv] to use in generated Cargo 14manifests. This flag is **only** used when splicing a manifest from direct package 15definitions. See `crates_repository::packages`. 16 17Returns: 18str: A json encoded string of the parameters provided 19""" 20return json . encode ( struct ( 21resolver_version = resolver_version , 22)) 23 24def download_extra_workspace_members ( repository_ctx , cache_dir , render_template_registry_url ): 25"""Download additional workspace members for use in splicing. 26 27Args: 28repository_ctx (repository_ctx): The rule's context object. 29cache_dir (path): A directory in which to download and extract extra workspace members 30render_template_registry_url (str): The base template to use for determining the crate's registry URL. 31 32Returns: 33list: A list of information related to the downloaded crates 34- manifest: The path of the manifest. 35- url: The url the manifest came from. 36- sha256: The sha256 checksum of the new manifest. 37""" 38manifests = [] 39extra_workspace_members = repository_ctx . attr . extra_workspace_members 40if extra_workspace_members : 41repository_ctx . report_progress ( "Downloading extra workspace members." ) 42 43for name , spec in repository_ctx . attr . extra_workspace_members . items (): 44spec = struct ( ** json . decode ( spec )) 45 46url = render_template_registry_url 47url = url . replace ( "{name}" , name ) 48url = url . replace ( "{version}" , spec . version ) 49 50if spec . sha256 : 51result = repository_ctx . download_and_extract ( 52output = cache_dir , 53url = url , 54sha256 = spec . sha256 , 55type = "tar.gz" , 56) 57else : 58result = repository_ctx . download_and_extract ( 59output = cache_dir , 60url = url , 61type = "tar.gz" , 62) 63 64manifest = repository_ctx . path ( "{}/{}-{}/Cargo.toml" . format ( 65cache_dir , 66name , 67spec . version , 68)) 69 70if not manifest . exists : 71fail ( "Extra workspace member '{}' has no root Cargo.toml file" . format ( name )) 72 73manifests . append ( struct ( 74manifest = str ( manifest ), 75url = url , 76sha256 = result . sha256 , 77)) 78 79return manifests 80 81def kebab_case_keys ( data ): 82"""Ensure the key value of the data given are kebab-case 83 84Args: 85data (dict): A deserialized json blob 86 87Returns: 88dict: The same `data` but with kebab-case keys 89""" 90return { 91key . lower (). replace ( "_" , "-" ): val 92for ( key , val ) in data . items () 93} 94 95def compile_splicing_manifest ( splicing_config , manifests , cargo_config_path , packages ): 96"""Produce a manifest containing required components for splciing a new Cargo workspace 97 98[cargo_config]: https://doc.rust-lang.org/cargo/reference/config.html 99[cargo_toml]: https://doc.rust-lang.org/cargo/reference/manifest.html 100 101Args: 102splicing_config (dict): A deserialized `splicing_config` 103manifests (dict): A mapping of paths to Bazel labels which represent [Cargo manifests][cargo_toml]. 104cargo_config_path (str): The absolute path to a [Cargo config][cargo_config]. 105packages (dict): A set of crates (packages) specifications to depend on 106 107Returns: 108dict: A dictionary representation of a `cargo_bazel::splicing::SplicingManifest` 109""" 110 111# Deserialize information about direct packges 112direct_packages_info = { 113# Ensure the data is using kebab-case as that's what `cargo_toml::DependencyDetail` expects. 114pkg : kebab_case_keys ( dict ( json . decode ( data ))) 115for ( pkg , data ) in packages . items () 116} 117 118# Auto-generated splicier manifest values 119splicing_manifest_content = { 120"cargo_config" : cargo_config_path , 121"direct_packages" : direct_packages_info , 122"manifests" : manifests , 123} 124 125return dict ( splicing_config . items () + splicing_manifest_content . items ()) 126 127def create_splicing_manifest ( repository_ctx ): 128"""Produce a manifest containing required components for splciing a new Cargo workspace 129 130Args: 131repository_ctx (repository_ctx): The rule's context object. 132 133Returns: 134path: The path to a json encoded manifest 135""" 136 137manifests = { str ( repository_ctx . path ( m )): str ( m ) for m in repository_ctx . attr . manifests } 138 139if repository_ctx . attr . cargo_config : 140cargo_config = str ( repository_ctx . path ( repository_ctx . attr . cargo_config )) 141else : 142cargo_config = None 143 144# Load user configurable splicing settings 145config = json . decode ( repository_ctx . attr . splicing_config or splicing_config ()) 146 147repo_dir = repository_ctx . path ( "." ) 148 149splicing_manifest = repository_ctx . path ( "{}/splicing_manifest.json" . format ( repo_dir )) 150 151data = compile_splicing_manifest ( 152splicing_config = config , 153manifests = manifests , 154cargo_config_path = cargo_config , 155packages = repository_ctx . attr . packages , 156) 157 158# Serialize information required for splicing 159repository_ctx . file ( 160splicing_manifest , 161json . encode_indent ( 162data , 163indent = " " * 4 , 164), 165) 166 167return splicing_manifest 168 169def splice_workspace_manifest ( repository_ctx , generator , lockfile , splicing_manifest , cargo , rustc ): 170"""Splice together a Cargo workspace from various other manifests and package definitions 171 172Args: 173repository_ctx (repository_ctx): The rule's context object. 174generator (path): The `cargo-bazel` binary. 175lockfile (path): The path to a "lock" file for reproducible `cargo-bazel` renderings. 176splicing_manifest (path): The path to a splicing manifest. 177cargo (path): The path to a Cargo binary. 178rustc (path): The Path to a Rustc binary. 179 180Returns: 181path: The path to a Cargo metadata json file found in the spliced workspace root. 182""" 183repository_ctx . report_progress ( "Splicing Cargo workspace." ) 184repo_dir = repository_ctx . path ( "." ) 185 186# Download extra workspace members 187crates_cache_dir = repository_ctx . path ( "{}/.crates_cache" . format ( repo_dir )) 188extra_manifest_info = download_extra_workspace_members ( 189repository_ctx = repository_ctx , 190cache_dir = crates_cache_dir , 191render_template_registry_url = repository_ctx . attr . extra_workspace_member_url_template , 192) 193 194extra_manifests_manifest = repository_ctx . path ( "{}/extra_manifests_manifest.json" . format ( repo_dir )) 195repository_ctx . file ( 196extra_manifests_manifest , 197json . encode_indent ( struct ( 198manifests = extra_manifest_info , 199), indent = " " * 4 ), 200) 201 202splicing_output_dir = repository_ctx . path ( "splicing-output" ) 203 204# Generate a workspace root which contains all workspace members 205arguments = [ 206generator , 207"splice" , 208"--output-dir" , 209splicing_output_dir , 210"--splicing-manifest" , 211splicing_manifest , 212"--extra-manifests-manifest" , 213extra_manifests_manifest , 214"--cargo" , 215cargo , 216"--rustc" , 217rustc , 218] 219 220# Optionally set the splicing workspace directory to somewhere within the repository directory 221# to improve the debugging experience. 222if CARGO_BAZEL_DEBUG in repository_ctx . os . environ : 223arguments . extend ([ 224"--workspace-dir" , 225repository_ctx . path ( "{}/splicing-workspace" . format ( repo_dir )), 226]) 227 228# Splicing accepts a Cargo.lock file in some scenarios. Ensure it's passed 229# if the lockfile is a actually a Cargo lockfile. 230if lockfile . kind == "cargo" : 231arguments . extend ([ 232"--cargo-lockfile" , 233lockfile . path , 234]) 235 236env = { 237"CARGO" : str ( cargo ), 238"RUSTC" : str ( rustc ), 239"RUST_BACKTRACE" : "full" , 240} 241 242# Add any Cargo environment variables to the `cargo-bazel` execution 243env . update ( cargo_environ ( repository_ctx )) 244 245execute ( 246repository_ctx = repository_ctx , 247args = arguments , 248env = env , 249) 250 251# This file must have been produced by the execution above. 252spliced_lockfile = repository_ctx . path ( "{}/Cargo.lock" . format ( splicing_output_dir )) 253if not spliced_lockfile . exists : 254fail ( "Lockfile file does not exist: {}" . format ( spliced_lockfile )) 255spliced_metadata = repository_ctx . path ( "{}/metadata.json" . format ( splicing_output_dir )) 256if not spliced_metadata . exists : 257fail ( "Metadata file does not exist: {}" . format ( spliced_metadata )) 258 259return spliced_metadata