|
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 | + # std depends on everything |
| 39 | + # |
| 40 | + # core only depends on alloc, but we poke adler in there |
| 41 | + # because that needs to be before miniz_oxide |
| 42 | + # |
| 43 | + # alloc depends on the allocator_library if it's configured, but we |
| 44 | + # do that later. |
| 45 | + dot_a_files = [_make_dota(ctx, f) for f in std_rlibs] |
| 46 | + |
| 47 | + alloc_inputs = depset([ |
| 48 | + cc_common.create_library_to_link( |
| 49 | + actions = ctx.actions, |
| 50 | + feature_configuration = feature_configuration, |
| 51 | + cc_toolchain = cc_toolchain, |
| 52 | + static_library = f, |
| 53 | + pic_static_library = f, |
| 54 | + ) |
| 55 | + for f in dot_a_files |
| 56 | + if "alloc" in f.basename |
| 57 | + ]) |
| 58 | + core_inputs = depset([ |
| 59 | + cc_common.create_library_to_link( |
| 60 | + actions = ctx.actions, |
| 61 | + feature_configuration = feature_configuration, |
| 62 | + cc_toolchain = cc_toolchain, |
| 63 | + static_library = f, |
| 64 | + pic_static_library = f, |
| 65 | + ) |
| 66 | + for f in dot_a_files |
| 67 | + if "core" in f.basename or "adler" in f.basename |
| 68 | + ], transitive = [alloc_inputs], order = "topological") |
| 69 | + between_core_and_std_inputs = depset([ |
| 70 | + cc_common.create_library_to_link( |
| 71 | + actions = ctx.actions, |
| 72 | + feature_configuration = feature_configuration, |
| 73 | + cc_toolchain = cc_toolchain, |
| 74 | + static_library = f, |
| 75 | + pic_static_library = f, |
| 76 | + ) |
| 77 | + for f in dot_a_files |
| 78 | + if "core" not in f.basename and "alloc" not in f.basename and "std" not in f.basename and not "adler" in f.basename |
| 79 | + ], transitive = [core_inputs], order = "topological") |
| 80 | + std_inputs = depset([ |
| 81 | + cc_common.create_library_to_link( |
| 82 | + actions = ctx.actions, |
| 83 | + feature_configuration = feature_configuration, |
| 84 | + cc_toolchain = cc_toolchain, |
| 85 | + static_library = f, |
| 86 | + pic_static_library = f, |
| 87 | + ) |
| 88 | + for f in dot_a_files |
| 89 | + if "std" in f.basename |
| 90 | + ], transitive = [between_core_and_std_inputs], order = "topological") |
| 91 | + link_inputs.append(cc_common.create_linker_input( |
| 92 | + owner = rust_lib.label, |
| 93 | + libraries = std_inputs, |
| 94 | + )) |
| 95 | + |
| 96 | + allocator_inputs = None |
| 97 | + if allocator_library: |
| 98 | + allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs] |
| 99 | + |
| 100 | + libstd_and_allocator_ccinfo = None |
| 101 | + if link_inputs: |
| 102 | + return CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset( |
| 103 | + link_inputs, |
| 104 | + transitive = allocator_inputs, |
| 105 | + order = "topological", |
| 106 | + ))) |
| 107 | + return None |
| 108 | + |
3 | 109 | def _rust_toolchain_impl(ctx):
|
4 | 110 | """The rust_toolchain implementation
|
5 | 111 |
|
@@ -38,11 +144,13 @@ def _rust_toolchain_impl(ctx):
|
38 | 144 | default_edition = ctx.attr.default_edition,
|
39 | 145 | compilation_mode_opts = compilation_mode_opts,
|
40 | 146 | crosstool_files = ctx.files._crosstool,
|
| 147 | + libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, ctx.attr.rust_lib, ctx.attr.allocator_library), |
41 | 148 | )
|
42 | 149 | return [toolchain]
|
43 | 150 |
|
44 | 151 | rust_toolchain = rule(
|
45 | 152 | implementation = _rust_toolchain_impl,
|
| 153 | + fragments = ["cpp"], |
46 | 154 | attrs = {
|
47 | 155 | "binary_ext": attr.string(
|
48 | 156 | doc = "The extension for binaries created from rustc.",
|
@@ -131,6 +239,12 @@ rust_toolchain = rule(
|
131 | 239 | "_crosstool": attr.label(
|
132 | 240 | default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
|
133 | 241 | ),
|
| 242 | + "_cc_toolchain": attr.label( |
| 243 | + default = "@bazel_tools//tools/cpp:current_cc_toolchain", |
| 244 | + ), |
| 245 | + "allocator_library": attr.label( |
| 246 | + doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.", |
| 247 | + ), |
134 | 248 | },
|
135 | 249 | doc = """Declares a Rust toolchain for use.
|
136 | 250 |
|
|
0 commit comments