(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:
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.
(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).
(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.
#1542 seems related
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.
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.
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. ;)
I've started working on this at: https://github.com/christiaanb/cabal
Currently you can do: cabal install --global --enable-relocatable
Noting the following:
-- bindir: $prefix/bin
-- libdir: $prefix/lib
-- libexecdir: $prefix/libexec
-- datadir: $prefix/share
-- datasubdir: $arch-$os-$compiler/$pkgid
-- 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 188.8.131.52.
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:
What I've tested until now:
I'll do some more testing, and then move on to cleaning up a bit. After that I'll start testing on Linux.
@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.
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.
@christiaanb Very exciting, can't wait. I'll go ahead and start using this as soon as it's merged into cabal HEAD :)
@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?
@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.
cabal install --user --enable-relocatable
@christiaanb Ah, got it, thanks. In that case, it should be safe to do something like this:
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?
@gibiansky Actually... --user is not going to work, because the package configuration database lives in .ghc, not in .cabal.
@gibiansky: The .cabal directory is already almost entirely relocatable, if you are willing to specify the --config-file option every time you use cabal-install. The specified config can contain a remote-repo-cache entry pointing to the right directory.
The only non-relocatable item in the .cabal directory is the setup-exe-cache. This was reported as issue #1234, which was closed without being resolved.
@christiaanb: I believe the issues of relocatable GHC packages and relocatable .cabal directory are separate.
With relocatable GHC packages, we will be halfway to having relocatable Cabal sandboxes, as each sandbox contains a GHC package configuration database. The cabal exec and cabal repl commands already make use of this database.
One of the remaining issue for relocatable Cabal sandboxes is the cabal.sandbox.config file, which is currently assumed to contain absolute paths for all entries, such as local-repo, logs-dir, world-file, package-db, build-summary, and prefix.
@christiaanb: Does PR#2255 being merged mean this issue is fully fixed and can be closed?
No, this issue cannot be closed. At the end of #2255 it says:
As said, this partially implements #462. To fully implement that issue, we would additionally need a deploy command. Which you would call like: cabal deploy --deploy-dir= which would collect the binaries, libraries of the dependencies (in case of a dynamically linked executable), and data-files, and turn them into a relocatable bundle at the specified directory.
@christiaanb: From the point of view of the user, what would be the difference between cabal deploy --deploy-dir=... and cabal install --prefix=...?
cabal deploy --deploy-dir=...
cabal install --prefix=...
@mietek: cabal deploy is meant for a developer who wants to package his application for distribution to users. It would be equivalent to an .app bundle on OS X. cabal install is only something that's meant to be used by developers, not by end-users of an application that happens to be written in Haskell.
@christiaanb Are you still working on this and (if so) how close is this to landing (modulo code review)?
@BardurArantsson No, #2255 is the last I worked on it. #2255 provides a good start, I'd say it gets you 33% to 50% of achieving the goals of this (#462) issue. What is still needed:
I don't see myself working on the above issues any time soon. What #2255 does get you is (mostly) relocatable sandboxes: http://christiaanb.github.io/posts/relocatable-sandboxes/