Using cabal with multiple GHC versions

2019-01-26 04:09发布

I got both ghc6 and ghc7 on my desktop. To install new packages (for the specific ghc version), I use cabal with the flag --with-compiler=<ghc-dir> to specify for which ghc i want the package installed.

I do cabal update before installing any new package. But how to I specify for which ghc I want the update? I mean, there is no --with-compiler flag as with cabal install. I would think that just like I use ghc-pkg7 for ghc7, there would be cabal7. Apart from the cabal install command which I know to which ghc version it is applying, I don't know which ghc is affected with the other cabal commands.

Also, when I do ghc-pkg check I find problems with some packages and I don't know how to fix.

I would really like to know - if anyone does have - good practices when using multiple ghc versions on the same machine. e.g. how to proceed with installation, updates, etc.

5条回答
We Are One
2楼-- · 2019-01-26 04:46

cabal update has no --with-compiler option because it is completely compiler-agnostic -- it doesn't involve GHC or any other compiler at all. It just downloads a list of available packages from Hackage, nothing more. You can expect that something similar applies to other commands which do not let you choose a compiler (as the dev team was careful to avoid making cabal GHC-specific, let alone specific to a particular GHC version, wherever that makes sense).

查看更多
做自己的国王
3楼-- · 2019-01-26 04:53

I thought --with-ghc is the appropriate option. I use to have multiple ghc versions on my machine. They all have different names like ghc-6.12.3 or ghc-7.4.1. They are usually installed with these names and a default compiler is chosen by creating symbolic links from, say, ghc to ghc-7.4.1. I think you do not need different cabal directories for different ghc versions. Cabal creates a ghc-6.12.3 subdirectory of lib/yourpkg for GHC-6.12.3 files and so on. Also cabal update does not update installed packages, it only fetches the current package list from your favorite package servers. This should be the same for all installed compilers.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-26 04:54

You can create two configuration files, say ~/.cabal/config and ~/.cabal/config7, and use the un(der)documented option --config-file to select between the two.

查看更多
狗以群分
5楼-- · 2019-01-26 04:58

This answer serves to complement the other answers (that are already enlightening).

First of all, know that there are significant differences between minor versions of GHC. For example, the change from GHC 7.8 to GHC 7.10 (cf. burning bridges proposal). So, it might be better to name your GHC binaries also including minor version numbers, like: ghc7.8 and ghc7.10.

Supposing you have several GHCs installed with the following names:

/usr/bin/ghc
/usr/bin/ghc-pkg
/usr/bin/haddock
...
/usr/bin/ghc-7.8
/usr/bin/ghc-pkg-7.8
/usr/bin/haddock-ghc-7.8
...
/usr/bin/ghc-7.6
/usr/bin/ghc-pkg-7.6
/usr/bin/haddock-ghc-7.6
...
(and so on)

For GHC 7.8, you can create a file called ~/.cabal-ghc-7.8/config with the following contents (that point to the locations described above):

remote-repo: hackage.haskell.org:http://hackage.haskell.org/packages/archive
remote-repo-cache: /home/<USER>/.cabal-ghc-7.8/packages
world-file:        /home/<USER>/.cabal-ghc-7.8/world
compiler: ghc
extra-prog-path:   /home/<USER>/.cabal-ghc-7.8/bin
build-summary:     /home/<USER>/.cabal-ghc-7.8/logs/build.log
remote-build-reporting: anonymous
jobs: $ncpus

install-dirs user
  prefix:          /home/<USER>/.cabal-ghc-7.8

program-locations
  ghc-location:     /usr/bin/ghc-7.8
  ghc-pkg-location: /usr/bin/ghc-pkg-7.8
  haddock-location: /usr/bin/haddock-ghc-7.8
  hpc-location:     /usr/bin/hpc-ghc-7.8
  hsc2hs-location:  /usr/bin/hsc2hs-ghc-7.8

You can create an executable possibly called cabal-ghc-7.8 in your PATH (it uses the --config-file option described in n.m.'s answer):

#!/bin/bash
exec cabal --config-file=$HOME/.cabal-ghc-7.8/config "$@"

Now, in your cabalized source dir, you can simply run cabal-ghc-7.8 build to build your source files using GHC 7.8. (or cabal-ghc-7.8 test or anything else)

You can repeat the process for all the GHCs you have installed. Of course, you should not worry about the standard named GHC. By default, Cabal searches for a GHC named ghc.

This answer assumes a UNIX/Linux system (like the use of bash), but can be adapted to other systems with small changes.

查看更多
时光不老,我们不散
6楼-- · 2019-01-26 05:11

Now (cabal version 1.24.0.0), one can

$ cabal install <package name> -w path-to-ghc-executable-1 
$ cabal install <package name> -w path-to-ghc-executable-2

to install packages separately for multiple different GHCs.

(The meta-variable path-to-ghc-executable-i can be, like, /usr/bin/ghc-7.10.3, /usr/local/bin/ghc-8.4.3 etc.)

How did I know? This command can be found via https://www.haskell.org/cabal/users-guide/installing-packages.html or cabal install --help.

The post-condition looks like this: I installed QuickCheck for ghc-8.0.1 and ghc-6.12.1, then ~/.cabal/lib looks like:

➜  lib pwd      
/home/lee/.cabal/lib
➜  lib tree -L 2
.
├── x86_64-linux-ghc-6.12.1
│   └── QuickCheck-2.8.2-GeOZQRSfeF7EtuZhBovRUq
└── x86_64-linux-ghc-8.0.1
    └── QuickCheck-2.9.2-AzbjWrJo3WFD60ZxKurQ3s
查看更多
登录 后发表回答