fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
82a437cc
0.8.0
1"""Utilities directly related to the `generate` step of `cargo-bazel`.""" 2 3load ( ":common_utils.bzl" , "CARGO_BAZEL_ISOLATED" , "REPIN_ENV_VARS" , "cargo_environ" , "execute" ) 4 5CARGO_BAZEL_GENERATOR_SHA256 = "CARGO_BAZEL_GENERATOR_SHA256" 6CARGO_BAZEL_GENERATOR_URL = "CARGO_BAZEL_GENERATOR_URL" 7 8GENERATOR_ENV_VARS = [ 9CARGO_BAZEL_GENERATOR_URL , 10CARGO_BAZEL_GENERATOR_SHA256 , 11] 12 13CRATES_REPOSITORY_ENVIRON = GENERATOR_ENV_VARS + REPIN_ENV_VARS + [ 14CARGO_BAZEL_ISOLATED , 15] 16 17def get_generator ( repository_ctx , host_triple ): 18"""Query network resources to locate a `cargo-bazel` binary 19 20Args: 21repository_ctx (repository_ctx): The rule's context object. 22host_triple (string): A string representing the host triple 23 24Returns: 25tuple(path, dict): The path to a `cargo-bazel` binary and the host sha256 pairing. 26The pairing (dict) may be `None` if there is no need to update the attribute 27""" 28use_environ = False 29for var in GENERATOR_ENV_VARS : 30if var in repository_ctx . os . environ : 31use_environ = True 32 33output = repository_ctx . path ( "cargo-bazel.exe" if "win" in repository_ctx . os . name else "cargo-bazel" ) 34 35# The `generator` attribute is the next highest priority behind 36# environment variables. We check those first before deciding to 37# use an explicitly provided variable. 38if not use_environ and repository_ctx . attr . generator : 39generator = repository_ctx . path ( Label ( repository_ctx . attr . generator )) 40 41# Resolve a few levels of symlinks to ensure we're accessing the direct binary 42for _ in range ( 1 , 100 ): 43real_generator = generator . realpath 44if real_generator == generator : 45 break 46generator = real_generator 47return generator , None 48 49# The environment variable will take precedence if set 50if use_environ : 51generator_sha256 = repository_ctx . os . environ . get ( CARGO_BAZEL_GENERATOR_SHA256 ) 52generator_url = repository_ctx . os . environ . get ( CARGO_BAZEL_GENERATOR_URL ) 53else : 54generator_sha256 = repository_ctx . attr . generator_sha256s . get ( host_triple ) 55generator_url = repository_ctx . attr . generator_urls . get ( host_triple ) 56 57if not generator_url : 58fail (( 59"No generator URL was found either in the `CARGO_BAZEL_GENERATOR_URL` " + 60"environment variable or for the `{}` triple in the `generator_urls` attribute" 61). format ( host_triple )) 62 63# Download the file into place 64if generator_sha256 : 65repository_ctx . download ( 66output = output , 67url = generator_url , 68sha256 = generator_sha256 , 69executable = True , 70) 71return output , None 72 73result = repository_ctx . download ( 74output = output , 75url = generator_url , 76executable = True , 77) 78 79return output , { host_triple : result . sha256 } 80 81def render_config ( 82build_file_template = "//:BUILD.{name}-{version}.bazel" , 83crate_label_template = "@{repository}__{name}-{version}//:{target}" , 84crate_repository_template = "{repository}__{name}-{version}" , 85crates_module_template = "//:{file}" , 86default_package_name = None , 87platforms_template = "@rules_rust//rust/platform:{triple}" , 88regen_command = None , 89vendor_mode = None ): 90"""Various settings used to configure rendered outputs 91 92The template parameters each support a select number of format keys. A description of each key 93can be found below where the supported keys for each template can be found in the parameter docs 94 95| key | definition | 96| --- | --- | 97| `name` | The name of the crate. Eg `tokio` | 98| `repository` | The rendered repository name for the crate. Directly relates to `crate_repository_template`. | 99| `triple` | A platform triple. Eg `x86_64-unknown-linux-gnu` | 100| `version` | The crate version. Eg `1.2.3` | 101| `target` | The library or binary target of the crate | 102| `file` | The basename of a file | 103 104Args: 105build_file_template (str, optional): The base template to use for BUILD file names. The available format keys 106are [`{name}`, {version}`]. 107crate_label_template (str, optional): The base template to use for crate labels. The available format keys 108are [`{repository}`, `{name}`, `{version}`, `{target}`]. 109crate_repository_template (str, optional): The base template to use for Crate label repository names. The 110available format keys are [`{repository}`, `{name}`, `{version}`]. 111crates_module_template (str, optional): The pattern to use for the `defs.bzl` and `BUILD.bazel` 112file names used for the crates module. The available format keys are [`{file}`]. 113default_package_name (str, optional): The default package name to use in the rendered macros. This affects the 114auto package detection of things like `all_crate_deps`. 115platforms_template (str, optional): The base template to use for platform names. 116See [platforms documentation](https://docs.bazel.build/versions/main/platforms.html). The available format 117keys are [`{triple}`]. 118regen_command (str, optional): An optional command to demonstrate how generated files should be regenerated. 119vendor_mode (str, optional): An optional configuration for rendirng content to be rendered into repositories. 120 121Returns: 122string: A json encoded struct to match the Rust `config::RenderConfig` struct 123""" 124return json . encode ( struct ( 125build_file_template = build_file_template , 126crate_label_template = crate_label_template , 127crate_repository_template = crate_repository_template , 128crates_module_template = crates_module_template , 129default_package_name = default_package_name , 130platforms_template = platforms_template , 131regen_command = regen_command , 132vendor_mode = vendor_mode , 133)) 134 135def _crate_id ( name , version ): 136"""Creates a `cargo_bazel::config::CrateId`. 137 138Args: 139name (str): The name of the crate 140version (str): The crate's version 141 142Returns: 143str: A serialized representation of a CrateId 144""" 145return "{} {}" . format ( name , version ) 146 147def collect_crate_annotations ( annotations , repository_name ): 148"""Deserialize and sanitize crate annotations. 149 150Args: 151annotations (dict): A mapping of crate names to lists of serialized annotations 152repository_name (str): The name of the repository that owns the annotations 153 154Returns: 155dict: A mapping of `cargo_bazel::config::CrateId` to sets of annotations 156""" 157annotations = { name : [ json . decode ( a ) for a in annotation ] for name , annotation in annotations . items ()} 158crate_annotations = {} 159for name , annotation in annotations . items (): 160for ( version , data ) in annotation : 161if name == "*" and version != "*" : 162fail ( 163"Wildcard crate names must have wildcard crate versions. " + 164"Please update the `annotations` attribute of the {} crates_repository" . format ( 165repository_name , 166), 167) 168id = _crate_id ( name , version ) 169if id in crate_annotations : 170fail ( "Found duplicate entries for {}" . format ( id )) 171 172crate_annotations . update ({ id : data }) 173return crate_annotations 174 175def _read_cargo_config ( repository_ctx ): 176if repository_ctx . attr . cargo_config : 177config = repository_ctx . path ( repository_ctx . attr . cargo_config ) 178return repository_ctx . read ( config ) 179return None 180 181def _update_render_config ( config , repository_name ): 182"""Add the repository name to the render config 183 184Args: 185config (dict): A `render_config` struct 186repository_name (str): The name of the repository that owns the config 187 188Returns: 189struct: An updated `render_config`. 190""" 191 192# Add the repository name as it's very relevant to rendering. 193config . update ({ "repository_name" : repository_name }) 194 195return struct ( ** config ) 196 197def _get_render_config ( repository_ctx ): 198if repository_ctx . attr . render_config : 199config = dict ( json . decode ( repository_ctx . attr . render_config )) 200else : 201config = dict ( json . decode ( render_config ())) 202 203if not config . get ( "regen_command" ): 204config [ "regen_command" ] = "bazel sync --only={}" . format ( 205repository_ctx . name , 206) 207 208return config 209 210def compile_config ( crate_annotations , generate_build_scripts , cargo_config , render_config , supported_platform_triples , repository_name , repository_ctx = None ): 211"""Create a config file for generating crate targets 212 213[cargo_config]: https://doc.rust-lang.org/cargo/reference/config.html 214 215Args: 216crate_annotations (dict): Extra settings to apply to crates. See 217`crates_repository.annotations` or `crates_vendor.annotations`. 218generate_build_scripts (bool): Whether or not to globally disable build scripts. 219cargo_config (str): The optional contents of a [Cargo config][cargo_config]. 220render_config (dict): The deserialized dict of the `render_config` function. 221supported_platform_triples (list): A list of platform triples 222repository_name (str): The name of the repository being generated 223repository_ctx (repository_ctx, optional): A repository context object used for enabling 224certain functionality. 225 226Returns: 227struct: A struct matching a `cargo_bazel::config::Config`. 228""" 229annotations = collect_crate_annotations ( crate_annotations , repository_name ) 230 231# Load additive build files if any have been provided. 232unexpected = [] 233for name , data in annotations . items (): 234f = data . pop ( "additive_build_file" , None ) 235if f and not repository_ctx : 236unexpected . append ( name ) 237f = None 238content = [ x for x in [ 239data . pop ( "additive_build_file_content" , None ), 240repository_ctx . read ( Label ( f )) if f else None , 241] if x ] 242if content : 243data . update ({ "additive_build_file_content" : "\n ". join ( content )}) 244 245if unexpected : 246fail ( "The following annotations use `additive_build_file` which is not supported for {}: {}" . format ( repository_name , unexpected )) 247 248config = struct ( 249generate_build_scripts = generate_build_scripts , 250annotations = annotations , 251cargo_config = cargo_config , 252rendering = _update_render_config ( 253config = render_config , 254repository_name = repository_name , 255), 256supported_platform_triples = supported_platform_triples , 257) 258 259return config 260 261def generate_config ( repository_ctx ): 262"""Generate a config file from various attributes passed to the rule. 263 264Args: 265repository_ctx (repository_ctx): The rule's context object. 266 267Returns: 268struct: A struct containing the path to a config and it's contents 269""" 270 271config = compile_config ( 272crate_annotations = repository_ctx . attr . annotations , 273generate_build_scripts = repository_ctx . attr . generate_build_scripts , 274cargo_config = _read_cargo_config ( repository_ctx ), 275render_config = _get_render_config ( repository_ctx ), 276supported_platform_triples = repository_ctx . attr . supported_platform_triples , 277repository_name = repository_ctx . name , 278repository_ctx = repository_ctx , 279) 280 281config_path = repository_ctx . path ( "cargo-bazel.json" ) 282repository_ctx . file ( 283config_path , 284json . encode_indent ( config , indent = " " * 4 ), 285) 286 287return config_path 288 289def get_lockfiles ( repository_ctx ): 290"""_summary_ 291 292Args: 293repository_ctx (repository_ctx): The rule's context object. 294 295Returns: 296struct: _description_ 297""" 298return struct ( 299cargo = repository_ctx . path ( repository_ctx . attr . cargo_lockfile ), 300bazel = repository_ctx . path ( repository_ctx . attr . lockfile ) if repository_ctx . attr . lockfile else None , 301) 302 303def determine_repin ( repository_ctx , generator , lockfile_path , config , splicing_manifest , cargo , rustc ): 304"""Use the `cargo-bazel` binary to determine whether or not dpeendencies need to be re-pinned 305 306Args: 307repository_ctx (repository_ctx): The rule's context object. 308generator (path): The path to a `cargo-bazel` binary. 309config (path): The path to a `cargo-bazel` config file. See `generate_config`. 310splicing_manifest (path): The path to a `cargo-bazel` splicing manifest. See `create_splicing_manifest` 311lockfile_path (path): The path to a "lock" file for reproducible outputs. 312cargo (path): The path to a Cargo binary. 313rustc (path): The path to a Rustc binary. 314 315Returns: 316bool: True if dependencies need to be re-pinned 317""" 318 319# If a repin environment variable is set, always repin 320for var in REPIN_ENV_VARS : 321if var in repository_ctx . os . environ and repository_ctx . os . environ [ var ]. lower () not in [ "false" , "no" , "0" , "off" ]: 322return True 323 324# If a deterministic lockfile was not added then always repin 325if not lockfile_path : 326return True 327 328# Run the binary to check if a repin is needed 329args = [ 330generator , 331"query" , 332"--lockfile" , 333lockfile_path , 334"--config" , 335config , 336"--splicing-manifest" , 337splicing_manifest , 338"--cargo" , 339cargo , 340"--rustc" , 341rustc , 342] 343 344env = { 345"CARGO" : str ( cargo ), 346"RUSTC" : str ( rustc ), 347"RUST_BACKTRACE" : "full" , 348} 349 350# Add any Cargo environment variables to the `cargo-bazel` execution 351env . update ( cargo_environ ( repository_ctx )) 352 353result = execute ( 354repository_ctx = repository_ctx , 355args = args , 356env = env , 357) 358 359# If it was determined repinning should occur but there was no 360# flag indicating repinning was requested, an error is raised 361# since repinning should be an explicit action 362if result . stdout . strip (). lower () == "repin" : 363# buildifier: disable=print 364( result . stderr ) 365fail (( 366"The current `lockfile` is out of date for '{}'. Please re-run " + 367"bazel using `CARGO_BAZEL_REPIN=true` if this is expected " + 368"and the lockfile should be updated." 369). format ( repository_ctx . name )) 370 371return False 372 373def execute_generator ( 374repository_ctx , 375lockfile_path , 376cargo_lockfile_path , 377generator , 378config , 379splicing_manifest , 380repository_dir , 381cargo , 382rustc , 383metadata = None ): 384"""Execute the `cargo-bazel` binary to produce `BUILD` and `.bzl` files. 385 386Args: 387repository_ctx (repository_ctx): The rule's context object. 388lockfile_path (path): The path to a "lock" file (file used for reproducible renderings). 389cargo_lockfile_path (path): The path to a "Cargo.lock" file within the root workspace. 390generator (path): The path to a `cargo-bazel` binary. 391config (path): The path to a `cargo-bazel` config file. 392splicing_manifest (path): The path to a `cargo-bazel` splicing manifest. See `create_splicing_manifest` 393repository_dir (path): The output path for the Bazel module and BUILD files. 394cargo (path): The path of a Cargo binary. 395rustc (path): The path of a Rustc binary. 396metadata (path, optional): The path to a Cargo metadata json file. If this is set, it indicates to 397the generator that repinning is required. This file must be adjacent to a `Cargo.toml` and 398`Cargo.lock` file. 399 400Returns: 401struct: The results of `repository_ctx.execute`. 402""" 403repository_ctx . report_progress ( "Generating crate BUILD files." ) 404 405args = [ 406generator , 407"generate" , 408"--cargo-lockfile" , 409cargo_lockfile_path , 410"--config" , 411config , 412"--splicing-manifest" , 413splicing_manifest , 414"--repository-dir" , 415repository_dir , 416"--cargo" , 417cargo , 418"--rustc" , 419rustc , 420] 421 422if lockfile_path : 423args . extend ([ 424"--lockfile" , 425lockfile_path , 426]) 427 428env = { 429"RUST_BACKTRACE" : "full" , 430} 431 432# Some components are not required unless re-pinning is enabled 433if metadata : 434args . extend ([ 435"--repin" , 436"--metadata" , 437metadata , 438]) 439env . update ({ 440"CARGO" : str ( cargo ), 441"RUSTC" : str ( rustc ), 442}) 443 444# Add any Cargo environment variables to the `cargo-bazel` execution 445env . update ( cargo_environ ( repository_ctx )) 446 447result = execute ( 448repository_ctx = repository_ctx , 449args = args , 450env = env , 451) 452 453return result