fork/rules_rust

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

Roberto Bampirules_rust: enable pipelined compilation. (#1275)867fc37c

0.9.0
3.2 KiB105 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            rustc_env = {},
78            is_test = False,
79        ),
80        output_hash = output_hash,
81    )
82
83wrap = rule(
84    implementation = _wrap_impl,
85    attrs = {
86        "crate_name": attr.string(),
87        "generate_metadata": attr.bool(default = False),
88        "target": attr.label(),
89        "_cc_toolchain": attr.label(
90            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
91        ),
92        "_error_format": attr.label(
93            default = Label("//:error_format"),
94        ),
95        "_process_wrapper": attr.label(
96            default = Label("//util/process_wrapper"),
97            executable = True,
98            allow_single_file = True,
99            cfg = "exec",
100        ),
101    },
102    toolchains = ["@rules_rust//rust:toolchain", "@bazel_tools//tools/cpp:toolchain_type"],
103    incompatible_use_toolchain_transition = True,
104    fragments = ["cpp"],
105)