fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
84f1d065
rust-analyzer
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 _no_at_label ( label ): 71"""Strips leading '@'s for stringified labels in the main repository for backwards-comaptibility reasons.""" 72s = str ( label ) 73if s . startswith ( "@@//" ): 74return s [ 2 :] 75if s . startswith ( "@//" ): 76return s [ 1 :] 77return s 78 79def create_splicing_manifest ( repository_ctx ): 80"""Produce a manifest containing required components for splciing a new Cargo workspace 81 82Args: 83repository_ctx (repository_ctx): The rule's context object. 84 85Returns: 86path: The path to a json encoded manifest 87""" 88 89manifests = { str ( repository_ctx . path ( m )): _no_at_label ( m ) for m in repository_ctx . attr . manifests } 90 91if repository_ctx . attr . cargo_config : 92cargo_config = str ( repository_ctx . path ( repository_ctx . attr . cargo_config )) 93else : 94cargo_config = None 95 96# Load user configurable splicing settings 97config = json . decode ( repository_ctx . attr . splicing_config or splicing_config ()) 98 99repo_dir = repository_ctx . path ( "." ) 100 101splicing_manifest = repository_ctx . path ( "{}/splicing_manifest.json" . format ( repo_dir )) 102 103data = compile_splicing_manifest ( 104splicing_config = config , 105manifests = manifests , 106cargo_config_path = cargo_config , 107packages = repository_ctx . attr . packages , 108) 109 110# Serialize information required for splicing 111repository_ctx . file ( 112splicing_manifest , 113json . encode_indent ( 114data , 115indent = " " * 4 , 116), 117) 118 119return splicing_manifest 120 121def splice_workspace_manifest ( repository_ctx , generator , cargo_lockfile , splicing_manifest , config_path , cargo , rustc ): 122"""Splice together a Cargo workspace from various other manifests and package definitions 123 124Args: 125repository_ctx (repository_ctx): The rule's context object. 126generator (path): The `cargo-bazel` binary. 127cargo_lockfile (path): The path to a "Cargo.lock" file. 128splicing_manifest (path): The path to a splicing manifest. 129config_path: The path to the config file (containing `cargo_bazel::config::Config`.) 130cargo (path): The path to a Cargo binary. 131rustc (path): The Path to a Rustc binary. 132 133Returns: 134path: The path to a Cargo metadata json file found in the spliced workspace root. 135""" 136repository_ctx . report_progress ( "Splicing Cargo workspace." ) 137repo_dir = repository_ctx . path ( "." ) 138 139splicing_output_dir = repository_ctx . path ( "splicing-output" ) 140 141# Generate a workspace root which contains all workspace members 142arguments = [ 143generator , 144"splice" , 145"--output-dir" , 146splicing_output_dir , 147"--splicing-manifest" , 148splicing_manifest , 149"--config" , 150config_path , 151"--cargo" , 152cargo , 153"--rustc" , 154rustc , 155"--cargo-lockfile" , 156cargo_lockfile , 157] 158 159# Optionally set the splicing workspace directory to somewhere within the repository directory 160# to improve the debugging experience. 161if CARGO_BAZEL_DEBUG in repository_ctx . os . environ : 162arguments . extend ([ 163"--workspace-dir" , 164repository_ctx . path ( "{}/splicing-workspace" . format ( repo_dir )), 165]) 166 167env = { 168"CARGO" : str ( cargo ), 169"RUSTC" : str ( rustc ), 170"RUST_BACKTRACE" : "full" , 171} 172 173# Ensure the short hand repin variable is set to the full name. 174if REPIN in repository_ctx . os . environ and CARGO_BAZEL_REPIN not in repository_ctx . os . environ : 175env . update ({ CARGO_BAZEL_REPIN : repository_ctx . os . environ [ REPIN ]}) 176 177# Add any Cargo environment variables to the `cargo-bazel` execution 178env . update ( cargo_environ ( repository_ctx )) 179 180execute ( 181repository_ctx = repository_ctx , 182args = arguments , 183env = env , 184) 185 186# This file must have been produced by the execution above. 187spliced_lockfile = repository_ctx . path ( "{}/Cargo.lock" . format ( splicing_output_dir )) 188if not spliced_lockfile . exists : 189fail ( "Lockfile file does not exist: {}" . format ( spliced_lockfile )) 190spliced_metadata = repository_ctx . path ( "{}/metadata.json" . format ( splicing_output_dir )) 191if not spliced_metadata . exists : 192fail ( "Metadata file does not exist: {}" . format ( spliced_metadata )) 193 194return spliced_metadata