How to link to Haskell static runtime with cabal and stack without hard coding ghc version?

Viewed 470

I have a project which exports a shared static library and I use the following part in my project.cabal file

executable libsxp.so
    main-is:       Somefile.hs
    default-language: Haskell2010
    ghc-options: -shared -dynamic -fPIC -lHSrts-ghc7.10.2

The version of GHC is controlled using Stack, so is there a way wherein I can either get and append the version to make -lHSrts-ghc{version} or is there some config for it? I tried setting

stack build --ghc-options='-O0 -lHSrts-ghc7.10.2' 

but it doesn't seem to pick it.

Also to clarify, cabal install is called by Stack and not by me.

2 Answers

The general principle is described in this answer: https://stackoverflow.com/a/6034881/1663197

Using the configure style in Cabal, you can write a little configure script that substitutes a variable for the output of the sdl-config command. The values will then be replaced in a $foo.buildinfo.in file, yielding a $foo.buildinfo file, that Cabal will include in the build process.


First you need to switch your cabal build-type to Configure in project.cabal. Configure style is described in cabal users guide. For build type Configure the contents of Setup.hs must be:

import Distribution.Simple
main = defaultMainWithHooks autoconfUserHooks

In case of handling GHC runtime version you can have a variable @GHC_VERSION@ corresponding to it in a project.buildinfo.in file:

ghc-options: -lHSrts-ghc@GHC_VERSION@

Finally you write a configure bash script that gets GHC version as mgsloan suggested and generates project.buildinfo file by substitution of @GHC_VERSION@ varibale in project.buildinfo.in file:

GHC_VERSION=$(stack exec -- ghc-pkg field ghc version --simple-output)
sed 's,@GHC_VERSION@,'"$GHC_VERSION"',' project.buildinfo.in > project.buildinfo

This way when build is started it will first execute configure script, then read project.buildinfo file and merge with project.cabal.


Also it may be worth to populate extra-source-files with configure and project.buildinfo.in; extra-tmp-files with project.buildinfo in project.cabal.

A more sophisticated solution may be inspired by this answer: https://stackoverflow.com/a/2940799/1663197

Related