4

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.

Cactus
  • 27,075
  • 9
  • 69
  • 149

2 Answers2

3

Does that cabal file work? If so, then it should be sufficient to do something like this:

executable libsxp.so
    ghc-options: -shared -dynamic -fPIC
    if impl (ghc >= 7.10.2 && < 7.10.3)
        ghc-options: -lHSrts-ghc7.10.2
    else if impl (ghc >= 7.10.3 && < 7.10.4)
        ghc-options: -lHSrts-ghc7.10.3
    else if ...

BTW, why does your executable end in .so? I've never seen that in an executable clause.

Are you sure you're using 7.10.2 and not 7.10.3? Try stack exec -- ghc --version

mgsloan
  • 3,245
  • 21
  • 20
  • Ya its 7.10.2 because of the resolver. We are exporting a bunch of haskell functions as an FFI in a shared lib , so its a shared lib - hence the so. Wouldn't this imply that I'll have to write a separate condition for each version ? Any chance I can substitute a variable in cabal flags ? Also thanks for the reply – Prakash Rajagaopal Mar 23 '16 at 03:12
  • Yes , it works and the lib also works well as an FFI. Its just that it is hard coded to 7.10.2 now , which means that it'll invariably fail the next time we change the resolver – Prakash Rajagaopal Mar 23 '16 at 03:17
  • I don't think you can substitute a variable in cabal flags. If you do find a good way to do that bit, you can get the GHC version by doing "stack exec -- ghc-pkg field ghc version --simple-output". So something like roughly `stack build --ghc-options -lHSrts-ghc$(stack exec -- ghc-pkg field ghc version --simple-output)` could do it – mgsloan Mar 24 '16 at 04:40
1

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

Community
  • 1
  • 1
AleXoundOS
  • 341
  • 1
  • 4
  • 13