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