fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
adf92b15
0.7.0
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" , "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 15eval exec env - BAZEL_REAL="${{BAZEL_REAL}}" BUILD_WORKSPACE_DIRECTORY="${{BUILD_WORKSPACE_DIRECTORY}}" {env} \\ 16"{bin}" { args } "$@" 17""" 18 19_WINDOWS_WRAPPER = """ \ 20@ECHO OFF 21set RUNTIME_PWD =% CD % 22{ env } 23 24call { bin } { args } % @% 25""" 26 27CARGO_BAZEL_GENERATOR_PATH = "CARGO_BAZEL_GENERATOR_PATH" 28 29def _runfiles_path(path, is_windows): 30if is_windows: 31runtime_pwd_var = "%RUNTIME_PWD%" 32else: 33runtime_pwd_var = "${RUNTIME_PWD}" 34if path.startswith("../"): 35return "{}/external/{}".format(runtime_pwd_var, path[len("../"):]) 36return "{}/{}".format(runtime_pwd_var, path) 37 38def _is_windows(ctx): 39toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain")] 40return "windows" in toolchain.target_triple 41 42def _get_output_package(ctx): 43# Determine output directory 44if ctx.attr.vendor_path.startswith("/"): 45output = ctx.attr.vendor_path 46else: 47output = "{}/{}".format( 48ctx.label.package, 49ctx.attr.vendor_path, 50) 51return output 52 53def _write_data_file(ctx, name, data): 54file = ctx.actions.declare_file("{}.{}".format(ctx.label.name, name)) 55ctx.actions.write( 56output = file, 57content = data, 58) 59return file 60 61def _prepare_manifest_path(target): 62""" Generate manifest paths that are resolvable by `cargo_bazel::SplicingManifest::resolve` 63 64Args : 65target ( Target ): A `crate_vendor.manifest` target 66 67Returns : 68str : A string representing the path to a manifest . 69 """ 70 files = target[DefaultInfo].files.to_list() 71 if len(files) != 1: 72 fail("The manifest {} hand an unexpected number of files: {}".format( 73 target.label, 74 files, 75 )) 76 77 manifest = files[0] 78 79 if target.label.workspace_root.startswith("external"): 80 # The short path of an external file is expected to start with `../` 81 if not manifest.short_path.startswith("../"): 82 fail("Unexpected shortpath for {}: {}".format( 83 manifest, 84 manifest.short_path, 85 )) 86 return manifest.short_path.replace("../", "${output_base}/external/", 1) 87 88 return "${build_workspace_directory}/" + manifest.short_path 89 90def _write_splicing_manifest(ctx): 91 # Deserialize information about direct packges 92 direct_packages_info = { 93 # Ensure the data is using kebab-case as that's what `cargo_toml::DependencyDetail` expects. 94 pkg: kebab_case_keys(dict(json.decode(data))) 95 for (pkg, data) in ctx.attr.packages.items() 96 } 97 98 # Manifests are required to be single files 99 manifests = {_prepare_manifest_path(m): str(m.label) for m in ctx.attr.manifests} 100 101 config = json.decode(ctx.attr.splicing_config or splicing_config()) 102 splicing_manifest_content = { 103 "cargo_config": _prepare_manifest_path(ctx.attr.cargo_config) if ctx.attr.cargo_config else None, 104 "direct_packages": direct_packages_info, 105 "manifests": manifests, 106 } 107 108 manifest = _write_data_file( 109 ctx = ctx, 110 name = "cargo-bazel-splicing-manifest.json", 111 data = json.encode_indent( 112 dict(dict(config).items() + splicing_manifest_content.items()), 113 indent = " " * 4, 114 ), 115 ) 116 117 is_windows = _is_windows(ctx) 118 119 args = ["--splicing-manifest", _runfiles_path(manifest.short_path, is_windows)] 120 runfiles = [manifest] + ctx.files.manifests + ([ctx.file.cargo_config] if ctx.attr.cargo_config else []) 121 return args, runfiles 122 123def _write_config_file(ctx): 124 rendering_config = dict(json.decode(render_config())) 125 126 output_pkg = _get_output_package(ctx) 127 128 if ctx.attr.mode == "local": 129 build_file_base_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel" 130 crate_label_template = "//{}/{{name}}-{{version}}:{{target}}".format( 131 output_pkg, 132 ) 133 else: 134 build_file_base_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel" 135 crate_label_template = rendering_config["crate_label_template"] 136 137 rendering_config.update({ 138 "build_file_template": build_file_base_template.format( 139 ctx.workspace_name, 140 output_pkg, 141 ), 142 "crate_label_template": crate_label_template, 143 "crates_module_template": "@{}//{}:{{file}}".format( 144 ctx.workspace_name, 145 output_pkg, 146 ), 147 "vendor_mode": ctx.attr.mode, 148 }) 149 150 config_data = compile_config( 151 crate_annotations = ctx.attr.annotations, 152 generate_build_scripts = ctx.attr.generate_build_scripts, 153 cargo_config = None, 154 render_config = rendering_config, 155 supported_platform_triples = ctx.attr.supported_platform_triples, 156 repository_name = ctx.attr.repository_name or ctx.label.name, 157 ) 158 159 config = _write_data_file( 160 ctx = ctx, 161 name = "cargo-bazel-config.json", 162 data = json.encode_indent( 163 config_data, 164 indent = " " * 4, 165 ), 166 ) 167 168 is_windows = _is_windows(ctx) 169 args = ["--config", _runfiles_path(config.short_path, is_windows)] 170 runfiles = [config] + ctx.files.manifests 171 return args, runfiles 172 173def _crates_vendor_impl(ctx): 174 toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain")] 175 is_windows = _is_windows(ctx) 176 177 environ = { 178 "CARGO": _runfiles_path(toolchain.cargo.short_path, is_windows), 179 "RUSTC": _runfiles_path(toolchain.rustc.short_path, is_windows), 180 } 181 182 args = ["vendor"] 183 184 cargo_bazel_runfiles = [] 185 186 # Allow action envs to override the use of the cargo-bazel target. 187 if CARGO_BAZEL_GENERATOR_PATH in ctx.var: 188 bin_path = ctx.var[CARGO_BAZEL_GENERATOR_PATH] 189 elif ctx.executable.cargo_bazel: 190 bin_path = _runfiles_path(ctx.executable.cargo_bazel.short_path, is_windows) 191 cargo_bazel_runfiles.append(ctx.executable.cargo_bazel) 192 else: 193 fail("{} is missing either the `cargo_bazel` attribute or the '{}' action env".format( 194 ctx.label, 195 CARGO_BAZEL_GENERATOR_PATH, 196 )) 197 198 # Generate config file 199 config_args, config_runfiles = _write_config_file(ctx) 200 args.extend(config_args) 201 cargo_bazel_runfiles.extend(config_runfiles) 202 203 # Generate splicing manifest 204 splicing_manifest_args, splicing_manifest_runfiles = _write_splicing_manifest(ctx) 205 args.extend(splicing_manifest_args) 206 cargo_bazel_runfiles.extend(splicing_manifest_runfiles) 207 208 # Add an optional `Cargo.lock` file. 209 if ctx.attr.cargo_lockfile: 210 args.extend([ 211 "--cargo-lockfile", 212 _runfiles_path(ctx.file.cargo_lockfile.short_path, is_windows), 213 ]) 214 cargo_bazel_runfiles.extend([ctx.file.cargo_lockfile]) 215 216 # Optionally include buildifier 217 if ctx.attr.buildifier: 218 args.extend(["--buildifier", _runfiles_path(ctx.executable.buildifier.short_path, is_windows)]) 219 cargo_bazel_runfiles.append(ctx.executable.buildifier) 220 221 # Dtermine platform specific settings 222 if is_windows: 223 extension = ".bat" 224 template = _WINDOWS_WRAPPER 225 env_template = "\nset {} = {} " 226else : 227extension = ".sh" 228template = _UNIX_WRAPPER 229env_template = "{}={}" 230 231# Write the wrapper script 232runner = ctx . actions . declare_file ( ctx . label . name + extension ) 233ctx . actions . write ( 234output = runner , 235content = template . format ( 236env = " " . join ([ env_template . format ( key , val ) for key , val in environ . items ()]), 237bin = bin_path , 238args = " " . join ( args ), 239), 240is_executable = True , 241) 242 243return DefaultInfo ( 244files = depset ([ runner ]), 245runfiles = ctx . runfiles ( 246files = cargo_bazel_runfiles , 247transitive_files = toolchain . all_files , 248), 249executable = runner , 250) 251 252crates_vendor = rule ( 253implementation = _crates_vendor_impl , 254doc = """\ 255A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. 256This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the 257rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule 258handles all the same [workflows](#workflows) `crate_universe` rules do. 259 260Example: 261 262Given the following workspace structure: 263 264```text 265[workspace]/ 266WORKSPACE 267BUILD 268Cargo.toml 2693rdparty/ 270BUILD 271src/ 272main.rs 273``` 274 275The following is something that'd be found in `3rdparty/BUILD`: 276 277```python 278load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate") 279 280crates_vendor( 281name = "crates_vendor", 282annotations = { 283"rand": [crate.annotation( 284default_features = False, 285features = ["small_rng"], 286)], 287}, 288cargo_lockfile = "//:Cargo.Bazel.lock", 289manifests = ["//:Cargo.toml"], 290mode = "remote", 291vendor_path = "crates", 292tags = ["manual"], 293) 294``` 295 296The above creates a target that can be run to write `BUILD` files into the `3rdparty` 297directory next to where the target is defined. To run it, simply call: 298 299```shell 300bazel run //3rdparty:crates_vendor 301``` 302 303<a id=" #crates_vendor_repinning_updating_dependencies"></a> 304 305### Repinning / Updating Dependencies 306 307Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin` 308flag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation. 309 310```shell 311bazel run //3rdparty:crates_vendor -- --repin 312``` 313 314Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) 315call against the generated workspace. The following table describes how to controll particular values passed to the 316`cargo update` command. 317 318| Value | Cargo command | 319| --- | --- | 320| Any of [`true`, `1`, `yes`, `on`] | `cargo update` | 321| `workspace` | `cargo update --workspace` | 322| `package_name` | `cargo upgrade --package package_name` | 323| `package_name@1.2.3` | `cargo upgrade --package package_name --precise 1.2.3` | 324 325""" , 326attrs = { 327"annotations" : attr . string_list_dict ( 328doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation)." , 329), 330"buildifier" : attr . label ( 331doc = "The path to a [buildifier](https://github.com/bazelbuild/buildtools/blob/5.0.1/buildifier/README.md) binary used to format generated BUILD files." , 332cfg = "exec" , 333executable = True , 334default = Label ( "//crate_universe/private/vendor:buildifier" ), 335), 336"cargo_bazel" : attr . label ( 337doc = ( 338"The cargo-bazel binary to use for vendoring. If this attribute is not set, then a " + 339"`{}` action env will be used." . format ( CARGO_BAZEL_GENERATOR_PATH ) 340), 341cfg = "exec" , 342executable = True , 343allow_files = True , 344default = CARGO_BAZEL_LABEL , 345), 346"cargo_config" : attr . label ( 347doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file." , 348allow_single_file = True , 349), 350"cargo_lockfile" : attr . label ( 351doc = "The path to an existing `Cargo.lock` file" , 352allow_single_file = True , 353), 354"generate_build_scripts" : attr . bool ( 355doc = ( 356"Whether or not to generate " + 357"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default." 358), 359default = True , 360), 361"manifests" : attr . label_list ( 362doc = "A list of Cargo manifests (`Cargo.toml` files)." , 363allow_files = [ "Cargo.toml" ], 364), 365"mode" : attr . string ( 366doc = ( 367"Flags determining how crates should be vendored. `local` is where crate source and BUILD files are " + 368"written to the repository. `remote` is where only BUILD files are written and repository rules " + 369"used to fetch source code." 370), 371values = [ 372"local" , 373"remote" , 374], 375default = "remote" , 376), 377"packages" : attr . string_dict ( 378doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec)." , 379), 380"repository_name" : attr . string ( 381doc = "The name of the repository to generate for `remote` vendor modes. If unset, the label name will be used" , 382), 383"splicing_config" : attr . string ( 384doc = ( 385 "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\% rsplicing_config `to "+ 386"generate the value for this field. If unset, the defaults defined there will be used." 387), 388), 389"supported_platform_triples" : attr . string_list ( 390doc = "A set of all platform triples to consider when generating dependencies." , 391default = SUPPORTED_PLATFORM_TRIPLES , 392), 393"vendor_path" : attr . string ( 394doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root" , 395default = "crates" , 396), 397}, 398executable = True , 399toolchains = [ "@rules_rust//rust:toolchain" ], 400) 401 402def _crates_vendor_remote_repository_impl ( repository_ctx ): 403build_file = repository_ctx . path ( repository_ctx . attr . build_file ) 404defs_module = repository_ctx . path ( repository_ctx . attr . defs_module ) 405 406repository_ctx . file ( "BUILD.bazel" , repository_ctx . read ( build_file )) 407repository_ctx . file ( "defs.bzl" , repository_ctx . read ( defs_module )) 408repository_ctx . file ( "crates.bzl" , "" ) 409repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 410repository_ctx . name , 411)) 412 413crates_vendor_remote_repository = repository_rule ( 414doc = "Creates a repository paired with `crates_vendor` targets using the `remote` vendor mode." , 415implementation = _crates_vendor_remote_repository_impl , 416attrs = { 417"build_file" : attr . label ( 418doc = "The BUILD file to use for the root package" , 419mandatory = True , 420), 421"defs_module" : attr . label ( 422doc = "The `defs.bzl` file to use in the repository" , 423mandatory = True , 424), 425}, 426)