fork/rules_rust

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

UebelAndreAdd `render_config` attribute to `crates_vendor`. (#1832)02557a47

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