commercialhaskell / commercialhaskell/stack

Compile all dependencies with -fPIC for a statically linked foreign library

Open
#5,458 3 comments 0 reactions 0 assignees View on GitHub
type: question
Dominant language
Haskell
Stars
4.1k
Forks
850
Avg merge
10h 37m
Merged PRs (30d)
4

Description

Hi! I have a Haskell project that I'd like to redistribute as an `.so` library with a C interface, but without any assumptions on Haskell being installed on the user machine.

To get the `.so` I use a `foreign-library` declaration in my Cabal file. For as long as I'm ok with dynamically linking all my dependencies, Stack seems to correctly recognize the library and build it. (One annoyance is that it doesn't install it be default when running `stack install`, but I've worked around that).

However, that doesn't give me a self-contained library, because `ldd` prints a ton of Haskell shared libs when I query the artifact. To help with that I tried adding `ghc-options: -static -optc-static -optl-static` to my `foreign-library` target, but this results in a huge log of errors from the linker (a selection of messages below, they're all quite similar):
```
/usr/bin/ld.gold: error: /home/apaszke/.stack/snapshots/x86_64-linux/42bb23596753746b1bb05fcdf285ae58aeafdf21d8f4b6d2c2efb42533aaf2e9/8.6.5/lib/x86_64-linux-ghc-8.6.5/store-0.7.2-DiSfahOiFAkIMjYQUgof09/libHSstore-0.7.2-DiSfahOiFAkIMjYQUgof09.a(Internal.o): requires unsupported dynamic reloc 11; recompile with -fPIC
...
/usr/bin/ld.gold: error: /home/apaszke/.stack/programs/x86_64-linux/ghc-8.6.5/lib/ghc-8.6.5/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0.a(Type.o): requires unsupported dynamic reloc 11; recompile with -fPIC
...
```
This seems to imply that all the static libraries stack produces are built without `-fPIC`, which makes sense because this is the default use case, but it completely breaks down for me. Is there any way to override this decision and have stack build _both_ shared and static libraries with `-fPIC` (and have it apply to all GHC libraries, all my dependencies, and my project)?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.