rules_foreign_cc linker behaviour

165 views
Skip to first unread message

Sam Bristow

unread,
Sep 13, 2023, 10:27:56 PM9/13/23
to bazel-discuss
Does anyone have an example of a library with rules_foreign_cc which exposes both  `out_shared_libs` and `out_static_libs`?

As far as I can tell, if a library has both defined then targets which depend in it will link in *both.

```
bazel-out/k8-fastbuild/bin/external/arrow/arrow_lib/lib/libarrow.a
-l:libarrow.so.1100
```

We have a couple of large libraries that are built using rules_foreign_cc and it would really help keep the size of our test binaries under control if we could have these dynamically linked while still keeping the actual cc_binary targets statically linked.
 
Is there something I'm missing? There are very few examples I can find online where libraries are providing both shared and static versions.
 
Thanks
Sam

Filip Filmar

unread,
Sep 13, 2023, 10:38:41 PM9/13/23
to Sam Bristow, bazel-discuss
On Wed, Sep 13, 2023 at 7:27 PM Sam Bristow <s.br...@rocketlab.co.nz> wrote:
Is there something I'm missing? There are very few examples I can find online where libraries are providing both shared and static versions.

I think the outputs are sorted into different output groups, so you may be able to get only the ones you need using the `filegroup(... output_group = "...")`.


F

Sam Bristow

unread,
Sep 14, 2023, 7:48:36 PM9/14/23
to bazel-discuss
I'm not sure how that will help with my problem unfortunately. The dependency between my code and the targets provided by `rules_foreign_cc` is in some library code that is called by both my `cc_binary` targets and my `cc_test` targets.

I've managed to dig a bit deeper into what is going on:

| out_static_libs  | out_shared_libs | Effect |
| ---------------- | --------------- | ------ |
| foo.a            | -               | Everything is statically linked,  |
| foo.a            | foo.so          | Dynamically linked but tests fail at runtime with linker error |
| foo.a            | foo.so.1        | Everything is statically linked + fails at runtime with linker error |
| -                | foo.so.1        | Small files, cc_binary is also dynamically linked |

So it seems like if I declare the out_shared_libs as a `foo.so` file Bazel's static/dynamic linking logic works. But it's trying to link against the versioned soname `foo.so.1` which isn't copied into the runfiles.
If I declare the out_shared_libs with the soname `foo.so.1` then Bazel links the library in twice.

I've found a couple of related issues but I still don't quite know what the next step is.

S
Reply all
Reply to author
Forward
0 new messages