fork/rules_rust

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

UebelAndreUpdated crate_universe docs (#1301)e07881fa

0.5.0
12.7 KiB373 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)"
12eval exec env - BUILD_WORKSPACE_DIRECTORY="${{BUILD_WORKSPACE_DIRECTORY}}" {env} \\
13"{bin}" {args} "$@"
14"""
15
16_WINDOWS_WRAPPER = """\
17@ECHO OFF
18set RUNTIME_PWD=%CD%
19{env}
20
21call {bin} {args} %@%
22"""
23
24CARGO_BAZEL_GENERATOR_PATH = "CARGO_BAZEL_GENERATOR_PATH"
25
26def _runfiles_path(path, is_windows):
27    if is_windows:
28        runtime_pwd_var = "%RUNTIME_PWD%"
29    else:
30        runtime_pwd_var = "${RUNTIME_PWD}"
31    if path.startswith("../"):
32        return "{}/external/{}".format(runtime_pwd_var, path[len("../"):])
33    return "{}/{}".format(runtime_pwd_var, path)
34
35def _is_windows(ctx):
36    toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain")]
37    return "windows" in toolchain.target_triple
38
39def _get_output_package(ctx):
40    # Determine output directory
41    if ctx.attr.vendor_path.startswith("/"):
42        output = ctx.attr.vendor_path
43    else:
44        output = "{}/{}".format(
45            ctx.label.package,
46            ctx.attr.vendor_path,
47        )
48    return output
49
50def _write_data_file(ctx, name, data):
51    file = ctx.actions.declare_file("{}.{}".format(ctx.label.name, name))
52    ctx.actions.write(
53        output = file,
54        content = data,
55    )
56    return file
57
58def _write_splicing_manifest(ctx):
59    # Deserialize information about direct packges
60    direct_packages_info = {
61        # Ensure the data is using kebab-case as that's what `cargo_toml::DependencyDetail` expects.
62        pkg: kebab_case_keys(dict(json.decode(data)))
63        for (pkg, data) in ctx.attr.packages.items()
64    }
65
66    # Manifests are required to be single files
67    manifests = {m[DefaultInfo].files.to_list()[0].short_path: str(m.label) for m in ctx.attr.manifests}
68
69    config = json.decode(ctx.attr.splicing_config or splicing_config())
70    splicing_manifest_content = {
71        # TODO: How do cargo config files get factored into vendored builds
72        "cargo_config": None,
73        "direct_packages": direct_packages_info,
74        "manifests": manifests,
75    }
76
77    manifest = _write_data_file(
78        ctx = ctx,
79        name = "cargo-bazel-splicing-manifest.json",
80        data = json.encode_indent(
81            dict(dict(config).items() + splicing_manifest_content.items()),
82            indent = " " * 4,
83        ),
84    )
85
86    is_windows = _is_windows(ctx)
87
88    args = ["--splicing-manifest", _runfiles_path(manifest.short_path, is_windows)]
89    runfiles = [manifest]
90    return args, runfiles
91
92def _write_extra_manifests_manifest(ctx):
93    manifest = _write_data_file(
94        ctx = ctx,
95        name = "cargo-bazel-extra-manifests-manifest.json",
96        data = json.encode(struct(
97            # TODO: This is for extra workspace members
98            manifests = [],
99        )),
100    )
101    is_windows = _is_windows(ctx)
102    args = ["--extra-manifests-manifest", _runfiles_path(manifest.short_path, is_windows)]
103    runfiles = [manifest]
104    return args, runfiles
105
106def _write_config_file(ctx):
107    rendering_config = dict(json.decode(render_config()))
108
109    output_pkg = _get_output_package(ctx)
110
111    if ctx.attr.mode == "local":
112        build_file_base_template = "@{}//{}/{{name}}-{{version}}:BUILD.bazel"
113        crate_label_template = "//{}/{{name}}-{{version}}:{{target}}".format(
114            output_pkg,
115        )
116    else:
117        build_file_base_template = "@{}//{}:BUILD.{{name}}-{{version}}.bazel"
118        crate_label_template = rendering_config["crate_label_template"]
119
120    rendering_config.update({
121        "build_file_template": build_file_base_template.format(
122            ctx.workspace_name,
123            output_pkg,
124        ),
125        "crate_label_template": crate_label_template,
126        "crates_module_template": "@{}//{}:{{file}}".format(
127            ctx.workspace_name,
128            output_pkg,
129        ),
130        "vendor_mode": ctx.attr.mode,
131    })
132
133    config_data = compile_config(
134        crate_annotations = ctx.attr.annotations,
135        generate_build_scripts = ctx.attr.generate_build_scripts,
136        cargo_config = None,
137        render_config = rendering_config,
138        supported_platform_triples = ctx.attr.supported_platform_triples,
139        repository_name = ctx.attr.repository_name or ctx.label.name,
140    )
141
142    config = _write_data_file(
143        ctx = ctx,
144        name = "cargo-bazel-config.json",
145        data = json.encode_indent(
146            config_data,
147            indent = " " * 4,
148        ),
149    )
150
151    is_windows = _is_windows(ctx)
152    args = ["--config", _runfiles_path(config.short_path, is_windows)]
153    runfiles = [config] + ctx.files.manifests
154    return args, runfiles
155
156def _crates_vendor_impl(ctx):
157    toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain")]
158    is_windows = _is_windows(ctx)
159
160    environ = {
161        "CARGO": _runfiles_path(toolchain.cargo.short_path, is_windows),
162        "RUSTC": _runfiles_path(toolchain.rustc.short_path, is_windows),
163    }
164
165    args = ["vendor"]
166
167    cargo_bazel_runfiles = []
168
169    # Allow action envs to override the use of the cargo-bazel target.
170    if CARGO_BAZEL_GENERATOR_PATH in ctx.var:
171        bin_path = ctx.var[CARGO_BAZEL_GENERATOR_PATH]
172    elif ctx.executable.cargo_bazel:
173        bin_path = _runfiles_path(ctx.executable.cargo_bazel.short_path, is_windows)
174        cargo_bazel_runfiles.append(ctx.executable.cargo_bazel)
175    else:
176        fail("{} is missing either the `cargo_bazel` attribute or the '{}' action env".format(
177            ctx.label,
178            CARGO_BAZEL_GENERATOR_PATH,
179        ))
180
181    # Generate config file
182    config_args, config_runfiles = _write_config_file(ctx)
183    args.extend(config_args)
184    cargo_bazel_runfiles.extend(config_runfiles)
185
186    # Generate splicing manifest
187    splicing_manifest_args, splicing_manifest_runfiles = _write_splicing_manifest(ctx)
188    args.extend(splicing_manifest_args)
189    cargo_bazel_runfiles.extend(splicing_manifest_runfiles)
190
191    # Generate extra-manifests manifest
192    extra_manifests_manifest_args, extra_manifests_manifest_runfiles = _write_extra_manifests_manifest(ctx)
193    args.extend(extra_manifests_manifest_args)
194    cargo_bazel_runfiles.extend(extra_manifests_manifest_runfiles)
195
196    # Optionally include buildifier
197    if ctx.attr.buildifier:
198        args.extend(["--buildifier", _runfiles_path(ctx.executable.buildifier.short_path, is_windows)])
199        cargo_bazel_runfiles.append(ctx.executable.buildifier)
200
201    # Dtermine platform specific settings
202    if is_windows:
203        extension = ".bat"
204        template = _WINDOWS_WRAPPER
205        env_template = "\nset {}={}"
206    else:
207        extension = ".sh"
208        template = _UNIX_WRAPPER
209        env_template = "{}={}"
210
211    # Write the wrapper script
212    runner = ctx.actions.declare_file(ctx.label.name + extension)
213    ctx.actions.write(
214        output = runner,
215        content = template.format(
216            env = " ".join([env_template.format(key, val) for key, val in environ.items()]),
217            bin = bin_path,
218            args = " ".join(args),
219        ),
220        is_executable = True,
221    )
222
223    return DefaultInfo(
224        files = depset([runner]),
225        runfiles = ctx.runfiles(
226            files = cargo_bazel_runfiles,
227            transitive_files = toolchain.all_files,
228        ),
229        executable = runner,
230    )
231
232crates_vendor = rule(
233    implementation = _crates_vendor_impl,
234    doc = """\
235A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace.
236This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the
237rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule
238handles all the same [workflows](#workflows) `crate_universe` rules do.
239
240Example: 
241
242Given the following workspace structure:
243```
244[workspace]/
245    WORKSPACE
246    BUILD
247    Cargo.toml
248    3rdparty/
249        BUILD
250    src/
251        main.rs
252```
253
254The following is something that'd be found in `3rdparty/BUILD`:
255
256```python
257load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate")
258
259crates_vendor(
260    name = "crates_vendor",
261    annotations = {
262        "rand": [crate.annotation(
263            default_features = False,
264            features = ["small_rng"],
265        )],
266    },
267    manifests = ["//:Cargo.toml"],
268    mode = "remote",
269    vendor_path = "crates",
270    tags = ["manual"],
271)
272```
273
274The above creates a target that can be run to write `BUILD` files into the `3rdparty`
275directory next to where the target is defined. To run it, simply call:
276
277```shell
278bazel run //3rdparty:crates_vendor
279```
280""",
281    attrs = {
282        "annotations": attr.string_list_dict(
283            doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation).",
284        ),
285        "buildifier": attr.label(
286            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.",
287            cfg = "exec",
288            executable = True,
289            default = Label("//crate_universe/private/vendor:buildifier"),
290        ),
291        "cargo_bazel": attr.label(
292            doc = (
293                "The cargo-bazel binary to use for vendoring. If this attribute is not set, then a " +
294                "`{}` action env will be used.".format(CARGO_BAZEL_GENERATOR_PATH)
295            ),
296            cfg = "exec",
297            executable = True,
298            allow_files = True,
299            default = CARGO_BAZEL_LABEL,
300        ),
301        "generate_build_scripts": attr.bool(
302            doc = (
303                "Whether or not to generate " +
304                "[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default."
305            ),
306            default = True,
307        ),
308        "manifests": attr.label_list(
309            doc = "A list of Cargo manifests (`Cargo.toml` files).",
310            allow_files = ["Cargo.toml"],
311        ),
312        "mode": attr.string(
313            doc = (
314                "Flags determining how crates should be vendored. `local` is where crate source and BUILD files are " +
315                "written to the repository. `remote` is where only BUILD files are written and repository rules " +
316                "used to fetch source code."
317            ),
318            values = [
319                "local",
320                "remote",
321            ],
322            default = "remote",
323        ),
324        "packages": attr.string_dict(
325            doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec).",
326        ),
327        "repository_name": attr.string(
328            doc = "The name of the repository to generate for `remote` vendor modes. If unset, the label name will be used",
329        ),
330        "splicing_config": attr.string(
331            doc = (
332                "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\%rsplicing_config` to " +
333                "generate the value for this field. If unset, the defaults defined there will be used."
334            ),
335        ),
336        "supported_platform_triples": attr.string_list(
337            doc = "A set of all platform triples to consider when generating dependencies.",
338            default = SUPPORTED_PLATFORM_TRIPLES,
339        ),
340        "vendor_path": attr.string(
341            doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root",
342            default = "crates",
343        ),
344    },
345    executable = True,
346    toolchains = ["@rules_rust//rust:toolchain"],
347)
348
349def _crates_vendor_remote_repository_impl(repository_ctx):
350    build_file = repository_ctx.path(repository_ctx.attr.build_file)
351    defs_module = repository_ctx.path(repository_ctx.attr.defs_module)
352
353    repository_ctx.file("BUILD.bazel", repository_ctx.read(build_file))
354    repository_ctx.file("defs.bzl", repository_ctx.read(defs_module))
355    repository_ctx.file("crates.bzl", "")
356    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
357        repository_ctx.name,
358    ))
359
360crates_vendor_remote_repository = repository_rule(
361    doc = "Creates a repository paired with `crates_vendor` targets using the `remote` vendor mode.",
362    implementation = _crates_vendor_remote_repository_impl,
363    attrs = {
364        "build_file": attr.label(
365            doc = "The BUILD file to use for the root package",
366            mandatory = True,
367        ),
368        "defs_module": attr.label(
369            doc = "The `defs.bzl` file to use in the repository",
370            mandatory = True,
371        ),
372    },
373)