How do I build basic GHC packages with -fPIC?

Alberto Valverde alberto at toscat.net
Wed Dec 27 08:20:30 UTC 2017


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 at 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 at 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 at 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
>> <http://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/ha
>> skell-experiment-shared-library-for-c-application
>> Related stack overflow issue: https://stackoverflow.com/ques
>> tions/47978884/how-do-i-recompile-ghc-with-fpic
>>
>> _______________________________________________
>> ghc-devs mailing list
>> ghc-devs at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20171227/2483cccc/attachment.html>


More information about the ghc-devs mailing list