pkg-config support insufficient for certain static linking scenarios
- Dominant language
- Haskell
- Stars
- 1.7k
- Forks
- 750
- Avg merge
- 4d 3h
- Merged PRs (30d)
- 28
Description
**Describe the bug**
Cabal unconditionally invokes `pkg-config --libs` when collecting arguments, which is not sufficient for certain static linking scenarios:
https://github.com/haskell/cabal/blob/00a2351789a460700a2567eb5ecc42cca0af913f/Cabal/src/Distribution/Simple/Configure.hs#L1640
When statically linking, it should use `pkg-config --static --libs` instead.
**To Reproduce**
1. Download [`hw-kafka-client`](https://github.com/haskell-works/hw-kafka-client) and change its `extra-libraries: rdkafka` to `pkgconfig-depends: rdkafka`.
2. Create a package with `build-depends: hw-kafka-client`, use a `cabal.project` file to have the test package use your local `hw-kafka-client` and attempt to build it using a musl64 cross-toolchain (easiest to use `haskell.nix` for this). Expect to see unresolved symbol errors of the form:
```
/nix/store/b07pcb91l2q1iasnqj74bajikd43b714-rdkafka-1.6.0-x86_64-unknown-linux-musl/lib/librdkafka.a(rdkafka_ssl.o):function rd_kafka_ssl_ctx_init: error: undefined reference to 'OpenSSL_version_num'
```
(The failure can be worked around with `extra-libraries: rdkafka ssl crypto z`.)
Cause of failure:
* The linker command line mentions `-lrdkafka` but not `-lssl -lcrypto`, which are required by objects in `librdkafka.a`.
* `rdkafka.pc` correctly lists the `-lssl` etc in `Libs.private`.
* Reading between the lines of the [`pkg-config` manual](https://people.freedesktop.org/~dbn/pkg-config-guide.html#writing), the `Libs.private` are only added when statically linking. You can confirm this by running `pkg-config --static --libs rdkafka` and `pkg-config --libs rdkafka` and comparing their output.
* Cabal only ever invokes `pkg-config --libs` as far as I can tell (see link at top of issue), so the musl64 build fails.
A package that has `build-depends: hw-kafka-client` will not statically link (using `musl64` cross-toolchain from `haskell.nix`) due to missing `-lssl` and `-lcrypto` on the linker command-line.
**Expected behavior**
Additional flags for static linking should make their way through to the linker command line, and the program should build without errors.
Contributor guide
Assessment
This issue has not been assessed yet.