Using cabal with multiple GHC versions

后端 未结 5 1006
粉色の甜心
粉色の甜心 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: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//.cabal-ghc-7.8/packages
    world-file:        /home//.cabal-ghc-7.8/world
    compiler: ghc
    extra-prog-path:   /home//.cabal-ghc-7.8/bin
    build-summary:     /home//.cabal-ghc-7.8/logs/build.log
    remote-build-reporting: anonymous
    jobs: $ncpus
    
    install-dirs user
      prefix:          /home//.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.

提交回复
热议问题