fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
3d65214d
0.7.0
1"""Utilities directly related to the `splicing` step of `cargo-bazel`.""" 2 3load ( ":common_utils.bzl" , "CARGO_BAZEL_REPIN" , "REPIN" , "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 kebab_case_keys ( data ): 25"""Ensure the key value of the data given are kebab-case 26 27Args: 28data (dict): A deserialized json blob 29 30Returns: 31dict: The same `data` but with kebab-case keys 32""" 33return { 34key . lower (). replace ( "_" , "-" ): val 35for ( key , val ) in data . items () 36} 37 38def compile_splicing_manifest ( splicing_config , manifests , cargo_config_path , packages ): 39"""Produce a manifest containing required components for splciing a new Cargo workspace 40 41[cargo_config]: https://doc.rust-lang.org/cargo/reference/config.html 42[cargo_toml]: https://doc.rust-lang.org/cargo/reference/manifest.html 43 44Args: 45splicing_config (dict): A deserialized `splicing_config` 46manifests (dict): A mapping of paths to Bazel labels which represent [Cargo manifests][cargo_toml]. 47cargo_config_path (str): The absolute path to a [Cargo config][cargo_config]. 48packages (dict): A set of crates (packages) specifications to depend on 49 50Returns: 51dict: A dictionary representation of a `cargo_bazel::splicing::SplicingManifest` 52""" 53 54# Deserialize information about direct packges 55direct_packages_info = { 56# Ensure the data is using kebab-case as that's what `cargo_toml::DependencyDetail` expects. 57pkg : kebab_case_keys ( dict ( json . decode ( data ))) 58for ( pkg , data ) in packages . items () 59} 60 61# Auto-generated splicier manifest values 62splicing_manifest_content = { 63"cargo_config" : cargo_config_path , 64"direct_packages" : direct_packages_info , 65"manifests" : manifests , 66} 67 68return dict ( splicing_config . items () + splicing_manifest_content . items ()) 69 70def create_splicing_manifest ( repository_ctx ): 71"""Produce a manifest containing required components for splciing a new Cargo workspace 72 73Args: 74repository_ctx (repository_ctx): The rule's context object. 75 76Returns: 77path: The path to a json encoded manifest 78""" 79 80manifests = { str ( repository_ctx . path ( m )): str ( m ) for m in repository_ctx . attr . manifests } 81 82if repository_ctx . attr . cargo_config : 83cargo_config = str ( repository_ctx . path ( repository_ctx . attr . cargo_config )) 84else : 85cargo_config = None 86 87# Load user configurable splicing settings 88config = json . decode ( repository_ctx . attr . splicing_config or splicing_config ()) 89 90repo_dir = repository_ctx . path ( "." ) 91 92splicing_manifest = repository_ctx . path ( "{}/splicing_manifest.json" . format ( repo_dir )) 93 94data = compile_splicing_manifest ( 95splicing_config = config , 96manifests = manifests , 97cargo_config_path = cargo_config , 98packages = repository_ctx . attr . packages , 99) 100 101# Serialize information required for splicing 102repository_ctx . file ( 103splicing_manifest , 104json . encode_indent ( 105data , 106indent = " " * 4 , 107), 108) 109 110return splicing_manifest 111 112def splice_workspace_manifest ( repository_ctx , generator , cargo_lockfile , splicing_manifest , cargo , rustc ): 113"""Splice together a Cargo workspace from various other manifests and package definitions 114 115Args: 116repository_ctx (repository_ctx): The rule's context object. 117generator (path): The `cargo-bazel` binary. 118cargo_lockfile (path): The path to a "Cargo.lock" file. 119splicing_manifest (path): The path to a splicing manifest. 120cargo (path): The path to a Cargo binary. 121rustc (path): The Path to a Rustc binary. 122 123Returns: 124path: The path to a Cargo metadata json file found in the spliced workspace root. 125""" 126repository_ctx . report_progress ( "Splicing Cargo workspace." ) 127repo_dir = repository_ctx . path ( "." ) 128 129splicing_output_dir = repository_ctx . path ( "splicing-output" ) 130 131# Generate a workspace root which contains all workspace members 132arguments = [ 133generator , 134"splice" , 135"--output-dir" , 136splicing_output_dir , 137"--splicing-manifest" , 138splicing_manifest , 139"--cargo" , 140cargo , 141"--rustc" , 142rustc , 143"--cargo-lockfile" , 144cargo_lockfile , 145] 146 147# Optionally set the splicing workspace directory to somewhere within the repository directory 148# to improve the debugging experience. 149if CARGO_BAZEL_DEBUG in repository_ctx . os . environ : 150arguments . extend ([ 151"--workspace-dir" , 152repository_ctx . path ( "{}/splicing-workspace" . format ( repo_dir )), 153]) 154 155env = { 156"CARGO" : str ( cargo ), 157"RUSTC" : str ( rustc ), 158"RUST_BACKTRACE" : "full" , 159} 160 161# Ensure the short hand repin variable is set to the full name. 162if REPIN in repository_ctx . os . environ and CARGO_BAZEL_REPIN not in repository_ctx . os . environ : 163env . update ({ CARGO_BAZEL_REPIN : repository_ctx . os . environ [ REPIN ]}) 164 165# Add any Cargo environment variables to the `cargo-bazel` execution 166env . update ( cargo_environ ( repository_ctx )) 167 168execute ( 169repository_ctx = repository_ctx , 170args = arguments , 171env = env , 172) 173 174# This file must have been produced by the execution above. 175spliced_lockfile = repository_ctx . path ( "{}/Cargo.lock" . format ( splicing_output_dir )) 176if not spliced_lockfile . exists : 177fail ( "Lockfile file does not exist: {}" . format ( spliced_lockfile )) 178spliced_metadata = repository_ctx . path ( "{}/metadata.json" . format ( splicing_output_dir )) 179if not spliced_metadata . exists : 180fail ( "Metadata file does not exist: {}" . format ( spliced_metadata )) 181 182return spliced_metadata