cabal icon indicating copy to clipboard operation
cabal copied to clipboard

pkg-config: Use --libs --static when building with using --enable-executable-static

Open alexbiehl opened this issue 5 years ago • 14 comments

When --enable-executable-static is enabled Cabal passes --static to pkg-config to obtain linker flags for static linking.


Please include the following checklist in your PR:

  • [X] Patches conform to the coding conventions.
  • [X] Any changes that could be relevant to users have been recorded in the changelog (add file to changelog.d directory).
  • [X] The documentation has been updated, if necessary.

Please also shortly describe how you tested your change. Bonus points for added tests!

alexbiehl avatar Jun 30 '20 10:06 alexbiehl

@nh2 does this look good? If you say so, I'll merge this without requiring a test case

phadej avatar Jul 07 '20 10:07 phadej

The motion is great, and I want and need this, but I think what's in this PR is not enough to make it work well.

I already did most of this work in the past, please read https://github.com/nh2/static-haskell-nix/issues/62.

Key important part:

At NixCon I found that this requires adding a new field to ghc-pkg's .conf files, like extra-libraries-static, because libraries can't know at the time they invoke pkg-config whether they will eventually be linked statically or not, so we must carry along both sets of flags.

This requires Cabal changes (which I provide in 2 branches, linked from there), and a GHC change (which I got blocked on because of Hadrian bugs).

I think the right way to do this is to fix Hadrian (I already provided a WIP GHC PR, linked, but then ran out of time for now), and then do the GHC side and rebase the Cabal patches.

I'd be happy to advise on those topics if you'd like to do it @alexbiehl because i'm currently too busy to finish it myself.

nh2 avatar Jul 07 '20 21:07 nh2

Great! I lam willing to take this on. It will take a while as I am on vacations though. So feel free to take this up again in the meantime. I will leave a note when I start working on it actively.

alexbiehl avatar Jul 10 '20 10:07 alexbiehl

@alexbiehl any plans to return to this?

gbaz avatar Aug 11 '21 18:08 gbaz

Hi! Thanks for the gentle reminder. I lost track of this but static linking is still near and dear to my heart and would love to get this done. I have put this on my backlog for tue weekend to evaluate and see what’s left to do.

alexbiehl avatar Aug 11 '21 19:08 alexbiehl

Oh, great news. In case it matters, here's a probably superficially related newer issue not yet mentioned in pingbacks above: #7236,

Mikolaj avatar Aug 11 '21 19:08 Mikolaj

@alexbiehl would you like my help with fixing the conflicts of your PR?

Kleidukos avatar Jun 02 '23 10:06 Kleidukos

@Kleidukos sure! Feel free to to resolve and push!

alexbiehl avatar Jun 02 '23 10:06 alexbiehl

@Kleidukos: I have an impression the actual change got literally lost while formatting. If I'm mistaken, Could you point out what the original change has been? Ideally, it'd be in a commit separate from the formatting changes.

Mikolaj avatar Jun 20 '23 09:06 Mikolaj

I remove needs-review for now because it currently needs some love (see Mikolaj's comment above)..

ulysses4ever avatar Jul 09 '23 00:07 ulysses4ever

Hello, I am going through old PRs to check whether they are stale.

If this PR is still “live”, write a comment and I will remove the consider closing label. Otherwise in ≃ 2 weeks this PR will be closed.

ffaf1 avatar May 08 '25 18:05 ffaf1

All I see in the current diff are formatting changes, and current master seems to invoke pkg-config --libs --static.

https://github.com/haskell/cabal/blob/6953972bfa6ec706d171cda8bfcba2b7354a8206/Cabal/src/Distribution/Simple/Configure.hs#L2440

JackKelly-Bellroy avatar May 10 '25 05:05 JackKelly-Bellroy

I don't see this working at all.

I'm linking against a Haskell library, which itself requires further libraries when statically linking against it.

Cabal doesn't seem to gather the pkgconfig-depends from libraries in the depgraph to unpack further private libs. If you try to add those explicitly to your current cabal file (which is a hack)... then it doesn't work either, because it inserts them at the beginning of the linker line, which is useless.

hasufell avatar Sep 20 '25 15:09 hasufell

Here's a minimal reproducer: https://github.com/hasufell/cabal-extra-libraries

curl-1.3.8-inplace.conf will show:

extra-libraries-static:
    curl ssl crypto z brotlidec brotlicommon zstd ssl crypto dl psl
    unistring idn2 unistring nghttp2 cares pthread

...which is correctly derived from pkg-config --libs --static libcurl, but it never makes it into the linker line of the executable that depends on the haskell curl library.

hasufell avatar Sep 21 '25 09:09 hasufell