[Haskell-cafe] GHC API arguments via cabal/stack?

Daniel Gröber dxld at darkboxed.org
Fri Mar 11 12:30:02 UTC 2016

On Fri, Mar 11, 2016 at 09:27:40AM +0000, David Turner wrote:
> I've also briefly contemplated using the Cabal library to read my .cabal
> file and work out what to do, but I'm unsure that this would work nicely
> under stack. At least, I'm not sure quite what to do with all the package
> databases and other stuff that stack does for you.

Using the Cabal library to do this works very well in general, we do
this in ghc-mod these days. The only way I've seen problems creep up
so far is if cabal-install is linked against a different version of
Cabal than your program. If your analysis program doesn't have to work
on the same cabal dist directory as cabal-install (like ghc-mod does)
then that's a non problem though.

Anyways we've wrapped all the ugly stuff to work around this problem
into a library called cabal-helper:

Here's a little example program using that library which should do
what you want:

import System.Environment
import System.Directory
import System.Process
import Distribution.Helper
import Data.Char
import Data.List

main = do
  [project_dir] <- getArgs
  setCurrentDirectory project_dir
  dist_dir <- dropWhileEnd isSpace <$> readProcess "stack" ["path", "--dist-dir"] ""
  print =<< (runQuery (defaultQueryEnv "." dist_dir) $ ghcOptions)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160311/4ce0c914/attachment.sig>

More information about the Haskell-Cafe mailing list