Statically link GMP to an Haskell application using GHC (+ LLVM)
Asked Answered
S

1

16
  1. How can I drop dynamic dependency on libgmp and go from this:

    linux-vdso.so.1 =>  (0x00007fffdccb1000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007fb01afc1000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb01acc7000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb01aabe000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb01a8ba000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb01a69d000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb01a2df000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fb01b249000)
    

    to this (currently desired):

    linux-vdso.so.1 =>  (0x00007fffdccb1000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb01acc7000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb01aabe000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb01a8ba000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb01a69d000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb01a2df000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fb01b249000)
    

    in a clean and portable way that just works on all GNU/Linux distributions (and not messing up with BSDs (including OS X))?

  2. Do you see any other dependencies that may cause problems in the currently desired list as given above when distributing a single Haskell binary targeting multiple GNU/Linux distributions?


Notes:

Shewmaker answered 10/5, 2012 at 18:10 Comment(8)
What's your deploy process like? Specifically what's preventing you from deploying libgmp along with your application? What do you mean not messing up on BSDs including OSX? You can't run the same binary on both OSX and Linux.Deliberate
@AndrewMyers I use Cabal for builds. Deploy libgmp? How? I want to support at least Windows, Linux, OS X and FreeBSD. If I need to build a shared/dynamic library version of libgmp for each platform to deploy it along with my app, that is just too much work. Not messing up: a solution that works preferably not only on a single OS; was thinking of someone possibly suggesting using something like locate libgmp and use whatever it may return at link time and locate behaving differently on different OSes. (Replace locate with any other tool here as you wish.)Shewmaker
It sounds like you're talking about deploying a binary, which you'll have to build for each platform. Since you have to build it for each platform you have to have a version of libgmp for each platform anyway so you can just package this with your binary. Am I missing something about how you're planning to distribute your application?Deliberate
Do you actually need to support fast Integer operations? Will simple-integer not suffice?Isaiah
@ThomasM.DuBuisson simple-integer does suffice for the time being actually. Do I not have to recompile GHC rather than just my app for being able to use it?Shewmaker
@ThomasM.DuBuisson and in the near future, I guess we will do cryptography which may need fast Integer operations and thus GMP.Shewmaker
@AndrewMyers Given Platform = OS + Arch, I want to build only once for Linux + x86_64, i.e. I do not want my users to deal with presence, absence of GMP on a particular GNU/Linux distribution. In short I have a single static version of a library somewhere, GHC and my app: I want GHC to statically link the library with my application. That is all :)Shewmaker
@CetinSert Yes, you must recompile GHC to use simple-integer.Isaiah
L
15

If you pass -optl-static -optl-pthread to GHC, it'll statically link all the runtime library dependencies, including GMP. Setting ld-options: -static -pthread in your Cabal file should accomplish the same thing.

That means you'll statically link in glibc too, but that probably won't be a problem, although it might increase binary size quite a bit. Using an alternative libc like musl or uClibc should help counteract that, if it's a problem for you.

Laurelaureano answered 11/5, 2012 at 10:15 Comment(5)
I'm not sure. The last time I statically linked a program with GHC, I got errors relating to pthread symbols if I didn't pass it. It might not be necessary any more.Laurelaureano
I just linked an app as statically as possible. Windows and OS X: libgmp is statically compiled. Linux: -static for ghc and ld is enough without -pthread; libc is statically linked but warns about dynamic loading used inside libc. FreeBSD: always complains about pthread symbols whether I use -pthread or not.Shewmaker
I guess I will have to accept that as an answer soon because it works despite the warnings (perhaps only with glibc). There seems to be no working way to statically link only libgmp without going all the way.Shewmaker
This answer to a question I asked a while ago about deploying Yesod to Heroku may be relevant to your warnings.Deliberate
@AndrewMyers: Right; a situation like that could be avoided by using an alternative libc which doesn't have such a requirement.Laurelaureano

© 2022 - 2024 — McMap. All rights reserved.