|
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 _ltl(library, ctx, cc_toolchain, feature_configuration): |
| 23 | + return cc_common.create_library_to_link( |
| 24 | + actions = ctx.actions, |
| 25 | + feature_configuration = feature_configuration, |
| 26 | + cc_toolchain = cc_toolchain, |
| 27 | + static_library = library, |
| 28 | + pic_static_library = library, |
| 29 | + ) |
| 30 | + |
| 31 | +def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library): |
| 32 | + """Make the CcInfo (if possible) for libstd and allocator libraries. |
| 33 | +
|
| 34 | + Args: |
| 35 | + ctx (ctx): The rule's context object. |
| 36 | + rust_lib: The rust standard library. |
| 37 | + allocator_library: The target to use for providing allocator functions. |
| 38 | +
|
| 39 | +
|
| 40 | + Returns: |
| 41 | + A CcInfo object for the required libraries, or None if no such libraries are available. |
| 42 | + """ |
| 43 | + cc_toolchain, feature_configuration = find_cc_toolchain(ctx) |
| 44 | + link_inputs = [] |
| 45 | + std_rlibs = [f for f in rust_lib.files.to_list() if f.basename.endswith(".rlib")] |
| 46 | + if std_rlibs: |
| 47 | + # std depends on everything |
| 48 | + # |
| 49 | + # core only depends on alloc, but we poke adler in there |
| 50 | + # because that needs to be before miniz_oxide |
| 51 | + # |
| 52 | + # alloc depends on the allocator_library if it's configured, but we |
| 53 | + # do that later. |
| 54 | + dot_a_files = [_make_dota(ctx, f) for f in std_rlibs] |
| 55 | + |
| 56 | + alloc_files = [f for f in dot_a_files if "alloc" in f.basename and "std" not in f.basename] |
| 57 | + between_alloc_and_core_files = [f for f in dot_a_files if "compiler_builtins" in f.basename] |
| 58 | + core_files = [f for f in dot_a_files if ("core" in f.basename or "adler" in f.basename) and "std" not in f.basename] |
| 59 | + between_core_and_std_files = [ |
| 60 | + f |
| 61 | + for f in dot_a_files |
| 62 | + if "alloc" not in f.basename and "compiler_builtins" not in f.basename and "core" not in f.basename and "adler" not in f.basename and "std" not in f.basename |
| 63 | + ] |
| 64 | + std_files = [f for f in dot_a_files if "std" in f.basename] |
| 65 | + |
| 66 | + partitioned_files_len = len(alloc_files) + len(between_alloc_and_core_files) + len(core_files) + len(between_core_and_std_files) + len(std_files) |
| 67 | + if partitioned_files_len != len(dot_a_files): |
| 68 | + partitioned = alloc_files + between_alloc_and_core_files + core_files + between_core_and_std_files + std_files |
| 69 | + for f in sorted(partitioned): |
| 70 | + # buildifier: disable=print |
| 71 | + print("File partitioned: {}".format(f.basename)) |
| 72 | + fail("rust_toolchain couldn't properly partition rlibs in rust_lib. Partitioned {} out of {} files. This is probably a bug in the rule implementation.".format(partitioned_files_len, len(dot_a_files))) |
| 73 | + |
| 74 | + alloc_inputs = depset( |
| 75 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in alloc_files], |
| 76 | + ) |
| 77 | + between_alloc_and_core_inputs = depset( |
| 78 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in between_alloc_and_core_files], |
| 79 | + transitive = [alloc_inputs], |
| 80 | + order = "topological", |
| 81 | + ) |
| 82 | + core_inputs = depset( |
| 83 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in core_files], |
| 84 | + transitive = [between_alloc_and_core_inputs], |
| 85 | + order = "topological", |
| 86 | + ) |
| 87 | + between_core_and_std_inputs = depset( |
| 88 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in between_core_and_std_files], |
| 89 | + transitive = [core_inputs], |
| 90 | + order = "topological", |
| 91 | + ) |
| 92 | + std_inputs = depset( |
| 93 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in std_files], |
| 94 | + transitive = [between_core_and_std_inputs], |
| 95 | + order = "topological", |
| 96 | + ) |
| 97 | + |
| 98 | + link_inputs.append(cc_common.create_linker_input( |
| 99 | + owner = rust_lib.label, |
| 100 | + libraries = std_inputs, |
| 101 | + )) |
| 102 | + |
| 103 | + allocator_inputs = None |
| 104 | + if allocator_library: |
| 105 | + allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs] |
| 106 | + |
| 107 | + libstd_and_allocator_ccinfo = None |
| 108 | + if link_inputs: |
| 109 | + return CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset( |
| 110 | + link_inputs, |
| 111 | + transitive = allocator_inputs, |
| 112 | + order = "topological", |
| 113 | + ))) |
| 114 | + return None |
| 115 | + |
3 | 116 | def _rust_toolchain_impl(ctx):
|
4 | 117 | """The rust_toolchain implementation
|
5 | 118 |
|
@@ -38,12 +151,17 @@ def _rust_toolchain_impl(ctx):
|
38 | 151 | default_edition = ctx.attr.default_edition,
|
39 | 152 | compilation_mode_opts = compilation_mode_opts,
|
40 | 153 | crosstool_files = ctx.files._crosstool,
|
| 154 | + libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, ctx.attr.rust_lib, ctx.attr.allocator_library), |
41 | 155 | )
|
42 | 156 | return [toolchain]
|
43 | 157 |
|
44 | 158 | rust_toolchain = rule(
|
45 | 159 | implementation = _rust_toolchain_impl,
|
| 160 | + fragments = ["cpp"], |
46 | 161 | attrs = {
|
| 162 | + "allocator_library": attr.label( |
| 163 | + doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.", |
| 164 | + ), |
47 | 165 | "binary_ext": attr.string(
|
48 | 166 | doc = "The extension for binaries created from rustc.",
|
49 | 167 | mandatory = True,
|
@@ -128,6 +246,9 @@ rust_toolchain = rule(
|
128 | 246 | "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations"
|
129 | 247 | ),
|
130 | 248 | ),
|
| 249 | + "_cc_toolchain": attr.label( |
| 250 | + default = "@bazel_tools//tools/cpp:current_cc_toolchain", |
| 251 | + ), |
131 | 252 | "_crosstool": attr.label(
|
132 | 253 | default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
|
133 | 254 | ),
|
|
0 commit comments