Skip to content

Add support for self-contained object files (musl). #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
@@ -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",
1 change: 1 addition & 0 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
@@ -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,
56 changes: 43 additions & 13 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -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:
@@ -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,
),
]

@@ -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("""\
@@ -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],
@@ -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):