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