Skip to content

Commit

Permalink
Add support for self-contained object files (musl). (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jul 30, 2021
1 parent 568d47a commit c2f5701
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ StdLibInfo = provider(
"between_core_and_std_files": "List[File]: `.a` files related to all modules except `adler`, `alloc`, `compiler_builtins`, `core`, and `std`.",
"core_files": "List[File]: `.a` files related to the `core` and `adler` modules",
"dot_a_files": "Depset[File]: Generated `.a` files",
"self_contained_files": "List[File]: All `.o` files from the `self-contained` directory.",
"srcs": "List[Target]: The original `src` attribute.",
"std_files": "Depset[File]: `.a` files associated with the `std` module.",
"std_rlibs": "List[File]: All `.rlib` files",
Expand Down
1 change: 1 addition & 0 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ rust_stdlib_filegroup(
"lib/rustlib/{target_triple}/lib/*.rlib",
"lib/rustlib/{target_triple}/lib/*{dylib_ext}",
"lib/rustlib/{target_triple}/lib/*{staticlib_ext}",
"lib/rustlib/{target_triple}/lib/self-contained/**",
],
# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245
allow_empty = True,
Expand Down
56 changes: 43 additions & 13 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def _rust_stdlib_filegroup_impl(ctx):
between_core_and_std_files = []
std_files = []
alloc_files = []
self_contained_files = [
file
for file in rust_lib
if file.basename.endswith(".o") and "self-contained" in file.path
]

std_rlibs = [f for f in rust_lib if f.basename.endswith(".rlib")]
if std_rlibs:
Expand Down Expand Up @@ -53,6 +58,7 @@ def _rust_stdlib_filegroup_impl(ctx):
between_core_and_std_files = between_core_and_std_files,
std_files = std_files,
alloc_files = alloc_files,
self_contained_files = self_contained_files,
),
]

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

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

if rust_stdlib_info.self_contained_files:
compilation_outputs = cc_common.create_compilation_outputs(
objects = depset(rust_stdlib_info.self_contained_files),
)

linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs(
name = ctx.label.name,
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
compilation_outputs = compilation_outputs,
)

cc_infos.append(CcInfo(
linking_context = linking_context,
))

if rust_stdlib_info.std_rlibs:
alloc_inputs = depset(
[_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files],
Expand Down Expand Up @@ -160,22 +183,29 @@ def _make_libstd_and_allocator_ccinfo(ctx, rust_lib, allocator_library):
order = "topological",
)

link_inputs.append(cc_common.create_linker_input(
link_inputs = cc_common.create_linker_input(
owner = rust_lib.label,
libraries = std_inputs,
))
)

allocator_inputs = None
if allocator_library:
allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs]
allocator_inputs = None
if allocator_library:
allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs]

cc_infos.append(CcInfo(
linking_context = cc_common.create_linking_context(
linker_inputs = depset(
[link_inputs],
transitive = allocator_inputs,
order = "topological",
),
),
))

libstd_and_allocator_ccinfo = None
if link_inputs:
return CcInfo(linking_context = cc_common.create_linking_context(linker_inputs = depset(
link_inputs,
transitive = allocator_inputs,
order = "topological",
)))
if cc_infos:
return cc_common.merge_cc_infos(
direct_cc_infos = cc_infos,
)
return None

def _rust_toolchain_impl(ctx):
Expand Down

0 comments on commit c2f5701

Please sign in to comment.