|
1 | 1 | """The rust_toolchain rule definition and implementation."""
|
2 | 2 |
|
| 3 | +load( |
| 4 | + "//rust/private:utils.bzl", |
| 5 | + "find_cc_toolchain", |
| 6 | +) |
| 7 | + |
| 8 | +def _make_dota(ctx, f): |
| 9 | + """Add a symlink for a file that ends in .a, so it can be used as a staticlib. |
| 10 | +
|
| 11 | + Args: |
| 12 | + ctx (ctx): The rule's context object. |
| 13 | + f (File): The file to symlink. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + The symlink's File. |
| 17 | + """ |
| 18 | + dot_a = ctx.actions.declare_file(f.basename + ".a", sibling = f) |
| 19 | + ctx.actions.symlink(output = dot_a, target_file = f) |
| 20 | + return dot_a |
| 21 | + |
| 22 | +def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library): |
| 23 | + """Make the CcInfo (if possible) for libstd and allocator libraries. |
| 24 | +
|
| 25 | + Args: |
| 26 | + ctx (ctx): The rule's context object. |
| 27 | + rust_lib: The rust standard library. |
| 28 | + allocator_library: The target to use for providing allocator functions. |
| 29 | +
|
| 30 | +
|
| 31 | + Returns: |
| 32 | + A CcInfo object for the required libraries, or None if no such libraries are available. |
| 33 | + """ |
| 34 | + cc_toolchain, feature_configuration = find_cc_toolchain(ctx) |
| 35 | + link_inputs = [] |
| 36 | + std_rlibs = [f for f in rust_lib.files.to_list() if f.basename.endswith(".rlib")] |
| 37 | + if std_rlibs: |
| 38 | + dot_a_files = [_make_dota(ctx, f) for f in std_rlibs] |
| 39 | + link_inputs.append(cc_common.create_linker_input( |
| 40 | + owner = rust_lib.label, |
| 41 | + libraries = depset( |
| 42 | + [ |
| 43 | + cc_common.create_library_to_link( |
| 44 | + actions = ctx.actions, |
| 45 | + feature_configuration = feature_configuration, |
| 46 | + cc_toolchain = cc_toolchain, |
| 47 | + static_library = f, |
| 48 | + pic_static_library = f, |
| 49 | + ) |
| 50 | + for f in dot_a_files |
| 51 | + ], |
| 52 | + ), |
| 53 | + )) |
| 54 | + if allocator_library: |
| 55 | + link_inputs.append(cc_common.create_linker_input( |
| 56 | + owner = allocator_library.allocator_library.label, |
| 57 | + libraries = depset([ |
| 58 | + cc_common.create_library_to_link( |
| 59 | + # TODO(augie): wat? this needs no actions |
| 60 | + actions = ctx.actions, |
| 61 | + feature_configuration = feature_configuration, |
| 62 | + cc_toolchain = cc_toolchain, |
| 63 | + static_library = f, |
| 64 | + ) |
| 65 | + for f in allocator_library.files.to_list() |
| 66 | + ]), |
| 67 | + )) |
| 68 | + libstd_and_allocator_ccinfo = None |
| 69 | + if link_inputs: |
| 70 | + return CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset(link_inputs))) |
| 71 | + return None |
| 72 | + |
| 73 | + |
3 | 74 | def _rust_toolchain_impl(ctx):
|
4 | 75 | """The rust_toolchain implementation
|
5 | 76 |
|
@@ -38,11 +109,13 @@ def _rust_toolchain_impl(ctx):
|
38 | 109 | default_edition = ctx.attr.default_edition,
|
39 | 110 | compilation_mode_opts = compilation_mode_opts,
|
40 | 111 | crosstool_files = ctx.files._crosstool,
|
| 112 | + libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, ctx.attr.rust_lib, ctx.attr.allocator_library), |
41 | 113 | )
|
42 | 114 | return [toolchain]
|
43 | 115 |
|
44 | 116 | rust_toolchain = rule(
|
45 | 117 | implementation = _rust_toolchain_impl,
|
| 118 | + fragments = ["cpp"], |
46 | 119 | attrs = {
|
47 | 120 | "binary_ext": attr.string(
|
48 | 121 | doc = "The extension for binaries created from rustc.",
|
@@ -131,6 +204,12 @@ rust_toolchain = rule(
|
131 | 204 | "_crosstool": attr.label(
|
132 | 205 | default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
|
133 | 206 | ),
|
| 207 | + "_cc_toolchain": attr.label( |
| 208 | + default = "@bazel_tools//tools/cpp:current_cc_toolchain", |
| 209 | + ), |
| 210 | + "allocator_library": attr.label( |
| 211 | + doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.", |
| 212 | + ), |
134 | 213 | },
|
135 | 214 | doc = """Declares a Rust toolchain for use.
|
136 | 215 |
|
|
0 commit comments