Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 599676 - Compile static with dev-lang/ghc (-fPIC)
Summary: Compile static with dev-lang/ghc (-fPIC)
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo's Haskell Language team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-11-13 20:38 UTC by merlin
Modified: 2017-02-27 22:49 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description merlin 2016-11-13 20:38:02 UTC
It seems that we can't make static builds with actual ghc and its core libraries. Explain if I'm wrong but here is an example with a small LibTest.hs file you want build as static library:

> module LibTest
> where
> 
> func :: String
> func = "42"

Normally, ghc would need to link it with ghc-prim for String. To do so, this library should have been compiled with -fPIC.

Compiling it with `ghc -shared -static -fPIC -o libTest.so LibTest.hs` gives the following result:
> Linking libTest.so ...
> /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib64/ghc-7.10.3/ghcpr_8TmvWUcS1U1IKHT0levwg3/libHSghc-prim-0.4.0.0-8TmvWUcS1U1IKHT0levwg3.a(CString__4.o): relocation R_X86_64_32S against `stg_upd_frame_info' can not be used when making a shared object; recompile with -fPIC
> /usr/lib64/ghc-7.10.3/ghcpr_8TmvWUcS1U1IKHT0levwg3/libHSghc-prim-0.4.0.0-8TmvWUcS1U1IKHT0levwg3.a: error adding symbols: Bad value
> collect2: error: ld returned 1 exit status

I also tried to do the same with dev-haskell/parsec. Is there a way to compile these with -fPIC without affecting this CFLAG to the portage global one?
Comment 1 Michael Orlitzky gentoo-dev 2016-11-13 20:52:06 UTC
This problem is at least known.. quoting from the GHC docs:

  In principle you can use -shared without -dynamic in the link step.
  That means to statically link the rts all the base libraries into your
  new shared library. This would make a very big, but standalone shared
  library. On most platforms however that would require all the static
  libraries to have been built with -fPIC so that the code is suitable
  to include into a shared library and we do not do that at the moment.

You can use the HCFLAGS variable to set Haskell-only compiler flags, but I would be surprised if that seeps down into the GHC build system itself.
Comment 2 Sergei Trofimovich (RETIRED) gentoo-dev 2016-11-13 21:14:01 UTC
There is no difference between:
- build a shared C library of static C libraries
- build a shared haskell library of static haskell libraries

As Michael said you can do the both things by building everything
explicitly as -fPIC.

In theory you just need to set HFLAGS=-fPIC. I've made it
pass HCFLAGS to ghc itself as well (at least 7.10.3 has it)

https://gitweb.gentoo.org/repo/gentoo.git/tree/dev-lang/ghc/ghc-7.10.3.ebuild#n495

I don't remember how much I tested it last time. I have
vague recollection that it worked.

After HCFLAGS change you'll need to rebuild ghc and all the libraries
you want as PIC. There might be a few fixes upstream i've
forgot to pull to apply -fPIC properly on all artifacts ghc
tries to build but things should work.

Side note: you necessarily don't have to link statically into
a shared library. You can pull all shared libs in a single
directory and use -runpath on an executable to pull things in.
Something like https://trofi.github.io/posts/144-ghc-and-static-linkage.html

Does it make any sense?