I think you need place the build.mk in it's place before running GHC's configure script.

On Thu, Dec 28, 2017 at 12:44 PM, Viacheslav Lotsmanov <lotsmanov89@gmail.com> wrote:

Now I took my build.mk and just replaced lines with SRC_HC_OPTS and SRC_CC_OPTS with ones from your example:

SRC_HC_OPTS += -fPIC
SRC_CC_OPTS += -fPIC

And also I run every `cabal install` with `--ghc-option=-fPIC --ghc-option=-optc-fPIC` but still gets the same errors:

/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
/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
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

Is it even possible?


On 12/28/2017 03:24 AM, Viacheslav Lotsmanov wrote:

I've tried to replace my whole build.mk with your example:

SRC_HC_OPTS += -fPIC
SRC_CC_OPTS += -fPIC

And then got this error:

Installing library in /ghc-8.2.2-fpic/lib/ghc-8.2.2/ghci-8.2.2
"inplace/bin/ghc-cabal" copy compiler stage2 "strip" '' '/ghc-8.2.2-fpic' '/ghc-8.2.2-fpic/lib/ghc-8.2.2' '/ghc-8.2.2-fpic/share/doc/ghc-8.2.2/html/libraries' 'v p dyn'
Installing library in /ghc-8.2.2-fpic/lib/ghc-8.2.2/ghc-8.2.2
ghc-cabal: Error: Could not find module: DriverBkp with any suffix: ["p_hi"]
in the search path: ["stage2/build"]
ghc.mk:986: recipe for target 'install_packages' failed
make[1]: *** [install_packages] Error 1
make: *** [install] Error 2
Makefile:122: recipe for target 'install' failed
The command '/bin/sh -c 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        && rm -rf /compile /my-build.mk         && 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

Should I add something else to this file?


On 12/27/2017 01:20 PM, Alberto Valverde wrote:
BTW, this is the Makefile that builds the extension I mentioned: https://github.com/albertov/pg_schedule/blob/master/Makefile.
When LINK_STATICALLY=TRUE it produces an '.so' which only links dynamically to system libraries, all haskell libraries are linked statically:

alberto@albertows:~/src/thelonius$ ldd /nix/store/1cy055y8ycs2acqa8w8qf6dbsnx7cc1b-pg_schedule-1.0/lib/schedule.so  
       linux-vdso.so.1 (0x00007fff191b9000)
       libm.so.6 => /nix/store/fysbl29a8p8sa9z3wpnqpn56a0b54fap-glibc-2.26-75/lib/libm.so.6 (0x00007fae8aec6000)
       libgmp.so.10 => /nix/store/jc8l6hlwl8hc590riqqkk0pr55sjfng2-gmp-6.1.2/lib/libgmp.so.10 (0x00007fae8ac33000)
       libc.so.6 => /nix/store/fysbl29a8p8sa9z3wpnqpn56a0b54fap-glibc-2.26-75/lib/libc.so.6 (0x00007fae8a880000)
       /nix/store/h1a1ncbkkhapzm0509plqjlfrgxw22f3-glibc-2.25-49/lib64/ld-linux-x86-64.so.2 (0x00007fae8cd4d000)


On Wed, Dec 27, 2017 at 9:15 AM, Alberto Valverde <alberto@toscat.net> wrote:
Hi,

I'm manage to link statically all haskell libraries to a dynamically loadable postgreSQL extension written in haskell using:

SRC_HC_OPTS += -fPIC
SRC_CC_OPTS += -fPIC

in build.mk and passing these options to every package cabal builds:

--ghc-option=-fPIC --ghc-option=-optc-fPIC

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?)

HTH,
Alberto

On Tue, Dec 26, 2017 at 9:11 PM, Viacheslav Lotsmanov <lotsmanov89@gmail.com> wrote:

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


_______________________________________________
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs