Skip to content
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

rules_rust: make it possible to use non-staticlib rust_library targets in c++ deps #603

Merged
merged 4 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def _shortest_src_with_basename(srcs, basename):
def _rust_library_impl(ctx):
"""The implementation of the `rust_library` rule.

This rule provides CcInfo, so it can be used everywhere Bazel
expects rules_cc, but care must be taken to have the correct
dependencies on an allocator and std implemetation as needed.

Args:
ctx (ctx): The rule's context object

Expand All @@ -149,6 +153,9 @@ def _rust_library_impl(ctx):
def _rust_static_library_impl(ctx):
"""The implementation of the `rust_static_library` rule.

This rule provides CcInfo, so it can be used everywhere Bazel
expects rules_cc.

Args:
ctx (ctx): The rule's context object

Expand All @@ -160,6 +167,9 @@ def _rust_static_library_impl(ctx):
def _rust_shared_library_impl(ctx):
"""The implementation of the `rust_shared_library` rule.

This rule provides CcInfo, so it can be used everywhere Bazel
expects rules_cc.

Args:
ctx (ctx): The rule's context object

Expand Down Expand Up @@ -643,8 +653,8 @@ _rust_test_attrs = {
"env": attr.string_dict(
mandatory = False,
doc = _tidy("""
Specifies additional environment variables to set when the test is executed by bazel test.
Values are subject to `$(execpath)` and
Specifies additional environment variables to set when the test is executed by bazel test.
Values are subject to `$(execpath)` and
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
"""),
),
Expand Down
18 changes: 16 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def rustc_compile_action(
]

def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configuration):
"""If the produced crate is suitable yield a CcInfo to allow for interop with cc rules
"""If the produced crate is suitable yield a CcInfo to allow for interop with cc rules

Args:
ctx (ctx): The rule's context object
Expand All @@ -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):
durin42 marked this conversation as resolved.
Show resolved Hide resolved
return []

if crate_info.type == "staticlib":
Expand All @@ -655,6 +655,20 @@ 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)
durin42 marked this conversation as resolved.
Show resolved Hide resolved
# TODO(hlopko): handle PIC/NOPIC correctly
library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = dot_a,
hlopko marked this conversation as resolved.
Show resolved Hide resolved
pic_static_library = dot_a,
)
elif crate_info.type == "cdylib":
library_to_link = cc_common.create_library_to_link(
actions = ctx.actions,
Expand Down