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