bazel will only link to system libraries if you explicitly tell it to --- as you say, linkopts = [ "-lpng" ].
The issue I'm facing is situations like with rules_proto:
- the developer wants to use protobufs in their program.
- they pull in rules_proto (as is recommended).
- rules_proto pulls in com_github_protocolbuffers_protobuf 3.20.0.
- this is compiled for both the host, for use in protoc, and for the target.
- target binaries are then linked against this local version of com_github_protocolbuffers_protobuf.
(This linking may be static or dynamic-to-a-.so-in-runfiles, which is essentially the same thing.)
Now,
with my package maintainer hat on, I need to make this produce legal
binaries to go into a package. If the binary uses
dynamic-to-a-.so-in-runfiles linking, then it's theoretically possible
to pull the binary out and point it at the system shared library; but on
the Debian system I'm typing this on, the latest version of
libprotobuf-dev is 3.12.4-1+build7, which is rather older than the one
bazel used, and even then the idea of distributing official binaries
which are built against a different version of the headers than the
library they're linked to is terrible. It's not as if, as a maintainer, I
can patch out the dependency on protobuf 3.20.0 --- that dependency is
buried deep inside rules_proto, which is in turn pulled from an external
repository. Remember, I am not the author here; I'm just the
maintainer.
As a Debian maintainer I always want binaries to be built against system libraries except when absolutely necessary. But it looks like bazel is designed to never build against system libraries (unless the developer opted for this up front). I don't think these design goals are compatible.
I
recently discovered that meson actually has support for this built in;
the developer can declare a dependency on an embedded library
('subproject' in meson terminology), which is then distributed along
with the source. But, if a system library version of the dependency is
available, then that is used in preference to the embedded subproject.
Is anything like that available in bazel?