fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Cameron MulhernFixes crates_vendor labels in remote mode when used from the root workspace (#1575)a364d448

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