Skip to content

Support relocatable packages #462

Open

Description

@bos

(Imported from Trac #469, reported by @dcoutts on 2009-01-20)

This is both useful and doable on both Windows and Unix systems.

We already have limited support for relocatable (also sometimes called prefix-independent) packages on Windows. Specifically, a relocatable package is often what people want when they want to prepare a package for redistribution, especially on Windows for use with an installer. What is required is that an exe and all supporting files (data files and potentially also other shared libs) exist together in one directory hierarchy and that that directory can be placed anywhere in the filesystem and the exe will still be able to find all its associated support files.

There are some restrictions on preparing a relocatable package. In particular the user must configure it such that all install directories for the various kinds of files (libdir, datadir etc) are relative to the $prefix.

Configuring a relocatable package should be something that is done explicitly. At the moment it is done simply by configuring it in the right way, but Cabal is never aware that the user is trying to construct a relocatable package. If it is explicit then we can take different actions if necessary (as it is on unix) and do additional checks.

So a configure option --relocatable or something should be used. Installing a relocatable package is somewhat of an exception because it involves using a prefix which would otherwise be empty. Creating an image for a relocatable package would use a empty $prefix.

The tricky issues that have to be addressed are:

  • data files for the program itself
  • data files in library dependencies
  • shared libraries
The mechanism for finding data files at runtime is the Paths_pkgname module that Cabal generates. On Windows this can make use of the Win32 api that lets a .exe program discover where it was run from. On unix there is no reliable equivalent but the next best alternative is to use wrapper scripts to set environment variables before running the real program.

This mechanism can work pretty well for data files that belong to the executable. It is rather harder for data files in dependent packages (see #459). One option would be to copy the data files belonging to dependent packages and install them along with the executable (checking for clashes). This would involve being able to locate the data files of installed libraries which would require extending the data stored for installed packages. Libraries that require data files are relatively rare (though this should be checked) so this feature could be punted for an initial version.

In future there will also be the issue of shared libraries to consider. That is, when Haskell implementations can produce Haskell libs as shared libraries. This complicates the construction of relocatable packages. Again the approach taken on Windows and Unix will have to be different. On Windows it will be possible to copy the .dll files into the same directory as the .exe file (or a subdir if using .manifest files). On Unix a wrapper script using LD_LIBRARY_PATH will probably be necessary.

Activity

  1. bos commented on May 24, 2012

    @bos
    ContributorAuthor

    (Imported comment by @dcoutts on 2009-01-20)

    Note that the mechanism to support relocatable packages can and should also be used to let us run programs from the build tree and still enable them to find their data files in the build tree.

    The use of environment variables in the current Paths_pkgname module already allow this use, at least on Unix. We just need to take advantage of it by generating wrapper scripts.

    This would also be nice for supporting running programs with runhaskell (eg for hugs or ghc).

  2. bos commented on May 24, 2012

    @bos
    ContributorAuthor

    (Imported comment by @bos on 2009-01-20)

    This would definitely be nice to have. I have a small application that I'd like to redistribute in binary form, but it needs data files, and I've no idea how I'd create a relocatable package on Linux, OS X, or Windows at the moment.

    At least on OS X and Linux, I can fudge by just installing it in /usr, but I don't have any such luxury on Windows.

  3. hvr commented on Dec 10, 2013

    @hvr
    Member

    #1542 seems related

  4. simonmar commented on Jun 30, 2014

    @simonmar
    Member

    Are there any plans to do this? I've just discovered we had a local hack in our custom build system to find data files relative to an executable, and it broke with a recent change to the default datadir in Cabal. I'd love to be able to get rid of our custom hacks and use real relocatable package support. It can't be that hard, given that we already do it on Windows.

  5. simonmar commented on Jun 30, 2014

    @simonmar
    Member

    Also, getExecutablePath works well on Linux and OS X these days. Probably the custom code we have for Win32 can go away in favour of a call to this.

  6. tibbe commented on Jun 30, 2014

    @tibbe
    Contributor

    I don't think there's anyone who has time to do it, but on the other hand it shouldn't be that hard for an adventurous coder that understands the problem. ;)

  7. christiaanb commented on Nov 24, 2014

    @christiaanb
    Collaborator

    I've started working on this at: https://github.com/christiaanb/cabal

    Currently you can do: cabal install --global --enable-relocatable
    Noting the following:

    • Only tested on OS X 10.8, with XCode 4
    • Does not handle packages with data files
    • Correctly handles dynamically linked executables
    • Correctly handles packages to be used with GHCi, given you update your cabal config file to follow GHC's default directory hierarcy:
    install-dirs global
      prefix: /opt/ghc/7.8.3
      -- bindir: $prefix/bin
      -- libdir: $prefix/lib
      libsubdir: $compiler/$pkgkey
      -- libexecdir: $prefix/libexec
      -- datadir: $prefix/share
      -- datasubdir: $arch-$os-$compiler/$pkgid
      docdir: $datadir/doc/ghc/html/libraries/$pkgid
      htmldir: $docdir
      -- haddockdir: $htmldir
      -- sysconfdir: $prefix/etc
    

    I'll report back soon to tell you if it's possible to build a relocatable Haskell platform. Currently I have a completely relocatable GHC 7.8.3 install, with a dynamically linked cabal-install 1.21.1.0.

  8. christiaanb commented on Nov 26, 2014

    @christiaanb
    Collaborator

    As of christiaanb@08f4d93 --enable-relocatable now support data-files, using getExecutablePath. Also, you don't need to adjust your cabal config file anymore.

    I have now build a relocatable version of the Haskell Platform 2014.2.0.0, with:

    • Dynamically linked libraries
    • Dynamically linked executables

    What I've tested until now:

    • Load libraries in ghci.
    • Build the examples of alex to ensure data-files for executables work. alex stores templates using the data-files mechanism, and loads them based on the Paths module.

    I'll do some more testing, and then move on to cleaning up a bit. After that I'll start testing on Linux.

  9. gibiansky commented on Dec 1, 2014

    @gibiansky

    @christiaanb What's the current state of this ticket? Is this likely to make it into a cabal release in the upcoming future, and, if so, when(ish)? I would like to add some relocatable packages to the Mac GHC App (ghcformacosx/ghc-dot-app#19), and am curious whether this work would make it easy to do so.

  10. christiaanb commented on Dec 1, 2014

    @christiaanb
    Collaborator

    The only thing that I need to do is have cabal exec pass the correct DYLD_LIBRARY_PATH, as you cannot currently execute a dynamically linked executable from within the build directory. The reason is that the rpaths are setup in such a way that they are relative to the dependent libraries when the executable is installed.

    Additionally, I'd like to test it on Linux, as I've currently only tested in on OS X. At the moment I've setup relocatable packages to fail configuring when the platform is unsupported.

  11. gibiansky commented on Dec 2, 2014

    @gibiansky

    @christiaanb Very exciting, can't wait. I'll go ahead and start using this as soon as it's merged into cabal HEAD :)

  12. gibiansky commented on Dec 23, 2014

    @gibiansky

    @christiaanb Do relocatable packages enable a relocatable .cabal directory? I tried to do so using a relative --prefix, but this complained at me that prefix had to be absolute. Is there a workaround for this?

  13. christiaanb commented on Dec 23, 2014

    @christiaanb
    Collaborator

    @gibiansky It needs an absolute prefix to know where to put things. As long as the specified --prefix is the root of stuff like --bindir and --libdir, everything will be alright. So a cabal install --user --enable-relocatable will create a relocatable .cabal directory.

  14. gibiansky commented on Dec 23, 2014

    @gibiansky

    @christiaanb Ah, got it, thanks. In that case, it should be safe to do something like this:

    1. Create a config.template file with prefix set to PREFIX or something like that.
    2. Wrap cabal in a script which determines its current directory at runtime, and generates a config file from the config.template where PREFIX is replaced by the current directory $(pwd).
    3. Feed cabal the new config via --config.

    This way we can emulate having a relative directory --prefix, and we should be able to pick up our entire .cabal folder along with any configuration and move it. Am I understanding correctly?

  15. christiaanb commented on Dec 23, 2014

    @christiaanb
    Collaborator

    @gibiansky Actually... --user is not going to work, because the package configuration database lives in .ghc, not in .cabal.

  16. 10 remaining items

  17. christiaanb commented on Sep 18, 2015

    @christiaanb
    Collaborator

    @mantkiew: what's the cross-platform support for file-embed, and how well does it scale? as in, is it "OK" to embed a 100 MB, or even 1GB, data-file in the executable?

  18. mantkiew commented on Sep 18, 2015

    @mantkiew

    I confirm that it works on Windows and Linux. I haven't tested on MacOS yet. It certainly is not a universal mechanism and there will be size limits. Besides, keeping 1GB on the heap does not make any sense. I only used it for small files.

  19. modified the milestones: Cabal 1.24, on Feb 21, 2016
  20. modified the milestones: Cabal 2.0, ⊥ on Sep 6, 2016
  21. essandess commented on Sep 3, 2019

    @essandess

    @bos @simonmar @christiaanb
    At MacPorts we’ve automated stack builds and this has been successful for packages that don’t use datadir.

    However, cabal’s lack of a GNU-standard capability for DESTDIR and PREFIX to produce relocatable binaries and installs is causing major problems for package managers that need to build with Haskell development tools. E.g. see:

    There’s obviously a lot of interest in the capability and the lack of it is holding back deployment of Haskell-based tools.

    Are there any plans to tackle this soon?

    Related:

  22. 23Skidoo commented on Sep 4, 2019

    @23Skidoo
    Member

    No one is actively working on this AFAIK, but I think it'd make a good GSoC project.

  23. essandess commented on Sep 6, 2019

    @essandess

    The approach described by hlint's author @ndmitchell solves the issue of hardcoded cabal data-files in the binary.

    All that's required is to specify the path within a file called Paths_packagename.hs, which is normally automatically generated by cabal with its own paths. Here is an automated stack build that solves the issue along with related files:

    Also, setting the environment variable packagename_datadir at runtime overrides the binary's hardcoded packagename_datadir path.

    Also see commercialhaskell/stack#5026.

  24. added a commit that references this issue on Nov 17, 2020
  25. added
    old-milestone: ⊥
    on Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Cabal: otherold-milestone: ⊥type: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions