cc_import dependency in custom rule fails

556 views
Skip to first unread message

Gregg Reynolds

unread,
Sep 23, 2021, 10:01:03 AM9/23/21
to bazel-discuss
I've got a rule that takes cc dependencies.  If I pass it a cc_import target, I get nothing - DefaultInfo.files is empty.

My workaround is to use filegroup.  But I'd rather use cc_import only.

Is there some way for a custom rule to get the output from cc_import?  According to Integrating with C++ rules, "Through [CcInfo, CompilationContext, LinkingContext] it is possible to access information such as all transitive headers or libraries to link.", but of course it offers no clue as to how to do this. Most of this stuff is effectively undocumented. I've poked around the source code, but I can't see how to proceed.  Any help would be appreciated.

Gregg

yalun shi

unread,
Sep 26, 2021, 2:29:35 PM9/26/21
to Gregg Reynolds, bazel-discuss
bazel3.7.2
tensorflow2.6.0
When I execute the following command, an error occurs:
bazel build //tensorflow/tools/android/inference_interface:libtensorflow_inference.so  --crosstool_top=//external:android/crosstool   --host_crosstool_top=@bazel_tools//tools/cpp:toolchain  --cpu=armeabi-v7a

error:
INFO: Repository 'go_sdk' used the following cache hits instead of downloading the corresponding file.
 * Hash 'aea86e3c73495f205929cfebba0d63f1382c8ac59be081b6351681415f4063cf' for https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
If the definition of 'go_sdk' was updated, verify that the hashes were also updated.
ERROR: An error occurred during the fetch of repository 'go_sdk':
   Traceback (most recent call last):
        File "/home/shiyalun/.cache/bazel/_bazel_shiyalun/e734bd7599f02ff604855d4be6fbc13c/external/io_bazel_rules_go/go/private/sdk.bzl", line 51, column 16, in _go_download_sdk_impl
                _remote_sdk(ctx, [url.format(filename) for url in ctx.attr.urls], ctx.attr.strip_prefix, sha256)
        File "/home/shiyalun/.cache/bazel/_bazel_shiyalun/e734bd7599f02ff604855d4be6fbc13c/external/io_bazel_rules_go/go/private/sdk.bzl", line 128, column 17, in _remote_sdk
                fail("error extracting Go SDK:\n" + res.stdout + res.stderr)
Error in fail: error extracting Go SDK:
Timed out
ERROR: Analysis of target '//tensorflow/tools/android/inference_interface:libtensorflow_inference.so' failed; build aborted: error extracting Go SDK:








--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAO40Mi%3DJmZdGyJEdQaZhyLudKcfUGRaDVdg77Zy38TTM0Sot0g%40mail.gmail.com.

Herrmann, Andreas

unread,
Sep 27, 2021, 8:02:25 AM9/27/21
to yalun shi, Gregg Reynolds, bazel-discuss
Hi Gregg,

You could try if using cc_library instead of cc_import helps in this case, i.e. add the .a|.so file to the srcs attribute of a cc_library rule instead of the static|shared_library of a cc_import. See the following thread for some other motivations for using cc_library in place of cc_import. https://groups.google.com/g/bazel-discuss/c/pQLmBVNJGZ4/m/lpsJxEhdFQAJ

The docs are not fully linked through, but, I think all relevant providers are documented in the bazel docs. If a link is missing you can often infer the provider name from the attribute name or the docstring.

I hope that helps.

Best, Andreas

Gregg Reynolds

unread,
Sep 27, 2021, 9:51:28 AM9/27/21
to Herrmann, Andreas, yalun shi, bazel-discuss
On Mon, Sep 27, 2021 at 7:02 AM Herrmann, Andreas <andreas....@tweag.io> wrote:
Hi Gregg,

You could try if using cc_library instead of cc_import helps in this case, i.e. add the .a|.so file to the srcs attribute of a cc_library rule instead of the static|shared_library of a cc_import. See the following thread for some other motivations for using cc_library in place of cc_import. https://groups.google.com/g/bazel-discuss/c/pQLmBVNJGZ4/m/lpsJxEhdFQAJ

Thanks.  The solution turns out to be very easy, just use the latest experimental cc_import:

load("@bazel_tools//tools/build_defs/cc:cc_import.bzl", "cc_import")

and 

--experimental_starlark_cc_import

Then the rule looks like this:

lctx = dep[CcInfo].linking_context
for linputs in  lctx.linker_inputs.to_list():
    for lib in linputs.libraries:
        if lib.static_library:
            print(" LINKLIB: %s" % lib.static_library)

There's no documentation for the new cc_import but the code is easy to read:  https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_import.bzl
Reply all
Reply to author
Forward
0 new messages