Hi there. My actual main goal is to build my own shared library written in Haskell that would be compatible with application written in C even without knowing that is is written in Haskell. So for now I compiled my shared library but I only could dynamically link it to Haskell dependencies such as "base" and "ghc-prim" packages. But I want to statically link Haskell dependencies but I realized it isn't simple and straightforward task.

On Freenode's #haskell I was advised I should build GHC from scratch with -fPIC, on the Linux (I'm using Fedora Workstation 25 on x86_64) I couldn't go forward without this step. So I wrote some Dockerfile based on Debian 9, skipping first part which is containing 'apt-get update' and installing 'build-essential' here is what I have:

COPY my-build.mk /my-build.mk

RUN mkdir /compile && cd /compile \
    && wget https://downloads.haskell.org/~ghc/8.2.2/ghc-8.2.2-src.tar.xz \
    && tar -xvf ghc-8.2.2-src.tar.xz \
    && rm ghc-8.2.2-src.tar.xz \
    && cd ghc-8.2.2/ \
    && ./configure --prefix=/ghc-8.2.2-fpic --disable-library-profiling --enable-shared \
    && cp /my-build.mk mk/build.mk \
    && make install \
    && cd /usr/local/bin \
    && ls /ghc-8.2.2-fpic/bin/ | xargs -I{} ln -s /ghc-8.2.2-fpic/bin/{}

And as you can see I just use my own prepared my-build.mk file which is:

SRC_HC_OPTS          = -H64m -O
EXTRA_HC_OPTS        = -fPIC
SRC_CC_OPTS          = -fPIC -O
GhcStage1HcOpts      = -fasm -O0
GhcStage2HcOpts      = -fasm -O0
GhcLibHcOpts         = -fasm -O2
GhcLibWays           = v dyn
DYNAMIC_GHC_PROGRAMS = YES
DYNAMIC_BY_DEFAULT   = NO
SplitObjs            = NO
HADDOCK_DOCS         = NO
BUILD_DOCBOOK_HTML   = NO
BUILD_DOCBOOK_PS     = NO
BUILD_DOCBOOK_PDF    = NO
V                    = 1
LATEX_DOCS           = NO
HSCOLOUR_SRCS        = NO
BeConservative       = YES

I just combined it from parts I found in the internet during searching answers to my questions. So I built this container, I also installed dependencies by this commands:

cd /mnt
cabal update
cabal sandbox init
cabal install --enable-shared --ghc-option=-fPIC happy alex
cabal install --enable-shared --ghc-option=-fPIC base-unicode-symbols filepath process directory lens containers qm-interpolated-string

And when I tried to build my app by following commands (first command compiles some C-code to automatically initialize Haskell runtime, see link posted below, not sure if -static, -shared or -fPIC means something here but it's work in progress):

ghc -static -shared -fPIC -optc-DMODULE=Foo src/lib-autoinit.c -outputdir builddir
ghc
-package-db=SOME_CABALS_SANDBOX_PKGDB_DIR --make -static -shared -fPIC src/Foo.hs builddir/src/lib-autoinit.o -o builddir/libfoo.o -isrc -outputdir builddir -Wall -O2

I failed with a lot of similar errors like this one:

/usr/bin/ld.gold: error: /ghc-8.2.2-fpic/lib/ghc-8.2.2/ghc-prim-0.5.1.1/libHSghc-prim-0.5.1.1.a(Classes.o): requires dynamic R_X86_64_PC32 reloc against 'stg_ap_0_fast' which may overflow at runtime; recompile with -fPIC

What have I missed? What should I do to make this happen?

Any progress could be found here (Dockerfile, sources of modules, build-scripts): https://github.com/unclechu/haskell-experiment-shared-library-for-c-application
Related stack overflow issue: https://stackoverflow.com/questions/47978884/how-do-i-recompile-ghc-with-fpic