From 77675aebd702493b37babc2c8abbcaccb2628323 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Wed, 24 Feb 2021 12:25:54 -0500 Subject: [PATCH 1/4] cleanup: remove stray trailing whitespace My editor is configured to clean up trailing whitespace, and I'm tired of having to back out these changes when I commit. --- rust/private/rust.bzl | 4 ++-- rust/private/rustc.bzl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 17131c6b9e..09a1c40779 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -643,8 +643,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. """), ), diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index aa2df0569f..65d605f75b 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -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 From b37dacdb7982c179b1b32267e159818b8688e364 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Wed, 24 Feb 2021 12:26:44 -0500 Subject: [PATCH 2/4] rust: document that rust_{shared,static}_library provide CcInfo --- rust/private/rust.bzl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 09a1c40779..4e176785ad 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -149,6 +149,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 @@ -160,6 +163,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 From a1836cf2659b76ca9979413c309c91e7eadc42d8 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Thu, 18 Feb 2021 11:35:30 -0500 Subject: [PATCH 3/4] rules_rust: make it possible to use non-staticlib rust_library targets 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. --- rust/private/rust.bzl | 4 ++++ rust/private/rustc.bzl | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 4e176785ad..e0088a1092 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -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 diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 65d605f75b..cfcdeaa7ac 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -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": @@ -655,6 +655,19 @@ 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) + # 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, + ) elif crate_info.type == "cdylib": library_to_link = cc_common.create_library_to_link( actions = ctx.actions, From 92406e4b81d2fc3c07b92c5f8843b96707b988a3 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Wed, 24 Feb 2021 12:36:39 -0500 Subject: [PATCH 4/4] rustc: also list rlibs as pic_static_library On most platforms the default output from rustc is PIC code[0], so this should be safe. 0: https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model --- rust/private/rustc.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index cfcdeaa7ac..26490cbd6c 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -667,6 +667,7 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur feature_configuration = feature_configuration, cc_toolchain = cc_toolchain, static_library = dot_a, + pic_static_library = dot_a, ) elif crate_info.type == "cdylib": library_to_link = cc_common.create_library_to_link(