Skip to content

Cabal should probably add extra-lib-dirs to RPATH #7339

Closed

Description

@bgamari

Describe the bug
In GHC #19350 it was noted that dynamic libraries findable via extra-lib-dirs are not found in GHCi on POSIX platforms. While in principle GHC has the information that it needs to find these libraries from the package database registration, due to limitations of the the POSIX dynamic linking interface, it is not possible for GHC to do better here.

I believe the solution here is to (on POSIX platforms) add Cabal's extra-lib-dirs paths to RPATH during the final link of libraries. This isn't a perfect solution (since it may result in spurious RPATH entries) but this seems to be the best we can do.

To Reproduce
See this testcase.

Expected behavior
GHCi is able to load libraries with foreign dependencies findable only via extra-lib-dirs.

System information

  • Operating system
  • cabal, ghc versions

Additional context
Add any other context about the problem here.

Activity

  1. phadej commented on Mar 21, 2021

    @phadej
    Collaborator

    How this interacts with change proposed in #7094

  2. bgamari commented on Mar 22, 2021

    @bgamari
    ContributorAuthor

    In #7094 we were essentially forced into placing more RPATH logic in GHC (specifically when targeting Darwin) since the patching we are forced to do in Darwin is rather involved. This was a pragmatic choice, but ultimately it was hack that meant conflating two distinct notions. Specifically, it means that GHC uses -L inconsistently with how other compilers treat that flag (since usually it merely affect the compile-time search path; in GHC it now also affects the run-time search behvaior).

    To address GHC #19350 we could use a similar approach, teaching GHC to implicitly add -L directories to RPATH. However I think we should avoid this. -L and RPATH affect two distinct things; while on Darwin, due to the nature of the environment, we can conflate these, doing so on Linux would cause trouble for packagers (since you would almost certainly end up with redundant RPATH entries in some cases; e.g. when you are building a package in a temporary prefix for installation elsewhere). By keeping these two concepts distinct, GHC remains flexible enough to be usable in such cases.

    In short, there is not appreciable interaction between #7094. #7094 is a pragmatic exception to the rule that -L and RPATH are orthogonal concerns. Here I am suggesting that the compiler continue considering them to be orthogonal but we add the policy logic for determining RPATH to Cabal.

  3. phadej commented on Mar 22, 2021

    @phadej
    Collaborator

    I don't understand what this issue suggests?

    Change extra-lib-dirs: directories to not generate -L flags, but -rpath ones? But on Darwin we won't as rpath support is not hasInstallNameTool, and then -rpath stuff is passed as -L anyway?

  4. bgamari commented on Mar 22, 2021

    @bgamari
    ContributorAuthor

    Concretely, extra-lib-dirs should generate both -L flags and -rpath flags.

  5. phadej commented on Mar 22, 2021

    @phadej
    Collaborator

    I was also going to suggest

    Perhaps GHC should grow a -rpath flag of its own to abstract this away?

    But you did it yourself.

  6. phadej commented on Mar 22, 2021

    @phadej
    Collaborator
  7. phadej commented on Mar 22, 2021

    @phadej
    Collaborator

    And to clarify. I'm just asking questions. I have no opinion on this, slightly worried that linking is becoming ad-hoc spagetti, but maybe it is just what it is.

  8. bgamari commented on Mar 22, 2021

    @bgamari
    ContributorAuthor

    Unfortunately it does tend to be quite platform specific. There was sadly a reason why libtool was necessary for so many years.

  9. angerman commented on Apr 29, 2021

    @angerman
    Collaborator

    Concretely, extra-lib-dirs should generate both -L flags and -rpath flags.

    I'm not sure I agree, it should generate -L sure, but leave the rpath logic on darwin up to ghc. The whole idea that cabal takes care of rpath and ghc takes care of rpath is not very good. Ideally cabal would just delegate to hc, and hc would deal with the actual linking, let's not replicate linking logic in both.

  10. bgamari commented on Jun 22, 2021

    @bgamari
    ContributorAuthor

    GHC #20026 is relevant here.

  11. Mikolaj commented on Jun 25, 2021

    @Mikolaj
    Member

    Also, #7382 seems related.

  12. added
    re: dynamic-linking
    on Nov 13, 2021
  13. Mikolaj commented on Jun 2, 2022

    @Mikolaj
    Member

    Please somebody with competence and a stake drive this issue. There is narrow window to get it into cabal 3.8.

  14. self-assigned this
    on Nov 13, 2023
  15. bgamari commented on Nov 14, 2023

    @bgamari
    ContributorAuthor

    I'm not sure I agree, it should generate -L sure, but leave the rpath logic on darwin up to ghc.

    I don't believe that we are actually in disagreement. I agree that it is necessary for GHC to handle the invocation of the linker and post-processing of objects to realize the RUNPATH needed for a particular link.

    However, I do not believe it is necessary for GHC to conflate static and runtime library search paths. Cabal currently uses GHC's -optl-Wl,-rpath and -L flags to precisely specify the compile-time and runtime library search paths needed for a particular install plan. From the perspective of Darwin (the issue fixed in #7094) this status quo is unfortunate since we do not benefit from the clever rpath handling introduced in ghc@4ff93292243888545da452ea4d4c1987f2343591.

    However, this is not a fundamental issue: it can be easily avoided by introducing a -rpath flag in GHC's command-line interface, providing a reliable and precise way for the user to influence the runtime library search path. This would allow us to fix the issue pointed out in this ticket (that extra-lib-dirs don't make it into RPATH) while giving us a more robust way to address the Darwin issue addressed in #7094.

    One question that remains is whether Cabal should make a distinction between compile-time and run-time library search paths. Arguably it should (e.g. by perhaps introducing extra-run-time-lib-dirs and extra-compile-time-lib-dirs fields in the .project syntax and LocalBuildInfo).

  16. added a commit that references this issue on Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

re: dynamic-linkingtype: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions