<div dir="ltr">I think you need place the <a href="http://build.mk">build.mk</a> in it's place before running GHC's configure script.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 28, 2017 at 12:44 PM, Viacheslav Lotsmanov <span dir="ltr"><<a href="mailto:lotsmanov89@gmail.com" target="_blank">lotsmanov89@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    <p>Now I took my <a href="http://build.mk" target="_blank">build.mk</a> and just replaced lines with <tt>SRC_HC_OPTS</tt>
      and <tt>SRC_CC_OPTS</tt> with ones from your example:<tt><br>
      </tt></p><span class="">
    <p><tt>SRC_HC_OPTS += -fPIC</tt><tt><br>
      </tt><tt>SRC_CC_OPTS += -fPIC</tt></p>
    </span><p>And also I run every <tt>`cabal install`</tt> with <tt>`--ghc-option=-fPIC
        --ghc-option=-optc-fPIC`</tt> but still gets the same errors:</p>
    <p><span class=""><tt>/usr/bin/ld.gold: error:
/ghc-8.2.2-fpic/lib/ghc-8.2.2/<wbr>ghc-prim-0.5.1.1/libHSghc-<wbr>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><tt><br>
      </tt><tt>/usr/bin/ld.gold: error:
/ghc-8.2.2-fpic/lib/ghc-8.2.2/<wbr>ghc-prim-0.5.1.1/libHSghc-<wbr>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><tt><br>
      </tt></span><tt>collect2: error: ld returned 1 exit status</tt><tt><br>
      </tt><tt>`gcc' failed in phase `Linker'. (Exit code: 1)</tt></p>
    <p>Is it even possible?<br>
    </p><div><div class="h5">
    <br>
    <div class="m_5901498364755408491moz-cite-prefix">On 12/28/2017 03:24 AM, Viacheslav
      Lotsmanov wrote:<br>
    </div>
    <blockquote type="cite">
      
      <p>I've tried to replace my whole <i><a href="http://build.mk" target="_blank">build.mk</a></i> with your
        example:</p>
      <p><tt>SRC_HC_OPTS += -fPIC</tt><tt><br>
        </tt><tt>SRC_CC_OPTS += -fPIC</tt></p>
      <p>And then got this error:</p>
      <p><tt>Installing library in
          /ghc-8.2.2-fpic/lib/ghc-8.2.2/<wbr>ghci-8.2.2</tt><tt><br>
        </tt><tt>"inplace/bin/ghc-cabal" copy compiler stage2 "strip" ''
          '/ghc-8.2.2-fpic' '/ghc-8.2.2-fpic/lib/ghc-8.2.<wbr>2'
          '/ghc-8.2.2-fpic/share/doc/<wbr>ghc-8.2.2/html/libraries' 'v p dyn'</tt><tt><br>
        </tt><tt>Installing library in
          /ghc-8.2.2-fpic/lib/ghc-8.2.2/<wbr>ghc-8.2.2</tt><tt><br>
        </tt><tt>ghc-cabal: Error: Could not find module: DriverBkp with
          any suffix: ["p_hi"]</tt><tt><br>
        </tt><tt>in the search path: ["stage2/build"]</tt><tt><br>
        </tt><tt><a href="http://ghc.mk:986" target="_blank">ghc.mk:986</a>: recipe for target 'install_packages' failed</tt><tt><br>
        </tt><tt>make[1]: *** [install_packages] Error 1</tt><tt><br>
        </tt><tt>make: *** [install] Error 2</tt><tt><br>
        </tt><tt>Makefile:122: recipe for target 'install' failed</tt><tt><br>
        </tt><tt>The command '/bin/sh -c mkdir /compile && cd
          /compile   && wget <a class="m_5901498364755408491moz-txt-link-freetext" href="https://downloads.haskell.org/%7Eghc/8.2.2/ghc-8.2.2-src.tar.xz" target="_blank">https://downloads.haskell.org/<wbr>~ghc/8.2.2/ghc-8.2.2-src.tar.<wbr>xz</a> 
          && 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      <wbr>          
          --disable-library-profiling   <wbr>         --enable-shared        
          && cp /<a href="http://my-build.mk" target="_blank">my-build.mk</a> mk/<a href="http://build.mk" target="_blank">build.mk</a>  && make
          install        && rm -rf /compile /<a href="http://my-build.mk" target="_blank">my-build.mk</a>        
          && cd /usr/local/bin    && ls
          /ghc-8.2.2-fpic/bin/ | xargs -I{} ln -s
          /ghc-8.2.2-fpic/bin/{}' returned a non-zero code: 2</tt></p>
      <p>Should I add something else to this file?<br>
      </p>
      <br>
      <div class="m_5901498364755408491moz-cite-prefix">On 12/27/2017 01:20 PM, Alberto
        Valverde wrote:<br>
      </div>
      <blockquote type="cite">
        <div dir="ltr">BTW, this is the Makefile that builds the
          extension I mentioned: <a href="https://github.com/albertov/pg_schedule/blob/master/Makefile" target="_blank">https://github.com/<wbr>albertov/pg_schedule/blob/<wbr>master/Makefile</a>.
          <div>When LINK_STATICALLY=TRUE it produces an '.so' which only
            links dynamically to system libraries, all haskell libraries
            are linked statically:</div>
          <div><br>
          </div>
          <div><span style="font-family:monospace"><span style="font-weight:bold;color:rgb(84,255,84)">alberto@albertows</span><span style="color:rgb(0,0,0)">:</span><span style="font-weight:bold;color:rgb(84,84,255)">~/src/<wbr>thelonius</span><span style="color:rgb(0,0,0)">$ ldd
/nix/store/<wbr>1cy055y8ycs2acqa8w8qf6dbsnx7cc<wbr>1b-pg_schedule-1.0/lib/<wbr>schedule.so
                 </span><br>
                     linux-vdso.so.1 (0x00007fff191b9000) <br>
                     libm.so.6 =>
              /nix/store/<wbr>fysbl29a8p8sa9z3wpnqpn56a0b54f<wbr>ap-glibc-2.26-75/lib/libm.so.6
              (0x00007fae8aec6000) <br>
                     libgmp.so.10 =>
              /nix/store/<wbr>jc8l6hlwl8hc590riqqkk0pr55sjfn<wbr>g2-gmp-6.1.2/lib/libgmp.so.10
              (0x00007fae8ac33000) <br>
                     libc.so.6 =>
              /nix/store/<wbr>fysbl29a8p8sa9z3wpnqpn56a0b54f<wbr>ap-glibc-2.26-75/lib/libc.so.6
              (0x00007fae8a880000) <br>
       /nix/store/<wbr>h1a1ncbkkhapzm0509plqjlfrgxw22<wbr>f3-glibc-2.25-49/lib64/ld-<wbr>linux-x86-64.so.2
              (0x00007fae8cd4d000)<br>
              <br>
            </span></div>
          <div><br>
          </div>
          <div>
            <div class="gmail_extra">
              <div class="gmail_quote">On Wed, Dec 27, 2017 at 9:15 AM,
                Alberto Valverde <span dir="ltr"><<a href="mailto:alberto@toscat.net" target="_blank">alberto@toscat.net</a>></span>
                wrote:<br>
                <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                  <div dir="ltr">Hi,
                    <div><br>
                    </div>
                    <div>I'm manage to link statically all haskell
                      libraries to a dynamically loadable postgreSQL
                      extension written in haskell using:</div>
                    <div><br>
                    </div>
                    <div>SRC_HC_OPTS += -fPIC</div>
                    <div>SRC_CC_OPTS += -fPIC</div>
                    <div><br>
                    </div>
                    <div>in <a href="http://build.mk" target="_blank">build.mk</a> and passing
                      these options to every package cabal builds:</div>
                    <div><br>
                    </div>
                    <div>--ghc-option=-fPIC --ghc-option=-optc-fPIC</div>
                    <div><br>
                    </div>
                    <div>I'm using nix but it should work in any
                      environment if you make sure every package cabal
                      build gets these options (maybe by setting them in
                      $HOME/.cabal/config?)</div>
                    <div><br>
                    </div>
                    <div>HTH,</div>
                    <div>Alberto</div>
                  </div>
                  <div class="gmail_extra"><br>
                    <div class="gmail_quote">
                      <div>
                        <div class="m_5901498364755408491gmail-h5">On Tue, Dec 26, 2017 at
                          9:11 PM, Viacheslav Lotsmanov <span dir="ltr"><<a href="mailto:lotsmanov89@gmail.com" target="_blank">lotsmanov89@gmail.com</a>></span>
                          wrote:<br>
                        </div>
                      </div>
                      <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                        <div>
                          <div class="m_5901498364755408491gmail-h5">
                            <div 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 <a href="http://my-build.mk" target="_blank">my-build.mk</a>
                                /<a href="http://my-build.mk" target="_blank">my-build.mk</a></tt><tt><br>
                              </tt><tt><br>
                              </tt><tt>RUN mkdir /compile && cd
                                /compile \</tt><tt><br>
                              </tt><tt>    && wget <a class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621moz-txt-link-freetext" href="https://downloads.haskell.org/%7Eghc/8.2.2/ghc-8.2.2-src.tar.xz" target="_blank">https://downloads.haskell.org/<wbr>~ghc/8.2.2/ghc-8.2.2-src.tar.x<wbr>z</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 /<a href="http://my-build.mk" target="_blank">my-build.mk</a>
                                mk/<a href="http://build.mk" target="_blank">build.mk</a>
                                \</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><a href="http://my-build.mk" target="_blank">my-build.mk</a></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="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-s">ghc
                                  -static -shared -fPIC
                                  -optc-DMODULE=Foo src/lib-autoinit.c
                                  -outputdir builddir<br>
                                  ghc </span></tt><tt><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-s"><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-s">-package-db=SOME_CABALS_SANDBO<wbr>X_PKGDB_DIR
                                  </span>--make<span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-pds"></span></span></tt><tt>
                              </tt><tt><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-s"><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-pds"></span>-static<span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-pds"></span></span></tt><tt>
                              </tt><tt><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-s"><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-pds"></span>-shared<span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-pds"></span></span></tt><tt>
                              </tt><tt><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-s"><span class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621pl-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/<wbr>ghc-prim-0.5.1.1/libHSghc-prim<wbr>-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="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621moz-txt-link-freetext" href="https://github.com/unclechu/haskell-experiment-shared-library-for-c-application" target="_blank">https://github.com/unclechu/ha<wbr>skell-experiment-shared-librar<wbr>y-for-c-application</a><br>
                                Related stack overflow issue: <a class="m_5901498364755408491gmail-m_-3108370329929368587m_1732321520409461621moz-txt-link-freetext" href="https://stackoverflow.com/questions/47978884/how-do-i-recompile-ghc-with-fpic" target="_blank">https://stackoverflow.com/ques<wbr>tions/47978884/how-do-i-recomp<wbr>ile-ghc-with-fpic</a><br>
                              </p>
                            </div>
                            <br>
                          </div>
                        </div>
                        ______________________________<wbr>_________________<br>
                        ghc-devs mailing list<br>
                        <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
                        <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/ghc-devs</a><br>
                        <br>
                      </blockquote>
                    </div>
                    <br>
                  </div>
                </blockquote>
              </div>
              <br>
            </div>
          </div>
        </div>
      </blockquote>
      <br>
    </blockquote>
    <br>
  </div></div></div>

</blockquote></div><br></div>