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
+ dot_a = ctx .actions .declare_file (f .basename + ".a" , sibling = f )
10
+ ctx .actions .symlink (output = dot_a , target_file = f )
11
+ return dot_a
12
+
3
13
def _rust_toolchain_impl (ctx ):
4
14
"""The rust_toolchain implementation
5
15
@@ -17,6 +27,42 @@ def _rust_toolchain_impl(ctx):
17
27
for k , v in ctx .attr .debug_info .items ():
18
28
if not k in ctx .attr .opt_level :
19
29
fail ("Compilation mode {} is not defined in opt_level but is defined debug_info" .format (k ))
30
+ link_inputs = []
31
+ std_rlibs = [f for f in ctx .attr .rust_lib .files .to_list () if f .basename .endswith (".rlib" )]
32
+ cc_toolchain , feature_configuration = find_cc_toolchain (ctx )
33
+ if std_rlibs :
34
+ link_inputs .append (cc_common .create_linker_input (
35
+ owner = ctx .attr .rust_lib .label ,
36
+ libraries = depset (
37
+ [
38
+ cc_common .create_library_to_link (
39
+ # TODO(augie): wat? this needs no actions
40
+ actions = ctx .actions ,
41
+ feature_configuration = feature_configuration ,
42
+ cc_toolchain = cc_toolchain ,
43
+ static_library = _make_dota (ctx , f ),
44
+ )
45
+ for f in std_rlibs
46
+ ],
47
+ ),
48
+ ))
49
+ if ctx .attr .allocator_library :
50
+ link_inputs .append (cc_common .create_linker_input (
51
+ owner = ctx .attr .allocator_library .allocator_library .label ,
52
+ libraries = depset ([
53
+ cc_common .create_library_to_link (
54
+ # TODO(augie): wat? this needs no actions
55
+ actions = ctx .actions ,
56
+ feature_configuration = feature_configuration ,
57
+ cc_toolchain = cc_toolchain ,
58
+ static_library = f ,
59
+ )
60
+ for f in ctx .attr .allocator_library .files .to_list ()
61
+ ]),
62
+ ))
63
+ extra_libstd_and_allocator_ccinfo = None
64
+ if link_inputs :
65
+ extra_libstd_and_allocator_ccinfo = CcInfo (linking_context = cc_common .create_linking_context (linker_inputs = depset (link_inputs )))
20
66
21
67
toolchain = platform_common .ToolchainInfo (
22
68
rustc = ctx .file .rustc ,
@@ -38,11 +84,13 @@ def _rust_toolchain_impl(ctx):
38
84
default_edition = ctx .attr .default_edition ,
39
85
compilation_mode_opts = compilation_mode_opts ,
40
86
crosstool_files = ctx .files ._crosstool ,
87
+ extra_libstd_and_allocator_ccinfo = extra_libstd_and_allocator_ccinfo ,
41
88
)
42
89
return [toolchain ]
43
90
44
91
rust_toolchain = rule (
45
92
implementation = _rust_toolchain_impl ,
93
+ fragments = ["cpp" ],
46
94
attrs = {
47
95
"binary_ext" : attr .string (
48
96
doc = "The extension for binaries created from rustc." ,
@@ -131,6 +179,12 @@ rust_toolchain = rule(
131
179
"_crosstool" : attr .label (
132
180
default = Label ("@bazel_tools//tools/cpp:current_cc_toolchain" ),
133
181
),
182
+ "_cc_toolchain" : attr .label (
183
+ default = "@bazel_tools//tools/cpp:current_cc_toolchain" ,
184
+ ),
185
+ "allocator_library" : attr .label (
186
+ doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." ,
187
+ ),
134
188
},
135
189
doc = """Declares a Rust toolchain for use.
136
190
0 commit comments