8

The Haskell wiki states that you should use Cabal as your build system. However, it seems to me much more directed at producing packages then just building binaries. Basically, all I want to do is build every *.hs file in my src/ directory into a seperate binary in bin/. This makefile accomplishes this nicely, but I want to learn about Cabal and this seems like a good example to get me started:

GHC = ghc
GHCFLAGS = -outputdir bin
SRC = $(wildcard src/*.hs)
BIN = $(patsubst src/%.hs,%,$(SRC))

all: $(addprefix bin/, $(BIN))

bin/%: src/%.hs
    $(GHC) $(GHCFLAGS) $< -o $@

clean:
    rm bin/*

1 Answer 1

12

The easiest way to get started is to have Cabal generate a .cabal file for you that you can use as a starting point. To do this, go into your project directory and type

$ cabal init

It will then ask you a bunch of questions about your package. Some questions like author name and email only really matter if you plan on uploading your package to Hackage, so you can leave those blank if you want. After doing that, you can then edit the .cabal file to customize it. The generated file will contain a bunch of comments which should help you get started. After that, simply type

$ cabal configure
$ cabal build

The binary will by default be placed in ./dist/build/<name>/.

7
  • 1
    Thanks, that’s a nice start. However, running cabal configure says: Error: No 'Main-Is' field found for executable TestProject, presumably because I haven’t specified one in TestProject.cabal. But since I’m trying to build several executables, each with it’s own main I don’t really know what to do.
    – Psirus
    Commented Feb 15, 2012 at 20:30
  • 1
    @Psirus: You'll want one Executable section per executable you're building. You can then set the Main-Is field in each. If you chose "executable" when generating the file, there should be an example you can copy in the file.
    – hammar
    Commented Feb 15, 2012 at 20:32
  • Thank you for helping me out here. This also works, but I’d prefer a "wildcard" version, so that I don’t have to write basically the same thing over and over again. Also, but this is only minor, can I have all executables in the same directory?
    – Psirus
    Commented Feb 15, 2012 at 20:41
  • @Psirus The "wildcard" version might seem simple now, but it seems like the kind of thing that will come back and bite you later. (What happens when you decide to write a Utils.hs with some handy functions used by all the executables, for example?) For the second question: yes, you can have all the executables in the same directory; what is stopping you? Commented Feb 16, 2012 at 0:38
  • @DanielWagner Thanks for your advice, I’ll keep that in mind for when I start a "proper" project. As for "what happens if I write a Utils.hs used by all executables", that should be possible with a Makefile, so I expect it would be possible with Cabal as well? Also, wouldn’t copy & pasting the same thing over and over again go against the "Don’t repeat yourself" principle?
    – Psirus
    Commented Feb 16, 2012 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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