I'm currently debugging an issue in rules_haskell where if I have the following dependency graph,
cc_library(A)
^
|
haskell_library(B)
^
|
cc_binary(C)
then C links in B and A dynamically even when I set linkstatic = True on C. If instead I have,
cc_library(A)
^
|
cc_library(B)
^
|
cc_binary(C)
then C links B and A statically as expected. I use the C++ Starlark API to interoperate with the CC rules. If I do not include a dynamic library in the linking_context, then everything works fine, with static linking as expected. If I include *both* a static and a dynamic library in the linking_context for B, then C for some reasons prefers the dynamic library.
Any idea why this might happen? I've been trying to minimize my code but it's very difficult to reproduce the issue. What makes it harder still to debug is that LibraryToLink and other data structures are essentially opaque (no fields shown when I print them). So I don't know what providers I'm constructing differently than a cc_library rule would. I also get NullPointerException in some situations, though again, very hard to minimize.
What is the logic for selecting shared libraries from dependencies vs static libraries? Are static libraries always preferred unless linkstatic = False? Are there some circumstances in which dynamic libraries will be preferred?
--
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/8d9e6611-9474-4d3d-91a5-5479b67a22a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
> If I include *both* a static and a dynamic library in the linking_context for B, then C for some reasons prefers the dynamic library.
>
> Any idea why this might happen?
The reason seems to be that cc_binary uses "mostly static" linking by default, whereas surprisingly, cc_test uses dynamic linking by default. In a cc_test, unless you set `linkstatic = True`, then it will pick up the dynamic versions of its dependencies given a choice.
I came across this by pure chance, when grepping through the source code of Bazel looking for something else. I think that what I am observing is a consequence of this snippet, although I could be wrong: https://github.com/bazelbuild/bazel/blob/6ca40212f660c176086f53ec4548eba8f617e574/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java#L364-L373.
Moreover, this difference does seem to be documented, although subtly: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_binary.linkstatic.
So my question is now:
* What is the motivation for this difference in behaviour between cc_binary and cc_test?
--
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/b50436aa-b5bf-4ca5-bce0-4aff0f0f1b3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.