Replace compiler when building Haskell project with Cabal

cabal, compilation, compiler-construction, cross-platform, haskell

Solution

To build a Cabal project with either GHC or Haste, use the `cabal` binary for the former, and `haste-inst` (comes with haste) for the latter.

To have conditional code in in your modules, add `{-# LANGUAGE CPP #-}` and use `#ifdef __HASTE__`, which will only be defined by haste, but not by GHC. Note that `__GLASGOW_HASKELL__` is defined in both cases (which makes sense, as haste builds on GHC for large parts of the compilation). So you would use it like

{-# LANGUAGE CPP #-}

module Module where

compiler :: String
#ifdef __HASTE__
compiler = "haste"
#else
compiler = "GHC"
#endif

Theoretically, for conditional settings in the Cabal file something like this should work:

library
  exposed-modules:
        Module
  if impl(ghc)
        exposed-modules:
                Module.GHC
  if impl(haste)
        exposed-modules:
                Module.GHC
  build-depends:       base ==4.6.*

but it seems that even with `haste-inst`, `impl(ghc)` is true; bug report is filed.

Problem

Is it possible somehow to configure cabal project to use different compiler than GHC? Additional is it possible to control this by some flags? I want to compile my project with GHC or Haste (to JavaScript) based on some compilation flags. It would be ideal if I could set my cabal configuration or my custom script to do something like: ``` -- target JS cabal configure --target=js cabal build -- target Native cabal configure --target=native cabal build ```

Original source