Repository navigation
Description
(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
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
bos commentedon May 24, 2012
(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).
bos commentedon May 24, 2012
(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.
hvr commentedon Dec 10, 2013
#1542 seems related
simonmar commentedon Jun 30, 2014
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.
simonmar commentedon Jun 30, 2014
Also,
getExecutablePathworks 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.tibbe commentedon Jun 30, 2014
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. ;)
christiaanb commentedon Nov 24, 2014
I've started working on this at: https://github.com/christiaanb/cabal
Currently you can do: cabal install --global --enable-relocatable
Noting the following:
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.christiaanb commentedon Nov 26, 2014
As of christiaanb@08f4d93
--enable-relocatablenow support data-files, usinggetExecutablePath. 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:
ghci.alexto ensure data-files for executables work.alexstores templates using thedata-filesmechanism, and loads them based on thePathsmodule.I'll do some more testing, and then move on to cleaning up a bit. After that I'll start testing on Linux.
gibiansky commentedon Dec 1, 2014
@christiaanb What's the current state of this ticket? Is this likely to make it into a
cabalrelease 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.christiaanb commentedon Dec 1, 2014
The only thing that I need to do is have
cabal execpass the correctDYLD_LIBRARY_PATH, as you cannot currently execute a dynamically linked executable from within thebuilddirectory. The reason is that therpaths 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.
gibiansky commentedon Dec 2, 2014
@christiaanb Very exciting, can't wait. I'll go ahead and start using this as soon as it's merged into cabal
HEAD:)gibiansky commentedon Dec 23, 2014
@christiaanb Do relocatable packages enable a relocatable
.cabaldirectory? I tried to do so using a relative--prefix, but this complained at me thatprefixhad to be absolute. Is there a workaround for this?christiaanb commentedon Dec 23, 2014
@gibiansky It needs an absolute prefix to know where to put things. As long as the specified
--prefixis the root of stuff like--bindirand--libdir, everything will be alright. So acabal install --user --enable-relocatablewill create a relocatable.cabaldirectory.gibiansky commentedon Dec 23, 2014
@christiaanb Ah, got it, thanks. In that case, it should be safe to do something like this:
config.templatefile with prefix set toPREFIXor something like that.cabalin a script which determines its current directory at runtime, and generates aconfigfile from theconfig.templatewherePREFIXis replaced by the current directory$(pwd).configvia--config.This way we can emulate having a relative directory
--prefix, and we should be able to pick up our entire.cabalfolder along with any configuration and move it. Am I understanding correctly?christiaanb commentedon Dec 23, 2014
@gibiansky Actually...
--useris not going to work, because the package configuration database lives in.ghc, not in.cabal.10 remaining items
christiaanb commentedon Sep 18, 2015
@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?mantkiew commentedon Sep 18, 2015
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.
Document environment variables that can be used to change Paths values.
essandess commentedon Sep 3, 2019
@bos @simonmar @christiaanb
At MacPorts we’ve automated
stackbuilds and this has been successful for packages that don’t usedatadir.However,
cabal’s lack of a GNU-standard capability forDESTDIRandPREFIXto 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:
--prefix,--datadir, etc #3586stack build --prefixoption commercialhaskell/stack#84823Skidoo commentedon Sep 4, 2019
No one is actively working on this AFAIK, but I think it'd make a good GSoC project.
essandess commentedon Sep 6, 2019
The approach described by hlint's author @ndmitchell solves the issue of hardcoded
cabaldata-filesin the binary.All that's required is to specify the path within a file called
Paths_packagename.hs, which is normally automatically generated bycabalwith its own paths. Here is an automatedstackbuild that solves the issue along with related files:Also, setting the environment variable
packagename_datadirat runtime overrides the binary's hardcodedpackagename_datadirpath.Also see commercialhaskell/stack#5026.
Add Makefile for updating gitit