fork/rules_rust

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

Daniel Wagner-HallExternal path dependencies work (#3025)7edda6f1

main
2.8 KiB64 linesraw
1"""`local_crate_mirror` rule implementation."""
2
3load("//crate_universe/private:common_utils.bzl", "execute")
4load("//crate_universe/private:generate_utils.bzl", "get_generator")
5load("//crate_universe/private:urls.bzl", "CARGO_BAZEL_SHA256S", "CARGO_BAZEL_URLS")
6load("//rust/platform:triple.bzl", "get_host_triple")
7
8def _local_crate_mirror_impl(repository_ctx):
9    path = repository_ctx.path(repository_ctx.attr.path)
10
11    host_triple = get_host_triple(repository_ctx)
12
13    generator, _generator_sha256 = get_generator(repository_ctx, host_triple.str)
14
15    # TODO: Work out why we can't just symlink here and actually copy.
16    # illicitonion thinks it may be that symlinks didn't get invalidated properly?
17    for child in repository_ctx.path(path).readdir():
18        repository_ctx.execute(["cp", "-r", child, repository_ctx.path(child.basename)])
19
20    paths_to_track = execute(repository_ctx, ["find", path, "-type", "f"]).stdout.strip().split("\n")
21    for path_to_track in paths_to_track:
22        if path_to_track:
23            repository_ctx.read(path_to_track)
24
25    execute(repository_ctx, [generator, "render", "--options-json", repository_ctx.attr.options_json, "--output-path", repository_ctx.path("BUILD.bazel")])
26
27    repository_ctx.file("WORKSPACE.bazel", "")
28
29local_crate_mirror = repository_rule(
30    doc = """This is a private implementation detail of crate_universe, and should not be relied on in manually written code.
31
32This is effectively a `local_repository` rule impementation, but where the BUILD.bazel file is generated using the `cargo-bazel render` command.""",
33    implementation = _local_crate_mirror_impl,
34    attrs = {
35        "generator": attr.string(
36            doc = (
37                "The absolute label of a generator. Eg. `@cargo_bazel_bootstrap//:cargo-bazel`. " +
38                "This is typically used when bootstrapping"
39            ),
40        ),
41        "generator_sha256s": attr.string_dict(
42            doc = "Dictionary of `host_triple` -> `sha256` for a `cargo-bazel` binary.",
43            default = CARGO_BAZEL_SHA256S,
44        ),
45        "generator_urls": attr.string_dict(
46            doc = (
47                "URL template from which to download the `cargo-bazel` binary. `{host_triple}` and will be " +
48                "filled in according to the host platform."
49            ),
50            default = CARGO_BAZEL_URLS,
51        ),
52        "options_json": attr.string(
53            doc = "JSON serialized instance of a crate_universe::context::SingleBuildFileRenderContext",
54        ),
55        "path": attr.string(
56            # TODO: Verify what happens if this is not an absolute path.
57            doc = "Absolute path to the BUILD.bazel file to generate.",
58        ),
59        "quiet": attr.bool(
60            doc = "If stdout and stderr should not be printed to the terminal.",
61            default = True,
62        ),
63    },
64)