title: How do I recompile GHC with -fPIC?
body: <p><em>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 <a href="https://github.com/unclechu/haskell-experiment-shared-library-for-c-application" rel="noreferrer">here</a>.</em></p>
  
  <p>I tried to solve that by writing <a href="https://github.com/unclechu/haskell-experiment-shared-library-for-c-application/blob/228b652f900c106ebdcde52b842a2fe090a47867/docker/ghc-with-fpic/Dockerfile" rel="noreferrer">Dockerfile</a> where I build GHC 8.2.2 from scratch with replaced <em>mk/build.mk</em> file with <a href="https://github.com/unclechu/haskell-experiment-shared-library-for-c-application/blob/228b652f900c106ebdcde52b842a2fe090a47867/docker/ghc-with-fpic/my-build.mk" rel="noreferrer">this contents</a> (I just did kinda the same I saw in different stack overflow issues like <a href="https://stackoverflow.com/questions/27815467/haskell-dynamic-library/28131655#28131655">this one</a>) but when I try to link with <code>--make -static -shared -fPIC</code> I get a lot of similar errors about linking "ghc-prim", example of last one:</p>
  
  <pre><code>/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
  </code></pre>
  
  <p>What should I actually do to build GHC with -fPIC and link my library with static Haskell dependencies?</p>
  
  <p><code>Dockerfile</code> forked from <code>debian:stretch</code>, here is quote of last most important part of it:</p>
  
  <pre><code># ... 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 &amp;&amp; cd /compile \
      &amp;&amp; wget https://downloads.haskell.org/~ghc/8.2.2/ghc-8.2.2-src.tar.xz \
      &amp;&amp; tar -xvf ghc-8.2.2-src.tar.xz \
      &amp;&amp; rm ghc-8.2.2-src.tar.xz \
      &amp;&amp; cd ghc-8.2.2/ \
      &amp;&amp; ./configure --prefix=/ghc-8.2.2-fpic --disable-library-profiling --enable-shared \
      &amp;&amp; cp /my-build.mk mk/build.mk \
      &amp;&amp; make install \
      &amp;&amp; cd /usr/local/bin \
      &amp;&amp; ls /ghc-8.2.2-fpic/bin/ | xargs -I{} ln -s /ghc-8.2.2-fpic/bin/{}
  </code></pre>
  
  <p>And here is whole <code>my-build.mk</code> file:</p>
  
  <pre><code>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
  </code></pre>