Using cabal with multiple GHC versions

后端 未结 5 1002
粉色の甜心
粉色の甜心 2020-12-18 19:50

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=<

相关标签:
5条回答
  • 2020-12-18 20:34

    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.

    0 讨论(0)
  • 2020-12-18 20:36

    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.

    0 讨论(0)
  • 2020-12-18 20:38

    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).

    0 讨论(0)
  • 2020-12-18 20:46

    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
    
    0 讨论(0)
  • 2020-12-18 20:48

    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.

    0 讨论(0)
提交回复
热议问题