views:

198

answers:

2

What is an alternative to autotools in Haskell world? I want to be able to choose between different configurations of the same source code.

For example, there are at least two implementations of MD5 in Haskell: Data.Digest.OpenSSL.MD5 and Data.Digest.Pure.MD5. I'd like to write code in such a way that it can figure out which library is already installed, and didn't require to install the other.

In C I can use Autotools/Scons/CMake + cpp. In Python I can catch ImportError. Which tools should I use in Haskell?

+13  A: 

In Haskell you use Cabal configurations. At your project top-level directory, you put a file with the extension .cabal, e.g., <yourprojectname>.cabal. The contents are roughly:

Name:                myfancypackage
Version:             0.0
Description:         myfancypackage
License:             BSD3
License-file:        LICENSE
Author:              John Doe
Maintainer:          [email protected]
Build-Type:          Simple
Cabal-Version:       >=1.4

Flag pure-haskell-md5
  Description: Choose the purely Haskell MD5 implementation
  Default: False

Executable haq
  Main-is:           Haq.hs
  Build-Depends:     base-4.*
  if flag(pure-haskell-md5)
    Build-Depends:   pureMD5-0.2.*
  else
    Build-Depends:   hopenssl-1.1.*

The Cabal documentation has more details, in particular the section on Configurations.

nominolo
Thank you! This is exactly the answer I was looking for. As well as I understand, I have to use CC-Options and cpp. Right?
jetxee
Right, it's called `CPP-options`, `CC` is the C compiler. You also have to go build your project using cabal. Take a look at the Makefile and .cabal file from one of my projects: [Scion](http://github.com/nominolo/scion/tree/master). Also, you need to add {-# LANGUAGE CPP #-} to relevant files.
nominolo
Thanks. Now it's much more clear.
jetxee
+2  A: 

As nominolo says, Cabal is the tool to use. In particular, the 'configurations" syntax.

Don Stewart