Search code examples
haskellcabalffihaskell-stack

Haskell stack build including makefile


I am developing a library that uses some C bindings via inline-c. As of now, the build process involves a makefile, as follows, since we first need to produce C stubs from the inline-c macros, compile them into object code and link them dynamically, in this case to GHCi.

step1:
    ghc ${SRCDIR}/Internal/InlineC.hs -isrc/

step2:
    cc -c ${SRCDIR}/Internal/InlineC.c -o ${LIBDIR}/InlineC_c.o -I${PETSC_DIR_ARCH}/include -I${PETSC_DIR}/include

step3:
    ghci ${SRCDIR}/Test.hs ${SRCDIR}/Internal/InlineC.hs  ${LIBDIR}/InlineC_c.o  ${LIBDIR}/Internal.o -isrc/ -L${PETSC_DIR_ARCH}/lib -lpetsc -lmpich

Question

Is there a way to package up the above build sequence in a stack build recipe?

Thank you in advance


Solution

  • Yes, there is. Read carefully the Stack guide and adhere to the correct .cabal file syntax.

    More precisely, when in a similar situation (HaskellC -> C -> Haskell), make sure the .cabal file has:

    • Binding libraries within scope by specifying the include-dirs, extra-lib-dirs and extra-libraries field in the library stanza.

    • the c-sources field, pointing to a file with the same name as the Haskell file that contains the inline-c stuff (i.e. all the [C.exp| |] quasiquoters for wrapping C functions) but with the .c extension. This will trigger a warning because the C file doesn't exist yet at the beginning of the build, but it will ALSO call gcc, which will take care of the C output produced by inline-c.