<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>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.</p>
    <p>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:</p>
    <tt>COPY my-build.mk /my-build.mk</tt><tt><br>
    </tt><tt><br>
    </tt><tt>RUN mkdir /compile && cd /compile \</tt><tt><br>
    </tt><tt>    && wget
      <a class="moz-txt-link-freetext" href="https://downloads.haskell.org/~ghc/8.2.2/ghc-8.2.2-src.tar.xz">https://downloads.haskell.org/~ghc/8.2.2/ghc-8.2.2-src.tar.xz</a> \</tt><tt><br>
    </tt><tt>    && tar -xvf ghc-8.2.2-src.tar.xz \</tt><tt><br>
    </tt><tt>    && rm ghc-8.2.2-src.tar.xz \</tt><tt><br>
    </tt><tt>    && cd ghc-8.2.2/ \</tt><tt><br>
    </tt><tt>    && ./configure --prefix=/ghc-8.2.2-fpic
      --disable-library-profiling --enable-shared \</tt><tt><br>
    </tt><tt>    && cp /my-build.mk mk/build.mk \</tt><tt><br>
    </tt><tt>    && make install \</tt><tt><br>
    </tt><tt>    && cd /usr/local/bin \</tt><tt><br>
    </tt><tt>    && ls /ghc-8.2.2-fpic/bin/ | xargs -I{} ln -s
      /ghc-8.2.2-fpic/bin/{}</tt><br>
    <p>And as you can see I just use my own prepared <i>my-build.mk</i>
      file which is:</p>
    <tt>SRC_HC_OPTS          = -H64m -O</tt><tt><br>
    </tt><tt>EXTRA_HC_OPTS        = -fPIC</tt><tt><br>
    </tt><tt>SRC_CC_OPTS          = -fPIC -O</tt><tt><br>
    </tt><tt>GhcStage1HcOpts      = -fasm -O0</tt><tt><br>
    </tt><tt>GhcStage2HcOpts      = -fasm -O0</tt><tt><br>
    </tt><tt>GhcLibHcOpts         = -fasm -O2</tt><tt><br>
    </tt><tt>GhcLibWays           = v dyn</tt><tt><br>
    </tt><tt>DYNAMIC_GHC_PROGRAMS = YES</tt><tt><br>
    </tt><tt>DYNAMIC_BY_DEFAULT   = NO</tt><tt><br>
    </tt><tt>SplitObjs            = NO</tt><tt><br>
    </tt><tt>HADDOCK_DOCS         = NO</tt><tt><br>
    </tt><tt>BUILD_DOCBOOK_HTML   = NO</tt><tt><br>
    </tt><tt>BUILD_DOCBOOK_PS     = NO</tt><tt><br>
    </tt><tt>BUILD_DOCBOOK_PDF    = NO</tt><tt><br>
    </tt><tt>V                    = 1</tt><tt><br>
    </tt><tt>LATEX_DOCS           = NO</tt><tt><br>
    </tt><tt>HSCOLOUR_SRCS        = NO</tt><tt><br>
    </tt><tt>BeConservative       = YES</tt>
    <p>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:</p>
    <tt>cd /mnt</tt><tt><br>
    </tt><tt>cabal update</tt><tt><br>
    </tt><tt>cabal sandbox init</tt><tt><br>
    </tt><tt>cabal install --enable-shared --ghc-option=-fPIC happy alex</tt><tt><br>
    </tt><tt>cabal install --enable-shared --ghc-option=-fPIC
      base-unicode-symbols filepath process directory lens containers
      qm-interpolated-string</tt><br>
    <p>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 <i>-static</i>, <i>-shared</i>
      or <i>-fPIC</i> means something here but it's work in progress):</p>
    <tt><span class="pl-s">ghc -static -shared -fPIC -optc-DMODULE=Foo
        src/lib-autoinit.c -outputdir builddir<br>
        ghc </span></tt><tt><span class="pl-s"><span class="pl-s">-package-db=SOME_CABALS_SANDBOX_PKGDB_DIR
        </span>--make<span class="pl-pds"></span></span></tt><tt> </tt><tt><span
        class="pl-s"><span class="pl-pds"></span>-static<span
          class="pl-pds"></span></span></tt><tt> </tt><tt><span
        class="pl-s"><span class="pl-pds"></span>-shared<span
          class="pl-pds"></span></span></tt><tt> </tt><tt><span
        class="pl-s"><span class="pl-pds"></span>-fPIC src/Foo.hs
        builddir/src/lib-autoinit.o -o builddir/libfoo.o -isrc
        -outputdir builddir -Wall -O2<br>
      </span></tt>
    <p>I failed with a lot of similar errors like this one:</p>
    <tt>/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</tt><br>
    <p>What have I missed? What should I do to make this happen?</p>
    <p>Any progress could be found here (Dockerfile, sources of modules,
      build-scripts):
<a class="moz-txt-link-freetext" href="https://github.com/unclechu/haskell-experiment-shared-library-for-c-application">https://github.com/unclechu/haskell-experiment-shared-library-for-c-application</a><br>
      Related stack overflow issue:
<a class="moz-txt-link-freetext" href="https://stackoverflow.com/questions/47978884/how-do-i-recompile-ghc-with-fpic">https://stackoverflow.com/questions/47978884/how-do-i-recompile-ghc-with-fpic</a><br>
    </p>
  </body>
</html>