fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
0fc834bd
0.9.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_type")] 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 regen_command = "bazel run {}".format( 126 ctx.label, 127 ), 128 ))) 129 130 output_pkg = _get_output_package(ctx) 131 132 if ctx.attr.mode == "local": 133 build_file_base_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel" 134 crate_label_template = "//{}/{{name}}-{{version}}:{{target}}".format( 135 output_pkg, 136 ) 137 else: 138 build_file_base_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel" 139 crate_label_template = rendering_config["crate_label_template"] 140 141 rendering_config.update({ 142 "build_file_template": build_file_base_template.format( 143 ctx.workspace_name, 144 output_pkg, 145 ), 146 "crate_label_template": crate_label_template, 147 "crates_module_template": "@{}//{}:{{file}}".format( 148 ctx.workspace_name, 149 output_pkg, 150 ), 151 "vendor_mode": ctx.attr.mode, 152 }) 153 154 config_data = compile_config( 155 crate_annotations = ctx.attr.annotations, 156 generate_build_scripts = ctx.attr.generate_build_scripts, 157 cargo_config = None, 158 render_config = rendering_config, 159 supported_platform_triples = ctx.attr.supported_platform_triples, 160 repository_name = ctx.attr.repository_name or ctx.label.name, 161 ) 162 163 config = _write_data_file( 164 ctx = ctx, 165 name = "cargo-bazel-config.json", 166 data = json.encode_indent( 167 config_data, 168 indent = " " * 4, 169 ), 170 ) 171 172 is_windows = _is_windows(ctx) 173 args = ["--config", _runfiles_path(config.short_path, is_windows)] 174 runfiles = [config] + ctx.files.manifests 175 return args, runfiles 176 177def _crates_vendor_impl(ctx): 178 toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain_type")] 179 is_windows = _is_windows(ctx) 180 181 environ = { 182 "CARGO": _runfiles_path(toolchain.cargo.short_path, is_windows), 183 "RUSTC": _runfiles_path(toolchain.rustc.short_path, is_windows), 184 } 185 186 args = ["vendor"] 187 188 cargo_bazel_runfiles = [] 189 190 # Allow action envs to override the use of the cargo-bazel target. 191 if CARGO_BAZEL_GENERATOR_PATH in ctx.var: 192 bin_path = ctx.var[CARGO_BAZEL_GENERATOR_PATH] 193 elif ctx.executable.cargo_bazel: 194 bin_path = _runfiles_path(ctx.executable.cargo_bazel.short_path, is_windows) 195 cargo_bazel_runfiles.append(ctx.executable.cargo_bazel) 196 else: 197 fail("{} is missing either the `cargo_bazel` attribute or the '{}' action env".format( 198 ctx.label, 199 CARGO_BAZEL_GENERATOR_PATH, 200 )) 201 202 # Generate config file 203 config_args, config_runfiles = _write_config_file(ctx) 204 args.extend(config_args) 205 cargo_bazel_runfiles.extend(config_runfiles) 206 207 # Generate splicing manifest 208 splicing_manifest_args, splicing_manifest_runfiles = _write_splicing_manifest(ctx) 209 args.extend(splicing_manifest_args) 210 cargo_bazel_runfiles.extend(splicing_manifest_runfiles) 211 212 # Add an optional `Cargo.lock` file. 213 if ctx.attr.cargo_lockfile: 214 args.extend([ 215 "--cargo-lockfile", 216 _runfiles_path(ctx.file.cargo_lockfile.short_path, is_windows), 217 ]) 218 cargo_bazel_runfiles.extend([ctx.file.cargo_lockfile]) 219 220 # Optionally include buildifier 221 if ctx.attr.buildifier: 222 args.extend(["--buildifier", _runfiles_path(ctx.executable.buildifier.short_path, is_windows)]) 223 cargo_bazel_runfiles.append(ctx.executable.buildifier) 224 225 # Dtermine platform specific settings 226 if is_windows: 227 extension = ".bat" 228 template = _WINDOWS_WRAPPER 229 env_template = "\nset {} = {} " 230else : 231extension = ".sh" 232template = _UNIX_WRAPPER 233env_template = "{}={}" 234 235# Write the wrapper script 236runner = ctx . actions . declare_file ( ctx . label . name + extension ) 237ctx . actions . write ( 238output = runner , 239content = template . format ( 240env = " " . join ([ env_template . format ( key , val ) for key , val in environ . items ()]), 241bin = bin_path , 242args = " " . join ( args ), 243), 244is_executable = True , 245) 246 247return DefaultInfo ( 248files = depset ([ runner ]), 249runfiles = ctx . runfiles ( 250files = cargo_bazel_runfiles , 251transitive_files = toolchain . all_files , 252), 253executable = runner , 254) 255 256crates_vendor = rule ( 257implementation = _crates_vendor_impl , 258doc = """\ 259A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. 260This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the 261rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule 262handles all the same [workflows](#workflows) `crate_universe` rules do. 263 264Example: 265 266Given the following workspace structure: 267 268```text 269[workspace]/ 270WORKSPACE 271BUILD 272Cargo.toml 2733rdparty/ 274BUILD 275src/ 276main.rs 277``` 278 279The following is something that'd be found in `3rdparty/BUILD`: 280 281```python 282load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate") 283 284crates_vendor( 285name = "crates_vendor", 286annotations = { 287"rand": [crate.annotation( 288default_features = False, 289features = ["small_rng"], 290)], 291}, 292cargo_lockfile = "//:Cargo.Bazel.lock", 293manifests = ["//:Cargo.toml"], 294mode = "remote", 295vendor_path = "crates", 296tags = ["manual"], 297) 298``` 299 300The above creates a target that can be run to write `BUILD` files into the `3rdparty` 301directory next to where the target is defined. To run it, simply call: 302 303```shell 304bazel run //3rdparty:crates_vendor 305``` 306 307<a id=" #crates_vendor_repinning_updating_dependencies"></a> 308 309### Repinning / Updating Dependencies 310 311Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin` 312flag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation. 313 314```shell 315bazel run //3rdparty:crates_vendor -- --repin 316``` 317 318Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) 319call against the generated workspace. The following table describes how to controll particular values passed to the 320`cargo update` command. 321 322| Value | Cargo command | 323| --- | --- | 324| Any of [`true`, `1`, `yes`, `on`] | `cargo update` | 325| `workspace` | `cargo update --workspace` | 326| `package_name` | `cargo upgrade --package package_name` | 327| `package_name@1.2.3` | `cargo upgrade --package package_name --precise 1.2.3` | 328 329""" , 330attrs = { 331"annotations" : attr . string_list_dict ( 332doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation)." , 333), 334"buildifier" : attr . label ( 335doc = "The path to a [buildifier](https://github.com/bazelbuild/buildtools/blob/5.0.1/buildifier/README.md) binary used to format generated BUILD files." , 336cfg = "exec" , 337executable = True , 338default = Label ( "//crate_universe/private/vendor:buildifier" ), 339), 340"cargo_bazel" : attr . label ( 341doc = ( 342"The cargo-bazel binary to use for vendoring. If this attribute is not set, then a " + 343"`{}` action env will be used." . format ( CARGO_BAZEL_GENERATOR_PATH ) 344), 345cfg = "exec" , 346executable = True , 347allow_files = True , 348default = CARGO_BAZEL_LABEL , 349), 350"cargo_config" : attr . label ( 351doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file." , 352allow_single_file = True , 353), 354"cargo_lockfile" : attr . label ( 355doc = "The path to an existing `Cargo.lock` file" , 356allow_single_file = True , 357), 358"generate_build_scripts" : attr . bool ( 359doc = ( 360"Whether or not to generate " + 361"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default." 362), 363default = True , 364), 365"manifests" : attr . label_list ( 366doc = "A list of Cargo manifests (`Cargo.toml` files)." , 367allow_files = [ "Cargo.toml" ], 368), 369"mode" : attr . string ( 370doc = ( 371"Flags determining how crates should be vendored. `local` is where crate source and BUILD files are " + 372"written to the repository. `remote` is where only BUILD files are written and repository rules " + 373"used to fetch source code." 374), 375values = [ 376"local" , 377"remote" , 378], 379default = "remote" , 380), 381"packages" : attr . string_dict ( 382doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec)." , 383), 384"repository_name" : attr . string ( 385doc = "The name of the repository to generate for `remote` vendor modes. If unset, the label name will be used" , 386), 387"splicing_config" : attr . string ( 388doc = ( 389 "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\% rsplicing_config `to "+ 390"generate the value for this field. If unset, the defaults defined there will be used." 391), 392), 393"supported_platform_triples" : attr . string_list ( 394doc = "A set of all platform triples to consider when generating dependencies." , 395default = SUPPORTED_PLATFORM_TRIPLES , 396), 397"vendor_path" : attr . string ( 398doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root" , 399default = "crates" , 400), 401}, 402executable = True , 403toolchains = [ "@rules_rust//rust:toolchain_type" ], 404) 405 406def _crates_vendor_remote_repository_impl ( repository_ctx ): 407build_file = repository_ctx . path ( repository_ctx . attr . build_file ) 408defs_module = repository_ctx . path ( repository_ctx . attr . defs_module ) 409 410repository_ctx . file ( "BUILD.bazel" , repository_ctx . read ( build_file )) 411repository_ctx . file ( "defs.bzl" , repository_ctx . read ( defs_module )) 412repository_ctx . file ( "crates.bzl" , "" ) 413repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 414repository_ctx . name , 415)) 416 417crates_vendor_remote_repository = repository_rule ( 418doc = "Creates a repository paired with `crates_vendor` targets using the `remote` vendor mode." , 419implementation = _crates_vendor_remote_repository_impl , 420attrs = { 421"build_file" : attr . label ( 422doc = "The BUILD file to use for the root package" , 423mandatory = True , 424), 425"defs_module" : attr . label ( 426doc = "The `defs.bzl` file to use in the repository" , 427mandatory = True , 428), 429}, 430)