How can I manage multiple configurations of a single Haskell program?
Asked Answered
K

2

5

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?

Kerato answered 14/4, 2009 at 12:25 Comment(1)
Thanks, Norman, this is the better title.Kerato
D
14

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.

Diastema answered 14/4, 2009 at 13:4 Comment(2)
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?Kerato
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. Also, you need to add {-# LANGUAGE CPP #-} to relevant files.Diastema
A
2

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

Amaurosis answered 14/4, 2009 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.