From c2f5701dad159c3ce2b5997e6599445b1d21a123 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Fri, 30 Jul 2021 06:34:52 -0700 Subject: [PATCH] Add support for `self-contained` object files (musl). (#829) --- rust/private/providers.bzl | 1 + rust/private/repository_utils.bzl | 1 + rust/toolchain.bzl | 56 ++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/rust/private/providers.bzl b/rust/private/providers.bzl index adb209ef10..4879b0f8d7 100644 --- a/rust/private/providers.bzl +++ b/rust/private/providers.bzl @@ -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", diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl index a6b8cd8b24..cc76675370 100644 --- a/rust/private/repository_utils.bzl +++ b/rust/private/repository_utils.bzl @@ -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, diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index d25937fa2e..2a24c18457 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -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):