fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
a15e67d6
0.6.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 # Optionally include buildifier 209 if ctx.attr.buildifier: 210 args.extend(["--buildifier", _runfiles_path(ctx.executable.buildifier.short_path, is_windows)]) 211 cargo_bazel_runfiles.append(ctx.executable.buildifier) 212 213 # Dtermine platform specific settings 214 if is_windows: 215 extension = ".bat" 216 template = _WINDOWS_WRAPPER 217 env_template = "\nset {} = {} " 218else : 219extension = ".sh" 220template = _UNIX_WRAPPER 221env_template = "{}={}" 222 223# Write the wrapper script 224runner = ctx . actions . declare_file ( ctx . label . name + extension ) 225ctx . actions . write ( 226output = runner , 227content = template . format ( 228env = " " . join ([ env_template . format ( key , val ) for key , val in environ . items ()]), 229bin = bin_path , 230args = " " . join ( args ), 231), 232is_executable = True , 233) 234 235return DefaultInfo ( 236files = depset ([ runner ]), 237runfiles = ctx . runfiles ( 238files = cargo_bazel_runfiles , 239transitive_files = toolchain . all_files , 240), 241executable = runner , 242) 243 244crates_vendor = rule ( 245implementation = _crates_vendor_impl , 246doc = """\ 247A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. 248This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the 249rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule 250handles all the same [workflows](#workflows) `crate_universe` rules do. 251 252Example: 253 254Given the following workspace structure: 255``` 256[workspace]/ 257WORKSPACE 258BUILD 259Cargo.toml 2603rdparty/ 261BUILD 262src/ 263main.rs 264``` 265 266The following is something that'd be found in `3rdparty/BUILD`: 267 268```python 269load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate") 270 271crates_vendor( 272name = "crates_vendor", 273annotations = { 274"rand": [crate.annotation( 275default_features = False, 276features = ["small_rng"], 277)], 278}, 279manifests = ["//:Cargo.toml"], 280mode = "remote", 281vendor_path = "crates", 282tags = ["manual"], 283) 284``` 285 286The above creates a target that can be run to write `BUILD` files into the `3rdparty` 287directory next to where the target is defined. To run it, simply call: 288 289```shell 290bazel run //3rdparty:crates_vendor 291``` 292""" , 293attrs = { 294"annotations" : attr . string_list_dict ( 295doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation)." , 296), 297"buildifier" : attr . label ( 298doc = "The path to a [buildifier](https://github.com/bazelbuild/buildtools/blob/5.0.1/buildifier/README.md) binary used to format generated BUILD files." , 299cfg = "exec" , 300executable = True , 301default = Label ( "//crate_universe/private/vendor:buildifier" ), 302), 303"cargo_bazel" : attr . label ( 304doc = ( 305"The cargo-bazel binary to use for vendoring. If this attribute is not set, then a " + 306"`{}` action env will be used." . format ( CARGO_BAZEL_GENERATOR_PATH ) 307), 308cfg = "exec" , 309executable = True , 310allow_files = True , 311default = CARGO_BAZEL_LABEL , 312), 313"cargo_config" : attr . label ( 314doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file." , 315allow_single_file = True , 316), 317"generate_build_scripts" : attr . bool ( 318doc = ( 319"Whether or not to generate " + 320"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default." 321), 322default = True , 323), 324"manifests" : attr . label_list ( 325doc = "A list of Cargo manifests (`Cargo.toml` files)." , 326allow_files = [ "Cargo.toml" ], 327), 328"mode" : attr . string ( 329doc = ( 330"Flags determining how crates should be vendored. `local` is where crate source and BUILD files are " + 331"written to the repository. `remote` is where only BUILD files are written and repository rules " + 332"used to fetch source code." 333), 334values = [ 335"local" , 336"remote" , 337], 338default = "remote" , 339), 340"packages" : attr . string_dict ( 341doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec)." , 342), 343"repository_name" : attr . string ( 344doc = "The name of the repository to generate for `remote` vendor modes. If unset, the label name will be used" , 345), 346"splicing_config" : attr . string ( 347doc = ( 348 "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\% rsplicing_config `to "+ 349"generate the value for this field. If unset, the defaults defined there will be used." 350), 351), 352"supported_platform_triples" : attr . string_list ( 353doc = "A set of all platform triples to consider when generating dependencies." , 354default = SUPPORTED_PLATFORM_TRIPLES , 355), 356"vendor_path" : attr . string ( 357doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root" , 358default = "crates" , 359), 360}, 361executable = True , 362toolchains = [ "@rules_rust//rust:toolchain" ], 363) 364 365def _crates_vendor_remote_repository_impl ( repository_ctx ): 366build_file = repository_ctx . path ( repository_ctx . attr . build_file ) 367defs_module = repository_ctx . path ( repository_ctx . attr . defs_module ) 368 369repository_ctx . file ( "BUILD.bazel" , repository_ctx . read ( build_file )) 370repository_ctx . file ( "defs.bzl" , repository_ctx . read ( defs_module )) 371repository_ctx . file ( "crates.bzl" , "" ) 372repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 373repository_ctx . name , 374)) 375 376crates_vendor_remote_repository = repository_rule ( 377doc = "Creates a repository paired with `crates_vendor` targets using the `remote` vendor mode." , 378implementation = _crates_vendor_remote_repository_impl , 379attrs = { 380"build_file" : attr . label ( 381doc = "The BUILD file to use for the root package" , 382mandatory = True , 383), 384"defs_module" : attr . label ( 385doc = "The `defs.bzl` file to use in the repository" , 386mandatory = True , 387), 388}, 389)