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