Introduction: I'm actually trying to build a shared-library written in Haskell that compatible with C (could be used from C code without knowing it is written in Haskell), but I want all Haskell dependencies being statically linked, for now I can only dynamically link all dependencies (for every packages including "base" and "ghc-prim" packages. This experiment presented here.
I tried to solve that by writing Dockerfile where I build GHC 8.2.2 from scratch with replaced mk/build.mk file with this contents (I just did kinda the same I saw in different stack overflow issues like this one) but when I try to link with --make -static -shared -fPIC
I get a lot of similar errors about linking "ghc-prim", example of last 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 should I actually do to build GHC with -fPIC and link my library with static Haskell dependencies?
Dockerfile
forked from debian:stretch
, here is quote of last most important part of it:
# ... apt-get update ... installing build-essential and other stuff ...
# see https://stackoverflow.com/a/28131655/774228
# for info about customizations in 'build.mk'
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 here is whole my-build.mk
file:
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