fork/rules_rust

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

UebelAndreDeleted "extra workspace member" functionality from crate_universe (#1406)a15e67d6

0.6.0
13.3 KiB389 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")]
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
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 {}={}"
218    else:
219        extension = ".sh"
220        template = _UNIX_WRAPPER
221        env_template = "{}={}"
222
223    # Write the wrapper script
224    runner = ctx.actions.declare_file(ctx.label.name + extension)
225    ctx.actions.write(
226        output = runner,
227        content = template.format(
228            env = " ".join([env_template.format(key, val) for key, val in environ.items()]),
229            bin = bin_path,
230            args = " ".join(args),
231        ),
232        is_executable = True,
233    )
234
235    return DefaultInfo(
236        files = depset([runner]),
237        runfiles = ctx.runfiles(
238            files = cargo_bazel_runfiles,
239            transitive_files = toolchain.all_files,
240        ),
241        executable = runner,
242    )
243
244crates_vendor = rule(
245    implementation = _crates_vendor_impl,
246    doc = """\
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]/
257    WORKSPACE
258    BUILD
259    Cargo.toml
260    3rdparty/
261        BUILD
262    src/
263        main.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(
272    name = "crates_vendor",
273    annotations = {
274        "rand": [crate.annotation(
275            default_features = False,
276            features = ["small_rng"],
277        )],
278    },
279    manifests = ["//:Cargo.toml"],
280    mode = "remote",
281    vendor_path = "crates",
282    tags = ["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""",
293    attrs = {
294        "annotations": attr.string_list_dict(
295            doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation).",
296        ),
297        "buildifier": attr.label(
298            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.",
299            cfg = "exec",
300            executable = True,
301            default = Label("//crate_universe/private/vendor:buildifier"),
302        ),
303        "cargo_bazel": attr.label(
304            doc = (
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            ),
308            cfg = "exec",
309            executable = True,
310            allow_files = True,
311            default = CARGO_BAZEL_LABEL,
312        ),
313        "cargo_config": attr.label(
314            doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file.",
315            allow_single_file = True,
316        ),
317        "generate_build_scripts": attr.bool(
318            doc = (
319                "Whether or not to generate " +
320                "[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default."
321            ),
322            default = True,
323        ),
324        "manifests": attr.label_list(
325            doc = "A list of Cargo manifests (`Cargo.toml` files).",
326            allow_files = ["Cargo.toml"],
327        ),
328        "mode": attr.string(
329            doc = (
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            ),
334            values = [
335                "local",
336                "remote",
337            ],
338            default = "remote",
339        ),
340        "packages": attr.string_dict(
341            doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec).",
342        ),
343        "repository_name": attr.string(
344            doc = "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(
347            doc = (
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(
353            doc = "A set of all platform triples to consider when generating dependencies.",
354            default = SUPPORTED_PLATFORM_TRIPLES,
355        ),
356        "vendor_path": attr.string(
357            doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root",
358            default = "crates",
359        ),
360    },
361    executable = True,
362    toolchains = ["@rules_rust//rust:toolchain"],
363)
364
365def _crates_vendor_remote_repository_impl(repository_ctx):
366    build_file = repository_ctx.path(repository_ctx.attr.build_file)
367    defs_module = repository_ctx.path(repository_ctx.attr.defs_module)
368
369    repository_ctx.file("BUILD.bazel", repository_ctx.read(build_file))
370    repository_ctx.file("defs.bzl", repository_ctx.read(defs_module))
371    repository_ctx.file("crates.bzl", "")
372    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
373        repository_ctx.name,
374    ))
375
376crates_vendor_remote_repository = repository_rule(
377    doc = "Creates a repository paired with `crates_vendor` targets using the `remote` vendor mode.",
378    implementation = _crates_vendor_remote_repository_impl,
379    attrs = {
380        "build_file": attr.label(
381            doc = "The BUILD file to use for the root package",
382            mandatory = True,
383        ),
384        "defs_module": attr.label(
385            doc = "The `defs.bzl` file to use in the repository",
386            mandatory = True,
387        ),
388    },
389)