fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
c77dd8d5
patching
1"""Rules for vendoring Bazel targets into existing workspaces""" 2 3load ( "//crate_universe/private:generate_utils.bzl" , "compile_config" , "render_config" ) 4load ( "//crate_universe/private:splicing_utils.bzl" , "kebab_case_keys" , generate_splicing_config = "splicing_config" ) 5load ( "//crate_universe/private:urls.bzl" , "CARGO_BAZEL_LABEL" ) 6load ( "//rust/platform:triple_mappings.bzl" , "SUPPORTED_PLATFORM_TRIPLES" ) 7 8_UNIX_WRAPPER = """\ 9#!/usr/bin/env bash 10set -euo pipefail 11export RUNTIME_PWD="$(pwd)" 12if [[ -z "${{BAZEL_REAL:-}}" ]]; then 13 BAZEL_REAL="$(which bazel || echo 'bazel')" 14fi 15 16# The path needs to be preserved to prevent bazel from starting with different 17# startup options (requiring a restart of bazel). 18# If you provide an empty path, bazel starts itself with 19# --default_system_javabase set to the empty string, but if you provide a path, 20# it may set it to a value (eg. "/usr/local/buildtools/java/jdk11"). 21exec env - BAZEL_REAL="${{BAZEL_REAL}}" BUILD_WORKSPACE_DIRECTORY="${{BUILD_WORKSPACE_DIRECTORY}}" PATH="${{PATH}}" {env} \\ 22"{bin}" { args } "$@" 23""" 24 25_WINDOWS_WRAPPER = """ \ 26@ECHO OFF 27set RUNTIME_PWD =% CD % 28{ env } 29 30call { bin } { args } % @% 31""" 32 33CARGO_BAZEL_GENERATOR_PATH = "CARGO_BAZEL_GENERATOR_PATH" 34 35def _default_render_config(): 36return json.decode(render_config()) 37 38def _runfiles_path(file, is_windows): 39if is_windows: 40runtime_pwd_var = "%RUNTIME_PWD%" 41else: 42runtime_pwd_var = "${RUNTIME_PWD}" 43 44return "{}/{}".format(runtime_pwd_var, file.short_path) 45 46def _is_windows(ctx): 47toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain_type")] 48return toolchain.target_os == "windows" 49 50def _get_output_package(ctx): 51# Determine output directory 52if ctx.attr.vendor_path.startswith("/"): 53output = ctx.attr.vendor_path 54else: 55output = "{}/{}".format( 56ctx.label.package, 57ctx.attr.vendor_path, 58) 59return output.lstrip("/") 60 61def _write_data_file(ctx, name, data): 62file = ctx.actions.declare_file("{}.{}".format(ctx.label.name, name)) 63ctx.actions.write( 64output = file, 65content = data, 66) 67return file 68 69def _prepare_manifest_path(target): 70""" Generate manifest paths that are resolvable by `cargo_bazel::SplicingManifest::resolve` 71 72Args : 73target ( Target ): A `crate_vendor.manifest` target 74 75Returns : 76str : A string representing the path to a manifest . 77""" 78files = target[DefaultInfo].files.to_list() 79if len(files) != 1: 80fail("The manifest {} hand an unexpected number of files: {}".format( 81target.label, 82files, 83)) 84 85manifest = files[0] 86 87if target.label.workspace_root.startswith("external"): 88# The short path of an external file is expected to start with `../` 89if not manifest.short_path.startswith("../"): 90fail("Unexpected shortpath for {}: {}".format( 91manifest, 92manifest.short_path, 93)) 94return manifest.short_path.replace("../", "${output_base}/external/", 1) 95 96return "${build_workspace_directory}/" + manifest.short_path 97 98def _write_splicing_manifest(ctx): 99# Manifests are required to be single files 100manifests = {_prepare_manifest_path(m): str(m.label) for m in ctx.attr.manifests} 101 102manifest = _write_data_file( 103ctx = ctx, 104name = "cargo-bazel-splicing-manifest.json", 105data = generate_splicing_manifest( 106packages = ctx.attr.packages, 107splicing_config = ctx.attr.splicing_config, 108cargo_config = ctx.attr.cargo_config, 109manifests = manifests, 110manifest_to_path = _prepare_manifest_path, 111), 112) 113 114is_windows = _is_windows(ctx) 115 116args = ["--splicing-manifest", _runfiles_path(manifest, is_windows)] 117runfiles = [manifest] + ctx.files.manifests + ([ctx.file.cargo_config] if ctx.attr.cargo_config else []) 118return args, runfiles 119 120def generate_splicing_manifest(packages, splicing_config, cargo_config, manifests, manifest_to_path): 121# Deserialize information about direct packages 122direct_packages_info = { 123# Ensure the data is using kebab-case as that's what `cargo_toml::DependencyDetail` expects. 124pkg: kebab_case_keys(dict(json.decode(data))) 125for (pkg, data) in packages.items() 126} 127 128config = json.decode(splicing_config or generate_splicing_config()) 129splicing_manifest_content = { 130"cargo_config": str(manifest_to_path(cargo_config)) if cargo_config else None, 131"direct_packages": direct_packages_info, 132"manifests": manifests, 133} 134 135return json.encode_indent( 136dict(dict(config).items() + splicing_manifest_content.items()), 137indent = " " * 4, 138) 139 140def _write_config_file(ctx): 141workspace_name = ctx.workspace_name 142if workspace_name == "__main__" or ctx.workspace_name == "_main": 143workspace_name = "" 144 145config = _write_data_file( 146ctx = ctx, 147name = "cargo-bazel-config.json", 148data = generate_config_file( 149ctx, 150mode = ctx.attr.mode, 151annotations = ctx.attr.annotations, 152generate_binaries = ctx.attr.generate_binaries, 153generate_build_scripts = ctx.attr.generate_build_scripts, 154generate_target_compatible_with = ctx.attr.generate_target_compatible_with, 155supported_platform_triples = ctx.attr.supported_platform_triples, 156repository_name = ctx.attr.repository_name, 157output_pkg = _get_output_package(ctx), 158workspace_name = workspace_name, 159render_config = dict(json.decode(ctx.attr.render_config)) if ctx.attr.render_config else None, 160), 161) 162 163is_windows = _is_windows(ctx) 164args = ["--config", _runfiles_path(config, is_windows)] 165runfiles = [config] + ctx.files.manifests 166return args, runfiles 167 168def generate_config_file( 169ctx, 170mode, 171annotations, 172generate_binaries, 173generate_build_scripts, 174generate_target_compatible_with, 175supported_platform_triples, 176repository_name, 177output_pkg, 178workspace_name, 179render_config, 180repository_ctx = None): 181""" Writes the rendering config to cargo - bazel - config . json . 182 183Args : 184ctx : The rule 's context . 185mode ( str ): The vendoring mode . 186annotations : Any annotations provided . 187generate_binaries ( bool ): Whether to generate binaries for the crates . 188generate_build_scripts ( bool ): Whether to generate BUILD . bazel files . 189generate_target_compatible_with ( bool ): DEPRECATED : Moved to `render_config` . 190supported_platform_triples ( str ): The platform triples to support in 191the generated BUILD . bazel files . 192repository_name ( str ): The name of the repository to generate . 193output_pkg : The path to the package containing the build files . 194workspace_name ( str ): The name of the workspace . 195render_config : The render config to use . 196repository_ctx ( repository_ctx , optional ): A repository context object 197used for enabling certain functionality . 198 199Returns : 200file : The cargo - bazel - config . json written . 201 """ 202 default_render_config = _default_render_config() 203 if render_config == None: 204 render_config = default_render_config 205 206 if mode == "local": 207 build_file_base_template = "//{}/{{name}}-{{version}}:BUILD.bazel".format(output_pkg) 208 if workspace_name != "": 209 build_file_base_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel".format(workspace_name, output_pkg) 210 crate_label_template = "//{}/{{name}}-{{version}}:{{target}}".format( 211 output_pkg, 212 ) 213 else: 214 build_file_base_template = "//{}:BUILD.{{name}}-{{version}}.bazel".format(output_pkg) 215 if workspace_name != "": 216 build_file_base_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel".format(workspace_name, output_pkg) 217 crate_label_template = render_config["crate_label_template"] 218 219 # If `workspace_name` is blank (such as when using modules), the `@{}//{}:{{file}}` template would generate 220 # a reference like `Label(@//<stuff>)`. This causes issues if the module doing the `crates_vendor`ing is not the root module. 221 # See: https://github.com/bazelbuild/rules_rust/issues/2661 222 crates_module_template_value = "//{}:{{file}}".format(output_pkg) 223 if workspace_name != "": 224 crates_module_template_value = "@{}//{}:{{file}}".format( 225 workspace_name, 226 output_pkg, 227 ) 228 229 updates = { 230 "build_file_template": build_file_base_template, 231 "crate_label_template": crate_label_template, 232 "crates_module_template": crates_module_template_value, 233 "vendor_mode": mode, 234 } 235 236 # "crate_label_template" is explicitly supported above in non-local modes 237 excluded_from_key_check = ["crate_label_template"] 238 239 for key in updates: 240 if (render_config[key] != default_render_config[key]) and key not in excluded_from_key_check: 241 fail("The `crates_vendor.render_config` attribute does not support the `{}` parameter. Please update {} to remove this value.".format( 242 key, 243 ctx.label, 244 )) 245 246 render_config.update(updates) 247 248 # Allow users to override the regen command. 249 if "regen_command" not in render_config or not render_config["regen_command"]: 250 render_config.update({"regen_command": "bazel run {}".format(ctx.label)}) 251 252 config_data = compile_config( 253 crate_annotations = annotations, 254 generate_binaries = generate_binaries, 255 generate_build_scripts = generate_build_scripts, 256 generate_target_compatible_with = generate_target_compatible_with, 257 cargo_config = None, 258 render_config = render_config, 259 supported_platform_triples = supported_platform_triples, 260 repository_name = repository_name or ctx.label.name, 261 repository_ctx = repository_ctx, 262 ) 263 264 return json.encode_indent( 265 config_data, 266 indent = " " * 4, 267 ) 268 269def _crates_vendor_impl(ctx): 270 toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain_type")] 271 is_windows = _is_windows(ctx) 272 273 environ = { 274 "CARGO": _runfiles_path(toolchain.cargo, is_windows), 275 "RUSTC": _runfiles_path(toolchain.rustc, is_windows), 276 } 277 278 args = ["vendor"] 279 280 cargo_bazel_runfiles = [] 281 282 # Allow action envs to override the use of the cargo-bazel target. 283 if CARGO_BAZEL_GENERATOR_PATH in ctx.var: 284 bin_path = ctx.var[CARGO_BAZEL_GENERATOR_PATH] 285 elif ctx.executable.cargo_bazel: 286 bin_path = _runfiles_path(ctx.executable.cargo_bazel, is_windows) 287 cargo_bazel_runfiles.append(ctx.executable.cargo_bazel) 288 else: 289 fail("{} is missing either the `cargo_bazel` attribute or the '{}' action env".format( 290 ctx.label, 291 CARGO_BAZEL_GENERATOR_PATH, 292 )) 293 294 # Generate config file 295 config_args, config_runfiles = _write_config_file(ctx) 296 args.extend(config_args) 297 cargo_bazel_runfiles.extend(config_runfiles) 298 299 # Generate splicing manifest 300 splicing_manifest_args, splicing_manifest_runfiles = _write_splicing_manifest(ctx) 301 args.extend(splicing_manifest_args) 302 cargo_bazel_runfiles.extend(splicing_manifest_runfiles) 303 304 # Add an optional `Cargo.lock` file. 305 if ctx.attr.cargo_lockfile: 306 args.extend([ 307 "--cargo-lockfile", 308 _runfiles_path(ctx.file.cargo_lockfile, is_windows), 309 ]) 310 cargo_bazel_runfiles.extend([ctx.file.cargo_lockfile]) 311 312 # Optionally include buildifier 313 if ctx.attr.buildifier: 314 args.extend(["--buildifier", _runfiles_path(ctx.executable.buildifier, is_windows)]) 315 cargo_bazel_runfiles.append(ctx.executable.buildifier) 316 317 # Optionally include an explicit `bazel` path 318 if ctx.attr.bazel: 319 args.extend(["--bazel", _runfiles_path(ctx.executable.bazel, is_windows)]) 320 cargo_bazel_runfiles.append(ctx.executable.bazel) 321 322 # Determine platform specific settings 323 if is_windows: 324 extension = ".bat" 325 template = _WINDOWS_WRAPPER 326 env_template = "\nset {} = {} " 327else : 328extension = ".sh" 329template = _UNIX_WRAPPER 330env_template = "{}={}" 331 332# Write the wrapper script 333runner = ctx . actions . declare_file ( ctx . label . name + extension ) 334ctx . actions . write ( 335output = runner , 336content = template . format ( 337env = " " . join ([ env_template . format ( key , val ) for key , val in environ . items ()]), 338bin = bin_path , 339args = " " . join ( args ), 340), 341is_executable = True , 342) 343 344return DefaultInfo ( 345files = depset ([ runner ]), 346runfiles = ctx . runfiles ( 347files = cargo_bazel_runfiles , 348transitive_files = toolchain . all_files , 349), 350executable = runner , 351) 352 353CRATES_VENDOR_ATTRS = { 354 "annotations ": attr . string_list_dict ( 355doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation)." , 356), 357"bazel" : attr . label ( 358doc = "The path to a bazel binary used to locate the output_base for the current workspace." , 359cfg = "exec" , 360executable = True , 361allow_files = True , 362), 363"buildifier" : attr . label ( 364doc = "The path to a [buildifier](https://github.com/bazelbuild/buildtools/blob/5.0.1/buildifier/README.md) binary used to format generated BUILD files." , 365cfg = "exec" , 366executable = True , 367allow_files = True , 368default = Label ( "//crate_universe/private/vendor:buildifier" ), 369), 370"cargo_bazel" : attr . label ( 371doc = ( 372"The cargo-bazel binary to use for vendoring. If this attribute is not set, then a " + 373"`{}` action env will be used." . format ( CARGO_BAZEL_GENERATOR_PATH ) 374), 375cfg = "exec" , 376executable = True , 377allow_files = True , 378default = CARGO_BAZEL_LABEL , 379), 380"cargo_config" : attr . label ( 381doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file." , 382allow_single_file = True , 383), 384"cargo_lockfile" : attr . label ( 385doc = "The path to an existing `Cargo.lock` file" , 386allow_single_file = True , 387), 388"generate_binaries" : attr . bool ( 389doc = ( 390"Whether to generate `rust_binary` targets for all the binary crates in every package. " + 391"By default only the `rust_library` targets are generated." 392), 393default = False , 394), 395"generate_build_scripts" : attr . bool ( 396doc = ( 397"Whether or not to generate " + 398"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default." 399), 400default = True , 401), 402"generate_target_compatible_with" : attr . bool ( 403doc = "DEPRECATED: Moved to `render_config`." , 404default = True , 405), 406"manifests" : attr . label_list ( 407doc = "A list of Cargo manifests (`Cargo.toml` files)." , 408allow_files = [ "Cargo.toml" ], 409), 410"mode" : attr . string ( 411doc = ( 412"Flags determining how crates should be vendored. `local` is where crate source and BUILD files are " + 413"written to the repository. `remote` is where only BUILD files are written and repository rules " + 414"used to fetch source code." 415), 416values = [ 417"local" , 418"remote" , 419], 420default = "remote" , 421), 422"packages" : attr . string_dict ( 423doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec)." , 424), 425"render_config" : attr . string ( 426doc = ( 427 "The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\\% render_config `to "+ 428"generate the value for this field. If unset, the defaults defined there will be used." 429), 430), 431"repository_name" : attr . string ( 432doc = "The name of the repository to generate for `remote` vendor modes. If unset, the label name will be used" , 433), 434"splicing_config" : attr . string ( 435doc = ( 436 "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\% rsplicing_config `to "+ 437"generate the value for this field. If unset, the defaults defined there will be used." 438), 439), 440"supported_platform_triples" : attr . string_list ( 441doc = "A set of all platform triples to consider when generating dependencies." , 442default = SUPPORTED_PLATFORM_TRIPLES , 443), 444"vendor_path" : attr . string ( 445doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root" , 446default = "crates" , 447), 448} 449 450crates_vendor = rule ( 451implementation = _crates_vendor_impl , 452doc = """\ 453A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. 454This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the 455rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule 456handles all the same [workflows](#workflows) `crate_universe` rules do. 457 458Example: 459 460Given the following workspace structure: 461 462```text 463[workspace]/ 464WORKSPACE 465BUILD 466Cargo.toml 4673rdparty/ 468BUILD 469src/ 470main.rs 471``` 472 473The following is something that'd be found in `3rdparty/BUILD`: 474 475```python 476load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate") 477 478crates_vendor( 479name = "crates_vendor", 480annotations = { 481"rand": [crate.annotation( 482default_features = False, 483features = ["small_rng"], 484)], 485}, 486cargo_lockfile = "//:Cargo.Bazel.lock", 487manifests = ["//:Cargo.toml"], 488mode = "remote", 489vendor_path = "crates", 490tags = ["manual"], 491) 492``` 493 494The above creates a target that can be run to write `BUILD` files into the `3rdparty` 495directory next to where the target is defined. To run it, simply call: 496 497```shell 498bazel run //3rdparty:crates_vendor 499``` 500 501### Patching 502 503If you need to replace crates in your third_party dependency tree with forks, the `patch` section can be used in your cargo manifest: 504 505```toml 506[patch.crates-io] 507fuse3 = { path = "forks/fuse3" } 508``` 509 510A build file for the patched crate will still be generated under the vendor path 511(such as `crates/fuse3-0.6.1/BUILD.bazel`), but `cargo vendor` will NOT download the 512crate to that location, and the generated BUILD file will point to your copy. For this 513to work, you need to define the following filegroups in your patch crate's BUILD.bazel: 514`:srcs`, `:crate_root`, `:compile_data`, and (if the crate has a build script) 515`:build_script_crate_root`. See the 516[patching example](https://github.com/bazelbuild/rules_rust/tree/main/examples/crate_universe/vendor_local_patching) 517for an example of what those BUILD files look like. 518 519This feature can also be used to avoid vendoring source code for crates that you never 520actually use for the platforms you're targeting. In that case you don't need an extra 521BUILD file, just crate a empty/stub Cargo.toml and `lib.rs`, and list the path to that 522in your `patch` section. 523 524<a id=" #crates_vendor_repinning_updating_dependencies"></a> 525 526### Repinning / Updating Dependencies 527 528Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin` 529flag to the `crates_vendor` binary. To update dependencies, simply add the flag to your `bazel run` invocation. 530 531```shell 532bazel run //3rdparty:crates_vendor -- --repin 533``` 534 535Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) 536call against the generated workspace. The following table describes how to control particular values passed to the 537`cargo update` command. 538 539| Value | Cargo command | 540| --- | --- | 541| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` | 542| Any of [`full`, `eager`, `all`] | `cargo update` | 543| `package_name` | `cargo upgrade --package package_name` | 544| `package_name@1.2.3` | `cargo upgrade --package package_name --precise 1.2.3` | 545 546""" , 547attrs = CRATES_VENDOR_ATTRS , 548executable = True , 549toolchains = [ "@rules_rust//rust:toolchain_type" ], 550) 551 552def _crates_vendor_remote_repository_impl ( repository_ctx ): 553build_file = repository_ctx . path ( repository_ctx . attr . build_file ) 554defs_module = repository_ctx . path ( repository_ctx . attr . defs_module ) 555 556repository_ctx . file ( "BUILD.bazel" , repository_ctx . read ( build_file )) 557repository_ctx . file ( "defs.bzl" , repository_ctx . read ( defs_module )) 558repository_ctx . file ( "crates.bzl" , "" ) 559repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 560repository_ctx . name , 561)) 562 563crates_vendor_remote_repository = repository_rule ( 564doc = "Creates a repository paired with `crates_vendor` targets using the `remote` vendor mode." , 565implementation = _crates_vendor_remote_repository_impl , 566attrs = { 567"build_file" : attr . label ( 568doc = "The BUILD file to use for the root package" , 569mandatory = True , 570), 571"defs_module" : attr . label ( 572doc = "The `defs.bzl` file to use in the repository" , 573mandatory = True , 574), 575}, 576)