Search code examples
haskellcabalffitemplate-haskell

Haskell inline-c multi-step build/link with Cabal


I am writing an FFI-heavy library that uses inline-c, and the build process is:

  • run GHC on the inline-c files, produce C
  • compile C produced in previous step into dynamic libraries
  • compile Haskell interface while linking both the object files produced in step 2 and the dynlibs of the wrapped library.

QUESTION: Currently I'm doing the above in a makefile (see https://github.com/ocramz/petsc-hs/blob/master/makefile ), but I'd like to package it up in a Cabal file (working version : https://github.com/ocramz/petsc-hs/blob/master/petsc-hs.cabal).

SUB-QUESTIONS:

  • How does one control the build/link sequence in Cabal?
  • Are relative paths supported in Cabal? couldn't find this mentioned in the guide and bash-like ~ doesn't work

Thank you in advance for all pointers


Solution

  • How does one control the build/link sequence in Cabal?

    You can use Build-Type: Custom in your cabal file and control everything with a custom Setup.hs.

    Have a look at the UserHooks data type for all of the phases of cabal which you can control.

    Some examples:

    Are relative paths supported in Cabal? couldn't find this mentioned in the guide and bash-like ~ doesn't work

    If your library is external to your cabal package, I would consider using pkg-config on Unix/Linux systems to have cabal locate the library. See the answers to this SO question: How to specify dependency on external C library in .cabal?

    If your library is part of your cabal package, check out this blog post and related github repo which shows how to write a custom Setup.hs file to build and install the library in the right place for cabal: