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