2012-09-03 7 views
7

Ich versuche, ein Programm, das Checks Haskell-Dateien für mich mit der GHC-API. Ich habe die Typprüfung für lokale Dateien bekommen, aber ich habe ein spezielles Cabal-Paket, das ich ebenfalls zur Verfügung haben muss (das gleiche Paket, zu dem diese ausführbare Datei gehört). Wie kann diese Importabhängigkeit hinzugefügt werden?Suche Cabal-Pakete bei der Verwendung der GHC-API

Ich versuchte auch kompilieren mit ghc-Befehlszeile, um herauszufinden, mit ghc -package PKG-VER --make Test.hs -v, aber es scheint nur im lokalen Verzeichnis für Importe zu suchen.

Meine aktuellen Code sieht wie folgt aus:

import   Control.Exception 
import   Control.Monad 
import   Control.Monad.State 
import   DynFlags 
import   Exception 
import   GHC 
import   GHC.Paths   (libdir) 
typecheck :: MonadIO m => [FilePath] -> FilePath -> m() 
typecheck otherincludes fp = 
    liftIO . defaultErrorHandler defaultLogAction . runGhc (Just libdir) $ do 
    dynflags <- getSessionDynFlags 
    void $ setSessionDynFlags dynflags { includePaths = otherIncludes ++ includePaths dynflags } 
    target <- guessTarget fp Nothing 
    setTargets [target] 
    void $ load LoadAllTargets 
    deps <- depanal [] False 
    forM_ deps $ \ms -> parseModule ms >>= typecheckModule 
+2

Das Paket http://hackage.haskell.org/package/buildwrapper macht genau dies. Sie können es als Referenz verwenden. – arrowd

Antwort

5

ich es geschafft, Ihr Code lesbar zu machen und sich typecheck wie folgt:

package Test where 
import   Control.Exception 
import   Control.Monad 
import   Control.Monad.State 
import   DynFlags 
import   Exception 
import   GHC 
import   GHC.Paths   (libdir) 
typecheck :: MonadIO m => [FilePath] -> FilePath -> m() 
typecheck otherincludes fp = 
    liftIO . defaultErrorHandler defaultLogAction . runGhc (Just libdir) $ do 
    dynflags <- getSessionDynFlags 
    void $ setSessionDynFlags dynflags { 
     includePaths = otherincludes ++ includePaths dynflags, 
     packageFlags = [ExposePackage "ghc"]} } 
    target <- guessTarget fp Nothing 
    setTargets [target] 
    void $ load LoadAllTargets 
    deps <- depanal [] False 
    forM_ deps $ \ms -> parseModule ms >>= typecheckModule 

hier ist, wie es in GHCI läuft:

$ ghci Test.hs -package ghc 
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help 
Loading package ghc-prim ... linking ... done. 
Loading package integer-gmp ... linking ... done. 
Loading package base ... linking ... done. 
Loading package array-0.4.0.0 ... linking ... done. 
Loading package deepseq-1.3.0.0 ... linking ... done. 
Loading package containers-0.4.2.1 ... linking ... done. 
Loading package filepath-1.3.0.0 ... linking ... done. 
Loading package old-locale-1.0.0.4 ... linking ... done. 
Loading package old-time-1.1.0.0 ... linking ... done. 
Loading package bytestring-0.9.2.1 ... linking ... done. 
Loading package unix-2.5.1.0 ... linking ... done. 
Loading package directory-1.1.0.2 ... linking ... done. 
Loading package pretty-1.1.1.0 ... linking ... done. 
Loading package process-1.1.0.1 ... linking ... done. 
Loading package Cabal-1.14.0 ... linking ... done. 
Loading package binary-0.5.1.0 ... linking ... done. 
Loading package bin-package-db-0.0.0.0 ... linking ... done. 
Loading package hoopl-3.8.7.3 ... linking ... done. 
Loading package hpc-0.5.1.1 ... linking ... done. 
Loading package template-haskell ... linking ... done. 
Loading package ghc-7.4.1 ... linking ... done. 
Ok, modules loaded: Test. 
Prelude Test> typecheck [] "Test.hs" 
Loading package transformers-0.3.0.0 ... linking ... done. 
Loading package mtl-2.1.1 ... linking ... done. 
Prelude Test> 

So scheint der Trick zu sein, die exposed-Pakete im dynflags Argument zuzu übergeben. Weitere Informationen finden Sie im Modul DynFlags.

+0

stellt sich heraus, ich hatte auch einige dumme Fehler (nicht in diesem Beispiel vorhanden), aber das war, was ich gefragt, also danke! –

+0

Nur als Update für die neuere GHC-API (7.10.2): Das Argument, um ein Paket "pkg" zu entlarven sollte sein: 'ExposePackage (PackageArg" pkg ") $ ModRenaming True []' wo True (nicht wirklich gut dokumentiert) macht das Paket sichtbar. – Dmitry

Verwandte Themen