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