fork/rules_rust

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

Paul FarielloFix data and compile_data for rust_doc (#1741)9803d303

rust-analyzer
3.3 KiB106 linesraw
1"""A custom rule that wraps a crate called to_wrap."""
2
3# buildifier: disable=bzl-visibility
4load("//rust/private:common.bzl", "rust_common")
5
6# buildifier: disable=bzl-visibility
7load("//rust/private:providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVariantInfo")
8
9# buildifier: disable=bzl-visibility
10load("//rust/private:rustc.bzl", "rustc_compile_action")
11
12def _wrap_impl(ctx):
13    rs_file = ctx.actions.declare_file(ctx.label.name + "_wrapped.rs")
14    crate_name = ctx.attr.crate_name if ctx.attr.crate_name else ctx.label.name
15    ctx.actions.run_shell(
16        outputs = [rs_file],
17        command = """cat <<EOF > {}
18// crate_name: {}
19use to_wrap::to_wrap;
20
21pub fn wrap() {{
22    to_wrap();
23}}
24EOF
25""".format(rs_file.path, crate_name),
26        mnemonic = "WriteWrapperRsFile",
27    )
28
29    toolchain = ctx.toolchains[Label("//rust:toolchain")]
30
31    # Determine unique hash for this rlib
32    output_hash = repr(hash(rs_file.path))
33    crate_type = "rlib"
34
35    rust_lib_name = "{prefix}{name}-{lib_hash}{extension}".format(
36        prefix = "lib",
37        name = crate_name,
38        lib_hash = output_hash,
39        extension = ".rlib",
40    )
41    rust_metadata_name = "{prefix}{name}-{lib_hash}{extension}".format(
42        prefix = "lib",
43        name = crate_name,
44        lib_hash = output_hash,
45        extension = ".rmeta",
46    )
47
48    tgt = ctx.attr.target
49    deps = [DepVariantInfo(
50        crate_info = tgt[CrateInfo] if CrateInfo in tgt else None,
51        dep_info = tgt[DepInfo] if DepInfo in tgt else None,
52        build_info = tgt[BuildInfo] if BuildInfo in tgt else None,
53        cc_info = tgt[CcInfo] if CcInfo in tgt else None,
54    )]
55
56    rust_lib = ctx.actions.declare_file(rust_lib_name)
57    rust_metadata = None
58    if ctx.attr.generate_metadata:
59        rust_metadata = ctx.actions.declare_file(rust_metadata_name)
60    return rustc_compile_action(
61        ctx = ctx,
62        attr = ctx.attr,
63        toolchain = toolchain,
64        crate_info = rust_common.create_crate_info(
65            name = crate_name,
66            type = crate_type,
67            root = rs_file,
68            srcs = depset([rs_file]),
69            deps = depset(deps),
70            proc_macro_deps = depset([]),
71            aliases = {},
72            output = rust_lib,
73            metadata = rust_metadata,
74            owner = ctx.label,
75            edition = "2018",
76            compile_data = depset([]),
77            compile_data_targets = depset([]),
78            rustc_env = {},
79            is_test = False,
80        ),
81        output_hash = output_hash,
82    )
83
84wrap = rule(
85    implementation = _wrap_impl,
86    attrs = {
87        "crate_name": attr.string(),
88        "generate_metadata": attr.bool(default = False),
89        "target": attr.label(),
90        "_cc_toolchain": attr.label(
91            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
92        ),
93        "_error_format": attr.label(
94            default = Label("//:error_format"),
95        ),
96        "_process_wrapper": attr.label(
97            default = Label("//util/process_wrapper"),
98            executable = True,
99            allow_single_file = True,
100            cfg = "exec",
101        ),
102    },
103    toolchains = ["@rules_rust//rust:toolchain", "@bazel_tools//tools/cpp:toolchain_type"],
104    incompatible_use_toolchain_transition = True,
105    fragments = ["cpp"],
106)