Skip to content

Commit

Permalink
rules_rust: make it possible to use non-staticlib rust_library target…
Browse files Browse the repository at this point in the history
…s in c++ deps

ld can handle .rlib archives, since they're really just standard ar(1)
archives like a .a file with some extra metadata. As a result, we can avoid
using staticlib crates when building mixed C++/Rust binaries.

This doesn't correctly handle libstd et al (yet), or deal with some generated
symbols that rustc produces when it drives the linking step, but it's a start.
  • Loading branch information
durin42 committed Feb 23, 2021
1 parent 32ad192 commit b1fb67d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
list: A list containing the CcInfo provider
"""

if crate_info.is_test or crate_info.type not in ("staticlib", "cdylib") or getattr(ctx.attr, "out_binary", False):
if crate_info.is_test or crate_info.type not in ("staticlib", "cdylib", "rlib", "lib") or getattr(ctx.attr, "out_binary", False):
return []

if crate_info.type == "staticlib":
Expand All @@ -655,6 +655,18 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
cc_toolchain = cc_toolchain,
static_library = crate_info.output,
)
elif crate_info.type in ("rlib", "lib"):
# bazel hard-codes a check for endswith((".a", ".pic.a",
# ".lib")) in create_library_to_link, so we work around that
# by creating a symlink to the .rlib with a .a extension.
dot_a = ctx.actions.declare_file(crate_info.name + ".a", sibling = crate_info.output)
ctx.actions.symlink(output = dot_a, target_file = crate_info.output)
library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = dot_a,
)
elif crate_info.type == "cdylib":
library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
Expand Down

0 comments on commit b1fb67d

Please sign in to comment.