Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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
share|improve this question

2 Answers 2

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.

share|improve this answer
    
Thank you! Can I use different project.cabal options also? In the code I can use #ifdef and I want for example the JS version not to have the build-depends of graphviz? – Wojciech Danilo Jul 12 '13 at 17:09
    
additional - does {-# LANGUAGE CPP #-} is somehow connected with the ghc cpp backend? I want to use the LLVM backend. – Wojciech Danilo Jul 12 '13 at 17:10
    
@danilo2 To answer the second question: the CPP pragma doesn't have to do anything with C++. It allows one to use the C preprocessor -- aka "cpp". The cpp only does conditional preprocessing (including or excluding parts of code) using #if and #ifdef, but normally (in the case of Haskell) doesn't influence compilation itself. – phg Jul 12 '13 at 18:19
    
@phg I understand I cannot create C preprocessor macros with this pragma? – Wojciech Danilo Jul 12 '13 at 18:21
    
My answer was partially incorrect, corrected that. @danilo2: For different Cabal options, see the linked bug report. – Joachim Breitner Jul 13 '13 at 14:00

While it's currently not possible to use impl(haste) in your cabal files, you can now check for flag(haste-inst) to see if your package is being built using haste-inst.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.