Skip to content

Commit c2f5701

Browse files
authored
Add support for self-contained object files (musl). (#829)
1 parent 568d47a commit c2f5701

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

rust/private/providers.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ StdLibInfo = provider(
5959
"between_core_and_std_files": "List[File]: `.a` files related to all modules except `adler`, `alloc`, `compiler_builtins`, `core`, and `std`.",
6060
"core_files": "List[File]: `.a` files related to the `core` and `adler` modules",
6161
"dot_a_files": "Depset[File]: Generated `.a` files",
62+
"self_contained_files": "List[File]: All `.o` files from the `self-contained` directory.",
6263
"srcs": "List[Target]: The original `src` attribute.",
6364
"std_files": "Depset[File]: `.a` files associated with the `std` module.",
6465
"std_rlibs": "List[File]: All `.rlib` files",

rust/private/repository_utils.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ rust_stdlib_filegroup(
147147
"lib/rustlib/{target_triple}/lib/*.rlib",
148148
"lib/rustlib/{target_triple}/lib/*{dylib_ext}",
149149
"lib/rustlib/{target_triple}/lib/*{staticlib_ext}",
150+
"lib/rustlib/{target_triple}/lib/self-contained/**",
150151
],
151152
# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245
152153
allow_empty = True,

rust/toolchain.bzl

+43-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ def _rust_stdlib_filegroup_impl(ctx):
1111
between_core_and_std_files = []
1212
std_files = []
1313
alloc_files = []
14+
self_contained_files = [
15+
file
16+
for file in rust_lib
17+
if file.basename.endswith(".o") and "self-contained" in file.path
18+
]
1419

1520
std_rlibs = [f for f in rust_lib if f.basename.endswith(".rlib")]
1621
if std_rlibs:
@@ -53,6 +58,7 @@ def _rust_stdlib_filegroup_impl(ctx):
5358
between_core_and_std_files = between_core_and_std_files,
5459
std_files = std_files,
5560
alloc_files = alloc_files,
61+
self_contained_files = self_contained_files,
5662
),
5763
]
5864

@@ -101,7 +107,7 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
101107
A CcInfo object for the required libraries, or None if no such libraries are available.
102108
"""
103109
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
104-
link_inputs = []
110+
cc_infos = []
105111

106112
if not rust_common.stdlib_info in ctx.attr.rust_lib:
107113
fail(dedent("""\
@@ -112,6 +118,23 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
112118
""").format(ctx.label, ctx.attr.rust_lib))
113119
rust_stdlib_info = ctx.attr.rust_lib[rust_common.stdlib_info]
114120

121+
if rust_stdlib_info.self_contained_files:
122+
compilation_outputs = cc_common.create_compilation_outputs(
123+
objects = depset(rust_stdlib_info.self_contained_files),
124+
)
125+
126+
linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
127+
name = ctx.label.name,
128+
actions = ctx.actions,
129+
feature_configuration = feature_configuration,
130+
cc_toolchain = cc_toolchain,
131+
compilation_outputs = compilation_outputs,
132+
)
133+
134+
cc_infos.append(CcInfo(
135+
linking_context = linking_context,
136+
))
137+
115138
if rust_stdlib_info.std_rlibs:
116139
alloc_inputs = depset(
117140
[_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files],
@@ -160,22 +183,29 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
160183
order = "topological",
161184
)
162185

163-
link_inputs.append(cc_common.create_linker_input(
186+
link_inputs = cc_common.create_linker_input(
164187
owner = rust_lib.label,
165188
libraries = std_inputs,
166-
))
189+
)
167190

168-
allocator_inputs = None
169-
if allocator_library:
170-
allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs]
191+
allocator_inputs = None
192+
if allocator_library:
193+
allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs]
194+
195+
cc_infos.append(CcInfo(
196+
linking_context = cc_common.create_linking_context(
197+
linker_inputs = depset(
198+
[link_inputs],
199+
transitive = allocator_inputs,
200+
order = "topological",
201+
),
202+
),
203+
))
171204

172-
libstd_and_allocator_ccinfo = None
173-
if link_inputs:
174-
return CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset(
175-
link_inputs,
176-
transitive = allocator_inputs,
177-
order = "topological",
178-
)))
205+
if cc_infos:
206+
return cc_common.merge_cc_infos(
207+
direct_cc_infos = cc_infos,
208+
)
179209
return None
180210

181211
def _rust_toolchain_impl(ctx):

0 commit comments

Comments
 (0)