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