|
1 | 1 | """The rust_toolchain rule definition and implementation."""
|
2 | 2 |
|
3 |
| -load( |
4 |
| - "//rust/private:utils.bzl", |
5 |
| - "find_cc_toolchain", |
6 |
| -) |
| 3 | +load("//rust/private:common.bzl", "rust_common") |
| 4 | +load("//rust/private:utils.bzl", "dedent", "find_cc_toolchain") |
7 | 5 |
|
8 |
| -def _make_dota(ctx, f): |
| 6 | +def _make_dota(ctx, file): |
9 | 7 | """Add a symlink for a file that ends in .a, so it can be used as a staticlib.
|
10 | 8 |
|
11 | 9 | Args:
|
12 | 10 | ctx (ctx): The rule's context object.
|
13 |
| - f (File): The file to symlink. |
| 11 | + file (File): The file to symlink. |
14 | 12 |
|
15 | 13 | Returns:
|
16 | 14 | The symlink's File.
|
17 | 15 | """
|
18 |
| - dot_a = ctx.actions.declare_file(f.basename + ".a", sibling = f) |
19 |
| - ctx.actions.symlink(output = dot_a, target_file = f) |
| 16 | + dot_a = ctx.actions.declare_file(file.basename + ".a", sibling = file) |
| 17 | + ctx.actions.symlink(output = dot_a, target_file = file) |
20 | 18 | return dot_a
|
21 | 19 |
|
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. |
| 20 | +def _rust_stdlib_filegroup_impl(ctx): |
| 21 | + rust_lib = ctx.files.srcs |
| 22 | + dot_a_files = [] |
| 23 | + between_alloc_and_core_files = [] |
| 24 | + core_files = [] |
| 25 | + between_core_and_std_files = [] |
| 26 | + std_files = [] |
| 27 | + alloc_files = [] |
33 | 28 |
|
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")] |
| 29 | + std_rlibs = [f for f in rust_lib if f.basename.endswith(".rlib")] |
46 | 30 | if std_rlibs:
|
47 | 31 | # std depends on everything
|
48 | 32 | #
|
@@ -71,26 +55,88 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
|
71 | 55 | print("File partitioned: {}".format(f.basename))
|
72 | 56 | 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 | 57 |
|
| 58 | + return [ |
| 59 | + DefaultInfo( |
| 60 | + files = depset(ctx.files.srcs), |
| 61 | + ), |
| 62 | + rust_common.stdlib_info( |
| 63 | + std_rlibs = std_rlibs, |
| 64 | + dot_a_files = dot_a_files, |
| 65 | + between_alloc_and_core_files = between_alloc_and_core_files, |
| 66 | + core_files = core_files, |
| 67 | + between_core_and_std_files = between_core_and_std_files, |
| 68 | + std_files = std_files, |
| 69 | + alloc_files = alloc_files, |
| 70 | + ), |
| 71 | + ] |
| 72 | + |
| 73 | +rust_stdlib_filegroup = rule( |
| 74 | + doc = "A dedicated filegroup-like rule for Rust stdlib artifacts.", |
| 75 | + implementation = _rust_stdlib_filegroup_impl, |
| 76 | + attrs = { |
| 77 | + "srcs": attr.label_list( |
| 78 | + allow_files = True, |
| 79 | + doc = "The list of targets/files that are components of the rust-stdlib file group", |
| 80 | + mandatory = True, |
| 81 | + ), |
| 82 | + }, |
| 83 | +) |
| 84 | + |
| 85 | +def _ltl(library, ctx, cc_toolchain, feature_configuration): |
| 86 | + return cc_common.create_library_to_link( |
| 87 | + actions = ctx.actions, |
| 88 | + feature_configuration = feature_configuration, |
| 89 | + cc_toolchain = cc_toolchain, |
| 90 | + static_library = library, |
| 91 | + pic_static_library = library, |
| 92 | + ) |
| 93 | + |
| 94 | +def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library): |
| 95 | + """Make the CcInfo (if possible) for libstd and allocator libraries. |
| 96 | +
|
| 97 | + Args: |
| 98 | + ctx (ctx): The rule's context object. |
| 99 | + rust_lib: The rust standard library. |
| 100 | + allocator_library: The target to use for providing allocator functions. |
| 101 | +
|
| 102 | +
|
| 103 | + Returns: |
| 104 | + A CcInfo object for the required libraries, or None if no such libraries are available. |
| 105 | + """ |
| 106 | + cc_toolchain, feature_configuration = find_cc_toolchain(ctx) |
| 107 | + link_inputs = [] |
| 108 | + |
| 109 | + if not rust_common.stdlib_info in ctx.attr.rust_lib: |
| 110 | + fail(dedent("""\ |
| 111 | + {} -- |
| 112 | + The `rust_lib` must be a target providing `rust_common.stdlib_info` |
| 113 | + (typically `rust_stdlib_filegroup` rule from @rules_rust//rust:defs.bzl). |
| 114 | + See https://github.com/bazelbuild/rules_rust/pull/802 for more information. |
| 115 | + |
| 116 | + """).format(ctx.label)) |
| 117 | + rust_stdlib_info = ctx.attr.rust_lib[rust_common.stdlib_info] |
| 118 | + |
| 119 | + if rust_stdlib_info.std_rlibs: |
74 | 120 | alloc_inputs = depset(
|
75 |
| - [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in alloc_files], |
| 121 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files], |
76 | 122 | )
|
77 | 123 | between_alloc_and_core_inputs = depset(
|
78 |
| - [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in between_alloc_and_core_files], |
| 124 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.between_alloc_and_core_files], |
79 | 125 | transitive = [alloc_inputs],
|
80 | 126 | order = "topological",
|
81 | 127 | )
|
82 | 128 | core_inputs = depset(
|
83 |
| - [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in core_files], |
| 129 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.core_files], |
84 | 130 | transitive = [between_alloc_and_core_inputs],
|
85 | 131 | order = "topological",
|
86 | 132 | )
|
87 | 133 | between_core_and_std_inputs = depset(
|
88 |
| - [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in between_core_and_std_files], |
| 134 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.between_core_and_std_files], |
89 | 135 | transitive = [core_inputs],
|
90 | 136 | order = "topological",
|
91 | 137 | )
|
92 | 138 | std_inputs = depset(
|
93 |
| - [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in std_files], |
| 139 | + [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.std_files], |
94 | 140 | transitive = [between_core_and_std_inputs],
|
95 | 141 | order = "topological",
|
96 | 142 | )
|
|
0 commit comments